X7ROOT File Manager
Current Path:
/opt/golang/1.22.0/src/net
opt
/
golang
/
1.22.0
/
src
/
net
/
📁
..
📄
addrselect.go
(9.69 KB)
📄
addrselect_test.go
(8.5 KB)
📄
cgo_aix.go
(582 B)
📄
cgo_android.go
(272 B)
📄
cgo_bsd.go
(343 B)
📄
cgo_darwin.go
(298 B)
📄
cgo_linux.go
(642 B)
📄
cgo_netbsd.go
(276 B)
📄
cgo_openbsd.go
(276 B)
📄
cgo_resnew.go
(580 B)
📄
cgo_resold.go
(579 B)
📄
cgo_socknew.go
(753 B)
📄
cgo_sockold.go
(842 B)
📄
cgo_solaris.go
(343 B)
📄
cgo_stub.go
(1.33 KB)
📄
cgo_unix.go
(11.35 KB)
📄
cgo_unix_cgo.go
(2.24 KB)
📄
cgo_unix_cgo_darwin.go
(461 B)
📄
cgo_unix_cgo_res.go
(911 B)
📄
cgo_unix_cgo_resn.go
(998 B)
📄
cgo_unix_syscall.go
(2.99 KB)
📄
cgo_unix_test.go
(1.44 KB)
📄
conf.go
(15.63 KB)
📄
conf_test.go
(12.28 KB)
📄
conn_test.go
(1.82 KB)
📄
dial.go
(25.68 KB)
📄
dial_test.go
(30.14 KB)
📄
dial_unix_test.go
(2.77 KB)
📄
dnsclient.go
(5.65 KB)
📄
dnsclient_test.go
(1.51 KB)
📄
dnsclient_unix.go
(24.26 KB)
📄
dnsclient_unix_test.go
(69.13 KB)
📄
dnsconfig.go
(1.73 KB)
📄
dnsconfig_unix.go
(4.16 KB)
📄
dnsconfig_unix_test.go
(7.07 KB)
📄
dnsconfig_windows.go
(1.6 KB)
📄
dnsname_test.go
(1.96 KB)
📄
error_plan9.go
(224 B)
📄
error_plan9_test.go
(437 B)
📄
error_posix.go
(543 B)
📄
error_posix_test.go
(981 B)
📄
error_test.go
(20.32 KB)
📄
error_unix.go
(382 B)
📄
error_unix_test.go
(723 B)
📄
error_windows.go
(355 B)
📄
error_windows_test.go
(757 B)
📄
example_test.go
(8.45 KB)
📄
external_test.go
(4.05 KB)
📄
fd_fake.go
(3.79 KB)
📄
fd_js.go
(627 B)
📄
fd_plan9.go
(3.56 KB)
📄
fd_posix.go
(4.29 KB)
📄
fd_unix.go
(5.43 KB)
📄
fd_wasip1.go
(496 B)
📄
fd_windows.go
(6.14 KB)
📄
file.go
(1.69 KB)
📄
file_plan9.go
(2.72 KB)
📄
file_stub.go
(481 B)
📄
file_test.go
(6.43 KB)
📄
file_unix.go
(2.5 KB)
📄
file_unix_test.go
(2.07 KB)
📄
file_wasip1.go
(2.2 KB)
📄
file_wasip1_test.go
(3.76 KB)
📄
file_windows.go
(521 B)
📄
hook.go
(894 B)
📄
hook_plan9.go
(211 B)
📄
hook_unix.go
(658 B)
📄
hook_windows.go
(720 B)
📄
hosts.go
(3.48 KB)
📄
hosts_test.go
(5.6 KB)
📁
http
📄
interface.go
(7.26 KB)
📄
interface_aix.go
(4.46 KB)
📄
interface_bsd.go
(2.82 KB)
📄
interface_bsd_test.go
(1.44 KB)
📄
interface_bsdvar.go
(718 B)
📄
interface_darwin.go
(1.29 KB)
📄
interface_freebsd.go
(1.29 KB)
📄
interface_linux.go
(6.96 KB)
📄
interface_linux_test.go
(3.65 KB)
📄
interface_plan9.go
(4.71 KB)
📄
interface_solaris.go
(2.13 KB)
📄
interface_stub.go
(814 B)
📄
interface_test.go
(9.76 KB)
📄
interface_unix_test.go
(4.85 KB)
📄
interface_windows.go
(5.43 KB)
📁
internal
📄
ip.go
(13.88 KB)
📄
ip_test.go
(25.6 KB)
📄
iprawsock.go
(7.11 KB)
📄
iprawsock_plan9.go
(874 B)
📄
iprawsock_posix.go
(3.89 KB)
📄
iprawsock_test.go
(5.98 KB)
📄
ipsock.go
(9.05 KB)
📄
ipsock_plan9.go
(7.48 KB)
📄
ipsock_plan9_test.go
(645 B)
📄
ipsock_posix.go
(7.86 KB)
📄
ipsock_test.go
(6.81 KB)
📄
listen_test.go
(20.5 KB)
📄
lookup.go
(29.04 KB)
📄
lookup_plan9.go
(9.94 KB)
📄
lookup_test.go
(40.62 KB)
📄
lookup_unix.go
(3.35 KB)
📄
lookup_windows.go
(13.8 KB)
📄
lookup_windows_test.go
(8.93 KB)
📄
mac.go
(1.88 KB)
📄
mac_test.go
(3.26 KB)
📁
mail
📄
main_cloexec_test.go
(693 B)
📄
main_conf_test.go
(1.39 KB)
📄
main_noconf_test.go
(467 B)
📄
main_plan9_test.go
(392 B)
📄
main_posix_test.go
(1.37 KB)
📄
main_test.go
(7.53 KB)
📄
main_unix_test.go
(1.19 KB)
📄
main_wasm_test.go
(284 B)
📄
main_windows_test.go
(1 KB)
📄
mockserver_test.go
(10.77 KB)
📄
mptcpsock_linux.go
(3.96 KB)
📄
mptcpsock_linux_test.go
(4.06 KB)
📄
mptcpsock_stub.go
(542 B)
📄
net.go
(24.56 KB)
📄
net_fake.go
(26.36 KB)
📄
net_fake_test.go
(2.79 KB)
📄
net_test.go
(14 KB)
📄
net_windows_test.go
(16.28 KB)
📄
netcgo_off.go
(222 B)
📄
netcgo_on.go
(220 B)
📄
netgo_netcgo.go
(453 B)
📄
netgo_off.go
(220 B)
📄
netgo_on.go
(218 B)
📁
netip
📄
nss.go
(5.48 KB)
📄
nss_test.go
(3.4 KB)
📄
packetconn_test.go
(3.02 KB)
📄
parse.go
(6.04 KB)
📄
parse_test.go
(1.55 KB)
📄
pipe.go
(5.43 KB)
📄
pipe_test.go
(1.2 KB)
📄
platform_test.go
(4.33 KB)
📄
port.go
(1.46 KB)
📄
port_test.go
(1.34 KB)
📄
port_unix.go
(1.24 KB)
📄
protoconn_test.go
(7.44 KB)
📄
rawconn.go
(2.7 KB)
📄
rawconn_stub_test.go
(631 B)
📄
rawconn_test.go
(4.33 KB)
📄
rawconn_unix_test.go
(2.96 KB)
📄
rawconn_windows_test.go
(3.12 KB)
📄
resolverdialfunc_test.go
(8.25 KB)
📄
rlimit_js.go
(346 B)
📄
rlimit_unix.go
(1.1 KB)
📁
rpc
📄
sendfile_linux.go
(1.12 KB)
📄
sendfile_linux_test.go
(1.93 KB)
📄
sendfile_stub.go
(344 B)
📄
sendfile_test.go
(8.25 KB)
📄
sendfile_unix_alt.go
(2.04 KB)
📄
sendfile_windows.go
(1.02 KB)
📄
server_test.go
(11.67 KB)
📁
smtp
📄
sock_bsd.go
(918 B)
📄
sock_cloexec.go
(1.38 KB)
📄
sock_linux.go
(993 B)
📄
sock_linux_test.go
(575 B)
📄
sock_plan9.go
(262 B)
📄
sock_posix.go
(6.29 KB)
📄
sock_stub.go
(390 B)
📄
sock_windows.go
(802 B)
📄
sockaddr_posix.go
(1.45 KB)
📄
sockopt_aix.go
(1.43 KB)
📄
sockopt_bsd.go
(2.21 KB)
📄
sockopt_fake.go
(955 B)
📄
sockopt_linux.go
(1.25 KB)
📄
sockopt_plan9.go
(406 B)
📄
sockopt_posix.go
(2.13 KB)
📄
sockopt_solaris.go
(1.25 KB)
📄
sockopt_windows.go
(1.51 KB)
📄
sockoptip_bsdvar.go
(867 B)
📄
sockoptip_linux.go
(735 B)
📄
sockoptip_posix.go
(1.38 KB)
📄
sockoptip_stub.go
(769 B)
📄
sockoptip_windows.go
(786 B)
📄
splice_linux.go
(1.69 KB)
📄
splice_stub.go
(376 B)
📄
splice_test.go
(13.48 KB)
📄
sys_cloexec.go
(962 B)
📄
tcpsock.go
(11.75 KB)
📄
tcpsock_plan9.go
(2.18 KB)
📄
tcpsock_posix.go
(6.16 KB)
📄
tcpsock_test.go
(17.68 KB)
📄
tcpsock_unix_test.go
(2.34 KB)
📄
tcpsockopt_darwin.go
(770 B)
📄
tcpsockopt_dragonfly.go
(698 B)
📄
tcpsockopt_openbsd.go
(365 B)
📄
tcpsockopt_plan9.go
(525 B)
📄
tcpsockopt_posix.go
(442 B)
📄
tcpsockopt_solaris.go
(1.15 KB)
📄
tcpsockopt_stub.go
(399 B)
📄
tcpsockopt_unix.go
(722 B)
📄
tcpsockopt_windows.go
(741 B)
📁
testdata
📁
textproto
📄
timeout_test.go
(29.11 KB)
📄
udpsock.go
(11.83 KB)
📄
udpsock_plan9.go
(4.64 KB)
📄
udpsock_plan9_test.go
(1.31 KB)
📄
udpsock_posix.go
(7.53 KB)
📄
udpsock_test.go
(17.24 KB)
📄
unixsock.go
(10.12 KB)
📄
unixsock_linux_test.go
(2.29 KB)
📄
unixsock_plan9.go
(1.24 KB)
📄
unixsock_posix.go
(6.64 KB)
📄
unixsock_readmsg_cloexec.go
(654 B)
📄
unixsock_readmsg_cmsg_cloexec.go
(332 B)
📄
unixsock_readmsg_other.go
(275 B)
📄
unixsock_readmsg_test.go
(2.51 KB)
📄
unixsock_test.go
(10.64 KB)
📄
unixsock_windows_test.go
(2.03 KB)
📁
url
📄
write_unix_test.go
(1.61 KB)
📄
writev_test.go
(5.03 KB)
📄
writev_unix.go
(666 B)
Editing: net_windows_test.go
// Copyright 2014 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package net import ( "bufio" "bytes" "fmt" "internal/testenv" "io" "os" "os/exec" "regexp" "sort" "strings" "syscall" "testing" "time" ) func toErrno(err error) (syscall.Errno, bool) { operr, ok := err.(*OpError) if !ok { return 0, false } syserr, ok := operr.Err.(*os.SyscallError) if !ok { return 0, false } errno, ok := syserr.Err.(syscall.Errno) if !ok { return 0, false } return errno, true } // TestAcceptIgnoreSomeErrors tests that windows TCPListener.AcceptTCP // handles broken connections. It verifies that broken connections do // not affect future connections. func TestAcceptIgnoreSomeErrors(t *testing.T) { recv := func(ln Listener, ignoreSomeReadErrors bool) (string, error) { c, err := ln.Accept() if err != nil { // Display windows errno in error message. errno, ok := toErrno(err) if !ok { return "", err } return "", fmt.Errorf("%v (windows errno=%d)", err, errno) } defer c.Close() b := make([]byte, 100) n, err := c.Read(b) if err == nil || err == io.EOF { return string(b[:n]), nil } errno, ok := toErrno(err) if ok && ignoreSomeReadErrors && (errno == syscall.ERROR_NETNAME_DELETED || errno == syscall.WSAECONNRESET) { return "", nil } return "", err } send := func(addr string, data string) error { c, err := Dial("tcp", addr) if err != nil { return err } defer c.Close() b := []byte(data) n, err := c.Write(b) if err != nil { return err } if n != len(b) { return fmt.Errorf(`Only %d chars of string "%s" sent`, n, data) } return nil } if envaddr := os.Getenv("GOTEST_DIAL_ADDR"); envaddr != "" { // In child process. c, err := Dial("tcp", envaddr) if err != nil { t.Fatal(err) } fmt.Printf("sleeping\n") time.Sleep(time.Minute) // process will be killed here c.Close() } ln, err := Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } defer ln.Close() // Start child process that connects to our listener. cmd := exec.Command(os.Args[0], "-test.run=TestAcceptIgnoreSomeErrors") cmd.Env = append(os.Environ(), "GOTEST_DIAL_ADDR="+ln.Addr().String()) stdout, err := cmd.StdoutPipe() if err != nil { t.Fatalf("cmd.StdoutPipe failed: %v", err) } err = cmd.Start() if err != nil { t.Fatalf("cmd.Start failed: %v\n", err) } outReader := bufio.NewReader(stdout) for { s, err := outReader.ReadString('\n') if err != nil { t.Fatalf("reading stdout failed: %v", err) } if s == "sleeping\n" { break } } defer cmd.Wait() // ignore error - we know it is getting killed const alittle = 100 * time.Millisecond time.Sleep(alittle) cmd.Process.Kill() // the only way to trigger the errors time.Sleep(alittle) // Send second connection data (with delay in a separate goroutine). result := make(chan error) go func() { time.Sleep(alittle) err := send(ln.Addr().String(), "abc") if err != nil { result <- err } result <- nil }() defer func() { err := <-result if err != nil { t.Fatalf("send failed: %v", err) } }() // Receive first or second connection. s, err := recv(ln, true) if err != nil { t.Fatalf("recv failed: %v", err) } switch s { case "": // First connection data is received, let's get second connection data. case "abc": // First connection is lost forever, but that is ok. return default: t.Fatalf(`"%s" received from recv, but "" or "abc" expected`, s) } // Get second connection data. s, err = recv(ln, false) if err != nil { t.Fatalf("recv failed: %v", err) } if s != "abc" { t.Fatalf(`"%s" received from recv, but "abc" expected`, s) } } func runCmd(args ...string) ([]byte, error) { removeUTF8BOM := func(b []byte) []byte { if len(b) >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF { return b[3:] } return b } f, err := os.CreateTemp("", "netcmd") if err != nil { return nil, err } f.Close() defer os.Remove(f.Name()) cmd := fmt.Sprintf(`%s | Out-File "%s" -encoding UTF8`, strings.Join(args, " "), f.Name()) out, err := exec.Command("powershell", "-Command", cmd).CombinedOutput() if err != nil { if len(out) != 0 { return nil, fmt.Errorf("%s failed: %v: %q", args[0], err, string(removeUTF8BOM(out))) } var err2 error out, err2 = os.ReadFile(f.Name()) if err2 != nil { return nil, err2 } if len(out) != 0 { return nil, fmt.Errorf("%s failed: %v: %q", args[0], err, string(removeUTF8BOM(out))) } return nil, fmt.Errorf("%s failed: %v", args[0], err) } out, err = os.ReadFile(f.Name()) if err != nil { return nil, err } return removeUTF8BOM(out), nil } func checkNetsh(t *testing.T) { if testenv.Builder() == "windows-arm64-10" { // netsh was observed to sometimes hang on this builder. // We have not observed failures on windows-arm64-11, so for the // moment we are leaving the test enabled elsewhere on the theory // that it may have been a platform bug fixed in Windows 11. testenv.SkipFlaky(t, 52082) } out, err := runCmd("netsh", "help") if err != nil { t.Fatal(err) } if bytes.Contains(out, []byte("The following helper DLL cannot be loaded")) { t.Skipf("powershell failure:\n%s", err) } if !bytes.Contains(out, []byte("The following commands are available:")) { t.Skipf("powershell does not speak English:\n%s", out) } } func netshInterfaceIPShowInterface(ipver string, ifaces map[string]bool) error { out, err := runCmd("netsh", "interface", ipver, "show", "interface", "level=verbose") if err != nil { return err } // interface information is listed like: // //Interface Local Area Connection Parameters //---------------------------------------------- //IfLuid : ethernet_6 //IfIndex : 11 //State : connected //Metric : 10 //... var name string lines := bytes.Split(out, []byte{'\r', '\n'}) for _, line := range lines { if bytes.HasPrefix(line, []byte("Interface ")) && bytes.HasSuffix(line, []byte(" Parameters")) { f := line[len("Interface "):] f = f[:len(f)-len(" Parameters")] name = string(f) continue } var isup bool switch string(line) { case "State : connected": isup = true case "State : disconnected": isup = false default: continue } if name != "" { if v, ok := ifaces[name]; ok && v != isup { return fmt.Errorf("%s:%s isup=%v: ipv4 and ipv6 report different interface state", ipver, name, isup) } ifaces[name] = isup name = "" } } return nil } func TestInterfacesWithNetsh(t *testing.T) { checkNetsh(t) toString := func(name string, isup bool) string { if isup { return name + ":up" } return name + ":down" } ift, err := Interfaces() if err != nil { t.Fatal(err) } have := make([]string, 0) for _, ifi := range ift { have = append(have, toString(ifi.Name, ifi.Flags&FlagUp != 0)) } sort.Strings(have) ifaces := make(map[string]bool) err = netshInterfaceIPShowInterface("ipv6", ifaces) if err != nil { t.Fatal(err) } err = netshInterfaceIPShowInterface("ipv4", ifaces) if err != nil { t.Fatal(err) } want := make([]string, 0) for name, isup := range ifaces { want = append(want, toString(name, isup)) } sort.Strings(want) if strings.Join(want, "/") != strings.Join(have, "/") { t.Fatalf("unexpected interface list %q, want %q", have, want) } } func netshInterfaceIPv4ShowAddress(name string, netshOutput []byte) []string { // Address information is listed like: // //Configuration for interface "Local Area Connection" // DHCP enabled: Yes // IP Address: 10.0.0.2 // Subnet Prefix: 10.0.0.0/24 (mask 255.255.255.0) // IP Address: 10.0.0.3 // Subnet Prefix: 10.0.0.0/24 (mask 255.255.255.0) // Default Gateway: 10.0.0.254 // Gateway Metric: 0 // InterfaceMetric: 10 // //Configuration for interface "Loopback Pseudo-Interface 1" // DHCP enabled: No // IP Address: 127.0.0.1 // Subnet Prefix: 127.0.0.0/8 (mask 255.0.0.0) // InterfaceMetric: 50 // addrs := make([]string, 0) var addr, subnetprefix string var processingOurInterface bool lines := bytes.Split(netshOutput, []byte{'\r', '\n'}) for _, line := range lines { if !processingOurInterface { if !bytes.HasPrefix(line, []byte("Configuration for interface")) { continue } if !bytes.Contains(line, []byte(`"`+name+`"`)) { continue } processingOurInterface = true continue } if len(line) == 0 { break } if bytes.Contains(line, []byte("Subnet Prefix:")) { f := bytes.Split(line, []byte{':'}) if len(f) == 2 { f = bytes.Split(f[1], []byte{'('}) if len(f) == 2 { f = bytes.Split(f[0], []byte{'/'}) if len(f) == 2 { subnetprefix = string(bytes.TrimSpace(f[1])) if addr != "" && subnetprefix != "" { addrs = append(addrs, addr+"/"+subnetprefix) } } } } } addr = "" if bytes.Contains(line, []byte("IP Address:")) { f := bytes.Split(line, []byte{':'}) if len(f) == 2 { addr = string(bytes.TrimSpace(f[1])) } } } return addrs } func netshInterfaceIPv6ShowAddress(name string, netshOutput []byte) []string { // Address information is listed like: // //Address ::1 Parameters //--------------------------------------------------------- //Interface Luid : Loopback Pseudo-Interface 1 //Scope Id : 0.0 //Valid Lifetime : infinite //Preferred Lifetime : infinite //DAD State : Preferred //Address Type : Other //Skip as Source : false // //Address XXXX::XXXX:XXXX:XXXX:XXXX%11 Parameters //--------------------------------------------------------- //Interface Luid : Local Area Connection //Scope Id : 0.11 //Valid Lifetime : infinite //Preferred Lifetime : infinite //DAD State : Preferred //Address Type : Other //Skip as Source : false // // TODO: need to test ipv6 netmask too, but netsh does not outputs it var addr string addrs := make([]string, 0) lines := bytes.Split(netshOutput, []byte{'\r', '\n'}) for _, line := range lines { if addr != "" { if len(line) == 0 { addr = "" continue } if string(line) != "Interface Luid : "+name { continue } addrs = append(addrs, addr) addr = "" continue } if !bytes.HasPrefix(line, []byte("Address")) { continue } if !bytes.HasSuffix(line, []byte("Parameters")) { continue } f := bytes.Split(line, []byte{' '}) if len(f) != 3 { continue } // remove scope ID if present f = bytes.Split(f[1], []byte{'%'}) // netsh can create IPv4-embedded IPv6 addresses, like fe80::5efe:192.168.140.1. // Convert these to all hexadecimal fe80::5efe:c0a8:8c01 for later string comparisons. ipv4Tail := regexp.MustCompile(`:\d+\.\d+\.\d+\.\d+$`) if ipv4Tail.Match(f[0]) { f[0] = []byte(ParseIP(string(f[0])).String()) } addr = string(bytes.ToLower(bytes.TrimSpace(f[0]))) } return addrs } func TestInterfaceAddrsWithNetsh(t *testing.T) { checkNetsh(t) outIPV4, err := runCmd("netsh", "interface", "ipv4", "show", "address") if err != nil { t.Fatal(err) } outIPV6, err := runCmd("netsh", "interface", "ipv6", "show", "address", "level=verbose") if err != nil { t.Fatal(err) } ift, err := Interfaces() if err != nil { t.Fatal(err) } for _, ifi := range ift { // Skip the interface if it's down. if (ifi.Flags & FlagUp) == 0 { continue } have := make([]string, 0) addrs, err := ifi.Addrs() if err != nil { t.Fatal(err) } for _, addr := range addrs { switch addr := addr.(type) { case *IPNet: if addr.IP.To4() != nil { have = append(have, addr.String()) } if addr.IP.To16() != nil && addr.IP.To4() == nil { // netsh does not output netmask for ipv6, so ignore ipv6 mask have = append(have, addr.IP.String()) } case *IPAddr: if addr.IP.To4() != nil { have = append(have, addr.String()) } if addr.IP.To16() != nil && addr.IP.To4() == nil { // netsh does not output netmask for ipv6, so ignore ipv6 mask have = append(have, addr.IP.String()) } } } sort.Strings(have) want := netshInterfaceIPv4ShowAddress(ifi.Name, outIPV4) wantIPv6 := netshInterfaceIPv6ShowAddress(ifi.Name, outIPV6) want = append(want, wantIPv6...) sort.Strings(want) if strings.Join(want, "/") != strings.Join(have, "/") { t.Errorf("%s: unexpected addresses list %q, want %q", ifi.Name, have, want) } } } // check that getmac exists as a powershell command, and that it // speaks English. func checkGetmac(t *testing.T) { out, err := runCmd("getmac", "/?") if err != nil { if strings.Contains(err.Error(), "term 'getmac' is not recognized as the name of a cmdlet") { t.Skipf("getmac not available") } t.Fatal(err) } if !bytes.Contains(out, []byte("network adapters on a system")) { t.Skipf("skipping test on non-English system") } } func TestInterfaceHardwareAddrWithGetmac(t *testing.T) { checkGetmac(t) ift, err := Interfaces() if err != nil { t.Fatal(err) } have := make(map[string]string) for _, ifi := range ift { if ifi.Flags&FlagLoopback != 0 { // no MAC address for loopback interfaces continue } have[ifi.Name] = ifi.HardwareAddr.String() } out, err := runCmd("getmac", "/fo", "list", "/v") if err != nil { t.Fatal(err) } // getmac output looks like: // //Connection Name: Local Area Connection //Network Adapter: Intel Gigabit Network Connection //Physical Address: XX-XX-XX-XX-XX-XX //Transport Name: \Device\Tcpip_{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} // //Connection Name: Wireless Network Connection //Network Adapter: Wireles WLAN Card //Physical Address: XX-XX-XX-XX-XX-XX //Transport Name: Media disconnected // //Connection Name: Bluetooth Network Connection //Network Adapter: Bluetooth Device (Personal Area Network) //Physical Address: N/A //Transport Name: Hardware not present // //Connection Name: VMware Network Adapter VMnet8 //Network Adapter: VMware Virtual Ethernet Adapter for VMnet8 //Physical Address: Disabled //Transport Name: Disconnected // want := make(map[string]string) group := make(map[string]string) // name / values for single adapter getValue := func(name string) string { value, found := group[name] if !found { t.Fatalf("%q has no %q line in it", group, name) } if value == "" { t.Fatalf("%q has empty %q value", group, name) } return value } processGroup := func() { if len(group) == 0 { return } tname := strings.ToLower(getValue("Transport Name")) if tname == "n/a" { // skip these return } addr := strings.ToLower(getValue("Physical Address")) if addr == "disabled" || addr == "n/a" { // skip these return } addr = strings.ReplaceAll(addr, "-", ":") cname := getValue("Connection Name") want[cname] = addr group = make(map[string]string) } lines := bytes.Split(out, []byte{'\r', '\n'}) for _, line := range lines { if len(line) == 0 { processGroup() continue } i := bytes.IndexByte(line, ':') if i == -1 { t.Fatalf("line %q has no : in it", line) } group[string(line[:i])] = string(bytes.TrimSpace(line[i+1:])) } processGroup() dups := make(map[string][]string) for name, addr := range want { if _, ok := dups[addr]; !ok { dups[addr] = make([]string, 0) } dups[addr] = append(dups[addr], name) } nextWant: for name, wantAddr := range want { if haveAddr, ok := have[name]; ok { if haveAddr != wantAddr { t.Errorf("unexpected MAC address for %q - %v, want %v", name, haveAddr, wantAddr) } continue } // We could not find the interface in getmac output by name. // But sometimes getmac lists many interface names // for the same MAC address. If that is the case here, // and we can match at least one of those names, // let's ignore the other names. if dupNames, ok := dups[wantAddr]; ok && len(dupNames) > 1 { for _, dupName := range dupNames { if haveAddr, ok := have[dupName]; ok && haveAddr == wantAddr { continue nextWant } } } t.Errorf("getmac lists %q, but it could not be found among Go interfaces %v", name, have) } }
Upload File
Create Folder