OSDN Git Service

03df5f1e1d52b86fd24eb7ebd45a57f64b57ae2c
[pf3gnuchains/gcc-fork.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 `asn1:"implicit,tag:5"`
34 }
35
36 type explicitTagTest struct {
37         A int `asn1:"explicit,tag:5"`
38 }
39
40 type ia5StringTest struct {
41         A string `asn1:"ia5"`
42 }
43
44 type printableStringTest struct {
45         A string `asn1:"printable"`
46 }
47
48 type optionalRawValueTest struct {
49         A RawValue `asn1:"optional"`
50 }
51
52 type testSET []int
53
54 func setPST(t *time.Time) *time.Time {
55         t.ZoneOffset = -28800
56         return t
57 }
58
59 type marshalTest struct {
60         in  interface{}
61         out string // hex encoded
62 }
63
64 var marshalTests = []marshalTest{
65         {10, "02010a"},
66         {127, "02017f"},
67         {128, "02020080"},
68         {-128, "020180"},
69         {-129, "0202ff7f"},
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"},
84         {
85                 "" +
86                         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
87                         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
88                         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
89                         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 127 times 'x'
90                 "137f" +
91                         "7878787878787878787878787878787878787878787878787878787878787878" +
92                         "7878787878787878787878787878787878787878787878787878787878787878" +
93                         "7878787878787878787878787878787878787878787878787878787878787878" +
94                         "78787878787878787878787878787878787878787878787878787878787878",
95         },
96         {
97                 "" +
98                         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
99                         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
100                         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
101                         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 128 times 'x'
102                 "138180" +
103                         "7878787878787878787878787878787878787878787878787878787878787878" +
104                         "7878787878787878787878787878787878787878787878787878787878787878" +
105                         "7878787878787878787878787878787878787878787878787878787878787878" +
106                         "7878787878787878787878787878787878787878787878787878787878787878",
107         },
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"},
116 }
117
118 func TestMarshal(t *testing.T) {
119         for i, test := range marshalTests {
120                 data, err := Marshal(test.in)
121                 if err != nil {
122                         t.Errorf("#%d failed: %s", i, err)
123                 }
124                 out, _ := hex.DecodeString(test.out)
125                 if bytes.Compare(out, data) != 0 {
126                         t.Errorf("#%d got: %x want %x", i, data, out)
127                 }
128         }
129 }