You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

428 lines
9.5 KiB

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