mirror of
https://github.com/golang/go
synced 2024-11-25 07:17:56 -07:00
net: don't import bytes or fmt in mac.go
Also add some more MAC tests. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5728065
This commit is contained in:
parent
eb5db57d1a
commit
82a9294d1b
@ -6,24 +6,26 @@
|
||||
|
||||
package net
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
import "errors"
|
||||
|
||||
const hexDigit = "0123456789abcdef"
|
||||
|
||||
// A HardwareAddr represents a physical hardware address.
|
||||
type HardwareAddr []byte
|
||||
|
||||
func (a HardwareAddr) String() string {
|
||||
var buf bytes.Buffer
|
||||
if len(a) == 0 {
|
||||
return ""
|
||||
}
|
||||
buf := make([]byte, 0, len(a)*3-1)
|
||||
for i, b := range a {
|
||||
if i > 0 {
|
||||
buf.WriteByte(':')
|
||||
buf = append(buf, ':')
|
||||
}
|
||||
fmt.Fprintf(&buf, "%02x", b)
|
||||
buf = append(buf, hexDigit[b>>4])
|
||||
buf = append(buf, hexDigit[b&0xF])
|
||||
}
|
||||
return buf.String()
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
// ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, or EUI-64 using one of the
|
||||
|
@ -43,12 +43,24 @@ func match(err error, s string) bool {
|
||||
return err != nil && strings.Contains(err.Error(), s)
|
||||
}
|
||||
|
||||
func TestParseMAC(t *testing.T) {
|
||||
for _, tt := range mactests {
|
||||
func TestMACParseString(t *testing.T) {
|
||||
for i, 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)
|
||||
}
|
||||
if tt.err == "" {
|
||||
// Verify that serialization works too, and that it round-trips.
|
||||
s := out.String()
|
||||
out2, err := ParseMAC(s)
|
||||
if err != nil {
|
||||
t.Errorf("%d. ParseMAC(%q) = %v", i, s, err)
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(out2, out) {
|
||||
t.Errorf("%d. ParseMAC(%q) = %v, want %v", i, s, out2, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user