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 "implicit,tag:5"
36 type explicitTagTest struct {
37 A int "explicit,tag:5"
40 type ia5StringTest struct {
44 type printableStringTest struct {
50 func setPST(t *time.Time) *time.Time {
55 type marshalTest struct {
57 out string // hex encoded
60 var marshalTests = []marshalTest{
66 {intStruct{64}, "3003020140"},
67 {twoIntStruct{64, 65}, "3006020140020141"},
68 {nestedStruct{intStruct{127}}, "3005300302017f"},
69 {[]byte{1, 2, 3}, "0403010203"},
70 {implicitTagTest{64}, "3003850140"},
71 {explicitTagTest{64}, "3005a503020140"},
72 {time.SecondsToUTC(0), "170d3730303130313030303030305a"},
73 {time.SecondsToUTC(1258325776), "170d3039313131353232353631365a"},
74 {setPST(time.SecondsToUTC(1258325776)), "17113039313131353232353631362d30383030"},
75 {BitString{[]byte{0x80}, 1}, "03020780"},
76 {BitString{[]byte{0x81, 0xf0}, 12}, "03030481f0"},
77 {ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"},
78 {ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"},
79 {"test", "130474657374"},
80 {ia5StringTest{"test"}, "3006160474657374"},
81 {printableStringTest{"test"}, "3006130474657374"},
82 {printableStringTest{"test*"}, "30071305746573742a"},
83 {rawContentsStruct{nil, 64}, "3003020140"},
84 {rawContentsStruct{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"},
85 {RawValue{Tag: 1, Class: 2, IsCompound: false, Bytes: []byte{1, 2, 3}}, "8103010203"},
86 {testSET([]int{10}), "310302010a"},
89 func TestMarshal(t *testing.T) {
90 for i, test := range marshalTests {
91 data, err := Marshal(test.in)
93 t.Errorf("#%d failed: %s", i, err)
95 out, _ := hex.DecodeString(test.out)
96 if bytes.Compare(out, data) != 0 {
97 t.Errorf("#%d got: %x want %x", i, data, out)