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