This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcommand_test.go
199 lines (165 loc) · 4.54 KB
/
command_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package mdm
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
"github.com/groob/plist"
)
// Basic tests will attempt to marshal and unmarshal mdm command structures to identify any naming or tag errors.
// Make sure a command can be marshalled to json
func testMarshalJSON(t *testing.T, cmd interface{}) {
jsonCmd, err := json.Marshal(cmd)
if err != nil {
t.Error(err)
}
fmt.Println(string(jsonCmd))
}
// Make sure a command can be marshalled to plist
func testMarshalPlist(t *testing.T, cmd interface{}) {
plistCmd, err := plist.MarshalIndent(cmd, "\t")
if err != nil {
t.Error(err)
}
fmt.Println(string(plistCmd))
}
func TestInstallProfile(t *testing.T) {
cmd := InstallProfile{Payload: []byte{00}}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestRemoveProfile(t *testing.T) {
cmd := RemoveProfile{Identifier: "io.micromdm.test.profile"}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestInstallProvisioningProfile(t *testing.T) {
cmd := InstallProvisioningProfile{ProvisioningProfile: []byte{00}}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestRemoveProvisioningProfile(t *testing.T) {
cmd := RemoveProvisioningProfile{UUID: "1111-2222-3333"}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestInstalledApplicationList(t *testing.T) {
cmd := InstalledApplicationList{Identifiers: []string{"io.micromdm.application"}, ManagedAppsOnly: true}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestDeviceInformation(t *testing.T) {
cmd := DeviceInformation{Queries: []string{"SerialNumber"}}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestDeviceLock(t *testing.T) {
cmd := DeviceLock{PIN: "123456", Message: "Locked", PhoneNumber: "123-4567"}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestClearPasscode(t *testing.T) {
cmd := ClearPasscode{UnlockToken: []byte{00}}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestEraseDevice(t *testing.T) {
cmd := EraseDevice{PIN: "123456"}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestRequestMirroring(t *testing.T) {
cmd := RequestMirroring{DestinationName: "Apple TV", ScanTime: "30", Password: "sekret"}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
//func TestRestrictions(t *testing.T) {
// cmd := Restrictions{}
//}
func TestSettings(t *testing.T) {
cmd := Settings{
Settings: []Setting{
Setting{
Item: "DeviceName",
DeviceName: stringPtr("foo"),
},
},
}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func stringPtr(s string) *string {
return &s
}
func TestDeleteUser(t *testing.T) {
cmd := DeleteUser{UserName: "joe", ForceDeletion: false}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestEnableLostMode(t *testing.T) {
cmd := EnableLostMode{Message: "Lost!", PhoneNumber: "123-4567", Footnote: "This is lost"}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestInstallApplication(t *testing.T) {
cmd := InstallApplication{ITunesStoreID: 1234567}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestApplyRedemptionCode(t *testing.T) {
cmd := ApplyRedemptionCode{Identifier: "id", RedemptionCode: "abcdefg"}
testMarshalJSON(t, cmd)
testMarshalPlist(t, cmd)
}
func TestMarshalWithOverlappingKeys(t *testing.T) {
rp := RemoveProfile{Identifier: "io.micromdm.test.profile"}
cmd := Command{RequestType: "RemoveProfile", RemoveProfile: rp}
out, err := plist.Marshal(&cmd)
if err != nil {
t.Fatal(err)
}
// check that output contains Identifier field
if !bytes.Contains(out, []byte("Identifier")) {
t.Errorf("expected %v plist to contain identifier key, got \n%s", cmd.RequestType, out)
}
}
func TestUnmarshalOverlaplingKeys(t *testing.T) {
data := []byte(`{
"request_type":"RemoveProfile",
"udid":"abcd",
"identifier":"aaaa"
}`)
var req CommandRequest
if err := json.Unmarshal(data, &req); err != nil {
t.Fatal(err)
}
if req.RemoveProfile.Identifier != "aaaa" {
t.Errorf("expected RemoveProfile struct with Identifier field")
}
}
func Test_PayloadWithNoFields(t *testing.T) {
data := []byte(`{
"request_type":"ProfileList",
"udid":"abcd"
}`)
var req CommandRequest
if err := json.Unmarshal(data, &req); err != nil {
t.Fatal(err)
}
payload, err := NewPayload(&req)
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
if err := plist.NewEncoder(buf).Encode(payload); err != nil {
t.Fatal(err)
}
var have Payload
if err := plist.NewDecoder(buf).Decode(&have); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(have, *payload) {
t.Errorf("have %+v\n want %+v", have, *payload)
}
}