cryptobyte_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // Copyright 2017 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 cryptobyte
  5. import (
  6. "bytes"
  7. "fmt"
  8. "testing"
  9. )
  10. func builderBytesEq(b *Builder, want ...byte) error {
  11. got := b.BytesOrPanic()
  12. if !bytes.Equal(got, want) {
  13. return fmt.Errorf("Bytes() = %v, want %v", got, want)
  14. }
  15. return nil
  16. }
  17. func TestBytes(t *testing.T) {
  18. var b Builder
  19. v := []byte("foobarbaz")
  20. b.AddBytes(v[0:3])
  21. b.AddBytes(v[3:4])
  22. b.AddBytes(v[4:9])
  23. if err := builderBytesEq(&b, v...); err != nil {
  24. t.Error(err)
  25. }
  26. s := String(b.BytesOrPanic())
  27. for _, w := range []string{"foo", "bar", "baz"} {
  28. var got []byte
  29. if !s.ReadBytes(&got, 3) {
  30. t.Errorf("ReadBytes() = false, want true (w = %v)", w)
  31. }
  32. want := []byte(w)
  33. if !bytes.Equal(got, want) {
  34. t.Errorf("ReadBytes(): got = %v, want %v", got, want)
  35. }
  36. }
  37. if len(s) != 0 {
  38. t.Errorf("len(s) = %d, want 0", len(s))
  39. }
  40. }
  41. func TestUint8(t *testing.T) {
  42. var b Builder
  43. b.AddUint8(42)
  44. if err := builderBytesEq(&b, 42); err != nil {
  45. t.Error(err)
  46. }
  47. var s String = b.BytesOrPanic()
  48. var v uint8
  49. if !s.ReadUint8(&v) {
  50. t.Error("ReadUint8() = false, want true")
  51. }
  52. if v != 42 {
  53. t.Errorf("v = %d, want 42", v)
  54. }
  55. if len(s) != 0 {
  56. t.Errorf("len(s) = %d, want 0", len(s))
  57. }
  58. }
  59. func TestUint16(t *testing.T) {
  60. var b Builder
  61. b.AddUint16(65534)
  62. if err := builderBytesEq(&b, 255, 254); err != nil {
  63. t.Error(err)
  64. }
  65. var s String = b.BytesOrPanic()
  66. var v uint16
  67. if !s.ReadUint16(&v) {
  68. t.Error("ReadUint16() == false, want true")
  69. }
  70. if v != 65534 {
  71. t.Errorf("v = %d, want 65534", v)
  72. }
  73. if len(s) != 0 {
  74. t.Errorf("len(s) = %d, want 0", len(s))
  75. }
  76. }
  77. func TestUint24(t *testing.T) {
  78. var b Builder
  79. b.AddUint24(0xfffefd)
  80. if err := builderBytesEq(&b, 255, 254, 253); err != nil {
  81. t.Error(err)
  82. }
  83. var s String = b.BytesOrPanic()
  84. var v uint32
  85. if !s.ReadUint24(&v) {
  86. t.Error("ReadUint8() = false, want true")
  87. }
  88. if v != 0xfffefd {
  89. t.Errorf("v = %d, want fffefd", v)
  90. }
  91. if len(s) != 0 {
  92. t.Errorf("len(s) = %d, want 0", len(s))
  93. }
  94. }
  95. func TestUint24Truncation(t *testing.T) {
  96. var b Builder
  97. b.AddUint24(0x10111213)
  98. if err := builderBytesEq(&b, 0x11, 0x12, 0x13); err != nil {
  99. t.Error(err)
  100. }
  101. }
  102. func TestUint32(t *testing.T) {
  103. var b Builder
  104. b.AddUint32(0xfffefdfc)
  105. if err := builderBytesEq(&b, 255, 254, 253, 252); err != nil {
  106. t.Error(err)
  107. }
  108. var s String = b.BytesOrPanic()
  109. var v uint32
  110. if !s.ReadUint32(&v) {
  111. t.Error("ReadUint8() = false, want true")
  112. }
  113. if v != 0xfffefdfc {
  114. t.Errorf("v = %x, want fffefdfc", v)
  115. }
  116. if len(s) != 0 {
  117. t.Errorf("len(s) = %d, want 0", len(s))
  118. }
  119. }
  120. func TestUMultiple(t *testing.T) {
  121. var b Builder
  122. b.AddUint8(23)
  123. b.AddUint32(0xfffefdfc)
  124. b.AddUint16(42)
  125. if err := builderBytesEq(&b, 23, 255, 254, 253, 252, 0, 42); err != nil {
  126. t.Error(err)
  127. }
  128. var s String = b.BytesOrPanic()
  129. var (
  130. x uint8
  131. y uint32
  132. z uint16
  133. )
  134. if !s.ReadUint8(&x) || !s.ReadUint32(&y) || !s.ReadUint16(&z) {
  135. t.Error("ReadUint8() = false, want true")
  136. }
  137. if x != 23 || y != 0xfffefdfc || z != 42 {
  138. t.Errorf("x, y, z = %d, %d, %d; want 23, 4294901244, 5", x, y, z)
  139. }
  140. if len(s) != 0 {
  141. t.Errorf("len(s) = %d, want 0", len(s))
  142. }
  143. }
  144. func TestUint8LengthPrefixedSimple(t *testing.T) {
  145. var b Builder
  146. b.AddUint8LengthPrefixed(func(c *Builder) {
  147. c.AddUint8(23)
  148. c.AddUint8(42)
  149. })
  150. if err := builderBytesEq(&b, 2, 23, 42); err != nil {
  151. t.Error(err)
  152. }
  153. var base, child String = b.BytesOrPanic(), nil
  154. var x, y uint8
  155. if !base.ReadUint8LengthPrefixed(&child) || !child.ReadUint8(&x) ||
  156. !child.ReadUint8(&y) {
  157. t.Error("parsing failed")
  158. }
  159. if x != 23 || y != 42 {
  160. t.Errorf("want x, y == 23, 42; got %d, %d", x, y)
  161. }
  162. if len(base) != 0 {
  163. t.Errorf("len(base) = %d, want 0", len(base))
  164. }
  165. if len(child) != 0 {
  166. t.Errorf("len(child) = %d, want 0", len(child))
  167. }
  168. }
  169. func TestUint8LengthPrefixedMulti(t *testing.T) {
  170. var b Builder
  171. b.AddUint8LengthPrefixed(func(c *Builder) {
  172. c.AddUint8(23)
  173. c.AddUint8(42)
  174. })
  175. b.AddUint8(5)
  176. b.AddUint8LengthPrefixed(func(c *Builder) {
  177. c.AddUint8(123)
  178. c.AddUint8(234)
  179. })
  180. if err := builderBytesEq(&b, 2, 23, 42, 5, 2, 123, 234); err != nil {
  181. t.Error(err)
  182. }
  183. var s, child String = b.BytesOrPanic(), nil
  184. var u, v, w, x, y uint8
  185. if !s.ReadUint8LengthPrefixed(&child) || !child.ReadUint8(&u) || !child.ReadUint8(&v) ||
  186. !s.ReadUint8(&w) || !s.ReadUint8LengthPrefixed(&child) || !child.ReadUint8(&x) || !child.ReadUint8(&y) {
  187. t.Error("parsing failed")
  188. }
  189. if u != 23 || v != 42 || w != 5 || x != 123 || y != 234 {
  190. t.Errorf("u, v, w, x, y = %d, %d, %d, %d, %d; want 23, 42, 5, 123, 234",
  191. u, v, w, x, y)
  192. }
  193. if len(s) != 0 {
  194. t.Errorf("len(s) = %d, want 0", len(s))
  195. }
  196. if len(child) != 0 {
  197. t.Errorf("len(child) = %d, want 0", len(child))
  198. }
  199. }
  200. func TestUint8LengthPrefixedNested(t *testing.T) {
  201. var b Builder
  202. b.AddUint8LengthPrefixed(func(c *Builder) {
  203. c.AddUint8(5)
  204. c.AddUint8LengthPrefixed(func(d *Builder) {
  205. d.AddUint8(23)
  206. d.AddUint8(42)
  207. })
  208. c.AddUint8(123)
  209. })
  210. if err := builderBytesEq(&b, 5, 5, 2, 23, 42, 123); err != nil {
  211. t.Error(err)
  212. }
  213. var base, child1, child2 String = b.BytesOrPanic(), nil, nil
  214. var u, v, w, x uint8
  215. if !base.ReadUint8LengthPrefixed(&child1) {
  216. t.Error("parsing base failed")
  217. }
  218. if !child1.ReadUint8(&u) || !child1.ReadUint8LengthPrefixed(&child2) || !child1.ReadUint8(&x) {
  219. t.Error("parsing child1 failed")
  220. }
  221. if !child2.ReadUint8(&v) || !child2.ReadUint8(&w) {
  222. t.Error("parsing child2 failed")
  223. }
  224. if u != 5 || v != 23 || w != 42 || x != 123 {
  225. t.Errorf("u, v, w, x = %d, %d, %d, %d, want 5, 23, 42, 123",
  226. u, v, w, x)
  227. }
  228. if len(base) != 0 {
  229. t.Errorf("len(base) = %d, want 0", len(base))
  230. }
  231. if len(child1) != 0 {
  232. t.Errorf("len(child1) = %d, want 0", len(child1))
  233. }
  234. if len(base) != 0 {
  235. t.Errorf("len(child2) = %d, want 0", len(child2))
  236. }
  237. }
  238. func TestPreallocatedBuffer(t *testing.T) {
  239. var buf [5]byte
  240. b := NewBuilder(buf[0:0])
  241. b.AddUint8(1)
  242. b.AddUint8LengthPrefixed(func(c *Builder) {
  243. c.AddUint8(3)
  244. c.AddUint8(4)
  245. })
  246. b.AddUint16(1286) // Outgrow buf by one byte.
  247. want := []byte{1, 2, 3, 4, 0}
  248. if !bytes.Equal(buf[:], want) {
  249. t.Errorf("buf = %v want %v", buf, want)
  250. }
  251. if err := builderBytesEq(b, 1, 2, 3, 4, 5, 6); err != nil {
  252. t.Error(err)
  253. }
  254. }
  255. func TestWriteWithPendingChild(t *testing.T) {
  256. var b Builder
  257. b.AddUint8LengthPrefixed(func(c *Builder) {
  258. c.AddUint8LengthPrefixed(func(d *Builder) {
  259. defer func() {
  260. if recover() == nil {
  261. t.Errorf("recover() = nil, want error; c.AddUint8() did not panic")
  262. }
  263. }()
  264. c.AddUint8(2) // panics
  265. defer func() {
  266. if recover() == nil {
  267. t.Errorf("recover() = nil, want error; b.AddUint8() did not panic")
  268. }
  269. }()
  270. b.AddUint8(2) // panics
  271. })
  272. defer func() {
  273. if recover() == nil {
  274. t.Errorf("recover() = nil, want error; b.AddUint8() did not panic")
  275. }
  276. }()
  277. b.AddUint8(2) // panics
  278. })
  279. }
  280. // ASN.1
  281. func TestASN1Int64(t *testing.T) {
  282. tests := []struct {
  283. in int64
  284. want []byte
  285. }{
  286. {-0x800000, []byte{2, 3, 128, 0, 0}},
  287. {-256, []byte{2, 2, 255, 0}},
  288. {-129, []byte{2, 2, 255, 127}},
  289. {-128, []byte{2, 1, 128}},
  290. {-1, []byte{2, 1, 255}},
  291. {0, []byte{2, 1, 0}},
  292. {1, []byte{2, 1, 1}},
  293. {2, []byte{2, 1, 2}},
  294. {127, []byte{2, 1, 127}},
  295. {128, []byte{2, 2, 0, 128}},
  296. {256, []byte{2, 2, 1, 0}},
  297. {0x800000, []byte{2, 4, 0, 128, 0, 0}},
  298. }
  299. for i, tt := range tests {
  300. var b Builder
  301. b.AddASN1Int64(tt.in)
  302. if err := builderBytesEq(&b, tt.want...); err != nil {
  303. t.Errorf("%v, (i = %d; in = %v)", err, i, tt.in)
  304. }
  305. var n int64
  306. s := String(b.BytesOrPanic())
  307. ok := s.ReadASN1Integer(&n)
  308. if !ok || n != tt.in {
  309. t.Errorf("s.ReadASN1Integer(&n) = %v, n = %d; want true, n = %d (i = %d)",
  310. ok, n, tt.in, i)
  311. }
  312. if len(s) != 0 {
  313. t.Errorf("len(s) = %d, want 0", len(s))
  314. }
  315. }
  316. }
  317. func TestASN1Uint64(t *testing.T) {
  318. tests := []struct {
  319. in uint64
  320. want []byte
  321. }{
  322. {0, []byte{2, 1, 0}},
  323. {1, []byte{2, 1, 1}},
  324. {2, []byte{2, 1, 2}},
  325. {127, []byte{2, 1, 127}},
  326. {128, []byte{2, 2, 0, 128}},
  327. {256, []byte{2, 2, 1, 0}},
  328. {0x800000, []byte{2, 4, 0, 128, 0, 0}},
  329. {0x7fffffffffffffff, []byte{2, 8, 127, 255, 255, 255, 255, 255, 255, 255}},
  330. {0x8000000000000000, []byte{2, 9, 0, 128, 0, 0, 0, 0, 0, 0, 0}},
  331. {0xffffffffffffffff, []byte{2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255}},
  332. }
  333. for i, tt := range tests {
  334. var b Builder
  335. b.AddASN1Uint64(tt.in)
  336. if err := builderBytesEq(&b, tt.want...); err != nil {
  337. t.Errorf("%v, (i = %d; in = %v)", err, i, tt.in)
  338. }
  339. var n uint64
  340. s := String(b.BytesOrPanic())
  341. ok := s.ReadASN1Integer(&n)
  342. if !ok || n != tt.in {
  343. t.Errorf("s.ReadASN1Integer(&n) = %v, n = %d; want true, n = %d (i = %d)",
  344. ok, n, tt.in, i)
  345. }
  346. if len(s) != 0 {
  347. t.Errorf("len(s) = %d, want 0", len(s))
  348. }
  349. }
  350. }