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