This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
marshal_test.go
81 lines (74 loc) · 2.1 KB
/
marshal_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
package command_test
import (
"encoding/json"
"io/ioutil"
"reflect"
"testing"
"github.com/groob/plist"
"github.com/micromdm/command"
"github.com/micromdm/mdm"
)
var marshalTests = []string{
"DeviceInformation",
"DeviceInformation_empty_queries",
"InstallProfile",
}
func TestMarshalEvent(t *testing.T) {
for _, tt := range marshalTests {
name := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
v := command.NewEvent(mustLoadPayload(t, name))
var other command.Event
if buf, err := command.MarshalEvent(v); err != nil {
t.Fatal(err)
} else if err := command.UnmarshalEvent(buf, &other); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(v, &other) {
t.Logf("\nwant: %#v\n, \nhave: %#v\n", v.Payload.Command, other.Payload.Command)
t.Fatalf("\nwant: %#v\n \nhave: %#v\n", v, other)
}
})
}
}
func BenchmarkMarshalProto(b *testing.B) {
for _, tt := range marshalTests {
v := command.NewEvent(mustLoadPayload(&testing.T{}, tt))
for n := 0; n < b.N; n++ {
var other command.Event
if buf, err := command.MarshalEvent(v); err != nil {
b.Fatal(err)
} else if err := command.UnmarshalEvent(buf, &other); err != nil {
b.Fatal(err)
} else if !reflect.DeepEqual(v, &other) {
b.Fatalf("\nwant: %#v\n \nhave: %#v\n", v, other)
}
}
}
}
func BenchmarkMarshalJSON(b *testing.B) {
for _, tt := range marshalTests {
v := command.NewEvent(mustLoadPayload(&testing.T{}, tt))
for n := 0; n < b.N; n++ {
var other command.Event
if buf, err := json.Marshal(&v); err != nil {
b.Fatal(err)
} else if err := json.Unmarshal(buf, &other); err != nil {
b.Fatal(err)
} else if !reflect.DeepEqual(v, &other) {
b.Fatalf("\nwant: %#v\n \nhave: %#v\n", v, other)
}
}
}
}
func mustLoadPayload(t *testing.T, name string) mdm.Payload {
var payload mdm.Payload
data, err := ioutil.ReadFile("testdata/" + name + ".plist")
if err != nil {
t.Fatalf("failed to open test file %q.plist, err: %s", name, err)
}
if err := plist.Unmarshal(data, &payload); err != nil {
t.Fatalf("failed to unmarshal plist %q, err: %s", name, err)
}
return payload
}