marshal_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. package asn1
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "math/big"
  9. "testing"
  10. "time"
  11. )
  12. type intStruct struct {
  13. A int
  14. }
  15. type twoIntStruct struct {
  16. A int
  17. B int
  18. }
  19. type bigIntStruct struct {
  20. A *big.Int
  21. }
  22. type nestedStruct struct {
  23. A intStruct
  24. }
  25. type rawContentsStruct struct {
  26. Raw RawContent
  27. A int
  28. }
  29. type implicitTagTest struct {
  30. A int `asn1:"implicit,tag:5"`
  31. }
  32. type explicitTagTest struct {
  33. A int `asn1:"explicit,tag:5"`
  34. }
  35. type flagTest struct {
  36. A Flag `asn1:"tag:0,optional"`
  37. }
  38. type generalizedTimeTest struct {
  39. A time.Time `asn1:"generalized"`
  40. }
  41. type ia5StringTest struct {
  42. A string `asn1:"ia5"`
  43. }
  44. type printableStringTest struct {
  45. A string `asn1:"printable"`
  46. }
  47. type optionalRawValueTest struct {
  48. A RawValue `asn1:"optional"`
  49. }
  50. type omitEmptyTest struct {
  51. A []string `asn1:"omitempty"`
  52. }
  53. type defaultTest struct {
  54. A int `asn1:"optional,default:1"`
  55. }
  56. type testSET []int
  57. var PST = time.FixedZone("PST", -8*60*60)
  58. type marshalTest struct {
  59. in interface{}
  60. out string // hex encoded
  61. }
  62. func farFuture() time.Time {
  63. t, err := time.Parse(time.RFC3339, "2100-04-05T12:01:01Z")
  64. if err != nil {
  65. panic(err)
  66. }
  67. return t
  68. }
  69. var marshalTests = []marshalTest{
  70. {10, "02010a"},
  71. {127, "02017f"},
  72. {128, "02020080"},
  73. {-128, "020180"},
  74. {-129, "0202ff7f"},
  75. {intStruct{64}, "3003020140"},
  76. {bigIntStruct{big.NewInt(0x123456)}, "30050203123456"},
  77. {twoIntStruct{64, 65}, "3006020140020141"},
  78. {nestedStruct{intStruct{127}}, "3005300302017f"},
  79. {[]byte{1, 2, 3}, "0403010203"},
  80. {implicitTagTest{64}, "3003850140"},
  81. {explicitTagTest{64}, "3005a503020140"},
  82. {flagTest{true}, "30028000"},
  83. {flagTest{false}, "3000"},
  84. {time.Unix(0, 0).UTC(), "170d3730303130313030303030305a"},
  85. {time.Unix(1258325776, 0).UTC(), "170d3039313131353232353631365a"},
  86. {time.Unix(1258325776, 0).In(PST), "17113039313131353134353631362d30383030"},
  87. {farFuture(), "180f32313030303430353132303130315a"},
  88. {generalizedTimeTest{time.Unix(1258325776, 0).UTC()}, "3011180f32303039313131353232353631365a"},
  89. {BitString{[]byte{0x80}, 1}, "03020780"},
  90. {BitString{[]byte{0x81, 0xf0}, 12}, "03030481f0"},
  91. {ObjectIdentifier([]int{1, 2, 3, 4}), "06032a0304"},
  92. {ObjectIdentifier([]int{1, 2, 840, 133549, 1, 1, 5}), "06092a864888932d010105"},
  93. {ObjectIdentifier([]int{2, 100, 3}), "0603813403"},
  94. {"test", "130474657374"},
  95. {
  96. "" +
  97. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
  98. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
  99. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
  100. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 127 times 'x'
  101. "137f" +
  102. "7878787878787878787878787878787878787878787878787878787878787878" +
  103. "7878787878787878787878787878787878787878787878787878787878787878" +
  104. "7878787878787878787878787878787878787878787878787878787878787878" +
  105. "78787878787878787878787878787878787878787878787878787878787878",
  106. },
  107. {
  108. "" +
  109. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
  110. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
  111. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +
  112. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // This is 128 times 'x'
  113. "138180" +
  114. "7878787878787878787878787878787878787878787878787878787878787878" +
  115. "7878787878787878787878787878787878787878787878787878787878787878" +
  116. "7878787878787878787878787878787878787878787878787878787878787878" +
  117. "7878787878787878787878787878787878787878787878787878787878787878",
  118. },
  119. {ia5StringTest{"test"}, "3006160474657374"},
  120. {optionalRawValueTest{}, "3000"},
  121. {printableStringTest{"test"}, "3006130474657374"},
  122. {printableStringTest{"test*"}, "30071305746573742a"},
  123. {rawContentsStruct{nil, 64}, "3003020140"},
  124. {rawContentsStruct{[]byte{0x30, 3, 1, 2, 3}, 64}, "3003010203"},
  125. {RawValue{Tag: 1, Class: 2, IsCompound: false, Bytes: []byte{1, 2, 3}}, "8103010203"},
  126. {testSET([]int{10}), "310302010a"},
  127. {omitEmptyTest{[]string{}}, "3000"},
  128. {omitEmptyTest{[]string{"1"}}, "30053003130131"},
  129. {"Σ", "0c02cea3"},
  130. {defaultTest{0}, "3003020100"},
  131. {defaultTest{1}, "3000"},
  132. {defaultTest{2}, "3003020102"},
  133. }
  134. func TestMarshal(t *testing.T) {
  135. for i, test := range marshalTests {
  136. data, err := Marshal(test.in)
  137. if err != nil {
  138. t.Errorf("#%d failed: %s", i, err)
  139. }
  140. out, _ := hex.DecodeString(test.out)
  141. if !bytes.Equal(out, data) {
  142. t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out)
  143. }
  144. }
  145. }
  146. func TestInvalidUTF8(t *testing.T) {
  147. _, err := Marshal(string([]byte{0xff, 0xff}))
  148. if err == nil {
  149. t.Errorf("invalid UTF8 string was accepted")
  150. }
  151. }