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.
14 type intStruct struct {
18 type twoIntStruct struct {
23 type nestedStruct struct {
27 type rawContentsStruct struct {
32 type implicitTagTest struct {
33 A int `asn1:"implicit,tag:5"`
36 type explicitTagTest struct {
37 A int `asn1:"explicit,tag:5"`
40 type ia5StringTest struct {
44 type printableStringTest struct {
45 A string `asn1:"printable"`
48 type optionalRawValueTest struct {
49 A RawValue `asn1:"optional"`
54 func setPST(t *time.Time) *time.Time {
59 type marshalTest struct {
61 out string // hex encoded
64 var marshalTests = []marshalTest{
70 {intStruct{64}, "3003020140"},
71 {twoIntStruct{64, 65}, "3006020140020141"},
72 {nestedStruct{intStruct{127}}, "3005300302017f"},
73 {[]byte{1, 2, 3}, "0403010203"},
74 {implicitTagTest{64}, "3003850140"},
75 {explicitTagTest{64}, "3005a503020140"},
76 {time.SecondsToUTC(0), "170d3730303130313030303030305a"},
77 {time.SecondsToUTC(1258325776), "170d3039313131353232353631365a"},
78 {setPST(time.SecondsToUTC(1258325776)), "17113039313131353232353631362d30383030"},
79 {BitString{[]byte{0x80}, 1}, "03020780"},
80 {BitString{[]byte{0x81, 0xf0}, 12}, "03030481f0"},
81 {ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"},
82 {ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"},
83 {"test", "130474657374"},
86 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
87 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
88 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
89 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 127 times 'x'
91 "7878787878787878787878787878787878787878787878787878787878787878" +
92 "7878787878787878787878787878787878787878787878787878787878787878" +
93 "7878787878787878787878787878787878787878787878787878787878787878" +
94 "78787878787878787878787878787878787878787878787878787878787878",
98 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
99 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
100 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
101 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 128 times 'x'
103 "7878787878787878787878787878787878787878787878787878787878787878" +
104 "7878787878787878787878787878787878787878787878787878787878787878" +
105 "7878787878787878787878787878787878787878787878787878787878787878" +
106 "7878787878787878787878787878787878787878787878787878787878787878",
108 {ia5StringTest{"test"}, "3006160474657374"},
109 {optionalRawValueTest{}, "3000"},
110 {printableStringTest{"test"}, "3006130474657374"},
111 {printableStringTest{"test*"}, "30071305746573742a"},
112 {rawContentsStruct{nil, 64}, "3003020140"},
113 {rawContentsStruct{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"},
114 {RawValue{Tag: 1, Class: 2, IsCompound: false, Bytes: []byte{1, 2, 3}}, "8103010203"},
115 {testSET([]int{10}), "310302010a"},
118 func TestMarshal(t *testing.T) {
119 for i, test := range marshalTests {
120 data, err := Marshal(test.in)
122 t.Errorf("#%d failed: %s", i, err)
124 out, _ := hex.DecodeString(test.out)
125 if bytes.Compare(out, data) != 0 {
126 t.Errorf("#%d got: %x want %x", i, data, out)