Skip to content
This repository was archived by the owner on Mar 18, 2021. It is now read-only.

Commit be0de79

Browse files
committed
fix directoy structure
1 parent d3571ab commit be0de79

25 files changed

+1016
-12
lines changed

Diff for: archiver.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package nskeyedarchiver
2+
3+
/*
4+
type NSKeyedObject struct {
5+
isPrimitive bool
6+
primitive interface{}
7+
}
8+
9+
Todo:
10+
func ArchiveXML([]NSKeyedObject) string {
11+
return ""
12+
}
13+
func ArchiveBin([]NSKeyedObject) []byte {
14+
return make([]byte, 0)
15+
}
16+
*/

Diff for: archiver_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package nskeyedarchiver_test
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"testing"
9+
10+
archiver "github.com/danielpaulus/nskeyedarchiver"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestDecoder(t *testing.T) {
15+
testCases := map[string]struct {
16+
filename string
17+
expected string
18+
}{
19+
"test one value": {"onevalue", "[true]"},
20+
"test all primitives": {"primitives", "[1,1,1,1.5,\"YXNkZmFzZGZhZHNmYWRzZg==\",true,\"Hello, World!\",\"Hello, World!\",\"Hello, World!\",false,false,42]"},
21+
"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!\"]]"},
22+
"test nested arrays": {"nestedarrays", "[[[true],[42,true,\"Hello, World!\"]]]"},
23+
"test dictionaries": {"dict", "[{\"array\":[true,\"Hello, World!\",42],\"int\":1,\"string\":\"string\"}]"},
24+
}
25+
26+
for _, tc := range testCases {
27+
dat, err := ioutil.ReadFile("fixtures/" + tc.filename + ".xml")
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
objects, err := archiver.Unarchive(dat)
32+
assert.NoError(t, err)
33+
assert.Equal(t, tc.expected, convertToJSON(objects))
34+
35+
dat, err = ioutil.ReadFile("fixtures/" + tc.filename + ".bin")
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
objects, err = archiver.Unarchive(dat)
40+
assert.Equal(t, tc.expected, convertToJSON(objects))
41+
}
42+
}
43+
44+
func TestValidation(t *testing.T) {
45+
46+
testCases := map[string]struct {
47+
filename string
48+
}{
49+
"$archiver key is missing": {"missing_archiver"},
50+
"$archiver is not nskeyedarchiver": {"wrong_archiver"},
51+
"$top key is missing": {"missing_top"},
52+
"$objects key is missing": {"missing_objects"},
53+
"$version key is missing": {"missing_version"},
54+
"$version is wrong": {"wrong_version"},
55+
"plist is invalid": {"broken_plist"},
56+
}
57+
58+
for _, tc := range testCases {
59+
dat, err := ioutil.ReadFile("fixtures/" + tc.filename + ".xml")
60+
if err != nil {
61+
log.Fatal(err)
62+
}
63+
_, err = archiver.Unarchive(dat)
64+
assert.Error(t, err)
65+
}
66+
}
67+
68+
func convertToJSON(obj interface{}) string {
69+
b, err := json.Marshal(obj)
70+
if err != nil {
71+
fmt.Println("error:", err)
72+
}
73+
return string(b)
74+
}

Diff for: archiverconstants.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package nskeyedarchiver
2+
3+
const (
4+
archiverKey = "$archiver"
5+
nsKeyedArchiver = "NSKeyedArchiver"
6+
versionKey = "$version"
7+
topKey = "$top"
8+
objectsKey = "$objects"
9+
nsObjects = "NS.objects"
10+
nsKeys = "NS.keys"
11+
class = "$class"
12+
className = "$classname"
13+
versionValue = 100000
14+
)
15+
16+
const (
17+
nsArray = "NSArray"
18+
nsMutableArray = "NSMutableArray"
19+
nsSet = "NSSet"
20+
nsMutableSet = "NSMutableSet"
21+
)
22+
23+
const (
24+
nsDictionary = "NSDictionary"
25+
nsMutableDictionary = "NSMutableDictionary"
26+
)

Diff for: archiverutils.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package nskeyedarchiver
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
9+
plist "howett.net/plist"
10+
)
11+
12+
//toUidList type asserts a []interface{} to a []plist.UID by iterating through the list.
13+
func toUidList(list []interface{}) []plist.UID {
14+
l := len(list)
15+
result := make([]plist.UID, l)
16+
for i := 0; i < l; i++ {
17+
result[i] = list[i].(plist.UID)
18+
}
19+
return result
20+
}
21+
22+
//plistFromBytes decodes a binary or XML based PLIST using the amazing github.com/DHowett/go-plist library and returns an interface{} or propagates the error raised by the library.
23+
func plistFromBytes(plistBytes []byte) (interface{}, error) {
24+
var test interface{}
25+
decoder := plist.NewDecoder(bytes.NewReader(plistBytes))
26+
27+
err := decoder.Decode(&test)
28+
if err != nil {
29+
return test, err
30+
}
31+
return test, nil
32+
}
33+
34+
//ToPlist converts a given struct to a Plist using the
35+
//github.com/DHowett/go-plist library. Make sure your struct is exported.
36+
//It returns a string containing the plist.
37+
func ToPlist(data interface{}) string {
38+
buf := &bytes.Buffer{}
39+
encoder := plist.NewEncoder(buf)
40+
encoder.Encode(data)
41+
return buf.String()
42+
}
43+
44+
//Print an object as JSON for debugging purposes, careful log.Fatals on error
45+
func printAsJSON(obj interface{}) {
46+
b, err := json.MarshalIndent(obj, "", " ")
47+
if err != nil {
48+
log.Fatalf("Error while marshalling Json:%s", err)
49+
}
50+
fmt.Print(string(b))
51+
}
52+
53+
//verifyCorrectArchiver makes sure the nsKeyedArchived plist has all the right keys and values and returns an error otherwise
54+
func verifyCorrectArchiver(nsKeyedArchiverData map[string]interface{}) error {
55+
if val, ok := nsKeyedArchiverData[archiverKey]; !ok {
56+
return fmt.Errorf("Invalid NSKeyedAchiverObject, missing key '%s'", archiverKey)
57+
} else {
58+
if stringValue := val.(string); stringValue != nsKeyedArchiver {
59+
return fmt.Errorf("Invalid value: %s for key '%s', expected: '%s'", stringValue, archiverKey, nsKeyedArchiver)
60+
}
61+
}
62+
if _, ok := nsKeyedArchiverData[topKey]; !ok {
63+
return fmt.Errorf("Invalid NSKeyedAchiverObject, missing key '%s'", topKey)
64+
}
65+
66+
if _, ok := nsKeyedArchiverData[objectsKey]; !ok {
67+
return fmt.Errorf("Invalid NSKeyedAchiverObject, missing key '%s'", objectsKey)
68+
}
69+
70+
if val, ok := nsKeyedArchiverData[versionKey]; !ok {
71+
return fmt.Errorf("Invalid NSKeyedAchiverObject, missing key '%s'", versionKey)
72+
} else {
73+
if stringValue := val.(uint64); stringValue != versionValue {
74+
return fmt.Errorf("Invalid value: %d for key '%s', expected: '%d'", stringValue, versionKey, versionValue)
75+
}
76+
}
77+
78+
return nil
79+
}

Diff for: fixtures/arrays.bin

645 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)