github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/encoding/asn1/marshal_test.go (about) 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 "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 omitEmptyTest struct { 58 A []string `asn1:"omitempty"` 59 } 60 61 type defaultTest struct { 62 A int `asn1:"optional,default:1"` 63 } 64 65 type testSET []int 66 67 var PST = time.FixedZone("PST", -8*60*60) 68 69 type marshalTest struct { 70 in interface{} 71 out string // hex encoded 72 } 73 74 func farFuture() time.Time { 75 t, err := time.Parse(time.RFC3339, "2100-04-05T12:01:01Z") 76 if err != nil { 77 panic(err) 78 } 79 return t 80 } 81 82 var marshalTests = []marshalTest{ 83 {10, "02010a"}, 84 {127, "02017f"}, 85 {128, "02020080"}, 86 {-128, "020180"}, 87 {-129, "0202ff7f"}, 88 {intStruct{64}, "3003020140"}, 89 {bigIntStruct{big.NewInt(0x123456)}, "30050203123456"}, 90 {twoIntStruct{64, 65}, "3006020140020141"}, 91 {nestedStruct{intStruct{127}}, "3005300302017f"}, 92 {[]byte{1, 2, 3}, "0403010203"}, 93 {implicitTagTest{64}, "3003850140"}, 94 {explicitTagTest{64}, "3005a503020140"}, 95 {time.Unix(0, 0).UTC(), "170d3730303130313030303030305a"}, 96 {time.Unix(1258325776, 0).UTC(), "170d3039313131353232353631365a"}, 97 {time.Unix(1258325776, 0).In(PST), "17113039313131353134353631362d30383030"}, 98 {farFuture(), "180f32313030303430353132303130315a"}, 99 {BitString{[]byte{0x80}, 1}, "03020780"}, 100 {BitString{[]byte{0x81, 0xf0}, 12}, "03030481f0"}, 101 {ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"}, 102 {ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"}, 103 {ObjectIdentifier([]int{2, 100, 3}), "0603813403"}, 104 {"test", "130474657374"}, 105 { 106 "" + 107 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 108 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 109 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 110 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 127 times 'x' 111 "137f" + 112 "7878787878787878787878787878787878787878787878787878787878787878" + 113 "7878787878787878787878787878787878787878787878787878787878787878" + 114 "7878787878787878787878787878787878787878787878787878787878787878" + 115 "78787878787878787878787878787878787878787878787878787878787878", 116 }, 117 { 118 "" + 119 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 120 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 121 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + 122 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 128 times 'x' 123 "138180" + 124 "7878787878787878787878787878787878787878787878787878787878787878" + 125 "7878787878787878787878787878787878787878787878787878787878787878" + 126 "7878787878787878787878787878787878787878787878787878787878787878" + 127 "7878787878787878787878787878787878787878787878787878787878787878", 128 }, 129 {ia5StringTest{"test"}, "3006160474657374"}, 130 {optionalRawValueTest{}, "3000"}, 131 {printableStringTest{"test"}, "3006130474657374"}, 132 {printableStringTest{"test*"}, "30071305746573742a"}, 133 {rawContentsStruct{nil, 64}, "3003020140"}, 134 {rawContentsStruct{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"}, 135 {RawValue{Tag: 1, Class: 2, IsCompound: false, Bytes: []byte{1, 2, 3}}, "8103010203"}, 136 {testSET([]int{10}), "310302010a"}, 137 {omitEmptyTest{[]string{}}, "3000"}, 138 {omitEmptyTest{[]string{"1"}}, "30053003130131"}, 139 {"Σ", "0c02cea3"}, 140 {defaultTest{0}, "3003020100"}, 141 {defaultTest{1}, "3000"}, 142 {defaultTest{2}, "3003020102"}, 143 } 144 145 func TestMarshal(t *testing.T) { 146 for i, test := range marshalTests { 147 data, err := Marshal(test.in) 148 if err != nil { 149 t.Errorf("#%d failed: %s", i, err) 150 } 151 out, _ := hex.DecodeString(test.out) 152 if !bytes.Equal(out, data) { 153 t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out) 154 155 } 156 } 157 } 158 159 func TestInvalidUTF8(t *testing.T) { 160 _, err := Marshal(string([]byte{0xff, 0xff})) 161 if err == nil { 162 t.Errorf("invalid UTF8 string was accepted") 163 } 164 }