-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathcommon_test.go
173 lines (160 loc) · 3.48 KB
/
common_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package androidbinary
import (
"bytes"
"io"
"reflect"
"testing"
)
var readStringPoolTests = []struct {
input []uint8
strings []string
styles []ResStringPoolSpan
}{
{
[]uint8{
0x01, 0x00, // Type = RES_STRING_POOL_TYPE
0x1C, 0x00, // HeaderSize = 28 bytes
0x3C, 0x00, 0x00, 0x00, // Size = 60
0x02, 0x00, 0x00, 0x00, // StringCount = 2
0x02, 0x00, 0x00, 0x00, // StyleScount = 2
0x00, 0x00, 0x00, 0x00, // Flags = 0x00
0x2C, 0x00, 0x00, 0x00, // StringStart = 44
0x34, 0x00, 0x00, 0x00, // StylesStart = 52
// StringIndexes
0x00, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00,
// StyleIndexes
0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
// Strings
0x01, 0x00, 0x61, 0x00,
0x01, 0x00, 0x42, 0x30,
// Styles
0x01, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00,
0x04, 0x00, 0x00, 0x00,
},
[]string{"a", "\u3042"},
[]ResStringPoolSpan{{1, 2}, {3, 4}},
},
}
func TestReadStringPool(t *testing.T) {
for _, tt := range readStringPoolTests {
buf := bytes.NewReader(tt.input)
sr := io.NewSectionReader(buf, 0, int64(len(tt.input)))
actual, err := readStringPool(sr)
if err != nil {
t.Errorf("got %v want no error", err)
}
if !reflect.DeepEqual(actual.Strings, tt.strings) {
t.Errorf("got %v want %v", actual.Strings, tt.strings)
}
if !reflect.DeepEqual(actual.Styles, tt.styles) {
t.Errorf("got %v want %v", actual.Styles, tt.styles)
}
}
}
var readUTF16Tests = []struct {
input []uint8
output string
}{
{
[]uint8{0x00, 0x00, 0x61, 0x00},
"",
},
{
[]uint8{0x01, 0x00, 0x61, 0x00},
"a",
},
{
[]uint8{0x01, 0x00, 0x42, 0x30},
"\u3042",
},
{
[]uint8{0x00, 0x80, 0x01, 0x00, 0x61, 0x00},
"a",
},
}
func TestReadUTF16(t *testing.T) {
for _, tt := range readUTF16Tests {
buf := bytes.NewReader(tt.input)
sr := io.NewSectionReader(buf, 0, int64(len(tt.input)))
actual, err := readUTF16(sr)
if err != nil {
t.Errorf("got %v want no error", err)
}
if actual != tt.output {
t.Errorf("got %v want %v", actual, tt.output)
}
}
}
var readUTF8Tests = []struct {
input []uint8
output string
}{
{
[]uint8{0x00, 0x00, 0x61},
"",
},
{
[]uint8{0x01, 0x01, 0x61},
"a",
},
{
[]uint8{0x01, 0x03, 0xE3, 0x81, 0x82},
"\u3042",
},
{
[]uint8{0x80, 0x01, 0x80, 0x01, 0x61},
"a",
},
}
func TestReadUTF8(t *testing.T) {
for _, tt := range readUTF8Tests {
buf := bytes.NewReader(tt.input)
sr := io.NewSectionReader(buf, 0, int64(len(tt.input)))
actual, err := readUTF8(sr)
if err != nil {
t.Errorf("got %v want no error", err)
}
if actual != tt.output {
t.Errorf("got %v want %v", actual, tt.output)
}
}
}
var newZeroFilledReaderTests = []struct {
input []uint8
actual int64
expected int64
output []uint8
}{
{
input: []uint8{0x01, 0x23, 0x45, 0x67},
actual: 4,
expected: 4,
output: []uint8{0x01, 0x23, 0x45, 0x67},
},
{
input: []uint8{0x01, 0x23, 0x45, 0x67},
actual: 4,
expected: 8,
output: []uint8{0x01, 0x23, 0x45, 0x67, 0x00, 0x00, 0x00, 0x00},
},
}
func TestNewZeroFilledReader(t *testing.T) {
for _, tt := range newZeroFilledReaderTests {
buf := bytes.NewReader(tt.input)
r, err := newZeroFilledReader(buf, tt.actual, tt.expected)
if err != nil {
t.Errorf("got %v want no error", err)
}
actualBytes := make([]uint8, tt.expected)
r.Read(actualBytes)
for i, a := range tt.output {
if actualBytes[i] != a {
t.Errorf("got %v(position %d) wants %v", actualBytes[i], i, a)
}
}
}
}