This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharchiver_test.go
91 lines (80 loc) · 2.64 KB
/
archiver_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
package nskeyedarchiver_test
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"testing"
archiver "github.com/danielpaulus/nskeyedarchiver"
"github.com/stretchr/testify/assert"
)
//TestDecoderJson tests if real DTX nsKeyedArchived plists can be decoded without error
func TestDecoderJson(t *testing.T) {
dat, err := ioutil.ReadFile("fixtures/payload_dump.json")
if err != nil {
log.Fatal(err)
}
var payloads []string
json.Unmarshal([]byte(dat), &payloads)
for _, plistHex := range payloads {
plistBytes, _ := hex.DecodeString(plistHex)
_, err := archiver.Unarchive(plistBytes)
assert.NoError(t, err)
}
}
func TestDecoder(t *testing.T) {
testCases := map[string]struct {
filename string
expected string
}{
"test one value": {"onevalue", "[true]"},
"test all primitives": {"primitives", "[1,1,1,1.5,\"YXNkZmFzZGZhZHNmYWRzZg==\",true,\"Hello, World!\",\"Hello, World!\",\"Hello, World!\",false,false,42]"},
"test arrays and sets": {"arrays", "[[1,1,1,1.5,\"YXNkZmFzZGZhZHNmYWRzZg==\",true,\"Hello, World!\",\"Hello, World!\",\"Hello, World!\",false,false,42],[true,\"Hello, World!\",42],[true],[42,true,\"Hello, World!\"]]"},
"test nested arrays": {"nestedarrays", "[[[true],[42,true,\"Hello, World!\"]]]"},
"test dictionaries": {"dict", "[{\"array\":[true,\"Hello, World!\",42],\"int\":1,\"string\":\"string\"}]"},
}
for _, tc := range testCases {
dat, err := ioutil.ReadFile("fixtures/" + tc.filename + ".xml")
if err != nil {
log.Fatal(err)
}
objects, err := archiver.Unarchive(dat)
assert.NoError(t, err)
assert.Equal(t, tc.expected, convertToJSON(objects))
dat, err = ioutil.ReadFile("fixtures/" + tc.filename + ".bin")
if err != nil {
log.Fatal(err)
}
objects, err = archiver.Unarchive(dat)
assert.Equal(t, tc.expected, convertToJSON(objects))
}
}
func TestValidation(t *testing.T) {
testCases := map[string]struct {
filename string
}{
"$archiver key is missing": {"missing_archiver"},
"$archiver is not nskeyedarchiver": {"wrong_archiver"},
"$top key is missing": {"missing_top"},
"$objects key is missing": {"missing_objects"},
"$version key is missing": {"missing_version"},
"$version is wrong": {"wrong_version"},
"plist is invalid": {"broken_plist"},
}
for _, tc := range testCases {
dat, err := ioutil.ReadFile("fixtures/" + tc.filename + ".xml")
if err != nil {
log.Fatal(err)
}
_, err = archiver.Unarchive(dat)
assert.Error(t, err)
}
}
func convertToJSON(obj interface{}) string {
b, err := json.Marshal(obj)
if err != nil {
fmt.Println("error:", err)
}
return string(b)
}