]> Cypherpunks repositories - gostls13.git/commitdiff
net: move MAC address parser into distinct file
authorMikio Hara <mikioh.mikioh@gmail.com>
Sun, 4 Mar 2012 22:42:07 +0000 (07:42 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Sun, 4 Mar 2012 22:42:07 +0000 (07:42 +0900)
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5727054

src/pkg/net/interface.go
src/pkg/net/interface_test.go
src/pkg/net/mac.go [new file with mode: 0644]
src/pkg/net/mac_test.go [new file with mode: 0644]

index 5e7b352ed50012943a323d6c5847d7d281953e2a..f25d046c171ea1bb4b512230b2a922726f3540b1 100644 (file)
@@ -6,11 +6,7 @@
 
 package net
 
-import (
-       "bytes"
-       "errors"
-       "fmt"
-)
+import "errors"
 
 var (
        errInvalidInterface         = errors.New("net: invalid interface")
@@ -20,77 +16,6 @@ var (
        errNoSuchMulticastInterface = errors.New("net: no such multicast interface")
 )
 
-// A HardwareAddr represents a physical hardware address.
-type HardwareAddr []byte
-
-func (a HardwareAddr) String() string {
-       var buf bytes.Buffer
-       for i, b := range a {
-               if i > 0 {
-                       buf.WriteByte(':')
-               }
-               fmt.Fprintf(&buf, "%02x", b)
-       }
-       return buf.String()
-}
-
-// ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, or EUI-64 using one of the
-// following formats:
-//   01:23:45:67:89:ab
-//   01:23:45:67:89:ab:cd:ef
-//   01-23-45-67-89-ab
-//   01-23-45-67-89-ab-cd-ef
-//   0123.4567.89ab
-//   0123.4567.89ab.cdef
-func ParseMAC(s string) (hw HardwareAddr, err error) {
-       if len(s) < 14 {
-               goto error
-       }
-
-       if s[2] == ':' || s[2] == '-' {
-               if (len(s)+1)%3 != 0 {
-                       goto error
-               }
-               n := (len(s) + 1) / 3
-               if n != 6 && n != 8 {
-                       goto error
-               }
-               hw = make(HardwareAddr, n)
-               for x, i := 0, 0; i < n; i++ {
-                       var ok bool
-                       if hw[i], ok = xtoi2(s[x:], s[2]); !ok {
-                               goto error
-                       }
-                       x += 3
-               }
-       } else if s[4] == '.' {
-               if (len(s)+1)%5 != 0 {
-                       goto error
-               }
-               n := 2 * (len(s) + 1) / 5
-               if n != 6 && n != 8 {
-                       goto error
-               }
-               hw = make(HardwareAddr, n)
-               for x, i := 0, 0; i < n; i += 2 {
-                       var ok bool
-                       if hw[i], ok = xtoi2(s[x:x+2], 0); !ok {
-                               goto error
-                       }
-                       if hw[i+1], ok = xtoi2(s[x+2:], s[4]); !ok {
-                               goto error
-                       }
-                       x += 5
-               }
-       } else {
-               goto error
-       }
-       return hw, nil
-
-error:
-       return nil, errors.New("invalid MAC address: " + s)
-}
-
 // Interface represents a mapping between network interface name
 // and index.  It also represents network interface facility
 // information.
index 769414e0eef8c78ff0be6ee9ced7095d138240a8..0a33bfdb517fe6eacddb100b5793a5621f3987c8 100644 (file)
@@ -6,8 +6,6 @@ package net
 
 import (
        "bytes"
-       "reflect"
-       "strings"
        "testing"
 )
 
@@ -96,46 +94,3 @@ func testMulticastAddrs(t *testing.T, ifmat []Addr) {
                }
        }
 }
