Source file src/net/net_windows_test.go

     1  // Copyright 2014 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  package net
     6  
     7  import (
     8  	"bufio"
     9  	"bytes"
    10  	"fmt"
    11  	"internal/testenv"
    12  	"io"
    13  	"os"
    14  	"os/exec"
    15  	"regexp"
    16  	"slices"
    17  	"strings"
    18  	"syscall"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func toErrno(err error) (syscall.Errno, bool) {
    24  	operr, ok := err.(*OpError)
    25  	if !ok {
    26  		return 0, false
    27  	}
    28  	syserr, ok := operr.Err.(*os.SyscallError)
    29  	if !ok {
    30  		return 0, false
    31  	}
    32  	errno, ok := syserr.Err.(syscall.Errno)
    33  	if !ok {
    34  		return 0, false
    35  	}
    36  	return errno, true
    37  }
    38  
    39  // TestAcceptIgnoreSomeErrors tests that windows TCPListener.AcceptTCP
    40  // handles broken connections. It verifies that broken connections do
    41  // not affect future connections.
    42  func TestAcceptIgnoreSomeErrors(t *testing.T) {
    43  	recv := func(ln Listener, ignoreSomeReadErrors bool) (string, error) {
    44  		c, err := ln.Accept()
    45  		if err != nil {
    46  			// Display windows errno in error message.
    47  			errno, ok := toErrno(err)
    48  			if !ok {
    49  				return "", err
    50  			}
    51  			return "", fmt.Errorf("%v (windows errno=%d)", err, errno)
    52  		}
    53  		defer c.Close()
    54  
    55  		b := make([]byte, 100)
    56  		n, err := c.Read(b)
    57  		if err == nil || err == io.EOF {
    58  			return string(b[:n]), nil
    59  		}
    60  		errno, ok := toErrno(err)
    61  		if ok && ignoreSomeReadErrors && (errno == syscall.ERROR_NETNAME_DELETED || errno == syscall.WSAECONNRESET) {
    62  			return "", nil
    63  		}
    64  		return "", err
    65  	}
    66  
    67  	send := func(addr string, data string) error {
    68  		c, err := Dial("tcp", addr)
    69  		if err != nil {
    70  			return err
    71  		}
    72  		defer c.Close()
    73  
    74  		b := []byte(data)
    75  		n, err := c.Write(b)
    76  		if err != nil {
    77  			return err
    78  		}
    79  		if n != len(b) {
    80  			return fmt.Errorf(`Only %d chars of string "%s" sent`, n, data)
    81  		}
    82  		return nil
    83  	}
    84  
    85  	if envaddr := os.Getenv("GOTEST_DIAL_ADDR"); envaddr != "" {
    86  		// In child process.
    87  		c, err := Dial("tcp", envaddr)
    88  		if err != nil {
    89  			t.Fatal(err)
    90  		}
    91  		fmt.Printf("sleeping\n")
    92  		time.Sleep(time.Minute) // process will be killed here
    93  		c.Close()
    94  	}
    95  
    96  	ln, err := Listen("tcp", "127.0.0.1:0")
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	defer ln.Close()
   101  
   102  	// Start child process that connects to our listener.
   103  	cmd := exec.Command(testenv.Executable(t), "-test.run=^TestAcceptIgnoreSomeErrors$")
   104  	cmd.Env = append(os.Environ(), "GOTEST_DIAL_ADDR="+ln.Addr().String())
   105  	stdout, err := cmd.StdoutPipe()
   106  	if err != nil {
   107  		t.Fatalf("cmd.StdoutPipe failed: %v", err)
   108  	}
   109  	err = cmd.Start()
   110  	if err != nil {
   111  		t.Fatalf("cmd.Start failed: %v\n", err)
   112  	}
   113  	outReader := bufio.NewReader(stdout)
   114  	for {
   115  		s, err := outReader.ReadString('\n')
   116  		if err != nil {
   117  			t.Fatalf("reading stdout failed: %v", err)
   118  		}
   119  		if s == "sleeping\n" {
   120  			break
   121  		}
   122  	}
   123  	defer cmd.Wait() // ignore error - we know it is getting killed
   124  
   125  	const alittle = 100 * time.Millisecond
   126  	time.Sleep(alittle)
   127  	cmd.Process.Kill() // the only way to trigger the errors
   128  	time.Sleep(alittle)
   129  
   130  	// Send second connection data (with delay in a separate goroutine).
   131  	result := make(chan error)
   132  	go func() {
   133  		time.Sleep(alittle)
   134  		err := send(ln.Addr().String(), "abc")
   135  		if err != nil {
   136  			result <- err
   137  		}
   138  		result <- nil
   139  	}()
   140  	defer func() {
   141  		err := <-result
   142  		if err != nil {
   143  			t.Fatalf("send failed: %v", err)
   144  		}
   145  	}()
   146  
   147  	// Receive first or second connection.
   148  	s, err := recv(ln, true)
   149  	if err != nil {
   150  		t.Fatalf("recv failed: %v", err)
   151  	}
   152  	switch s {
   153  	case "":
   154  		// First connection data is received, let's get second connection data.
   155  	case "abc":
   156  		// First connection is lost forever, but that is ok.
   157  		return
   158  	default:
   159  		t.Fatalf(`"%s" received from recv, but "" or "abc" expected`, s)
   160  	}
   161  
   162  	// Get second connection data.
   163  	s, err = recv(ln, false)
   164  	if err != nil {
   165  		t.Fatalf("recv failed: %v", err)
   166  	}
   167  	if s != "abc" {
   168  		t.Fatalf(`"%s" received from recv, but "abc" expected`, s)
   169  	}
   170  }
   171  
   172  func runCmd(args ...string) ([]byte, error) {
   173  	removeUTF8BOM := func(b []byte) []byte {
   174  		if len(b) >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF {
   175  			return b[3:]
   176  		}
   177  		return b
   178  	}
   179  	f, err := os.CreateTemp("", "netcmd")
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  	f.Close()
   184  	defer os.Remove(f.Name())
   185  	cmd := fmt.Sprintf(`%s | Out-File "%s" -encoding UTF8`, strings.Join(args, " "), f.Name())
   186  	out, err := exec.Command("powershell", "-Command", cmd).CombinedOutput()
   187  	if err != nil {
   188  		if len(out) != 0 {
   189  			return nil, fmt.Errorf("%s failed: %v: %q", args[0], err, string(removeUTF8BOM(out)))
   190  		}
   191  		var err2 error
   192  		out, err2 = os.ReadFile(f.Name())
   193  		if err2 != nil {
   194  			return nil, err2
   195  		}
   196  		if len(out) != 0 {
   197  			return nil, fmt.Errorf("%s failed: %v: %q", args[0], err, string(removeUTF8BOM(out)))
   198  		}
   199  		return nil, fmt.Errorf("%s failed: %v", args[0], err)
   200  	}
   201  	out, err = os.ReadFile(f.Name())
   202  	if err != nil {
   203  		return nil, err
   204  	}
   205  	return removeUTF8BOM(out), nil
   206  }
   207  
   208  func checkNetsh(t *testing.T) {
   209  	if testenv.Builder() == "windows-arm64-10" {
   210  		// netsh was observed to sometimes hang on this builder.
   211  		// We have not observed failures on windows-arm64-11, so for the
   212  		// moment we are leaving the test enabled elsewhere on the theory
   213  		// that it may have been a platform bug fixed in Windows 11.
   214  		testenv.SkipFlaky(t, 52082)
   215  	}
   216  	out, err := runCmd("netsh", "help")
   217  	if err != nil {
   218  		t.Fatal(err)
   219  	}
   220  	if bytes.Contains(out, []byte("The following helper DLL cannot be loaded")) {
   221  		t.Skipf("powershell failure:\n%s", err)
   222  	}
   223  	if !bytes.Contains(out, []byte("The following commands are available:")) {
   224  		t.Skipf("powershell does not speak English:\n%s", out)
   225  	}
   226  }
   227  
   228  func netshInterfaceIPShowInterface(ipver string, ifaces map[string]bool) error {
   229  	out, err := runCmd("netsh", "interface", ipver, "show", "interface", "level=verbose")
   230  	if err != nil {
   231  		return err
   232  	}
   233  	// interface information is listed like:
   234  	//
   235  	//Interface Local Area Connection Parameters
   236  	//----------------------------------------------
   237  	//IfLuid                             : ethernet_6
   238  	//IfIndex                            : 11
   239  	//State                              : connected
   240  	//Metric                             : 10
   241  	//...
   242  	var name string
   243  	for line := range bytes.SplitSeq(out, []byte{'\r', '\n'}) {
   244  		if bytes.HasPrefix(line, []byte("Interface ")) && bytes.HasSuffix(line, []byte(" Parameters")) {
   245  			f := line[len("Interface "):]
   246  			f = f[:len(f)-len(" Parameters")]
   247  			name = string(f)
   248  			continue
   249  		}
   250  		var isup bool
   251  		switch string(line) {
   252  		case "State                              : connected":
   253  			isup = true
   254  		case "State                              : disconnected":
   255  			isup = false
   256  		default:
   257  			continue
   258  		}
   259  		if name != "" {
   260  			if v, ok := ifaces[name]; ok && v != isup {
   261  				return fmt.Errorf("%s:%s isup=%v: ipv4 and ipv6 report different interface state", ipver, name, isup)
   262  			}
   263  			ifaces[name] = isup
   264  			name = ""
   265  		}
   266  	}
   267  	return nil
   268  }
   269  
   270  func TestInterfacesWithNetsh(t *testing.T) {
   271  	checkNetsh(t)
   272  
   273  	toString := func(name string, isup bool) string {
   274  		if isup {
   275  			return name + ":up"
   276  		}
   277  		return name + ":down"
   278  	}
   279  
   280  	ift, err := Interfaces()
   281  	if err != nil {
   282  		t.Fatal(err)
   283  	}
   284  	have := make([]string, 0)
   285  	for _, ifi := range ift {
   286  		have = append(have, toString(ifi.Name, ifi.Flags&FlagUp != 0))
   287  	}
   288  	slices.Sort(have)
   289  
   290  	ifaces := make(map[string]bool)
   291  	err = netshInterfaceIPShowInterface("ipv6", ifaces)
   292  	if err != nil {
   293  		t.Fatal(err)
   294  	}
   295  	err = netshInterfaceIPShowInterface("ipv4", ifaces)
   296  	if err != nil {
   297  		t.Fatal(err)
   298  	}
   299  	want := make([]string, 0)
   300  	for name, isup := range ifaces {
   301  		want = append(want, toString(name, isup))
   302  	}
   303  	slices.Sort(want)
   304  
   305  	if strings.Join(want, "/") != strings.Join(have, "/") {
   306  		t.Fatalf("unexpected interface list %q, want %q", have, want)
   307  	}
   308  }
   309  
   310  func netshInterfaceIPv4ShowAddress(name string, netshOutput []byte) []string {
   311  	// Address information is listed like:
   312  	//
   313  	//Configuration for interface "Local Area Connection"
   314  	//    DHCP enabled:                         Yes
   315  	//    IP Address:                           10.0.0.2
   316  	//    Subnet Prefix:                        10.0.0.0/24 (mask 255.255.255.0)
   317  	//    IP Address:                           10.0.0.3
   318  	//    Subnet Prefix:                        10.0.0.0/24 (mask 255.255.255.0)
   319  	//    Default Gateway:                      10.0.0.254
   320  	//    Gateway Metric:                       0
   321  	//    InterfaceMetric:                      10
   322  	//
   323  	//Configuration for interface "Loopback Pseudo-Interface 1"
   324  	//    DHCP enabled:                         No
   325  	//    IP Address:                           127.0.0.1
   326  	//    Subnet Prefix:                        127.0.0.0/8 (mask 255.0.0.0)
   327  	//    InterfaceMetric:                      50
   328  	//
   329  	addrs := make([]string, 0)
   330  	var addr, subnetprefix string
   331  	var processingOurInterface bool
   332  	for line := range bytes.SplitSeq(netshOutput, []byte{'\r', '\n'}) {
   333  		if !processingOurInterface {
   334  			if !bytes.HasPrefix(line, []byte("Configuration for interface")) {
   335  				continue
   336  			}
   337  			if !bytes.Contains(line, []byte(`"`+name+`"`)) {
   338  				continue
   339  			}
   340  			processingOurInterface = true
   341  			continue
   342  		}
   343  		if len(line) == 0 {
   344  			break
   345  		}
   346  		if bytes.Contains(line, []byte("Subnet Prefix:")) {
   347  			f := bytes.Split(line, []byte{':'})
   348  			if len(f) == 2 {
   349  				f = bytes.Split(f[1], []byte{'('})
   350  				if len(f) == 2 {
   351  					f = bytes.Split(f[0], []byte{'/'})
   352  					if len(f) == 2 {
   353  						subnetprefix = string(bytes.TrimSpace(f[1]))
   354  						if addr != "" && subnetprefix != "" {
   355  							addrs = append(addrs, addr+"/"+subnetprefix)
   356  						}
   357  					}
   358  				}
   359  			}
   360  		}
   361  		addr = ""
   362  		if bytes.Contains(line, []byte("IP Address:")) {
   363  			f := bytes.Split(line, []byte{':'})
   364  			if len(f) == 2 {
   365  				addr = string(bytes.TrimSpace(f[1]))
   366  			}
   367  		}
   368  	}
   369  	return addrs
   370  }
   371  
   372  func netshInterfaceIPv6ShowAddress(name string, netshOutput []byte) []string {
   373  	// Address information is listed like:
   374  	//
   375  	//Address ::1 Parameters
   376  	//---------------------------------------------------------
   377  	//Interface Luid     : Loopback Pseudo-Interface 1
   378  	//Scope Id           : 0.0
   379  	//Valid Lifetime     : infinite
   380  	//Preferred Lifetime : infinite
   381  	//DAD State          : Preferred
   382  	//Address Type       : Other
   383  	//Skip as Source     : false
   384  	//
   385  	//Address XXXX::XXXX:XXXX:XXXX:XXXX%11 Parameters
   386  	//---------------------------------------------------------
   387  	//Interface Luid     : Local Area Connection
   388  	//Scope Id           : 0.11
   389  	//Valid Lifetime     : infinite
   390  	//Preferred Lifetime : infinite
   391  	//DAD State          : Preferred
   392  	//Address Type       : Other
   393  	//Skip as Source     : false
   394  	//
   395  
   396  	// TODO: need to test ipv6 netmask too, but netsh does not outputs it
   397  	var addr string
   398  	addrs := make([]string, 0)
   399  	for line := range bytes.SplitSeq(netshOutput, []byte{'\r', '\n'}) {
   400  		if addr != "" {
   401  			if len(line) == 0 {
   402  				addr = ""
   403  				continue
   404  			}
   405  			if string(line) != "Interface Luid     : "+name {
   406  				continue
   407  			}
   408  			addrs = append(addrs, addr)
   409  			addr = ""
   410  			continue
   411  		}
   412  		if !bytes.HasPrefix(line, []byte("Address")) {
   413  			continue
   414  		}
   415  		if !bytes.HasSuffix(line, []byte("Parameters")) {
   416  			continue
   417  		}
   418  		f := bytes.Split(line, []byte{' '})
   419  		if len(f) != 3 {
   420  			continue
   421  		}
   422  		// remove scope ID if present
   423  		f = bytes.Split(f[1], []byte{'%'})
   424  
   425  		// netsh can create IPv4-embedded IPv6 addresses, like fe80::5efe:192.168.140.1.
   426  		// Convert these to all hexadecimal fe80::5efe:c0a8:8c01 for later string comparisons.
   427  		ipv4Tail := regexp.MustCompile(`:\d+\.\d+\.\d+\.\d+$`)
   428  		if ipv4Tail.Match(f[0]) {
   429  			f[0] = []byte(ParseIP(string(f[0])).String())
   430  		}
   431  
   432  		addr = string(bytes.ToLower(bytes.TrimSpace(f[0])))
   433  	}
   434  	return addrs
   435  }
   436  
   437  func TestInterfaceAddrsWithNetsh(t *testing.T) {
   438  	checkNetsh(t)
   439  
   440  	outIPV4, err := runCmd("netsh", "interface", "ipv4", "show", "address")
   441  	if err != nil {
   442  		t.Fatal(err)
   443  	}
   444  	outIPV6, err := runCmd("netsh", "interface", "ipv6", "show", "address", "level=verbose")
   445  	if err != nil {
   446  		t.Fatal(err)
   447  	}
   448  
   449  	ift, err := Interfaces()
   450  	if err != nil {
   451  		t.Fatal(err)
   452  	}
   453  	for _, ifi := range ift {
   454  		// Skip the interface if it's down.
   455  		if (ifi.Flags & FlagUp) == 0 {
   456  			continue
   457  		}
   458  		have := make([]string, 0)
   459  		addrs, err := ifi.Addrs()
   460  		if err != nil {
   461  			t.Fatal(err)
   462  		}
   463  		for _, addr := range addrs {
   464  			switch addr := addr.(type) {
   465  			case *IPNet:
   466  				if addr.IP.To4() != nil {
   467  					have = append(have, addr.String())
   468  				}
   469  				if addr.IP.To16() != nil && addr.IP.To4() == nil {
   470  					// netsh does not output netmask for ipv6, so ignore ipv6 mask
   471  					have = append(have, addr.IP.String())
   472  				}
   473  			case *IPAddr:
   474  				if addr.IP.To4() != nil {
   475  					have = append(have, addr.String())
   476  				}
   477  				if addr.IP.To16() != nil && addr.IP.To4() == nil {
   478  					// netsh does not output netmask for ipv6, so ignore ipv6 mask
   479  					have = append(have, addr.IP.String())
   480  				}
   481  			}
   482  		}
   483  		slices.Sort(have)
   484  
   485  		want := netshInterfaceIPv4ShowAddress(ifi.Name, outIPV4)
   486  		wantIPv6 := netshInterfaceIPv6ShowAddress(ifi.Name, outIPV6)
   487  		want = append(want, wantIPv6...)
   488  		slices.Sort(want)
   489  
   490  		if strings.Join(want, "/") != strings.Join(have, "/") {
   491  			t.Errorf("%s: unexpected addresses list %q, want %q", ifi.Name, have, want)
   492  		}
   493  	}
   494  }
   495  
   496  // check that getmac exists as a powershell command, and that it
   497  // speaks English.
   498  func checkGetmac(t *testing.T) {
   499  	out, err := runCmd("getmac", "/?")
   500  	if err != nil {
   501  		if strings.Contains(err.Error(), "term 'getmac' is not recognized as the name of a cmdlet") {
   502  			t.Skipf("getmac not available")
   503  		}
   504  		t.Fatal(err)
   505  	}
   506  	if !bytes.Contains(out, []byte("network adapters on a system")) {
   507  		t.Skipf("skipping test on non-English system")
   508  	}
   509  }
   510  
   511  func TestInterfaceHardwareAddrWithGetmac(t *testing.T) {
   512  	checkGetmac(t)
   513  
   514  	ift, err := Interfaces()
   515  	if err != nil {
   516  		t.Fatal(err)
   517  	}
   518  	have := make(map[string]string)
   519  	for _, ifi := range ift {
   520  		if ifi.Flags&FlagLoopback != 0 {
   521  			// no MAC address for loopback interfaces
   522  			continue
   523  		}
   524  		have[ifi.Name] = ifi.HardwareAddr.String()
   525  	}
   526  
   527  	out, err := runCmd("getmac", "/fo", "list", "/v")
   528  	if err != nil {
   529  		t.Fatal(err)
   530  	}
   531  	// getmac output looks like:
   532  	//
   533  	//Connection Name:  Local Area Connection
   534  	//Network Adapter:  Intel Gigabit Network Connection
   535  	//Physical Address: XX-XX-XX-XX-XX-XX
   536  	//Transport Name:   \Device\Tcpip_{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
   537  	//
   538  	//Connection Name:  Wireless Network Connection
   539  	//Network Adapter:  Wireles WLAN Card
   540  	//Physical Address: XX-XX-XX-XX-XX-XX
   541  	//Transport Name:   Media disconnected
   542  	//
   543  	//Connection Name:  Bluetooth Network Connection
   544  	//Network Adapter:  Bluetooth Device (Personal Area Network)
   545  	//Physical Address: N/A
   546  	//Transport Name:   Hardware not present
   547  	//
   548  	//Connection Name:  VMware Network Adapter VMnet8
   549  	//Network Adapter:  VMware Virtual Ethernet Adapter for VMnet8
   550  	//Physical Address: Disabled
   551  	//Transport Name:   Disconnected
   552  	//
   553  	want := make(map[string]string)
   554  	group := make(map[string]string) // name / values for single adapter
   555  	getValue := func(name string) string {
   556  		value, found := group[name]
   557  		if !found {
   558  			t.Fatalf("%q has no %q line in it", group, name)
   559  		}
   560  		if value == "" {
   561  			t.Fatalf("%q has empty %q value", group, name)
   562  		}
   563  		return value
   564  	}
   565  	processGroup := func() {
   566  		if len(group) == 0 {
   567  			return
   568  		}
   569  		tname := strings.ToLower(getValue("Transport Name"))
   570  		if tname == "n/a" {
   571  			// skip these
   572  			return
   573  		}
   574  		addr := strings.ToLower(getValue("Physical Address"))
   575  		if addr == "disabled" || addr == "n/a" {
   576  			// skip these
   577  			return
   578  		}
   579  		addr = strings.ReplaceAll(addr, "-", ":")
   580  		cname := getValue("Connection Name")
   581  		want[cname] = addr
   582  		group = make(map[string]string)
   583  	}
   584  	for line := range bytes.SplitSeq(out, []byte{'\r', '\n'}) {
   585  		if len(line) == 0 {
   586  			processGroup()
   587  			continue
   588  		}
   589  		i := bytes.IndexByte(line, ':')
   590  		if i == -1 {
   591  			t.Fatalf("line %q has no : in it", line)
   592  		}
   593  		group[string(line[:i])] = string(bytes.TrimSpace(line[i+1:]))
   594  	}
   595  	processGroup()
   596  
   597  	dups := make(map[string][]string)
   598  	for name, addr := range want {
   599  		if _, ok := dups[addr]; !ok {
   600  			dups[addr] = make([]string, 0)
   601  		}
   602  		dups[addr] = append(dups[addr], name)
   603  	}
   604  
   605  nextWant:
   606  	for name, wantAddr := range want {
   607  		if haveAddr, ok := have[name]; ok {
   608  			if haveAddr != wantAddr {
   609  				t.Errorf("unexpected MAC address for %q - %v, want %v", name, haveAddr, wantAddr)
   610  			}
   611  			continue
   612  		}
   613  		// We could not find the interface in getmac output by name.
   614  		// But sometimes getmac lists many interface names
   615  		// for the same MAC address. If that is the case here,
   616  		// and we can match at least one of those names,
   617  		// let's ignore the other names.
   618  		if dupNames, ok := dups[wantAddr]; ok && len(dupNames) > 1 {
   619  			for _, dupName := range dupNames {
   620  				if haveAddr, ok := have[dupName]; ok && haveAddr == wantAddr {
   621  					continue nextWant
   622  				}
   623  			}
   624  		}
   625  		t.Errorf("getmac lists %q, but it could not be found among Go interfaces %v", name, have)
   626  	}
   627  }
   628  

View as plain text