Source file src/net/main_conf_test.go

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !plan9
     6  
     7  package net
     8  
     9  import "testing"
    10  
    11  // forceGoDNS forces the resolver configuration to use the pure Go resolver
    12  // and returns a fixup function to restore the old settings.
    13  func forceGoDNS() func() {
    14  	c := systemConf()
    15  	oldGo := c.netGo
    16  	oldCgo := c.netCgo
    17  	fixup := func() {
    18  		c.netGo = oldGo
    19  		c.netCgo = oldCgo
    20  	}
    21  	c.netGo = true
    22  	c.netCgo = false
    23  	return fixup
    24  }
    25  
    26  // forceCgoDNS forces the resolver configuration to use the cgo resolver
    27  // and returns a fixup function to restore the old settings.
    28  // (On non-Unix systems forceCgoDNS returns nil.)
    29  func forceCgoDNS() func() {
    30  	c := systemConf()
    31  	oldGo := c.netGo
    32  	oldCgo := c.netCgo
    33  	fixup := func() {
    34  		c.netGo = oldGo
    35  		c.netCgo = oldCgo
    36  	}
    37  	c.netGo = false
    38  	c.netCgo = true
    39  	return fixup
    40  }
    41  
    42  func TestForceCgoDNS(t *testing.T) {
    43  	if !cgoAvailable {
    44  		t.Skip("cgo resolver not available")
    45  	}
    46  	defer forceCgoDNS()()
    47  	order, _ := systemConf().hostLookupOrder(nil, "go.dev")
    48  	if order != hostLookupCgo {
    49  		t.Fatalf("hostLookupOrder returned: %v, want cgo", order)
    50  	}
    51  }
    52  
    53  func TestForceGoDNS(t *testing.T) {
    54  	defer forceGoDNS()()
    55  	order, _ := systemConf().hostLookupOrder(nil, "go.dev")
    56  	if order == hostLookupCgo {
    57  		t.Fatalf("hostLookupOrder returned: %v, want go resolver order", order)
    58  	}
    59  }
    60  

View as plain text