| 1 |
package plist |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"io/ioutil" |
| 7 |
"math" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func BenchmarkBplistGenerate(b *testing.B) { |
| 12 |
for i := 0; i < b.N; i++ { |
| 13 |
d := newBplistGenerator(ioutil.Discard) |
| 14 |
d.generateDocument(plistValueTree) |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
func BenchmarkBplistParse(b *testing.B) { |
| 19 |
buf := bytes.NewReader(plistValueTreeAsBplist) |
| 20 |
b.ResetTimer() |
| 21 |
for i := 0; i < b.N; i++ { |
| 22 |
b.StartTimer() |
| 23 |
d := newBplistParser(buf) |
| 24 |
d.parseDocument() |
| 25 |
b.StopTimer() |
| 26 |
buf.Seek(0, 0) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func TestBplistInt128(t *testing.T) { |
| 31 |
bplist := []byte{0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30, 0x14, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19} |
| 32 |
expected := uint64(0x090a0b0c0d0e0f10) |
| 33 |
buf := bytes.NewReader(bplist) |
| 34 |
d := newBplistParser(buf) |
| 35 |
pval, _ := d.parseDocument() |
| 36 |
if pinteger, ok := pval.(*cfNumber); !ok || pinteger.value != expected { |
| 37 |
t.Error("Expected", expected, "received", pval) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestBplistSignedIntValues(t *testing.T) { |
| 42 |
bplist := []byte{ |
| 43 |
'b', 'p', 'l', 'i', 's', 't', '0', '0', |
| 44 |
|
| 45 |
|
| 46 |
0xA8, |
| 47 |
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 48 |
|
| 49 |
|
| 50 |
0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, |
| 51 |
|
| 52 |
|
| 53 |
0x10, 0x7f, |
| 54 |
|
| 55 |
|
| 56 |
0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, |
| 57 |
|
| 58 |
|
| 59 |
0x11, 0x7f, 0xff, |
| 60 |
|
| 61 |
|
| 62 |
0x13, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, |
| 63 |
|
| 64 |
|
| 65 |
0x12, 0x7f, 0xff, 0xff, 0xff, |
| 66 |
|
| 67 |
|
| 68 |
0x13, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 69 |
|
| 70 |
|
| 71 |
0x13, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 72 |
|
| 73 |
|
| 74 |
0x08, 0x11, 0x1a, 0x1c, 0x25, 0x28, 0x31, 0x36, 0x3f, |
| 75 |
|
| 76 |
|
| 77 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 78 |
0x00, |
| 79 |
0x01, |
| 80 |
0x01, |
| 81 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, |
| 82 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 83 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, |
| 84 |
} |
| 85 |
|
| 86 |
expectedValues := []int64{ |
| 87 |
math.MinInt8, |
| 88 |
math.MaxInt8, |
| 89 |
math.MinInt16, |
| 90 |
math.MaxInt16, |
| 91 |
math.MinInt32, |
| 92 |
math.MaxInt32, |
| 93 |
math.MinInt64, |
| 94 |
math.MaxInt64, |
| 95 |
} |
| 96 |
|
| 97 |
buf := bytes.NewReader(bplist) |
| 98 |
d := newBplistParser(buf) |
| 99 |
pval, _ := d.parseDocument() |
| 100 |
parsedValues := pval.(*cfArray).values |
| 101 |
for i, cfv := range parsedValues { |
| 102 |
value := int64(cfv.(*cfNumber).value) |
| 103 |
if value != expectedValues[i] { |
| 104 |
t.Error("Expected", expectedValues[i], "received", value) |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func TestBplistLatin1ToUTF16(t *testing.T) { |
| 110 |
expectedPrefix := []byte{0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30, 0xd1, 0x01, 0x02, 0x51, 0x5f, 0x6f, 0x10, 0x80} |
| 111 |
expectedPostfix := []byte{0x00, 0x08, 0x00, 0x0b, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x10} |
| 112 |
expectedBuf := bytes.NewBuffer(expectedPrefix) |
| 113 |
|
| 114 |
sBuf := &bytes.Buffer{} |
| 115 |
for i := uint16(0xc280); i <= 0xc2bf; i++ { |
| 116 |
binary.Write(sBuf, binary.BigEndian, i) |
| 117 |
binary.Write(expectedBuf, binary.BigEndian, i-0xc200) |
| 118 |
} |
| 119 |
|
| 120 |
for i := uint16(0xc380); i <= 0xc3bf; i++ { |
| 121 |
binary.Write(sBuf, binary.BigEndian, i) |
| 122 |
binary.Write(expectedBuf, binary.BigEndian, i-0xc300+0x0040) |
| 123 |
} |
| 124 |
|
| 125 |
expectedBuf.Write(expectedPostfix) |
| 126 |
|
| 127 |
var buf bytes.Buffer |
| 128 |
encoder := NewBinaryEncoder(&buf) |
| 129 |
|
| 130 |
data := map[string]string{ |
| 131 |
"_": string(sBuf.Bytes()), |
| 132 |
} |
| 133 |
if err := encoder.Encode(data); err != nil { |
| 134 |
t.Error(err.Error()) |
| 135 |
} |
| 136 |
|
| 137 |
if !bytes.Equal(buf.Bytes(), expectedBuf.Bytes()) { |
| 138 |
t.Error("Expected", expectedBuf.Bytes(), "received", buf.Bytes()) |
| 139 |
return |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestBplistNonPowerOfTwoOffsetIntSizes(t *testing.T) { |
| 144 |
bplist := []byte{ |
| 145 |
'b', 'p', 'l', 'i', 's', 't', '0', '0', |
| 146 |
|
| 147 |
|
| 148 |
0xA2, |
| 149 |
0x01, 0x02, |
| 150 |
|
| 151 |
|
| 152 |
0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, |
| 153 |
|
| 154 |
|
| 155 |
0x10, 0x7f, |
| 156 |
|
| 157 |
|
| 158 |
0x00, 0x00, 0x08, |
| 159 |
0x00, 0x00, 0x0b, |
| 160 |
0x00, 0x00, 0x14, |
| 161 |
|
| 162 |
|
| 163 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 164 |
0x00, |
| 165 |
0x03, |
| 166 |
0x01, |
| 167 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, |
| 168 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 169 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, |
| 170 |
} |
| 171 |
|
| 172 |
buf := bytes.NewReader(bplist) |
| 173 |
d := newBplistParser(buf) |
| 174 |
_, err := d.parseDocument() |
| 175 |
if err != nil { |
| 176 |
t.Error("Unexpected error", err) |
| 177 |
} |
| 178 |
} |