-
-var mactests = []struct {
-       in  string
-       out HardwareAddr
-       err string
-}{
-       {"01:23:45:67:89:AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
-       {"01-23-45-67-89-AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
-       {"0123.4567.89AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
-       {"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""},
-       {"01.02.03.04.05.06", nil, "invalid MAC address"},
-       {"01:02:03:04:05:06:", nil, "invalid MAC address"},
-       {"x1:02:03:04:05:06", nil, "invalid MAC address"},
-       {"01002:03:04:05:06", nil, "invalid MAC address"},
-       {"01:02003:04:05:06", nil, "invalid MAC address"},
-       {"01:02:03004:05:06", nil, "invalid MAC address"},
-       {"01:02:03:04005:06", nil, "invalid MAC address"},
-       {"01:02:03:04:05006", nil, "invalid MAC address"},
-       {"01-02:03:04:05:06", nil, "invalid MAC address"},
-       {"01:02-03-04-05-06", nil, "invalid MAC address"},
-       {"0123:4567:89AF", nil, "invalid MAC address"},
-       {"0123-4567-89AF", nil, "invalid MAC address"},
-       {"01:23:45:67:89:AB:CD:EF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
-       {"01-23-45-67-89-AB-CD-EF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
-       {"0123.4567.89AB.CDEF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
-}
-
-func match(err error, s string) bool {
-       if s == "" {
-               return err == nil
-       }
-       return err != nil && strings.Contains(err.Error(), s)
-}
-
-func TestParseMAC(t *testing.T) {
-       for _, tt := range mactests {
-               out, err := ParseMAC(tt.in)
-               if !reflect.DeepEqual(out, tt.out) || !match(err, tt.err) {
-                       t.Errorf("ParseMAC(%q) = %v, %v, want %v, %v", tt.in, out, err, tt.out,
-                               tt.err)
-               }
-       }
-}
diff --git a/src/pkg/net/mac.go b/src/pkg/net/mac.go
new file mode 100644 (file)
index 0000000..e0637d0
--- /dev/null
@@ -0,0 +1,84 @@
+// Copyright 2011 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.
+
+// MAC address manipulations
+
+package net
+
+import (
+       "bytes"
+       "errors"
+       "fmt"
+)
+
+// A HardwareAddr represents a physical hardware address.
+type HardwareAddr []byte
+
+func (a HardwareAddr) String() string {
+       var buf bytes.Buffer
+       for i, b := range a {
+               if i > 0 {
+                       buf.WriteByte(':')
+               }
+               fmt.Fprintf(&buf, "%02x", b)
+       }
+       return buf.String()
+}
+
+// ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, or EUI-64 using one of the
+// following formats:
+//   01:23:45:67:89:ab
+//   01:23:45:67:89:ab:cd:ef
+//   01-23-45-67-89-ab
+//   01-23-45-67-89-ab-cd-ef
+//   0123.4567.89ab
+//   0123.4567.89ab.cdef
+func ParseMAC(s string) (hw HardwareAddr, err error) {
+       if len(s) < 14 {
+               goto error
+       }
+
+       if s[2] == ':' || s[2] == '-' {
+               if (len(s)+1)%3 != 0 {
+                       goto error
+               }
+               n := (len(s) + 1) / 3
+               if n != 6 && n != 8 {
+                       goto error
+               }
+               hw = make(HardwareAddr, n)
+               for x, i := 0, 0; i < n; i++ {
+                       var ok bool
+                       if hw[i], ok = xtoi2(s[x:], s[2]); !ok {
+                               goto error
+                       }
+                       x += 3
+               }
+       } else if s[4] == '.' {
+               if (len(s)+1)%5 != 0 {
+                       goto error
+               }
+               n := 2 * (len(s) + 1) / 5
+               if n != 6 && n != 8 {
+                       goto error
+               }
+               hw = make(HardwareAddr, n)
+               for x, i := 0, 0; i < n; i += 2 {
+                       var ok bool
+                       if hw[i], ok = xtoi2(s[x:x+2], 0); !ok {
+                               goto error
+                       }
+                       if hw[i+1], ok = xtoi2(s[x+2:], s[4]); !ok {
+                               goto error
+                       }
+                       x += 5
+               }
+       } else {
+               goto error
+       }
+       return hw, nil
+
+error:
+       return nil, errors.New("invalid MAC address: " + s)
+}
diff --git a/src/pkg/net/mac_test.go b/src/pkg/net/mac_test.go
new file mode 100644 (file)
index 0000000..3837e74
--- /dev/null
@@ -0,0 +1,54 @@
+// Copyright 2011 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 (
+       "reflect"
+       "strings"
+       "testing"
+)
+
+var mactests = []struct {
+       in  string
+       out HardwareAddr
+       err string
+}{
+       {"01:23:45:67:89:AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
+       {"01-23-45-67-89-AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
+       {"0123.4567.89AB", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab}, ""},
+       {"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""},
+       {"01.02.03.04.05.06", nil, "invalid MAC address"},
+       {"01:02:03:04:05:06:", nil, "invalid MAC address"},
+       {"x1:02:03:04:05:06", nil, "invalid MAC address"},
+       {"01002:03:04:05:06", nil, "invalid MAC address"},
+       {"01:02003:04:05:06", nil, "invalid MAC address"},
+       {"01:02:03004:05:06", nil, "invalid MAC address"},
+       {"01:02:03:04005:06", nil, "invalid MAC address"},
+       {"01:02:03:04:05006", nil, "invalid MAC address"},
+       {"01-02:03:04:05:06", nil, "invalid MAC address"},
+       {"01:02-03-04-05-06", nil, "invalid MAC address"},
+       {"0123:4567:89AF", nil, "invalid MAC address"},
+       {"0123-4567-89AF", nil, "invalid MAC address"},
+       {"01:23:45:67:89:AB:CD:EF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
+       {"01-23-45-67-89-AB-CD-EF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
+       {"0123.4567.89AB.CDEF", HardwareAddr{1, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}, ""},
+}
+
+func match(err error, s string) bool {
+       if s == "" {
+               return err == nil
+       }
+       return err != nil && strings.Contains(err.Error(), s)
+}
+
+func TestParseMAC(t *testing.T) {
+       for _, tt := range mactests {
+               out, err := ParseMAC(tt.in)
+               if !reflect.DeepEqual(out, tt.out) || !match(err, tt.err) {
+                       t.Errorf("ParseMAC(%q) = %v, %v, want %v, %v", tt.in, out, err, tt.out,
+                               tt.err)
+               }
+       }
+}