-
Notifications
You must be signed in to change notification settings - Fork 7
/
format_test.go
324 lines (271 loc) · 9.28 KB
/
format_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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrp
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testPayload(t *testing.T, payload []byte) {
var (
assert = assert.New(t)
require = require.New(t)
original = Message{
Payload: payload,
}
decoded Message
output bytes.Buffer
encoder = NewEncoder(nil, Msgpack)
decoder = NewDecoder(nil, Msgpack)
)
encoder.Reset(&output)
require.NoError(encoder.Encode(&original))
decoder.Reset(&output)
require.NoError(decoder.Decode(&decoded))
// don't output the payload if it's a ridiculous size
if testing.Verbose() && len(payload) < 1024 {
fmt.Println(hex.Dump(output.Bytes()))
t.Logf("original.Payload=%s", original.Payload)
t.Logf("decoded.Payload=%s", decoded.Payload)
}
assert.Equal(payload, decoded.Payload)
}
func TestPayload(t *testing.T) {
t.Run("UTF8", func(t *testing.T) {
testPayload(t, []byte("this is clearly a UTF8 string"))
})
t.Run("Binary", func(t *testing.T) {
testPayload(t, []byte{0x00, 0x06, 0xFF, 0xF0})
})
t.Run("LargePayload", func(t *testing.T) {
// generate a very large random payload
payload := make([]byte, 70*1024)
rand.Read(payload)
testPayload(t, payload)
})
}
func TestSampleMsgpack(t *testing.T) {
var (
sampleEncoded = []byte{
0x85, 0xa8, 0x6d, 0x73, 0x67, 0x5f, 0x74, 0x79,
0x70, 0x65, 0x03, 0xb0, 0x74, 0x72, 0x61, 0x6e,
0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f,
0x75, 0x75, 0x69, 0x64, 0xd9, 0x24, 0x39, 0x34,
0x34, 0x37, 0x32, 0x34, 0x31, 0x63, 0x2d, 0x35,
0x32, 0x33, 0x38, 0x2d, 0x34, 0x63, 0x62, 0x39,
0x2d, 0x39, 0x62, 0x61, 0x61, 0x2d, 0x37, 0x30,
0x37, 0x36, 0x65, 0x33, 0x32, 0x33, 0x32, 0x38,
0x39, 0x39, 0xa6, 0x73, 0x6f, 0x75, 0x72, 0x63,
0x65, 0xd9, 0x26, 0x64, 0x6e, 0x73, 0x3a, 0x77,
0x65, 0x62, 0x70, 0x61, 0x2e, 0x63, 0x6f, 0x6d,
0x63, 0x61, 0x73, 0x74, 0x2e, 0x63, 0x6f, 0x6d,
0x2f, 0x76, 0x32, 0x2d, 0x64, 0x65, 0x76, 0x69,
0x63, 0x65, 0x2d, 0x63, 0x6f, 0x6e, 0x66, 0x69,
0x67, 0xa4, 0x64, 0x65, 0x73, 0x74, 0xb2, 0x73,
0x65, 0x72, 0x69, 0x61, 0x6c, 0x3a, 0x31, 0x32,
0x33, 0x34, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x69,
0x67, 0xa7, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61,
0x64, 0xc4, 0x45, 0x7b, 0x20, 0x22, 0x6e, 0x61,
0x6d, 0x65, 0x73, 0x22, 0x3a, 0x20, 0x5b, 0x20,
0x22, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x2e,
0x58, 0x5f, 0x43, 0x49, 0x53, 0x43, 0x4f, 0x5f,
0x43, 0x4f, 0x4d, 0x5f, 0x53, 0x65, 0x63, 0x75,
0x72, 0x69, 0x74, 0x79, 0x2e, 0x46, 0x69, 0x72,
0x65, 0x77, 0x61, 0x6c, 0x6c, 0x2e, 0x46, 0x69,
0x72, 0x65, 0x77, 0x61, 0x6c, 0x6c, 0x4c, 0x65,
0x76, 0x65, 0x6c, 0x22, 0x20, 0x5d, 0x20, 0x7d,
}
sampleMessage = SimpleRequestResponse{
Type: SimpleRequestResponseMessageType,
Source: "dns:webpa.comcast.com/v2-device-config",
Destination: "serial:1234/config",
TransactionUUID: "9447241c-5238-4cb9-9baa-7076e3232899",
Payload: []byte(
`{ "names": [ "Device.X_CISCO_COM_Security.Firewall.FirewallLevel" ] }`,
),
}
)
t.Run("Encode", func(t *testing.T) {
var (
assert = assert.New(t)
buffer bytes.Buffer
encoder = NewEncoder(&buffer, Msgpack)
decoder = NewDecoder(&buffer, Msgpack)
actualMessage SimpleRequestResponse
)
assert.NoError(encoder.Encode(&sampleMessage))
assert.NoError(decoder.Decode(&actualMessage))
assert.Equal(sampleMessage, actualMessage)
})
t.Run("Decode", func(t *testing.T) {
var (
assert = assert.New(t)
decoder = NewDecoder(bytes.NewBuffer(sampleEncoded), Msgpack)
actualMessage SimpleRequestResponse
)
assert.NoError(decoder.Decode(&actualMessage))
assert.Equal(sampleMessage, actualMessage)
})
t.Run("DecodeBytes", func(t *testing.T) {
var (
assert = assert.New(t)
decoder = NewDecoderBytes(sampleEncoded, Msgpack)
actualMessage SimpleRequestResponse
)
assert.NoError(decoder.Decode(&actualMessage))
assert.Equal(sampleMessage, actualMessage)
})
}
func testFormatFromContentTypeInvalid(t *testing.T, contentType string) {
assert := assert.New(t)
_, err := FormatFromContentType(contentType)
assert.Error(err)
// fallback won't matter if the content type is bad
_, err = FormatFromContentType(contentType, Msgpack)
assert.Error(err)
_, err = FormatFromContentType(contentType, JSON)
assert.Error(err)
}
func testFormatFromContentTypeValid(t *testing.T, contentType string, expected Format) {
assert := assert.New(t)
actual, err := FormatFromContentType(contentType)
assert.Equal(expected, actual)
assert.NoError(err)
// For a valid content type, fallback won't matter
actual, err = FormatFromContentType(contentType, Msgpack)
assert.Equal(expected, actual)
assert.NoError(err)
actual, err = FormatFromContentType(contentType, JSON)
assert.Equal(expected, actual)
assert.NoError(err)
}
func testFormatFromContentTypeFallback(t *testing.T) {
assert := assert.New(t)
actual, err := FormatFromContentType("", Msgpack)
assert.Equal(Msgpack, actual)
assert.NoError(err)
actual, err = FormatFromContentType("", JSON)
assert.Equal(JSON, actual)
assert.NoError(err)
}
func TestFormatFromContentType(t *testing.T) {
t.Run("Invalid", func(t *testing.T) {
testFormatFromContentTypeInvalid(t, "text/plain")
testFormatFromContentTypeInvalid(t, MimeTypeOctetStream)
})
t.Run("Valid", func(t *testing.T) {
testFormatFromContentTypeValid(t, MimeTypeMsgpack, Msgpack)
testFormatFromContentTypeValid(t, MimeTypeJson, JSON)
testFormatFromContentTypeValid(t, "text/json", JSON)
})
t.Run("Fallback", testFormatFromContentTypeFallback)
}
func testFormatString(t *testing.T) {
assert := assert.New(t)
assert.NotEmpty(JSON.String())
assert.NotEmpty(Msgpack.String())
assert.NotEmpty(Format(-1).String())
assert.NotEqual(JSON.String(), Msgpack.String())
}
func testFormatHandle(t *testing.T) {
assert := assert.New(t)
assert.NotNil(JSON.handle())
assert.NotNil(Msgpack.handle())
assert.Panics(func() { Format(999).handle() })
}
func testFormatContentType(t *testing.T) {
assert := assert.New(t)
assert.NotEmpty(JSON.ContentType())
assert.NotEmpty(Msgpack.ContentType())
assert.NotEqual(JSON.ContentType(), Msgpack.ContentType())
assert.Equal(MimeTypeOctetStream, Format(999).ContentType())
}
func TestFormat(t *testing.T) {
t.Run("String", testFormatString)
t.Run("Handle", testFormatHandle)
t.Run("ContentType", testFormatContentType)
}
// testTranscodeMessage expects a nonpointer reference to a WRP message struct as the original parameter
func testTranscodeMessage(t *testing.T, target, source Format, original interface{}) {
var (
assert = assert.New(t)
require = require.New(t)
originalValue = reflect.ValueOf(original)
encodeValue = reflect.New(originalValue.Type())
decodeValue = reflect.New(originalValue.Type())
)
// encodeValue is now a pointer to a copy of the original
encodeValue.Elem().Set(originalValue)
var (
sourceBuffer bytes.Buffer
sourceEncoder = NewEncoder(&sourceBuffer, source)
sourceDecoder = NewDecoder(&sourceBuffer, source)
targetBuffer bytes.Buffer
targetEncoder = NewEncoder(&targetBuffer, target)
targetDecoder = NewDecoder(&targetBuffer, target)
)
// create the input first
require.NoError(sourceEncoder.Encode(encodeValue.Interface()))
// now we can attempt the transcode
message, err := TranscodeMessage(targetEncoder, sourceDecoder)
assert.NotNil(message)
assert.NoError(err)
assert.NoError(targetDecoder.Decode(decodeValue.Interface()))
assert.Equal(encodeValue.Elem().Interface(), decodeValue.Elem().Interface())
}
func TestTranscodeMessage(t *testing.T) {
var (
expectedStatus int64 = 123
expectedRequestDeliveryResponse int64 = -1234
messages = []interface{}{
SimpleRequestResponse{},
SimpleRequestResponse{
Source: "foobar.com",
Destination: "mac:FFEEDDCCBBAA",
Payload: []byte("hi!"),
},
SimpleRequestResponse{
Source: "foobar.com",
Destination: "mac:FFEEDDCCBBAA",
ContentType: MimeTypeWrp,
Accept: MimeTypeWrp,
Status: &expectedStatus,
RequestDeliveryResponse: &expectedRequestDeliveryResponse,
Headers: []string{"X-Header-1", "X-Header-2"},
Metadata: map[string]string{"hi": "there"},
Payload: []byte("hi!"),
},
Message{},
Message{
Source: "foobar.com",
Destination: "mac:FFEEDDCCBBAA",
Payload: []byte("hi!"),
},
Message{
Source: "foobar.com",
Destination: "mac:FFEEDDCCBBAA",
ContentType: MimeTypeWrp,
Accept: MimeTypeWrp,
Status: &expectedStatus,
RequestDeliveryResponse: &expectedRequestDeliveryResponse,
Headers: []string{"X-Header-1", "X-Header-2"},
Metadata: map[string]string{"hi": "there"},
Payload: []byte("hi!"),
},
}
)
for _, target := range allFormats {
for _, source := range allFormats {
t.Run(fmt.Sprintf("%sTo%s", source, target), func(t *testing.T) {
for _, original := range messages {
testTranscodeMessage(t, target, source, original)
}
})
}
}
}