-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencoder_test.go
More file actions
222 lines (200 loc) · 5.08 KB
/
encoder_test.go
File metadata and controls
222 lines (200 loc) · 5.08 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package wbxml
import (
"bytes"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncoderEncodeTag(t *testing.T) {
tests := []struct {
tag StartElement
expected []byte
}{
{
tag: StartElement{Name: "SyncML"},
expected: []byte{0x2D},
},
{
tag: StartElement{Name: "SyncML", Content: true},
expected: []byte{0x6D},
},
{
tag: StartElement{Name: "SyncML", Content: true,
Attr: []Attr{Attr{Name: "A"}}},
expected: []byte{0xED, 05, 01},
},
{
tag: StartElement{Name: "CS"},
expected: []byte{0x00, 0x08, 0x05},
},
}
for testID, test := range tests {
buf := bytes.NewBuffer(nil)
{
e := NewEncoder(buf, syncMLTags, CodeSpace{0: CodePage{5: "A"}})
err := e.encodeTag(test.tag)
if err != nil {
t.Errorf("case %d: unexpected error: %s", testID, err)
continue
}
assert.Equal(t, test.expected, buf.Bytes(), "case %d", testID)
}
}
}
func TestEncoderToken(t *testing.T) {
for testID, tokens := range tokensExamples {
expected := encodingExamples[testID]
space := tagSpaceExamples[testID]
w := bytes.NewBuffer(nil)
e := NewEncoder(w, space.tags, space.attrs)
err := e.EncodeHeader(headerExamples[testID])
if err != nil {
t.Fatalf("case %d: encoding header: %s", testID, err)
}
for _, tok := range tokens {
if tok == nil {
break
}
err := e.EncodeToken(tok)
if err != nil {
t.Errorf("error: token %v: %s", tok, err)
}
}
assert.Equal(t, expected, w.Bytes(), "case %d", testID)
}
}
type fullmsg struct {
SyncHdr fullheader
SyncBody body
}
type fullheader struct {
VerDTD string
VerProto string
SessionID string
MsgID uint32
Source endpoint
Target endpoint
Meta meta
}
func TestEncoderEncode(t *testing.T) {
var syncMlMsg1 = fullmsg{
SyncHdr: fullheader{
VerDTD: "1.2",
VerProto: "m2m/1.2",
SessionID: "S7eNe",
MsgID: 94,
Source: endpoint{
LocURI: "tcp://Accueil.NocId.amm.fr",
},
Target: endpoint{
LocURI: "gdo:99005Z1338-21178",
},
Meta: meta{
EMI: emi{
Sign: []byte{0x30, 0x46, 0x02, 0x21, 0x00, 0x9a, 0x9f, 0x72, 0x4f, 0x51, 0x46, 0xb6, 0xe2, 0x6a, 0x35, 0x7b, 0x4b, 0x53, 0x22, 0x13, 0x88, 0xbe, 0xef, 0x1a, 0x95, 0xc6, 0xf4, 0xba, 0x9f, 0x05, 0x72, 0xd5, 0x85, 0x4f, 0x02, 0x3e, 0x54, 0x02, 0x21, 0x00, 0x8d, 0xd8, 0x85, 0xe0, 0x88, 0x28, 0x43, 0x6c, 0x6e, 0x2b, 0x08, 0xfb, 0xb8, 0x16, 0xd3, 0x59, 0x79, 0x1b, 0x9d, 0x8c, 0xb1, 0xca, 0x63, 0x34, 0xf8, 0x20, 0x1f, 0xee, 0x13, 0x09, 0x09, 0xa9},
},
},
},
SyncBody: body{
Status: status{
CmdID: 1,
MsgRef: 93,
CmdRef: 1,
Cmd: "Put",
Data: 500,
},
Final: true,
},
}
w := bytes.NewBuffer(nil)
d := NewEncoder(w, syncMLTags, CodeSpace{})
err := d.EncodeHeader(Header{
Version: 3,
PublicID: 0,
Charset: 3,
StringTable: []byte{0x12, 0x01},
})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
err = d.EncodeElement(syncMlMsg1, StartElement{Name: "SyncML"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !assert.Equal(t, syncMLInput, w.Bytes()) {
XML(os.Stdout, NewDecoder(w, syncMLTags, CodeSpace{}), " ")
}
}
type fullmsg2 struct {
SyncHdr fullheader
SyncBody body2
}
func (b body2) MarshalWBXML(e *Encoder, start StartElement) error {
start.Content = len(b) > 0
err := e.EncodeToken(start)
if err != nil {
return err
}
for _, cmd := range b {
var err error
switch cmd.(type) {
case status:
err = e.EncodeElement(cmd, StartElement{Name: "Status"})
case final:
err = e.EncodeElement(cmd, StartElement{Name: "Final"})
}
if err != nil {
return err
}
}
return e.EncodeToken(EndElement{Name: start.Name})
}
func TestEncoderEncodeWithMarshalWBXML(t *testing.T) {
var syncMlMsg2 = fullmsg2{
SyncHdr: fullheader{
VerDTD: "1.2",
VerProto: "m2m/1.2",
SessionID: "S7eNe",
MsgID: 94,
Source: endpoint{
LocURI: "tcp://Accueil.NocId.amm.fr",
},
Target: endpoint{
LocURI: "gdo:99005Z1338-21178",
},
Meta: meta{
EMI: emi{
Sign: []byte{0x30, 0x46, 0x02, 0x21, 0x00, 0x9a, 0x9f, 0x72, 0x4f, 0x51, 0x46, 0xb6, 0xe2, 0x6a, 0x35, 0x7b, 0x4b, 0x53, 0x22, 0x13, 0x88, 0xbe, 0xef, 0x1a, 0x95, 0xc6, 0xf4, 0xba, 0x9f, 0x05, 0x72, 0xd5, 0x85, 0x4f, 0x02, 0x3e, 0x54, 0x02, 0x21, 0x00, 0x8d, 0xd8, 0x85, 0xe0, 0x88, 0x28, 0x43, 0x6c, 0x6e, 0x2b, 0x08, 0xfb, 0xb8, 0x16, 0xd3, 0x59, 0x79, 0x1b, 0x9d, 0x8c, 0xb1, 0xca, 0x63, 0x34, 0xf8, 0x20, 0x1f, 0xee, 0x13, 0x09, 0x09, 0xa9},
},
},
},
SyncBody: body2{
status{
CmdID: 1,
MsgRef: 93,
CmdRef: 1,
Cmd: "Put",
Data: 500,
},
final(true),
},
}
w := bytes.NewBuffer(nil)
d := NewEncoder(w, syncMLTags, CodeSpace{})
err := d.EncodeHeader(Header{
Version: 3,
PublicID: 0,
Charset: 3,
StringTable: []byte{0x12, 0x01},
})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
err = d.EncodeElement(syncMlMsg2, StartElement{Name: "SyncML"})
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if !assert.Equal(t, syncMLInput, w.Bytes()) {
XML(os.Stdout, NewDecoder(w, syncMLTags, CodeSpace{}), " ")
}
}