-
Notifications
You must be signed in to change notification settings - Fork 7
/
utf8_test.go
95 lines (85 loc) · 2.41 KB
/
utf8_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
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrp
import (
"testing"
"unicode/utf8"
"github.com/stretchr/testify/assert"
)
func TestInvalidUtf8Decoding(t *testing.T) {
assert := assert.New(t)
/*
"\x85" - 5 name value pairs
"\xa8""msg_type" : "\x03" // 3
"\xa4""dest" : "\xac""\xed\xbf\xbft-address"
"\xa7""payload" : "\xc4""\x03" - len 3
"123"
"\xa6""source" : "\xae""source-address"
"\xb0""transaction_uuid" : "\xd9\x24""c07ee5e1-70be-444c-a156-097c767ad8aa"
*/
invalid := []byte{
0x85,
0xa8, 'm', 's', 'g', '_', 't', 'y', 'p', 'e', 0x03,
0xa4, 'd', 'e', 's', 't', 0xac /* \xed\xbf\xbf is invalid */, 0xed, 0xbf, 0xbf, 't', '-', 'a', 'd', 'd', 'r', 'e', 's', 's',
0xa7, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0xc4, 0x03, '1', '2', '3',
0xa6, 's', 'o', 'u', 'r', 'c', 'e', 0xae, 's', 'o', 'u', 'r', 'c', 'e', '-', 'a', 'd', 'd', 'r', 'e', 's', 's',
0xb0, 't', 'r', 'a', 'n', 's', 'a', 'c', 't', 'i', 'o', 'n', '_', 'u', 'u', 'i', 'd', 0xd9, 0x24, 'c', '0', '7', 'e', 'e', '5', 'e', '1', '-', '7', '0', 'b', 'e', '-', '4', '4', '4', 'c', '-', 'a', '1', '5', '6', '-', '0', '9', '7', 'c', '7', '6', '7', 'a', 'd', '8', 'a', 'a',
}
decoder := NewDecoderBytes(invalid, Msgpack)
msg := new(Message)
err := decoder.Decode(msg)
assert.Nil(err)
assert.True(utf8.ValidString(msg.Source))
assert.False(utf8.ValidString(msg.Destination))
err = UTF8(msg)
assert.ErrorIs(err, ErrNotUTF8)
}
func TestUTF8(t *testing.T) {
type Test struct {
unexported string
Name string
Age int
}
testVal := Test{
unexported: "this shouldn't be output",
Name: "Joe Schmoe",
Age: 415,
}
tests := []struct {
description string
value interface{}
expectedErr error
}{
{
description: "Success",
value: testVal,
},
{
description: "Pointer success",
value: &testVal,
},
{
description: "Non struct error",
value: 5,
expectedErr: ErrUnexpectedKind,
},
{
description: "UTF8 error",
value: Test{
Name: string([]byte{0xbf}),
},
expectedErr: ErrNotUTF8,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
err := UTF8(tc.value)
if tc.expectedErr == nil {
assert.NoError(err)
return
}
assert.ErrorIs(err, tc.expectedErr)
})
}
}