Skip to content

Commit

Permalink
net: add TestInterfaceHardwareAddrWithGetmac
Browse files Browse the repository at this point in the history
Use Windows getmac command to verify interface
MAC addresses net package returns.

The test is to be enabled once issue golang#12691 is fixed.

Updates golang#12691

Change-Id: Ic28c83303590cb4d48ee025250d4b6e30683bfd4
Reviewed-on: https://go-review.googlesource.com/17632
Reviewed-by: Mikio Hara <[email protected]>
Run-TryBot: Alex Brainman <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
alexbrainman committed Dec 9, 2015
1 parent 2fb931d commit 9a89ac3
Showing 1 changed file with 86 additions and 8 deletions.
94 changes: 86 additions & 8 deletions src/net/net_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -194,17 +194,17 @@ 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())
if err2 != nil {
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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 9a89ac3

Please sign in to comment.