| 1 |
package plist |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func BenchmarkXMLDecode(b *testing.B) { |
| 11 |
for i := 0; i < b.N; i++ { |
| 12 |
b.StopTimer() |
| 13 |
var bval interface{} |
| 14 |
buf := bytes.NewReader([]byte(plistValueTreeAsXML)) |
| 15 |
b.StartTimer() |
| 16 |
decoder := NewDecoder(buf) |
| 17 |
decoder.Decode(bval) |
| 18 |
b.StopTimer() |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
func BenchmarkBplistDecode(b *testing.B) { |
| 23 |
for i := 0; i < b.N; i++ { |
| 24 |
b.StopTimer() |
| 25 |
var bval interface{} |
| 26 |
buf := bytes.NewReader(plistValueTreeAsBplist) |
| 27 |
b.StartTimer() |
| 28 |
decoder := NewDecoder(buf) |
| 29 |
decoder.Decode(bval) |
| 30 |
b.StopTimer() |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestLaxDecode(t *testing.T) { |
| 35 |
var laxTestDataStringsOnlyAsXML = `{B=1;D="2013-11-27 00:34:00 +0000";I64=1;F64="3.0";U64=2;}` |
| 36 |
d := LaxTestData{} |
| 37 |
buf := bytes.NewReader([]byte(laxTestDataStringsOnlyAsXML)) |
| 38 |
decoder := NewDecoder(buf) |
| 39 |
decoder.lax = true |
| 40 |
err := decoder.Decode(&d) |
| 41 |
if err != nil { |
| 42 |
t.Error(err.Error()) |
| 43 |
} |
| 44 |
|
| 45 |
if d != laxTestData { |
| 46 |
t.Logf("Expected: %#v", laxTestData) |
| 47 |
t.Logf("Received: %#v", d) |
| 48 |
t.Fail() |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func TestIllegalLaxDecode(t *testing.T) { |
| 53 |
i := int64(0) |
| 54 |
u := uint64(0) |
| 55 |
f := float64(0) |
| 56 |
b := false |
| 57 |
plists := []struct { |
| 58 |
pl string |
| 59 |
d interface{} |
| 60 |
}{ |
| 61 |
{"<string>abc</string>", &i}, |
| 62 |
{"<string>abc</string>", &u}, |
| 63 |
{"<string>def</string>", &f}, |
| 64 |
{"<string>ghi</string>", &b}, |
| 65 |
{"<string>jkl</string>", []byte{0x00}}, |
| 66 |
} |
| 67 |
|
| 68 |
for _, plist := range plists { |
| 69 |
buf := bytes.NewReader([]byte(plist.pl)) |
| 70 |
decoder := NewDecoder(buf) |
| 71 |
decoder.lax = true |
| 72 |
err := decoder.Decode(plist.d) |
| 73 |
t.Logf("Error: %v", err) |
| 74 |
if err == nil { |
| 75 |
t.Error("Expected error, received nothing.") |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestIllegalDecode(t *testing.T) { |
| 81 |
i := int64(0) |
| 82 |
b := false |
| 83 |
plists := []struct { |
| 84 |
pl string |
| 85 |
d interface{} |
| 86 |
}{ |
| 87 |
{"<string>abc</string>", &i}, |
| 88 |
{"<data>ABC=</data>", &i}, |
| 89 |
{"<real>34.1</real>", &i}, |
| 90 |
{"<true>def</true>", &i}, |
| 91 |
{"<date>2010-01-01T00:00:00Z</date>", &i}, |
| 92 |
{"<integer>0</integer>", &b}, |
| 93 |
{"<array><integer>0</integer></array>", &b}, |
| 94 |
{"<dict><key>a</key><integer>0</integer></dict>", &b}, |
| 95 |
{"<array><true/><true/><true/></array>", &[1]int{1}}, |
| 96 |
{"<data>SGVsbG8=</data>", &[3]byte{}}, |
| 97 |
} |
| 98 |
|
| 99 |
for _, plist := range plists { |
| 100 |
buf := bytes.NewReader([]byte(plist.pl)) |
| 101 |
decoder := NewDecoder(buf) |
| 102 |
err := decoder.Decode(plist.d) |
| 103 |
t.Logf("Error: %v", err) |
| 104 |
if err == nil { |
| 105 |
t.Error("Expected error, received nothing.") |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func TestDecode(t *testing.T) { |
| 111 |
for _, test := range tests { |
| 112 |
subtest(t, test.Name, func(t *testing.T) { |
| 113 |
expVal := test.DecodeValue |
| 114 |
if expVal == nil { |
| 115 |
expVal = test.Value |
| 116 |
} |
| 117 |
|
| 118 |
expReflect := reflect.ValueOf(expVal) |
| 119 |
if !expReflect.IsValid() || isEmptyInterface(expReflect) { |
| 120 |
return |
| 121 |
} |
| 122 |
if expReflect.Kind() == reflect.Ptr || expReflect.Kind() == reflect.Interface { |
| 123 |
|
| 124 |
expReflect = expReflect.Elem() |
| 125 |
} |
| 126 |
expVal = expReflect.Interface() |
| 127 |
|
| 128 |
results := make(map[int]interface{}) |
| 129 |
for fmt, doc := range test.Documents { |
| 130 |
if test.SkipDecode[fmt] { |
| 131 |
return |
| 132 |
} |
| 133 |
subtest(t, FormatNames[fmt], func(t *testing.T) { |
| 134 |
val := reflect.New(expReflect.Type()).Interface() |
| 135 |
_, err := Unmarshal(doc, val) |
| 136 |
if err != nil { |
| 137 |
t.Error(err) |
| 138 |
} |
| 139 |
|
| 140 |
valReflect := reflect.ValueOf(val) |
| 141 |
if valReflect.Kind() == reflect.Ptr || valReflect.Kind() == reflect.Interface { |
| 142 |
|
| 143 |
valReflect = valReflect.Elem() |
| 144 |
val = valReflect.Interface() |
| 145 |
} |
| 146 |
|
| 147 |
results[fmt] = val |
| 148 |
if !reflect.DeepEqual(expVal, val) { |
| 149 |
t.Logf("Expected: %#v\n", expVal) |
| 150 |
t.Logf("Received: %#v\n", val) |
| 151 |
t.Fail() |
| 152 |
} |
| 153 |
}) |
| 154 |
} |
| 155 |
|
| 156 |
if results[BinaryFormat] != nil && results[XMLFormat] != nil { |
| 157 |
if !reflect.DeepEqual(results[BinaryFormat], results[XMLFormat]) { |
| 158 |
t.Log("Binary and XML decoding yielded different values.") |
| 159 |
t.Log("Binary:", results[BinaryFormat]) |
| 160 |
t.Log("XML :", results[XMLFormat]) |
| 161 |
t.Fail() |
| 162 |
} |
| 163 |
} |
| 164 |
}) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func TestInterfaceDecode(t *testing.T) { |
| 169 |
var xval interface{} |
| 170 |
buf := bytes.NewReader([]byte{98, 112, 108, 105, 115, 116, 48, 48, 214, 1, 13, 17, 21, 25, 27, 2, 14, 18, 22, 26, 28, 88, 105, 110, 116, 97, 114, 114, 97, 121, 170, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 1, 16, 8, 16, 16, 16, 32, 16, 64, 16, 2, 16, 9, 16, 17, 16, 33, 16, 65, 86, 102, 108, 111, 97, 116, 115, 162, 15, 16, 34, 66, 0, 0, 0, 35, 64, 80, 0, 0, 0, 0, 0, 0, 88, 98, 111, 111, 108, 101, 97, 110, 115, 162, 19, 20, 9, 8, 87, 115, 116, 114, 105, 110, 103, 115, 162, 23, 24, 92, 72, 101, 108, 108, 111, 44, 32, 65, 83, 67, 73, 73, 105, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 44, 0, 32, 78, 22, 117, 76, 84, 100, 97, 116, 97, 68, 1, 2, 3, 4, 84, 100, 97, 116, 101, 51, 65, 184, 69, 117, 120, 0, 0, 0, 8, 21, 30, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 68, 71, 76, 85, 94, 97, 98, 99, 107, 110, 123, 142, 147, 152, 157, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 166}) |
| 171 |
decoder := NewDecoder(buf) |
| 172 |
err := decoder.Decode(&xval) |
| 173 |
if err != nil { |
| 174 |
t.Log("Error:", err) |
| 175 |
t.Fail() |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestFormatDetection(t *testing.T) { |
| 180 |
type formatTest struct { |
| 181 |
expectedFormat int |
| 182 |
data []byte |
| 183 |
} |
| 184 |
plists := []formatTest{ |
| 185 |
{BinaryFormat, []byte{98, 112, 108, 105, 115, 116, 48, 48, 85, 72, 101, 108, 108, 111, 8, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14}}, |
| 186 |
{XMLFormat, []byte(`<string><*I3></string>`)}, |
| 187 |
{InvalidFormat, []byte(`bplist00`)}, |
| 188 |
{OpenStepFormat, []byte(`(1,2,3,4,5)`)}, |
| 189 |
{OpenStepFormat, []byte(`<abab>`)}, |
| 190 |
{GNUStepFormat, []byte(`(1,2,<*I3>)`)}, |
| 191 |
{InvalidFormat, []byte{0x00}}, |
| 192 |
} |
| 193 |
|
| 194 |
for i, fmttest := range plists { |
| 195 |
fmt, err := Unmarshal(fmttest.data, nil) |
| 196 |
if fmt != fmttest.expectedFormat { |
| 197 |
t.Errorf("plist %d: Wanted %s, received %s.", i, FormatNames[fmttest.expectedFormat], FormatNames[fmt]) |
| 198 |
} |
| 199 |
if err != nil { |
| 200 |
t.Logf("plist %d: Error: %v", i, err) |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func ExampleDecoder_Decode() { |
| 206 |
type sparseBundleHeader struct { |
| 207 |
InfoDictionaryVersion string `plist:"CFBundleInfoDictionaryVersion"` |
| 208 |
BandSize uint64 `plist:"band-size"` |
| 209 |
BackingStoreVersion int `plist:"bundle-backingstore-version"` |
| 210 |
DiskImageBundleType string `plist:"diskimage-bundle-type"` |
| 211 |
Size uint64 `plist:"size"` |
| 212 |
} |
| 213 |
|
| 214 |
buf := bytes.NewReader([]byte(`<?xml version="1.0" encoding="UTF-8"?> |
| 215 |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 216 |
<plist version="1.0"> |
| 217 |
<dict> |
| 218 |
<key>CFBundleInfoDictionaryVersion</key> |
| 219 |
<string>6.0</string> |
| 220 |
<key>band-size</key> |
| 221 |
<integer>8388608</integer> |
| 222 |
<key>bundle-backingstore-version</key> |
| 223 |
<integer>1</integer> |
| 224 |
<key>diskimage-bundle-type</key> |
| 225 |
<string>com.apple.diskimage.sparsebundle</string> |
| 226 |
<key>size</key> |
| 227 |
<integer>4398046511104</integer> |
| 228 |
</dict> |
| 229 |
</plist>`)) |
| 230 |
|
| 231 |
var data sparseBundleHeader |
| 232 |
decoder := NewDecoder(buf) |
| 233 |
err := decoder.Decode(&data) |
| 234 |
if err != nil { |
| 235 |
fmt.Println(err) |
| 236 |
} |
| 237 |
fmt.Println(data) |
| 238 |
|
| 239 |
|
| 240 |
} |