1
2
3
4
5 package asn1
6
7 import (
8 "bytes"
9 "encoding/hex"
10 "math/big"
11 "reflect"
12 "strings"
13 "testing"
14 "time"
15 )
16
17 type intStruct struct {
18 A int
19 }
20
21 type twoIntStruct struct {
22 A int
23 B int
24 }
25
26 type bigIntStruct struct {
27 A *big.Int
28 }
29
30 type nestedStruct struct {
31 A intStruct
32 }
33
34 type rawContentsStruct struct {
35 Raw RawContent
36 A int
37 }
38
39 type implicitTagTest struct {
40 A int `asn1:"implicit,tag:5"`
41 }
42
43 type explicitTagTest struct {
44 A int `asn1:"explicit,tag:5"`
45 }
46
47 type flagTest struct {
48 A Flag `asn1:"tag:0,optional"`
49 }
50
51 type generalizedTimeTest struct {
52 A time.Time `asn1:"generalized"`
53 }
54
55 type ia5StringTest struct {
56 A string `asn1:"ia5"`
57 }
58
59 type printableStringTest struct {
60 A string `asn1:"printable"`
61 }
62
63 type genericStringTest struct {
64 A string
65 }
66
67 type optionalRawValueTest struct {
68 A RawValue `asn1:"optional"`
69 }
70
71 type omitEmptyTest struct {
72 A []string `asn1:"omitempty"`
73 }
74
75 type defaultTest struct {
76 A int `asn1:"optional,default:1"`
77 }
78
79 type applicationTest struct {
80 A int `asn1:"application,tag:0"`
81 B int `asn1:"application,tag:1,explicit"`
82 }
83
84 type privateTest struct {
85 A int `asn1:"private,tag:0"`
86 B int `asn1:"private,tag:1,explicit"`
87 C int `asn1:"private,tag:31"`
88 D int `asn1:"private,tag:128"`
89 }
90
91 type numericStringTest struct {
92 A string `asn1:"numeric"`
93 }
94
95 type testAuthKeyID struct {
96 ID []byte `asn1:"optional,tag:0"`
97 Issuer RawValue `asn1:"optional,tag:1"`
98 SerialNumber *big.Int `asn1:"optional,tag:2"`
99 }
100
101 type testSET []int
102
103 var PST = time.FixedZone("PST", -8*60*60)
104
105 type marshalTest struct {
106 in interface{}
107 out string
108 }
109
110 func farFuture() time.Time {
111 t, err := time.Parse(time.RFC3339, "2100-04-05T12:01:01Z")
112 if err != nil {
113 panic(err)
114 }
115 return t
116 }
117
118 var marshalTests = []marshalTest{
119 {10, "02010a"},
120 {127, "02017f"},
121 {128, "02020080"},
122 {-128, "020180"},
123 {-129, "0202ff7f"},
124 {intStruct{64}, "3003020140"},
125 {bigIntStruct{big.NewInt(0x123456)}, "30050203123456"},
126 {twoIntStruct{64, 65}, "3006020140020141"},
127 {nestedStruct{intStruct{127}}, "3005300302017f"},
128 {[]byte{1, 2, 3}, "0403010203"},
129 {implicitTagTest{64}, "3003850140"},
130 {explicitTagTest{64}, "3005a503020140"},
131 {flagTest{true}, "30028000"},
132 {flagTest{false}, "3000"},
133 {time.Unix(0, 0).UTC(), "170d3730303130313030303030305a"},
134 {time.Unix(1258325776, 0).UTC(), "170d3039313131353232353631365a"},
135 {time.Unix(1258325776, 0).In(PST), "17113039313131353134353631362d30383030"},
136 {farFuture(), "180f32313030303430353132303130315a"},
137 {generalizedTimeTest{time.Unix(1258325776, 0).UTC()}, "3011180f32303039313131353232353631365a"},
138 {BitString{[]byte{0x80}, 1}, "03020780"},
139 {BitString{[]byte{0x81, 0xf0}, 12}, "03030481f0"},
140 {ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"},
141 {ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"},
142 {ObjectIdentifier([]int{2, 100, 3}), "0603813403"},
143 {"test", "130474657374"},
144 {
145 "" +
146 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
147 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
148 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
149 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
150 "137f" +
151 "7878787878787878787878787878787878787878787878787878787878787878" +
152 "7878787878787878787878787878787878787878787878787878787878787878" +
153 "7878787878787878787878787878787878787878787878787878787878787878" +
154 "78787878787878787878787878787878787878787878787878787878787878",
155 },
156 {
157 "" +
158 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
159 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
160 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
161 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
162 "138180" +
163 "7878787878787878787878787878787878787878787878787878787878787878" +
164 "7878787878787878787878787878787878787878787878787878787878787878" +
165 "7878787878787878787878787878787878787878787878787878787878787878" +
166 "7878787878787878787878787878787878787878787878787878787878787878",
167 },
168 {ia5StringTest{"test"}, "3006160474657374"},
169 {optionalRawValueTest{}, "3000"},
170 {printableStringTest{"test"}, "3006130474657374"},
171 {printableStringTest{"test*"}, "30071305746573742a"},
172 {genericStringTest{"test"}, "3006130474657374"},
173 {genericStringTest{"test*"}, "30070c05746573742a"},
174 {genericStringTest{"test&"}, "30070c057465737426"},
175 {rawContentsStruct{nil, 64}, "3003020140"},
176 {rawContentsStruct{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"},
177 {RawValue{Tag: 1, Class: 2, IsCompound: false, Bytes: []byte{1, 2, 3}}, "8103010203"},
178 {testSET([]int{10}), "310302010a"},
179 {omitEmptyTest{[]string{}}, "3000"},
180 {omitEmptyTest{[]string{"1"}}, "30053003130131"},
181 {"Σ", "0c02cea3"},
182 {defaultTest{0}, "3003020100"},
183 {defaultTest{1}, "3000"},
184 {defaultTest{2}, "3003020102"},
185 {applicationTest{1, 2}, "30084001016103020102"},
186 {privateTest{1, 2, 3, 4}, "3011c00101e103020102df1f0103df81000104"},
187 {numericStringTest{"1 9"}, "30051203312039"},
188 {testAuthKeyID{ID: []byte{0x01, 0x02, 0x03, 0x04}, SerialNumber: big.NewInt(0x12233445566)}, "300e8004010203048206012233445566"},
189 {testAuthKeyID{ID: []byte{0x01, 0x02, 0x03, 0x04}}, "3006800401020304"},
190 }
191
192 func TestMarshal(t *testing.T) {
193 for i, test := range marshalTests {
194 data, err := Marshal(test.in)
195 if err != nil {
196 t.Errorf("#%d failed: %s", i, err)
197 }
198 out, _ := hex.DecodeString(test.out)
199 if !bytes.Equal(out, data) {
200 t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out)
201
202 }
203 }
204 }
205
206 type marshalWithParamsTest struct {
207 in interface{}
208 params string
209 out string
210 }
211
212 var marshalWithParamsTests = []marshalWithParamsTest{
213 {intStruct{10}, "set", "310302010a"},
214 {intStruct{10}, "application", "600302010a"},
215 {intStruct{10}, "private", "e00302010a"},
216 }
217
218 func TestMarshalWithParams(t *testing.T) {
219 for i, test := range marshalWithParamsTests {
220 data, err := MarshalWithParams(test.in, test.params)
221 if err != nil {
222 t.Errorf("#%d failed: %s", i, err)
223 }
224 out, _ := hex.DecodeString(test.out)
225 if !bytes.Equal(out, data) {
226 t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out)
227
228 }
229 }
230 }
231
232 type marshalErrTest struct {
233 in interface{}
234 err string
235 }
236
237 var marshalErrTests = []marshalErrTest{
238 {bigIntStruct{nil}, "empty integer"},
239 {numericStringTest{"a"}, "invalid character"},
240 {ia5StringTest{"\xb0"}, "invalid character"},
241 {printableStringTest{"!"}, "invalid character"},
242 }
243
244 func TestMarshalError(t *testing.T) {
245 for i, test := range marshalErrTests {
246 _, err := Marshal(test.in)
247 if err == nil {
248 t.Errorf("#%d should fail, but success", i)
249 continue
250 }
251
252 if !strings.Contains(err.Error(), test.err) {
253 t.Errorf("#%d got: %v want %v", i, err, test.err)
254 }
255 }
256 }
257
258 func TestInvalidUTF8(t *testing.T) {
259 _, err := Marshal(string([]byte{0xff, 0xff}))
260 if err == nil {
261 t.Errorf("invalid UTF8 string was accepted")
262 }
263 }
264
265 func TestMarshalOID(t *testing.T) {
266 var marshalTestsOID = []marshalTest{
267 {[]byte("\x06\x01\x30"), "0403060130"},
268
269 {[]byte("\x06\x010"), "0403060130"},
270 {ObjectIdentifier([]int{2, 999, 3}), "0603883703"},
271 {ObjectIdentifier([]int{0, 0}), "060100"},
272 }
273 for i, test := range marshalTestsOID {
274 data, err := Marshal(test.in)
275 if err != nil {
276 t.Errorf("#%d failed: %s", i, err)
277 }
278 out, _ := hex.DecodeString(test.out)
279 if !bytes.Equal(out, data) {
280 t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out)
281 }
282 }
283 }
284
285 func TestIssue11130(t *testing.T) {
286 data := []byte("\x06\x010")
287 var v interface{}
288
289 _, err := Unmarshal(data, &v)
290 if err != nil {
291 t.Errorf("%v", err)
292 return
293 }
294 if reflect.TypeOf(v).String() != reflect.TypeOf(ObjectIdentifier{}).String() {
295 t.Errorf("marshal OID returned an invalid type")
296 return
297 }
298
299 data1, err := Marshal(v)
300 if err != nil {
301 t.Errorf("%v", err)
302 return
303 }
304
305 if !bytes.Equal(data, data1) {
306 t.Errorf("got: %q, want: %q \n", data1, data)
307 return
308 }
309
310 var v1 interface{}
311 _, err = Unmarshal(data1, &v1)
312 if err != nil {
313 t.Errorf("%v", err)
314 return
315 }
316 if !reflect.DeepEqual(v, v1) {
317 t.Errorf("got: %#v data=%q , want : %#v data=%q\n ", v1, data1, v, data)
318 }
319 }
320
321 func BenchmarkMarshal(b *testing.B) {
322 b.ReportAllocs()
323
324 for i := 0; i < b.N; i++ {
325 for _, test := range marshalTests {
326 Marshal(test.in)
327 }
328 }
329 }
330
View as plain text