encapsulation_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. package encapsulation
  2. import (
  3. "bytes"
  4. "io"
  5. "math/rand"
  6. "testing"
  7. )
  8. // Return a byte slice with non-trivial contents.
  9. func pseudorandomBuffer(n int) []byte {
  10. source := rand.NewSource(0)
  11. p := make([]byte, n)
  12. for i := 0; i < len(p); i++ {
  13. p[i] = byte(source.Int63() & 0xff)
  14. }
  15. return p
  16. }
  17. func mustWriteData(w io.Writer, p []byte) int {
  18. n, err := WriteData(w, p)
  19. if err != nil {
  20. panic(err)
  21. }
  22. return n
  23. }
  24. func mustWritePadding(w io.Writer, n int) int {
  25. n, err := WritePadding(w, n)
  26. if err != nil {
  27. panic(err)
  28. }
  29. return n
  30. }
  31. // Test that ReadData(WriteData()) recovers the original data.
  32. func TestRoundtrip(t *testing.T) {
  33. // Test above and below interesting thresholds.
  34. for _, i := range []int{
  35. 0x00, 0x01,
  36. 0x3e, 0x3f, 0x40, 0x41,
  37. 0xfe, 0xff, 0x100, 0x101,
  38. 0x1ffe, 0x1fff, 0x2000, 0x2001,
  39. 0xfffe, 0xffff, 0x10000, 0x10001,
  40. 0xffffe, 0xfffff,
  41. } {
  42. original := pseudorandomBuffer(i)
  43. var enc bytes.Buffer
  44. n, err := WriteData(&enc, original)
  45. if err != nil {
  46. t.Fatalf("size %d, WriteData returned error %v", i, err)
  47. }
  48. if enc.Len() != n {
  49. t.Fatalf("size %d, returned length was %d, written length was %d",
  50. i, n, enc.Len())
  51. }
  52. inverse := make([]byte, i)
  53. n, err = ReadData(&enc, inverse)
  54. if err != nil {
  55. t.Fatalf("size %d, ReadData returned error %v", i, err)
  56. }
  57. if !bytes.Equal(inverse[:n], original) {
  58. t.Fatalf("size %d, got <%x>, expected <%x>", i, inverse[:n], original)
  59. }
  60. }
  61. }
  62. // Test that WritePadding writes exactly as much as requested.
  63. func TestPaddingLength(t *testing.T) {
  64. // Test above and below interesting thresholds. WritePadding also gets
  65. // values above 0xfffff, the maximum value of a single length prefix.
  66. for _, i := range []int{
  67. 0x00, 0x01,
  68. 0x3f, 0x40, 0x41, 0x42,
  69. 0xff, 0x100, 0x101, 0x102,
  70. 0x2000, 0x2001, 0x2002, 0x2003,
  71. 0x10000, 0x10001, 0x10002, 0x10003,
  72. 0x100001, 0x100002, 0x100003, 0x100004,
  73. } {
  74. var enc bytes.Buffer
  75. n, err := WritePadding(&enc, i)
  76. if err != nil {
  77. t.Fatalf("size %d, WritePadding returned error %v", i, err)
  78. }
  79. if n != i {
  80. t.Fatalf("requested %d bytes, returned %d", i, n)
  81. }
  82. if enc.Len() != n {
  83. t.Fatalf("requested %d bytes, wrote %d bytes", i, enc.Len())
  84. }
  85. }
  86. }
  87. // Test that ReadData skips over padding.
  88. func TestSkipPadding(t *testing.T) {
  89. var data = [][]byte{{}, {}, []byte("hello"), {}, []byte("world")}
  90. var enc bytes.Buffer
  91. mustWritePadding(&enc, 10)
  92. mustWritePadding(&enc, 100)
  93. mustWriteData(&enc, data[0])
  94. mustWriteData(&enc, data[1])
  95. mustWritePadding(&enc, 10)
  96. mustWriteData(&enc, data[2])
  97. mustWriteData(&enc, data[3])
  98. mustWritePadding(&enc, 10)
  99. mustWriteData(&enc, data[4])
  100. mustWritePadding(&enc, 10)
  101. mustWritePadding(&enc, 10)
  102. for i, expected := range data {
  103. var actual [10]byte
  104. n, err := ReadData(&enc, actual[:])
  105. if err != nil {
  106. t.Fatalf("slice %d, got error %v, expected %v", i, err, nil)
  107. }
  108. if !bytes.Equal(actual[:n], expected) {
  109. t.Fatalf("slice %d, got <%x>, expected <%x>", i, actual[:n], expected)
  110. }
  111. }
  112. n, err := ReadData(&enc, nil)
  113. if n != 0 || err != io.EOF {
  114. t.Fatalf("got (%v, %v), expected (%v, %v)", n, err, 0, io.EOF)
  115. }
  116. }
  117. // Test that EOF before a length prefix returns io.EOF.
  118. func TestEOF(t *testing.T) {
  119. n, err := ReadData(bytes.NewReader(nil), nil)
  120. if n != 0 || err != io.EOF {
  121. t.Fatalf("got (%v, %v), expected (%v, %v)", n, err, 0, io.EOF)
  122. }
  123. }
  124. // Test that an EOF while reading a length prefix, or while reading the
  125. // subsequent data/padding, returns io.ErrUnexpectedEOF.
  126. func TestUnexpectedEOF(t *testing.T) {
  127. for _, test := range [][]byte{
  128. {0x40}, // expecting a second length byte
  129. {0xc0}, // expecting a second length byte
  130. {0x41, 0x80}, // expecting a third length byte
  131. {0xc1, 0x80}, // expecting a third length byte
  132. {0x02}, // expecting 2 bytes of padding
  133. {0x82}, // expecting 2 bytes of data
  134. {0x02, 'X'}, // expecting 1 byte of padding
  135. {0x82, 'X'}, // expecting 1 byte of data
  136. {0x41, 0x00}, // expecting 128 bytes of padding
  137. {0xc1, 0x00}, // expecting 128 bytes of data
  138. {0x41, 0x00, 'X'}, // expecting 127 bytes of padding
  139. {0xc1, 0x00, 'X'}, // expecting 127 bytes of data
  140. {0x41, 0x80, 0x00}, // expecting 32768 bytes of padding
  141. {0xc1, 0x80, 0x00}, // expecting 32768 bytes of data
  142. {0x41, 0x80, 0x00, 'X'}, // expecting 32767 bytes of padding
  143. {0xc1, 0x80, 0x00, 'X'}, // expecting 32767 bytes of data
  144. } {
  145. n, err := ReadData(bytes.NewReader(test), nil)
  146. if n != 0 || err != io.ErrUnexpectedEOF {
  147. t.Fatalf("<%x> got (%v, %v), expected (%v, %v)", test, n, err, 0, io.ErrUnexpectedEOF)
  148. }
  149. }
  150. }
  151. // Test that length encodings that are longer than they could be are still
  152. // interpreted.
  153. func TestNonMinimalLengthEncoding(t *testing.T) {
  154. for _, test := range []struct {
  155. enc []byte
  156. expected []byte
  157. }{
  158. {[]byte{0x81, 'X'}, []byte("X")},
  159. {[]byte{0xc0, 0x01, 'X'}, []byte("X")},
  160. {[]byte{0xc0, 0x80, 0x01, 'X'}, []byte("X")},
  161. } {
  162. var p [10]byte
  163. n, err := ReadData(bytes.NewReader(test.enc), p[:])
  164. if err != nil {
  165. t.Fatalf("<%x> got error %v, expected %v", test.enc, err, nil)
  166. }
  167. if !bytes.Equal(p[:n], test.expected) {
  168. t.Fatalf("<%x> got <%x>, expected <%x>", test.enc, p[:n], test.expected)
  169. }
  170. }
  171. }
  172. // Test that ReadData only reads up to 3 bytes of length prefix.
  173. func TestReadLimits(t *testing.T) {
  174. // Test the maximum length that's possible with 3 bytes of length
  175. // prefix.
  176. maxLength := (0x3f << 14) | (0x7f << 7) | 0x7f
  177. data := bytes.Repeat([]byte{'X'}, maxLength)
  178. prefix := []byte{0xff, 0xff, 0x7f} // encodes 0xfffff
  179. var p [0xfffff]byte
  180. n, err := ReadData(bytes.NewReader(append(prefix, data...)), p[:])
  181. if err != nil {
  182. t.Fatalf("got error %v, expected %v", err, nil)
  183. }
  184. if !bytes.Equal(p[:n], data) {
  185. t.Fatalf("got %d bytes unequal to %d bytes", len(p), len(data))
  186. }
  187. // Test a 4-byte prefix.
  188. prefix = []byte{0xc0, 0xc0, 0x80, 0x80} // encodes 0x100000
  189. data = bytes.Repeat([]byte{'X'}, maxLength+1)
  190. n, err = ReadData(bytes.NewReader(append(prefix, data...)), nil)
  191. if n != 0 || err != ErrTooLong {
  192. t.Fatalf("got (%v, %v), expected (%v, %v)", n, err, 0, ErrTooLong)
  193. }
  194. // Test that 4 bytes don't work, even when they encode an integer that
  195. // would fix in 3 bytes.
  196. prefix = []byte{0xc0, 0x80, 0x80, 0x80} // encodes 0x0
  197. data = []byte{}
  198. n, err = ReadData(bytes.NewReader(append(prefix, data...)), nil)
  199. if n != 0 || err != ErrTooLong {
  200. t.Fatalf("got (%v, %v), expected (%v, %v)", n, err, 0, ErrTooLong)
  201. }
  202. // Do the same tests with padding lengths.
  203. data = []byte("hello")
  204. prefix = []byte{0x7f, 0xff, 0x7f} // encodes 0xfffff
  205. padding := bytes.Repeat([]byte{'X'}, maxLength)
  206. enc := bytes.NewBuffer(append(prefix, padding...))
  207. mustWriteData(enc, data)
  208. n, err = ReadData(enc, p[:])
  209. if err != nil {
  210. t.Fatalf("got error %v, expected %v", err, nil)
  211. }
  212. if !bytes.Equal(p[:n], data) {
  213. t.Fatalf("got <%x>, expected <%x>", p[:n], data)
  214. }
  215. prefix = []byte{0x40, 0xc0, 0x80, 0x80} // encodes 0x100000
  216. padding = bytes.Repeat([]byte{'X'}, maxLength+1)
  217. enc = bytes.NewBuffer(append(prefix, padding...))
  218. mustWriteData(enc, data)
  219. n, err = ReadData(enc, nil)
  220. if n != 0 || err != ErrTooLong {
  221. t.Fatalf("got (%v, %v), expected (%v, %v)", n, err, 0, ErrTooLong)
  222. }
  223. prefix = []byte{0x40, 0x80, 0x80, 0x80} // encodes 0x0
  224. padding = []byte{}
  225. enc = bytes.NewBuffer(append(prefix, padding...))
  226. mustWriteData(enc, data)
  227. n, err = ReadData(enc, nil)
  228. if n != 0 || err != ErrTooLong {
  229. t.Fatalf("got (%v, %v), expected (%v, %v)", n, err, 0, ErrTooLong)
  230. }
  231. }
  232. // Test that WriteData and WritePadding only accept lengths that can be encoded
  233. // in up to 3 bytes of length prefix.
  234. func TestWriteLimits(t *testing.T) {
  235. maxLength := (0x3f << 14) | (0x7f << 7) | 0x7f
  236. var enc bytes.Buffer
  237. n, err := WriteData(&enc, bytes.Repeat([]byte{'X'}, maxLength))
  238. if n != maxLength+3 || err != nil {
  239. t.Fatalf("got (%d, %v), expected (%d, %v)", n, err, maxLength, nil)
  240. }
  241. enc.Reset()
  242. n, err = WriteData(&enc, bytes.Repeat([]byte{'X'}, maxLength+1))
  243. if n != 0 || err != ErrTooLong {
  244. t.Fatalf("got (%d, %v), expected (%d, %v)", n, err, 0, ErrTooLong)
  245. }
  246. // Padding gets an extra 3 bytes because the prefix is counted as part
  247. // of the length.
  248. enc.Reset()
  249. n, err = WritePadding(&enc, maxLength+3)
  250. if n != maxLength+3 || err != nil {
  251. t.Fatalf("got (%d, %v), expected (%d, %v)", n, err, maxLength+3, nil)
  252. }
  253. // Writing a too-long padding is okay because WritePadding will break it
  254. // into smaller chunks.
  255. enc.Reset()
  256. n, err = WritePadding(&enc, maxLength+4)
  257. if n != maxLength+4 || err != nil {
  258. t.Fatalf("got (%d, %v), expected (%d, %v)", n, err, maxLength+4, nil)
  259. }
  260. }
  261. // Test that WritePadding panics when given a negative length.
  262. func TestNegativeLength(t *testing.T) {
  263. for _, n := range []int{-1, ^0} {
  264. var enc bytes.Buffer
  265. panicked, nn, err := testNegativeLengthSub(t, &enc, n)
  266. if !panicked {
  267. t.Fatalf("WritePadding(%d) returned (%d, %v) instead of panicking", n, nn, err)
  268. }
  269. }
  270. }
  271. // Calls WritePadding(w, n) and augments the return value with a flag indicating
  272. // whether the call panicked.
  273. func testNegativeLengthSub(t *testing.T, w io.Writer, n int) (panicked bool, nn int, err error) {
  274. defer func() {
  275. if r := recover(); r != nil {
  276. panicked = true
  277. }
  278. }()
  279. t.Helper()
  280. nn, err = WritePadding(w, n)
  281. return false, n, err
  282. }
  283. // Test that MaxDataForSize panics when given a 0 length.
  284. func TestMaxDataForSizeZero(t *testing.T) {
  285. defer func() {
  286. if r := recover(); r == nil {
  287. t.Fatal("didn't panic")
  288. }
  289. }()
  290. MaxDataForSize(0)
  291. }
  292. // Test thresholds of available sizes for MaxDataForSize.
  293. func TestMaxDataForSize(t *testing.T) {
  294. for _, test := range []struct {
  295. size int
  296. expected int
  297. }{
  298. {0x01, 0x00},
  299. {0x02, 0x01},
  300. {0x3f, 0x3e},
  301. {0x40, 0x3e},
  302. {0x41, 0x3f},
  303. {0x1fff, 0x1ffd},
  304. {0x2000, 0x1ffd},
  305. {0x2001, 0x1ffe},
  306. {0xfffff, 0xffffc},
  307. {0x100000, 0xffffc},
  308. {0x100001, 0xffffc},
  309. {0x7fffffff, 0xffffc},
  310. } {
  311. max := MaxDataForSize(test.size)
  312. if max != test.expected {
  313. t.Fatalf("size %d, got %d, expected %d", test.size, max, test.expected)
  314. }
  315. }
  316. }
  317. // Test that ReadData truncates the data when the destination slice is too
  318. // short.
  319. func TestReadDataTruncate(t *testing.T) {
  320. var enc bytes.Buffer
  321. mustWriteData(&enc, []byte("12345678"))
  322. mustWriteData(&enc, []byte("abcdefgh"))
  323. var p [4]byte
  324. // First ReadData should return truncated "1234".
  325. n, err := ReadData(&enc, p[:])
  326. if err != io.ErrShortBuffer {
  327. t.Fatalf("got error %v, expected %v", err, io.ErrShortBuffer)
  328. }
  329. if !bytes.Equal(p[:n], []byte("1234")) {
  330. t.Fatalf("got <%x>, expected <%x>", p[:n], []byte("1234"))
  331. }
  332. // Second ReadData should return truncated "abcd", not the rest of
  333. // "12345678".
  334. n, err = ReadData(&enc, p[:])
  335. if err != io.ErrShortBuffer {
  336. t.Fatalf("got error %v, expected %v", err, io.ErrShortBuffer)
  337. }
  338. if !bytes.Equal(p[:n], []byte("abcd")) {
  339. t.Fatalf("got <%x>, expected <%x>", p[:n], []byte("abcd"))
  340. }
  341. // Last ReadData should give io.EOF.
  342. n, err = ReadData(&enc, p[:])
  343. if err != io.EOF {
  344. t.Fatalf("got error %v, expected %v", err, io.EOF)
  345. }
  346. }
  347. // Test that even when the result is truncated, ReadData fills the provided
  348. // buffer as much as possible (and not stop at the boundary of an internal Read,
  349. // say).
  350. func TestReadDataTruncateFull(t *testing.T) {
  351. pr, pw := io.Pipe()
  352. go func() {
  353. // Send one data chunk that will be delivered across two Read
  354. // calls.
  355. pw.Write([]byte{0x8a, 'h', 'e', 'l', 'l', 'o'})
  356. pw.Write([]byte{'w', 'o', 'r', 'l', 'd'})
  357. }()
  358. var p [8]byte
  359. n, err := ReadData(pr, p[:])
  360. if err != io.ErrShortBuffer {
  361. t.Fatalf("got error %v, expected %v", err, io.ErrShortBuffer)
  362. }
  363. // Should not stop after "hello".
  364. if !bytes.Equal(p[:n], []byte("hellowor")) {
  365. t.Fatalf("got <%x>, expected <%x>", p[:n], []byte("hellowor"))
  366. }
  367. }
  368. // Benchmark the ReadData function when reading from a stream of data packets of
  369. // different sizes.
  370. func BenchmarkReadData(b *testing.B) {
  371. pr, pw := io.Pipe()
  372. go func() {
  373. for {
  374. for length := 0; length < 128; length++ {
  375. WriteData(pw, paddingBuffer[:length])
  376. }
  377. }
  378. }()
  379. var p [128]byte
  380. for i := 0; i < b.N; i++ {
  381. _, err := ReadData(pr, p[:])
  382. if err != nil {
  383. b.Fatal(err)
  384. }
  385. }
  386. }