]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/asn1/marshal_test.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / asn1 / marshal_test.go
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.
4
5 package asn1
6
7 import (
8 "bytes"
9 "encoding/hex"
10 "testing"
11 "time"
12 )
13
14 type intStruct struct {
15 A int
16 }
17
18 type twoIntStruct struct {
19 A int
20 B int
21 }
22
23 type nestedStruct struct {
24 A intStruct
25 }
26
27 type rawContentsStruct struct {
28 Raw RawContent
29 A int
30 }
31
32 type implicitTagTest struct {
33 A int "implicit,tag:5"
34 }
35
36 type explicitTagTest struct {
37 A int "explicit,tag:5"
38 }
39
40 type ia5StringTest struct {
41 A string "ia5"
42 }
43
44 type printableStringTest struct {
45 A string "printable"
46 }
47
48 type testSET []int
49
50 func setPST(t *time.Time) *time.Time {
51 t.ZoneOffset = -28800
52 return t
53 }
54
55 type marshalTest struct {
56 in interface{}
57 out string // hex encoded
58 }
59
60 var marshalTests = []marshalTest{
61 {10, "02010a"},
62 {127, "02017f"},
63 {128, "02020080"},
64 {-128, "020180"},
65 {-129, "0202ff7f"},
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"},
87 }
88
89 func TestMarshal(t *testing.T) {
90 for i, test := range marshalTests {
91 data, err := Marshal(test.in)
92 if err != nil {
93 t.Errorf("#%d failed: %s", i, err)
94 }
95 out, _ := hex.DecodeString(test.out)
96 if bytes.Compare(out, data) != 0 {
97 t.Errorf("#%d got: %x want %x", i, data, out)
98 }
99 }
100 }