Skip to content

Commit 0927b47

Browse files
committed
net: adjust TestInterfaceHardwareAddrWithGetmac
Ignore adapters with "Transport Name: N/A" line in getmac command output. This allows us to skip duplicate MAC addresses. Fixes #19537. Change-Id: I6b7be9d31322f963e02023c8f1037f6e9042b479 Reviewed-on: https://go-review.googlesource.com/39071 Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Avelino <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent b5e964c commit 0927b47

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

src/net/net_windows_test.go

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -564,39 +564,49 @@ func TestInterfaceHardwareAddrWithGetmac(t *testing.T) {
564564
//Transport Name: Disconnected
565565
//
566566
want := make(map[string]string)
567-
var name string
567+
group := make(map[string]string) // name / values for single adapter
568+
getValue := func(name string) string {
569+
value, found := group[name]
570+
if !found {
571+
t.Fatalf("%q has no %q line in it", group, name)
572+
}
573+
if value == "" {
574+
t.Fatalf("%q has empty %q value", group, name)
575+
}
576+
return value
577+
}
578+
processGroup := func() {
579+
if len(group) == 0 {
580+
return
581+
}
582+
tname := strings.ToLower(getValue("Transport Name"))
583+
if tname == "n/a" {
584+
// skip these
585+
return
586+
}
587+
addr := strings.ToLower(getValue("Physical Address"))
588+
if addr == "disabled" || addr == "n/a" {
589+
// skip these
590+
return
591+
}
592+
addr = strings.Replace(addr, "-", ":", -1)
593+
cname := getValue("Connection Name")
594+
want[cname] = addr
595+
group = nil
596+
}
568597
lines := bytes.Split(out, []byte{'\r', '\n'})
569598
for _, line := range lines {
570-
if bytes.Contains(line, []byte("Connection Name:")) {
571-
f := bytes.Split(line, []byte{':'})
572-
if len(f) != 2 {
573-
t.Fatalf("unexpected \"Connection Name\" line: %q", line)
574-
}
575-
name = string(bytes.TrimSpace(f[1]))
576-
if name == "" {
577-
t.Fatalf("empty name on \"Connection Name\" line: %q", line)
578-
}
599+
if len(line) == 0 {
600+
processGroup()
601+
continue
579602
}
580-
if bytes.Contains(line, []byte("Physical Address:")) {
581-
if name == "" {
582-
t.Fatalf("no matching name found: %q", string(out))
583-
}
584-
f := bytes.Split(line, []byte{':'})
585-
if len(f) != 2 {
586-
t.Fatalf("unexpected \"Physical Address\" line: %q", line)
587-
}
588-
addr := string(bytes.ToLower(bytes.TrimSpace(f[1])))
589-
if addr == "" {
590-
t.Fatalf("empty address on \"Physical Address\" line: %q", line)
591-
}
592-
if addr == "disabled" || addr == "n/a" {
593-
continue
594-
}
595-
addr = strings.Replace(addr, "-", ":", -1)
596-
want[name] = addr
597-
name = ""
603+
i := bytes.IndexByte(line, ':')
604+
if i == -1 {
605+
t.Fatalf("line %q has no : in it", line)
598606
}
607+
group[string(line[:i])] = string(bytes.TrimSpace(line[i+1:]))
599608
}
609+
processGroup()
600610

601611
for name, wantAddr := range want {
602612
haveAddr, ok := have[name]

0 commit comments

Comments
 (0)