-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmacaddress_test.go
59 lines (54 loc) · 1.26 KB
/
macaddress_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package egoscale
import (
"encoding/json"
"testing"
)
func TestMACAddressMustParse(t *testing.T) {
defer func() {
if r := recover(); r != nil {
_, ok := r.(error)
if !ok {
t.Error(r)
}
}
}()
MustParseMAC("foo")
t.Error("invalid mac should panic")
}
func TestMACAddressMarshalJSON(t *testing.T) {
nic := &Nic{
MACAddress: MAC48(0x01, 0x23, 0x45, 0x67, 0x89, 0xab),
}
j, err := json.Marshal(nic)
if err != nil {
t.Fatal(err)
}
s := string(j)
expected := `{"macaddress":"01:23:45:67:89:ab"}`
if expected != s {
t.Errorf("bad json serialization, got %q, expected %s", s, expected)
}
}
func TestMACAddressUnmarshalJSON(t *testing.T) {
s := `{"macaddress": "01:23:45:67:89:ab"}`
nic := &Nic{}
if err := json.Unmarshal([]byte(s), nic); err != nil {
t.Errorf("no errors were expected, %s", err)
} else if nic.MACAddress == nil {
t.Errorf("a macaddress was expected, got %+v", nic)
}
}
func TestMACAddressUnmarshalJSONFailure(t *testing.T) {
ss := []string{
`{"macaddress": 123}`,
`{"macaddress": "123"}`,
`{"macaddress": "01:23:45:67:89:a"}`,
`{"macaddress": "01:23:45:67:89:ab\""}`,
}
nic := &Nic{}
for _, s := range ss {
if err := json.Unmarshal([]byte(s), nic); err == nil {
t.Errorf("an error was expected, %#v", nic)
}
}
}