mirror of
https://github.com/golang/go
synced 2024-11-17 15:54:39 -07:00
net: add TestInterfaceHardwareAddrWithGetmac
Use Windows getmac command to verify interface MAC addresses net package returns. The test is to be enabled once issue #12691 is fixed. Updates #12691 Change-Id: Ic28c83303590cb4d48ee025250d4b6e30683bfd4 Reviewed-on: https://go-review.googlesource.com/17632 Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com> Run-TryBot: Alex Brainman <alex.brainman@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
2fb931d08f
commit
9a89ac35fe
@ -177,14 +177,14 @@ func isWindowsXP(t *testing.T) bool {
|
||||
return major < 6
|
||||
}
|
||||
|
||||
func runNetsh(args ...string) ([]byte, error) {
|
||||
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 := ioutil.TempFile("", "netsh")
|
||||
f, err := ioutil.TempFile("", "netcmd")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -194,7 +194,7 @@ func runNetsh(args ...string) ([]byte, error) {
|
||||
out, err := exec.Command("powershell", "-Command", cmd).CombinedOutput()
|
||||
if err != nil {
|
||||
if len(out) != 0 {
|
||||
return nil, fmt.Errorf("netsh failed: %v: %q", err, string(removeUTF8BOM(out)))
|
||||
return nil, fmt.Errorf("%s failed: %v: %q", args[0], err, string(removeUTF8BOM(out)))
|
||||
}
|
||||
var err2 error
|
||||
out, err2 = ioutil.ReadFile(f.Name())
|
||||
@ -202,9 +202,9 @@ func runNetsh(args ...string) ([]byte, error) {
|
||||
return nil, err2
|
||||
}
|
||||
if len(out) != 0 {
|
||||
return nil, fmt.Errorf("netsh failed: %v: %q", err, string(removeUTF8BOM(out)))
|
||||
return nil, fmt.Errorf("%s failed: %v: %q", args[0], err, string(removeUTF8BOM(out)))
|
||||
}
|
||||
return nil, fmt.Errorf("netsh failed: %v", err)
|
||||
return nil, fmt.Errorf("%s failed: %v", args[0], err)
|
||||
}
|
||||
out, err = ioutil.ReadFile(f.Name())
|
||||
if err != nil {
|
||||
@ -214,7 +214,7 @@ func runNetsh(args ...string) ([]byte, error) {
|
||||
}
|
||||
|
||||
func netshInterfaceIPShowConfig() ([]string, error) {
|
||||
out, err := runNetsh("netsh", "interface", "ip", "show", "config")
|
||||
out, err := runCmd("netsh", "interface", "ip", "show", "config")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -255,7 +255,7 @@ func TestInterfacesWithNetsh(t *testing.T) {
|
||||
}
|
||||
|
||||
func netshInterfaceIPv4ShowAddress(name string) ([]string, error) {
|
||||
out, err := runNetsh("netsh", "interface", "ipv4", "show", "address", "name=\""+name+"\"")
|
||||
out, err := runCmd("netsh", "interface", "ipv4", "show", "address", "name=\""+name+"\"")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -296,7 +296,7 @@ func netshInterfaceIPv4ShowAddress(name string) ([]string, error) {
|
||||
|
||||
func netshInterfaceIPv6ShowAddress(name string) ([]string, error) {
|
||||
// TODO: need to test ipv6 netmask too, but netsh does not outputs it
|
||||
out, err := runNetsh("netsh", "interface", "ipv6", "show", "address", "interface=\""+name+"\"")
|
||||
out, err := runCmd("netsh", "interface", "ipv6", "show", "address", "interface=\""+name+"\"")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -373,3 +373,81 @@ func TestInterfaceAddrsWithNetsh(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInterfaceHardwareAddrWithGetmac(t *testing.T) {
|
||||
t.Skip("skipping test; see https://golang.org/issue/12691")
|
||||
if isWindowsXP(t) {
|
||||
t.Skip("Windows XP does not have powershell command")
|
||||
}
|
||||
ift, err := Interfaces()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
have := make([]string, 0)
|
||||
for _, ifi := range ift {
|
||||
if ifi.Flags&FlagLoopback != 0 {
|
||||
// no MAC for loopback interfaces
|
||||
continue
|
||||
}
|
||||
have = append(have, ifi.Name+"="+ifi.HardwareAddr.String())
|
||||
}
|
||||
sort.Strings(have)
|
||||
|
||||
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: XX-XX-XX-XX-XX-XX
|
||||
//Transport Name: Media disconnected
|
||||
//
|
||||
want := make([]string, 0)
|
||||
var name string
|
||||
lines := bytes.Split(out, []byte{'\r', '\n'})
|
||||
for _, line := range lines {
|
||||
if bytes.Contains(line, []byte("Connection Name:")) {
|
||||
f := bytes.Split(line, []byte{':'})
|
||||
if len(f) != 2 {
|
||||
t.Fatal("unexpected \"Connection Name\" line: %q", line)
|
||||
}
|
||||
name = string(bytes.TrimSpace(f[1]))
|
||||
if name == "" {
|
||||
t.Fatal("empty name on \"Connection Name\" line: %q", line)
|
||||
}
|
||||
}
|
||||
if bytes.Contains(line, []byte("Physical Address:")) {
|
||||
if name == "" {
|
||||
t.Fatal("no matching name found: %q", string(out))
|
||||
}
|
||||
f := bytes.Split(line, []byte{':'})
|
||||
if len(f) != 2 {
|
||||
t.Fatal("unexpected \"Physical Address\" line: %q", line)
|
||||
}
|
||||
addr := string(bytes.TrimSpace(f[1]))
|
||||
if addr == "" {
|
||||
t.Fatal("empty address on \"Physical Address\" line: %q", line)
|
||||
}
|
||||
addr = strings.Replace(addr, "-", ":", -1)
|
||||
want = append(want, name+"="+addr)
|
||||
name = ""
|
||||
}
|
||||
}
|
||||
sort.Strings(want)
|
||||
|
||||
if strings.Join(want, "/") != strings.Join(have, "/") {
|
||||
t.Fatalf("unexpected MAC addresses %q, want %q", have, want)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user