-
Notifications
You must be signed in to change notification settings - Fork 6
/
mli_test.go
224 lines (197 loc) · 5.2 KB
/
mli_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
/*
* Copyright 2020 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package simplemli
import (
"encoding/hex"
"testing"
)
type MLICase struct {
Name string
Size int
Encoded string
Invalid string
Value int
EmbeddedHeader int
}
func TestMLIs(t *testing.T) {
mc := []MLICase{
{
Name: "2I",
Size: Size2I,
Encoded: "002d",
Invalid: "0001",
Value: 43,
},
{
Name: "2E",
Size: Size2E,
Encoded: "002b",
Value: 43,
},
{
Name: "4I",
Size: Size4I,
Encoded: "00000035",
Invalid: "00000001",
Value: 49,
},
{
Name: "4E",
Size: Size4E,
Encoded: "00000022",
Value: 34,
},
{
Name: "2EE",
Size: Size2EE,
Encoded: "0036",
Value: 56,
EmbeddedHeader: 2,
},
{
Name: "2BCD2",
Size: Size2BCD2,
Encoded: "00000288",
Invalid: "00000001",
Value: 284,
},
{
Name: "A4E",
Size: SizeA4E,
Encoded: "30303433",
Value: 43,
},
}
// Execute Various Test Cases
for _, c := range mc {
t.Run("Decode "+c.Name, func(t *testing.T) {
b, err := hex.DecodeString(c.Encoded)
if err != nil {
t.Errorf("Unable to decode test case sample payload hex - %s", err)
t.FailNow()
}
n, err := Decode(c.Name, &b)
if err != nil {
t.Errorf("Unexpected error decoding sample MLI - %s", err)
}
if n != c.Value {
t.Errorf("Unexpected value returned from MLI %s, got %d expected %d", c.Encoded, n, c.Value)
}
})
t.Run("Encode "+c.Name, func(t *testing.T) {
b, err := Encode(c.Name, c.Value)
if err != nil {
t.Errorf("Unable to encode test case length - %s", err)
t.FailNow()
}
if hex.EncodeToString(b) != c.Encoded {
t.Errorf("Encoded value does not match expectations, got %s, expected %s", hex.EncodeToString(b), c.Encoded)
}
})
t.Run("Encode & Decode "+c.Name, func(t *testing.T) {
b, err := Encode(c.Name, c.Value)
if err != nil {
t.Errorf("Unable to encode test case length - %s", err)
t.FailNow()
}
n, err := Decode(c.Name, &b)
if err != nil {
t.Errorf("Unexpected error decoding sample MLI - %s", err)
}
if n != c.Value {
t.Errorf("Unexpected value returned from MLI %s, got %d expected %d", c.Encoded, n, c.Value)
}
})
t.Run("Zero Byte Encode & Decode "+c.Name, func(t *testing.T) {
b, err := Encode(c.Name, 0+c.EmbeddedHeader)
if err != nil {
t.Errorf("Unable to encode test case length - %s", err)
t.FailNow()
}
n, err := Decode(c.Name, &b)
if err != nil {
t.Errorf("Unexpected error decoding sample MLI - %s", err)
}
if n != 0+c.EmbeddedHeader {
t.Errorf("Unexpected value returned from MLI %s, got %d expected %d", hex.EncodeToString(b), n, 0)
}
})
if c.Invalid != "" {
t.Run("Invalid MLI value "+c.Name, func(t *testing.T) {
b, err := hex.DecodeString(c.Invalid)
if err != nil {
t.Errorf("Unable to decode test case sample payload hex - %s", err)
t.FailNow()
}
_, err = Decode(c.Name, &b)
if err == nil || err != ErrLength {
t.Errorf("Expected error decoding invalid MLI got %s", err)
}
})
}
}
}
func TestInvalid(t *testing.T) {
t.Run("Encode", func(t *testing.T) {
_, err := Encode("Invalid", 0)
if err == nil {
t.Errorf("Expected error when calling Encode with bad mli type - got nil")
}
})
t.Run("Encode with negative number", func(t *testing.T) {
_, err := Encode("2I", -1)
if err == nil {
t.Errorf("Expected error when calling Encode with a negative number - got nil")
}
})
t.Run("Decode", func(t *testing.T) {
_, err := Decode("Invalid", &empty)
if err == nil {
t.Errorf("Expected error when calling Decode with bad mli type - got nil")
}
})
t.Run("A4E Random String", func(t *testing.T) {
b := []byte("helo")
_, err := Decode("A4E", &b)
if err == nil {
t.Errorf("Expected error when feeding decode a random string - got nil")
}
})
}
func TestBadSizedBytes(t *testing.T) {
tl := map[string]int{
"2I": Size2I,
"2E": Size2E,
"4I": Size4I,
"4E": Size4E,
"2EE": Size2EE,
"2BCD2": Size2BCD2,
}
for k, v := range tl {
t.Run(k+" Bigger than expected test", func(t *testing.T) {
b := make([]byte, v+10000)
_, err := Decode(k, &b)
if err == nil {
t.Errorf("Expected error when sending too big byte slice to decode sent %d for mli type %s", len(b), k)
}
})
t.Run(k+" Smaller than expected test", func(t *testing.T) {
b := make([]byte, v-1)
_, err := Decode(k, &b)
if err == nil {
t.Errorf("Expected error when sending too small byte slice to decode sent %d for mli type %s", len(b), k)
}
})
}
}