1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 type intStruct
struct {
21 type twoIntStruct
struct {
26 type bigIntStruct
struct {
30 type nestedStruct
struct {
34 type rawContentsStruct
struct {
39 type implicitTagTest
struct {
40 A
int `asn1:"implicit,tag:5"`
43 type explicitTagTest
struct {
44 A
int `asn1:"explicit,tag:5"`
47 type flagTest
struct {
48 A Flag
`asn1:"tag:0,optional"`
51 type generalizedTimeTest
struct {
52 A time
.Time
`asn1:"generalized"`
55 type ia5StringTest
struct {
59 type printableStringTest
struct {
60 A
string `asn1:"printable"`
63 type genericStringTest
struct {
67 type optionalRawValueTest
struct {
68 A RawValue
`asn1:"optional"`
71 type omitEmptyTest
struct {
72 A
[]string `asn1:"omitempty"`
75 type defaultTest
struct {
76 A
int `asn1:"optional,default:1"`
79 type applicationTest
struct {
80 A
int `asn1:"application,tag:0"`
81 B
int `asn1:"application,tag:1,explicit"`
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"` // tag size should be 2 octet
88 D
int `asn1:"private,tag:128"` // tag size should be 3 octet
91 type numericStringTest
struct {
92 A
string `asn1:"numeric"`
97 var PST
= time
.FixedZone("PST", -8*60*60)
99 type marshalTest
struct {
101 out
string // hex encoded
104 func farFuture() time
.Time
{
105 t
, err
:= time
.Parse(time
.RFC3339
, "2100-04-05T12:01:01Z")
112 var marshalTests
= []marshalTest
{
118 {intStruct
{64}, "3003020140"},
119 {bigIntStruct
{big
.NewInt(0x123456)}, "30050203123456"},
120 {twoIntStruct
{64, 65}, "3006020140020141"},
121 {nestedStruct
{intStruct
{127}}, "3005300302017f"},
122 {[]byte{1, 2, 3}, "0403010203"},
123 {implicitTagTest
{64}, "3003850140"},
124 {explicitTagTest
{64}, "3005a503020140"},
125 {flagTest
{true}, "30028000"},
126 {flagTest
{false}, "3000"},
127 {time
.Unix(0, 0).UTC(), "170d3730303130313030303030305a"},
128 {time
.Unix(1258325776, 0).UTC(), "170d3039313131353232353631365a"},
129 {time
.Unix(1258325776, 0).In(PST
), "17113039313131353134353631362d30383030"},
130 {farFuture(), "180f32313030303430353132303130315a"},
131 {generalizedTimeTest
{time
.Unix(1258325776, 0).UTC()}, "3011180f32303039313131353232353631365a"},
132 {BitString
{[]byte{0x80}, 1}, "03020780"},
133 {BitString
{[]byte{0x81, 0xf0}, 12}, "03030481f0"},
134 {ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"},
135 {ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"},
136 {ObjectIdentifier([]int{2, 100, 3}), "0603813403"},
137 {"test", "130474657374"},
140 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
141 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
142 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
143 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 127 times 'x'
145 "7878787878787878787878787878787878787878787878787878787878787878" +
146 "7878787878787878787878787878787878787878787878787878787878787878" +
147 "7878787878787878787878787878787878787878787878787878787878787878" +
148 "78787878787878787878787878787878787878787878787878787878787878",
152 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
153 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
154 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
155 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 128 times 'x'
157 "7878787878787878787878787878787878787878787878787878787878787878" +
158 "7878787878787878787878787878787878787878787878787878787878787878" +
159 "7878787878787878787878787878787878787878787878787878787878787878" +
160 "7878787878787878787878787878787878787878787878787878787878787878",
162 {ia5StringTest
{"test"}, "3006160474657374"},
163 {optionalRawValueTest
{}, "3000"},
164 {printableStringTest
{"test"}, "3006130474657374"},
165 {printableStringTest
{"test*"}, "30071305746573742a"},
166 {genericStringTest
{"test"}, "3006130474657374"},
167 {genericStringTest
{"test*"}, "30070c05746573742a"},
168 {genericStringTest
{"test&"}, "30070c057465737426"},
169 {rawContentsStruct
{nil, 64}, "3003020140"},
170 {rawContentsStruct
{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"},
171 {RawValue
{Tag
: 1, Class
: 2, IsCompound
: false, Bytes
: []byte{1, 2, 3}}, "8103010203"},
172 {testSET([]int{10}), "310302010a"},
173 {omitEmptyTest
{[]string{}}, "3000"},
174 {omitEmptyTest
{[]string{"1"}}, "30053003130131"},
176 {defaultTest
{0}, "3003020100"},
177 {defaultTest
{1}, "3000"},
178 {defaultTest
{2}, "3003020102"},
179 {applicationTest
{1, 2}, "30084001016103020102"},
180 {privateTest
{1, 2, 3, 4}, "3011c00101e103020102df1f0103df81000104"},
181 {numericStringTest
{"1 9"}, "30051203312039"},
184 func TestMarshal(t
*testing
.T
) {
185 for i
, test
:= range marshalTests
{
186 data
, err
:= Marshal(test
.in
)
188 t
.Errorf("#%d failed: %s", i
, err
)
190 out
, _
:= hex
.DecodeString(test
.out
)
191 if !bytes
.Equal(out
, data
) {
192 t
.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i
, data
, out
, data
, out
)
198 type marshalWithParamsTest
struct {
201 out
string // hex encoded
204 var marshalWithParamsTests
= []marshalWithParamsTest
{
205 {intStruct
{10}, "set", "310302010a"},
206 {intStruct
{10}, "application", "600302010a"},
207 {intStruct
{10}, "private", "e00302010a"},
210 func TestMarshalWithParams(t
*testing
.T
) {
211 for i
, test
:= range marshalWithParamsTests
{
212 data
, err
:= MarshalWithParams(test
.in
, test
.params
)
214 t
.Errorf("#%d failed: %s", i
, err
)
216 out
, _
:= hex
.DecodeString(test
.out
)
217 if !bytes
.Equal(out
, data
) {
218 t
.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i
, data
, out
, data
, out
)
224 type marshalErrTest
struct {
229 var marshalErrTests
= []marshalErrTest
{
230 {bigIntStruct
{nil}, "empty integer"},
231 {numericStringTest
{"a"}, "invalid character"},
232 {ia5StringTest
{"\xb0"}, "invalid character"},
233 {printableStringTest
{"!"}, "invalid character"},
236 func TestMarshalError(t
*testing
.T
) {
237 for i
, test
:= range marshalErrTests
{
238 _
, err
:= Marshal(test
.in
)
240 t
.Errorf("#%d should fail, but success", i
)
244 if !strings
.Contains(err
.Error(), test
.err
) {
245 t
.Errorf("#%d got: %v want %v", i
, err
, test
.err
)
250 func TestInvalidUTF8(t
*testing
.T
) {
251 _
, err
:= Marshal(string([]byte{0xff, 0xff}))
253 t
.Errorf("invalid UTF8 string was accepted")
257 func TestMarshalOID(t
*testing
.T
) {
258 var marshalTestsOID
= []marshalTest
{
259 {[]byte("\x06\x01\x30"), "0403060130"}, // bytes format returns a byte sequence \x04
260 // {ObjectIdentifier([]int{0}), "060100"}, // returns an error as OID 0.0 has the same encoding
261 {[]byte("\x06\x010"), "0403060130"}, // same as above "\x06\x010" = "\x06\x01" + "0"
262 {ObjectIdentifier([]int{2, 999, 3}), "0603883703"}, // Example of ITU-T X.690
263 {ObjectIdentifier([]int{0, 0}), "060100"}, // zero OID
265 for i
, test
:= range marshalTestsOID
{
266 data
, err
:= Marshal(test
.in
)
268 t
.Errorf("#%d failed: %s", i
, err
)
270 out
, _
:= hex
.DecodeString(test
.out
)
271 if !bytes
.Equal(out
, data
) {
272 t
.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i
, data
, out
, data
, out
)
277 func TestIssue11130(t
*testing
.T
) {
278 data
:= []byte("\x06\x010") // == \x06\x01\x30 == OID = 0 (the figure)
280 // v has Zero value here and Elem() would panic
281 _
, err
:= Unmarshal(data
, &v
)
286 if reflect
.TypeOf(v
).String() != reflect
.TypeOf(ObjectIdentifier
{}).String() {
287 t
.Errorf("marshal OID returned an invalid type")
291 data1
, err
:= Marshal(v
)
297 if !bytes
.Equal(data
, data1
) {
298 t
.Errorf("got: %q, want: %q \n", data1
, data
)
303 _
, err
= Unmarshal(data1
, &v1
)
308 if !reflect
.DeepEqual(v
, v1
) {
309 t
.Errorf("got: %#v data=%q , want : %#v data=%q\n ", v1
, data1
, v
, data
)
313 func BenchmarkMarshal(b
*testing
.B
) {
316 for i
:= 0; i
< b
.N
; i
++ {
317 for _
, test
:= range marshalTests
{