-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugger_test.go
90 lines (79 loc) · 3 KB
/
plugger_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package plugger
import (
"reflect"
"testing"
)
type testCaseContact struct {
contact Contact
raw []byte
}
// testContacts are test cases for the (un)marshalling of contacts.
var testContacts = []testCaseContact{
{
contact: Contact{Name: "Valid 1", Type: ContactGroup, ID: 1, Tone: false},
raw: []byte{0x01, 0x00, 0x00, 0xC1, 0x56, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x69, 0x00,
0x64, 0x00, 0x20, 0x00, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
contact: Contact{Name: "Valid 2", Type: ContactGroup, ID: 2, Tone: true},
raw: []byte{0x02, 0x00, 0x00, 0xE1, 0x56, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x69, 0x00,
0x64, 0x00, 0x20, 0x00, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
contact: Contact{Name: "Valid 3", Type: ContactPrivate, ID: 3, Tone: false},
raw: []byte{0x03, 0x00, 0x00, 0xC2, 0x56, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x69, 0x00,
0x64, 0x00, 0x20, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
contact: Contact{Name: "Valid 4", Type: ContactPrivate, ID: 4, Tone: true},
raw: []byte{0x04, 0x00, 0x00, 0xE2, 0x56, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x69, 0x00,
0x64, 0x00, 0x20, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
contact: Contact{Name: "Valid 5", Type: ContactAll, ID: 16777215, Tone: false},
raw: []byte{0xFF, 0xFF, 0xFF, 0xC3, 0x56, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x69, 0x00,
0x64, 0x00, 0x20, 0x00, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
}
// testDeepEqual takes a format string, expected, and actual values. If the
// values do not match (using reflect.DeepEqual), the values are printed
// together and a testing error state is set.
func testDeepEqual(t *testing.T, f string, exp, actual interface{}) bool {
res := reflect.DeepEqual(exp, actual)
if !res {
t.Logf("Expected value: "+f+"\n", exp)
t.Logf("Actual value: "+f+"\n", actual)
t.Errorf("Expected and actual values do not match")
}
return res
}
// TestContactBinaryMarshal checks that the defined test cases marshal properly
// from structs to binary.
func TestContactBinaryMarshal(t *testing.T) {
for i, test := range testContacts {
t.Logf("Marshalling contact %d...\n", i)
res, err := test.contact.MarshalBinary()
if err != nil {
t.Error(err)
}
testDeepEqual(t, "%x", res, test.raw)
}
}
// TestContactBinaryUnmarshal checks that the defined test cases unmarshal
// properly from binary to structs.
func TestContactBinaryUnmarshal(t *testing.T) {
for i, test := range testContacts {
t.Logf("Unmarshalling contact %d...\n", i)
res := NewContact()
err := res.UnmarshalBinary(test.raw)
if err != nil {
t.Error(err)
}
testDeepEqual(t, "%+v", res, test.contact)
}
}