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