File: bplist_test.go

package info (click to toggle)
golang-github-dhowett-go-plist 0.0~git20181124.0.591f970-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 376 kB
  • sloc: makefile: 2
file content (141 lines) | stat: -rw-r--r-- 3,914 bytes parent folder | download
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
package plist

import (
	"bytes"
	"encoding/binary"
	"io/ioutil"
	"math"
	"testing"
)

func BenchmarkBplistGenerate(b *testing.B) {
	for i := 0; i < b.N; i++ {
		d := newBplistGenerator(ioutil.Discard)
		d.generateDocument(plistValueTree)
	}
}

func BenchmarkBplistParse(b *testing.B) {
	buf := bytes.NewReader(plistValueTreeAsBplist)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		b.StartTimer()
		d := newBplistParser(buf)
		d.parseDocument()
		b.StopTimer()
		buf.Seek(0, 0)
	}
}

func TestBplistInt128(t *testing.T) {
	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}
	expected := uint64(0x090a0b0c0d0e0f10)
	buf := bytes.NewReader(bplist)
	d := newBplistParser(buf)
	pval, _ := d.parseDocument()
	if pinteger, ok := pval.(*cfNumber); !ok || pinteger.value != expected {
		t.Error("Expected", expected, "received", pval)
	}
}

func TestBplistSignedIntValues(t *testing.T) {
	bplist := []byte{
		'b', 'p', 'l', 'i', 's', 't', '0', '0',

		// Array (8 entries)
		0xA8,
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,

		// 0xFFFFFFFFFFFFFF80 (MinInt8, sign extended)
		0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,

		// 0x7F (MaxInt8)
		0x10, 0x7f,

		// 0xFFFFFFFFFFFF8000 (MinInt16, sign extended)
		0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00,

		// 0x7FFF (MaxInt16)
		0x11, 0x7f, 0xff,

		// 0xFFFFFFFF80000000 (MinInt32, sign extended)
		0x13, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00,

		// 0x7FFFFFFF (MaxInt32)
		0x12, 0x7f, 0xff, 0xff, 0xff,

		// 0x8000000000000000 (MinInt64)
		0x13, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

		// 0x7FFFFFFFFFFFFFFF (MaxInt64)
		0x13, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,

		// Offset table
		0x08, 0x11, 0x1a, 0x1c, 0x25, 0x28, 0x31, 0x36, 0x3f,

		// Trailer
		0x00, 0x00, 0x00, 0x00, 0x00,
		0x00,
		0x01,
		0x01,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48,
	}

	expectedValues := []int64{
		math.MinInt8,
		math.MaxInt8,
		math.MinInt16,
		math.MaxInt16,
		math.MinInt32,
		math.MaxInt32,
		math.MinInt64,
		math.MaxInt64,
	}

	buf := bytes.NewReader(bplist)
	d := newBplistParser(buf)
	pval, _ := d.parseDocument()
	parsedValues := pval.(*cfArray).values
	for i, cfv := range parsedValues {
		value := int64(cfv.(*cfNumber).value)
		if value != expectedValues[i] {
			t.Error("Expected", expectedValues[i], "received", value)
		}
	}
}

func TestBplistLatin1ToUTF16(t *testing.T) {
	expectedPrefix := []byte{0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30, 0xd1, 0x01, 0x02, 0x51, 0x5f, 0x6f, 0x10, 0x80}
	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}
	expectedBuf := bytes.NewBuffer(expectedPrefix)

	sBuf := &bytes.Buffer{}
	for i := uint16(0xc280); i <= 0xc2bf; i++ {
		binary.Write(sBuf, binary.BigEndian, i)
		binary.Write(expectedBuf, binary.BigEndian, i-0xc200)
	}

	for i := uint16(0xc380); i <= 0xc3bf; i++ {
		binary.Write(sBuf, binary.BigEndian, i)
		binary.Write(expectedBuf, binary.BigEndian, i-0xc300+0x0040)
	}

	expectedBuf.Write(expectedPostfix)

	var buf bytes.Buffer
	encoder := NewBinaryEncoder(&buf)

	data := map[string]string{
		"_": string(sBuf.Bytes()),
	}
	if err := encoder.Encode(data); err != nil {
		t.Error(err.Error())
	}

	if !bytes.Equal(buf.Bytes(), expectedBuf.Bytes()) {
		t.Error("Expected", expectedBuf.Bytes(), "received", buf.Bytes())
		return
	}
}