github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/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  	"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 testSET []int
    84  
    85  var PST = time.FixedZone("PST", -8*60*60)
    86  
    87  type marshalTest struct {
    88  	in  interface{}
    89  	out string // hex encoded
    90  }
    91  
    92  func farFuture() time.Time {
    93  	t, err := time.Parse(time.RFC3339, "2100-04-05T12:01:01Z")
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  	return t
    98  }
    99  
   100  var marshalTests = []marshalTest{
   101  	{10, "02010a"},
   102  	{127, "02017f"},
   103  	{128, "02020080"},
   104  	{-128, "020180"},
   105  	{-129, "0202ff7f"},
   106  	{intStruct{64}, "3003020140"},
   107  	{bigIntStruct{big.NewInt(0x123456)}, "30050203123456"},
   108  	{twoIntStruct{64, 65}, "3006020140020141"},
   109  	{nestedStruct{intStruct{127}}, "3005300302017f"},
   110  	{[]byte{1, 2, 3}, "0403010203"},
   111  	{implicitTagTest{64}, "3003850140"},
   112  	{explicitTagTest{64}, "3005a503020140"},
   113  	{flagTest{true}, "30028000"},
   114  	{flagTest{false}, "3000"},
   115  	{time.Unix(0, 0).UTC(), "170d3730303130313030303030305a"},
   116  	{time.Unix(1258325776, 0).UTC(), "170d3039313131353232353631365a"},
   117  	{time.Unix(1258325776, 0).In(PST), "17113039313131353134353631362d30383030"},
   118  	{farFuture(), "180f32313030303430353132303130315a"},
   119  	{generalizedTimeTest{time.Unix(1258325776, 0).UTC()}, "3011180f32303039313131353232353631365a"},
   120  	{BitString{[]byte{0x80}, 1}, "03020780"},
   121  	{BitString{[]byte{0x81, 0xf0}, 12}, "03030481f0"},
   122  	{ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"},
   123  	{ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"},
   124  	{ObjectIdentifier([]int{2, 100, 3}), "0603813403"},
   125  	{"test", "130474657374"},
   126  	{
   127  		"" +
   128  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   129  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   130  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   131  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 127 times 'x'
   132  		"137f" +
   133  			"7878787878787878787878787878787878787878787878787878787878787878" +
   134  			"7878787878787878787878787878787878787878787878787878787878787878" +
   135  			"7878787878787878787878787878787878787878787878787878787878787878" +
   136  			"78787878787878787878787878787878787878787878787878787878787878",
   137  	},
   138  	{
   139  		"" +
   140  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   141  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   142  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
   143  			"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 128 times 'x'
   144  		"138180" +
   145  			"7878787878787878787878787878787878787878787878787878787878787878" +
   146  			"7878787878787878787878787878787878787878787878787878787878787878" +
   147  			"7878787878787878787878787878787878787878787878787878787878787878" +
   148  			"7878787878787878787878787878787878787878787878787878787878787878",
   149  	},
   150  	{ia5StringTest{"test"}, "3006160474657374"},
   151  	{optionalRawValueTest{}, "3000"},
   152  	{printableStringTest{"test"}, "3006130474657374"},
   153  	{printableStringTest{"test*"}, "30071305746573742a"},
   154  	{genericStringTest{"test"}, "3006130474657374"},
   155  	{genericStringTest{"test*"}, "30070c05746573742a"},
   156  	{rawContentsStruct{nil, 64}, "3003020140"},
   157  	{rawContentsStruct{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"},
   158  	{RawValue{Tag: 1, Class: 2, IsCompound: false, Bytes: []byte{1, 2, 3}}, "8103010203"},
   159  	{testSET([]int{10}), "310302010a"},
   160  	{omitEmptyTest{[]string{}}, "3000"},
   161  	{omitEmptyTest{[]string{"1"}}, "30053003130131"},
   162  	{"Σ", "0c02cea3"},
   163  	{defaultTest{0}, "3003020100"},
   164  	{defaultTest{1}, "3000"},
   165  	{defaultTest{2}, "3003020102"},
   166  	{applicationTest{1, 2}, "30084001016103020102"},
   167  }
   168  
   169  func TestMarshal(t *testing.T) {
   170  	for i, test := range marshalTests {
   171  		data, err := Marshal(test.in)
   172  		if err != nil {
   173  			t.Errorf("#%d failed: %s", i, err)
   174  		}
   175  		out, _ := hex.DecodeString(test.out)
   176  		if !bytes.Equal(out, data) {
   177  			t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out)
   178  
   179  		}
   180  	}
   181  }
   182  
   183  type marshalErrTest struct {
   184  	in  interface{}
   185  	err string
   186  }
   187  
   188  var marshalErrTests = []marshalErrTest{
   189  	{bigIntStruct{nil}, "empty integer"},
   190  }
   191  
   192  func TestMarshalError(t *testing.T) {
   193  	for i, test := range marshalErrTests {
   194  		_, err := Marshal(test.in)
   195  		if err == nil {
   196  			t.Errorf("#%d should fail, but success", i)
   197  			continue
   198  		}
   199  
   200  		if !strings.Contains(err.Error(), test.err) {
   201  			t.Errorf("#%d got: %v want %v", i, err, test.err)
   202  		}
   203  	}
   204  }
   205  
   206  func TestInvalidUTF8(t *testing.T) {
   207  	_, err := Marshal(string([]byte{0xff, 0xff}))
   208  	if err == nil {
   209  		t.Errorf("invalid UTF8 string was accepted")
   210  	}
   211  }
   212  
   213  func BenchmarkMarshal(b *testing.B) {
   214  	b.ReportAllocs()
   215  
   216  	for i := 0; i < b.N; i++ {
   217  		for _, test := range marshalTests {
   218  			Marshal(test.in)
   219  		}
   220  	}
   221  }