OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [encoding/] [asn1/] [marshal_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "math/big"
11
        "testing"
12
        "time"
13
)
14
 
15
type intStruct struct {
16
        A int
17
}
18
 
19
type twoIntStruct struct {
20
        A int
21
        B int
22
}
23
 
24
type bigIntStruct struct {
25
        A *big.Int
26
}
27
 
28
type nestedStruct struct {
29
        A intStruct
30
}
31
 
32
type rawContentsStruct struct {
33
        Raw RawContent
34
        A   int
35
}
36
 
37
type implicitTagTest struct {
38
        A int `asn1:"implicit,tag:5"`
39
}
40
 
41
type explicitTagTest struct {
42
        A int `asn1:"explicit,tag:5"`
43
}
44
 
45
type ia5StringTest struct {
46
        A string `asn1:"ia5"`
47
}
48
 
49
type printableStringTest struct {
50
        A string `asn1:"printable"`
51
}
52
 
53
type optionalRawValueTest struct {
54
        A RawValue `asn1:"optional"`
55
}
56
 
57
type testSET []int
58
 
59
var PST = time.FixedZone("PST", -8*60*60)
60
 
61
type marshalTest struct {
62
        in  interface{}
63
        out string // hex encoded
64
}
65
 
66
var marshalTests = []marshalTest{
67
        {10, "02010a"},
68
        {127, "02017f"},
69
        {128, "02020080"},
70
        {-128, "020180"},
71
        {-129, "0202ff7f"},
72
        {intStruct{64}, "3003020140"},
73
        {bigIntStruct{big.NewInt(0x123456)}, "30050203123456"},
74
        {twoIntStruct{64, 65}, "3006020140020141"},
75
        {nestedStruct{intStruct{127}}, "3005300302017f"},
76
        {[]byte{1, 2, 3}, "0403010203"},
77
        {implicitTagTest{64}, "3003850140"},
78
        {explicitTagTest{64}, "3005a503020140"},
79
        {time.Unix(0, 0).UTC(), "170d3730303130313030303030305a"},
80
        {time.Unix(1258325776, 0).UTC(), "170d3039313131353232353631365a"},
81
        {time.Unix(1258325776, 0).In(PST), "17113039313131353232353631362d30383030"},
82
        {BitString{[]byte{0x80}, 1}, "03020780"},
83
        {BitString{[]byte{0x81, 0xf0}, 12}, "03030481f0"},
84
        {ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"},
85
        {ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"},
86
        {"test", "130474657374"},
87
        {
88
                "" +
89
                        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
90
                        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
91
                        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
92
                        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 127 times 'x'
93
                "137f" +
94
                        "7878787878787878787878787878787878787878787878787878787878787878" +
95
                        "7878787878787878787878787878787878787878787878787878787878787878" +
96
                        "7878787878787878787878787878787878787878787878787878787878787878" +
97
                        "78787878787878787878787878787878787878787878787878787878787878",
98
        },
99
        {
100
                "" +
101
                        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
102
                        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
103
                        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
104
                        "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 128 times 'x'
105
                "138180" +
106
                        "7878787878787878787878787878787878787878787878787878787878787878" +
107
                        "7878787878787878787878787878787878787878787878787878787878787878" +
108
                        "7878787878787878787878787878787878787878787878787878787878787878" +
109
                        "7878787878787878787878787878787878787878787878787878787878787878",
110
        },
111
        {ia5StringTest{"test"}, "3006160474657374"},
112
        {optionalRawValueTest{}, "3000"},
113
        {printableStringTest{"test"}, "3006130474657374"},
114
        {printableStringTest{"test*"}, "30071305746573742a"},
115
        {rawContentsStruct{nil, 64}, "3003020140"},
116
        {rawContentsStruct{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"},
117
        {RawValue{Tag: 1, Class: 2, IsCompound: false, Bytes: []byte{1, 2, 3}}, "8103010203"},
118
        {testSET([]int{10}), "310302010a"},
119
}
120
 
121
func TestMarshal(t *testing.T) {
122
        for i, test := range marshalTests {
123
                data, err := Marshal(test.in)
124
                if err != nil {
125
                        t.Errorf("#%d failed: %s", i, err)
126
                }
127
                out, _ := hex.DecodeString(test.out)
128
                if bytes.Compare(out, data) != 0 {
129
                        t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out)
130
 
131
                }
132
        }
133
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.