...
Run Format

Source file src/encoding/asn1/marshal_test.go

Documentation: encoding/asn1

     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  	"strings"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  type intStruct struct {
    17  	A int
    18  }
    19  
    20  type twoIntStruct struct {
    21  	A int
    22  	B int
    23  }
    24  
    25  type bigIntStruct struct {
    26  	A *big.Int
    27  }
    28  
    29  type nestedStruct struct {
    30  	A intStruct
    31  }
    32  
    33  type rawContentsStruct struct {
    34  	Raw RawContent
    35  	A   int
    36  }
    37  
    38  type implicitTagTest struct {
    39  	A int `asn1:"implicit,tag:5"`
    40  }
    41  
    42  type explicitTagTest struct {
    43  	A int `asn1:"explicit,tag:5"`
    44  }
    45  
    46  type flagTest struct {
    47  	A Flag `asn1:"tag:0,optional"`
    48  }
    49  
    50  type generalizedTimeTest struct {
    51  	A time.Time `asn1:"generalized"`
    52  }
    53  
    54  type ia5StringTest struct {
    55  	A string `asn1:"ia5"`
    56  }
    57  
    58  type printableStringTest struct {
    59  	A string `asn1:"printable"`
    60  }
    61  
    62  type genericStringTest struct {
    63  	A string
    64  }
    65  
    66  type optionalRawValueTest struct {
    67  	A RawValue `asn1:"optional"`
    68  }
    69  
    70  type omitEmptyTest struct {
    71  	A []string `asn1:"omitempty"`
    72  }
    73  
    74  type defaultTest struct {
    75  	A int `asn1:"optional,default:1"`
    76  }
    77  
    78  type applicationTest struct {
    79  	A int `asn1:"application,tag:0"`
    80  	B int `asn1:"application,tag:1,explicit"`
    81  }
    82  
    83  type numericStringTest struct {
    84  	A string `asn1:"numeric"`
    85  }
    86  
    87  type testSET []int
    88  
    89  var PST = time.FixedZone("PST", -8*60*60)
    90  
    91  type marshalTest struct {
    92  	in  interface{}
    93  	out string // hex encoded
    94  }
    95  
    96  func farFuture() time.Time {
    97  	t, err := time.Parse(time.RFC3339, "2100-04-05T12:01:01Z")
    98  	if err != nil {
    99  		panic(err)
   100  	}
   101  	return t
   102  }
   103  
   104  var marshalTests = []marshalTest{
   105  	{10, "02010a"},
   106  	{127, "02017f"},
   107  	{128, "02020080"},
   108  	{-128, "020180"},
   109  	{-129, "0202ff7f"},
   110  	{intStruct{64}, "3003020140"},
   111  	{bigIntStruct{big.NewInt(0x123456)}, "30050203123456"},
   112  	{twoIntStruct{64, 65}, "3006020140020141"},
   113  	{nestedStruct{intStruct{127}}, "3005300302017f"},
   114  	{[]byte{1, 2, 3}, "0403010203"},
   115  	{implicitTagTest{64}, "3003850140"},
   116  	{explicitTagTest{64}, "3005a503020140"},
   117  	{flagTest{true}, "30028000"},
   118  	{flagTest{false}, "3000"},
   119  	{time.Unix(0, 0).UTC(), "170d3730303130313030303030305a"},
   120  	{time.Unix(1258325776, 0).UTC(), "170d3039313131353232353631365a"},
   121  	{time.Unix(1258325776, 0).In(PST), "17113039313131353134353631362d30383030"},
   122  	{farFuture(), "180f32313030303430353132303130315a"},
   123  	{generalizedTimeTest{time.Unix(1258325776, 0).UTC()}, "3011180f32303039313131353232353631365a"},
   124  	{BitString{[]byte{0x80}, 1}, "03020780"},
   125  	{BitString{[]byte{0x81, 0xf0}, 12}, "03030481f0"},
   126  	{ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"},
   127  	{ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"},
   128  	{ObjectIdentifier([]int{2, 100, 3}), "0603813403"},
   129  	{"test", "130474657374"},
   130  	{
   131  		"" +
   132  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   133  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   134  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   135  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 127 times 'x'
   136  		"137f" +
   137  			"7878787878787878787878787878787878787878787878787878787878787878" +
   138  			"7878787878787878787878787878787878787878787878787878787878787878" +
   139  			"7878787878787878787878787878787878787878787878787878787878787878" +
   140  			"78787878787878787878787878787878787878787878787878787878787878",
   141  	},
   142  	{
   143  		"" +
   144  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   145  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   146  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   147  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 128 times 'x'
   148  		"138180" +
   149  			"7878787878787878787878787878787878787878787878787878787878787878" +
   150  			"7878787878787878787878787878787878787878787878787878787878787878" +
   151  			"7878787878787878787878787878787878787878787878787878787878787878" +
   152  			"7878787878787878787878787878787878787878787878787878787878787878",
   153  	},
   154  	{ia5StringTest{"test"}, "3006160474657374"},
   155  	{optionalRawValueTest{}, "3000"},
   156  	{printableStringTest{"test"}, "3006130474657374"},
   157  	{printableStringTest{"test*"}, "30071305746573742a"},
   158  	{genericStringTest{"test"}, "3006130474657374"},
   159  	{genericStringTest{"test*"}, "30070c05746573742a"},
   160  	{genericStringTest{"test&"}, "30070c057465737426"},
   161  	{rawContentsStruct{nil, 64}, "3003020140"},
   162  	{rawContentsStruct{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"},
   163  	{RawValue{Tag: 1, Class: 2, IsCompound: false, Bytes: []byte{1, 2, 3}}, "8103010203"},
   164  	{testSET([]int{10}), "310302010a"},
   165  	{omitEmptyTest{[]string{}}, "3000"},
   166  	{omitEmptyTest{[]string{"1"}}, "30053003130131"},
   167  	{"Σ", "0c02cea3"},
   168  	{defaultTest{0}, "3003020100"},
   169  	{defaultTest{1}, "3000"},
   170  	{defaultTest{2}, "3003020102"},
   171  	{applicationTest{1, 2}, "30084001016103020102"},
   172  	{numericStringTest{"1 9"}, "30051203312039"},
   173  }
   174  
   175  func TestMarshal(t *testing.T) {
   176  	for i, test := range marshalTests {
   177  		data, err := Marshal(test.in)
   178  		if err != nil {
   179  			t.Errorf("#%d failed: %s", i, err)
   180  		}
   181  		out, _ := hex.DecodeString(test.out)
   182  		if !bytes.Equal(out, data) {
   183  			t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out)
   184  
   185  		}
   186  	}
   187  }
   188  
   189  type marshalWithParamsTest struct {
   190  	in     interface{}
   191  	params string
   192  	out    string // hex encoded
   193  }
   194  
   195  var marshalWithParamsTests = []marshalWithParamsTest{
   196  	{intStruct{10}, "set", "310302010a"},
   197  	{intStruct{10}, "application", "600302010a"},
   198  }
   199  
   200  func TestMarshalWithParams(t *testing.T) {
   201  	for i, test := range marshalWithParamsTests {
   202  		data, err := MarshalWithParams(test.in, test.params)
   203  		if err != nil {
   204  			t.Errorf("#%d failed: %s", i, err)
   205  		}
   206  		out, _ := hex.DecodeString(test.out)
   207  		if !bytes.Equal(out, data) {
   208  			t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out)
   209  
   210  		}
   211  	}
   212  }
   213  
   214  type marshalErrTest struct {
   215  	in  interface{}
   216  	err string
   217  }
   218  
   219  var marshalErrTests = []marshalErrTest{
   220  	{bigIntStruct{nil}, "empty integer"},
   221  	{numericStringTest{"a"}, "invalid character"},
   222  	{ia5StringTest{"\xb0"}, "invalid character"},
   223  	{printableStringTest{"!"}, "invalid character"},
   224  }
   225  
   226  func TestMarshalError(t *testing.T) {
   227  	for i, test := range marshalErrTests {
   228  		_, err := Marshal(test.in)
   229  		if err == nil {
   230  			t.Errorf("#%d should fail, but success", i)
   231  			continue
   232  		}
   233  
   234  		if !strings.Contains(err.Error(), test.err) {
   235  			t.Errorf("#%d got: %v want %v", i, err, test.err)
   236  		}
   237  	}
   238  }
   239  
   240  func TestInvalidUTF8(t *testing.T) {
   241  	_, err := Marshal(string([]byte{0xff, 0xff}))
   242  	if err == nil {
   243  		t.Errorf("invalid UTF8 string was accepted")
   244  	}
   245  }
   246  
   247  func BenchmarkMarshal(b *testing.B) {
   248  	b.ReportAllocs()
   249  
   250  	for i := 0; i < b.N; i++ {
   251  		for _, test := range marshalTests {
   252  			Marshal(test.in)
   253  		}
   254  	}
   255  }
   256  

View as plain text