riff_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2016 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 riff
  5. import (
  6. "bytes"
  7. "testing"
  8. )
  9. func encodeU32(u uint32) []byte {
  10. return []byte{
  11. byte(u >> 0),
  12. byte(u >> 8),
  13. byte(u >> 16),
  14. byte(u >> 24),
  15. }
  16. }
  17. func TestShortChunks(t *testing.T) {
  18. // s is a RIFF(ABCD) with allegedly 256 bytes of data (excluding the
  19. // leading 8-byte "RIFF\x00\x01\x00\x00"). The first chunk of that ABCD
  20. // list is an abcd chunk of length m followed by n zeroes.
  21. for _, m := range []uint32{0, 8, 15, 200, 300} {
  22. for _, n := range []int{0, 1, 2, 7} {
  23. s := []byte("RIFF\x00\x01\x00\x00ABCDabcd")
  24. s = append(s, encodeU32(m)...)
  25. s = append(s, make([]byte, n)...)
  26. _, r, err := NewReader(bytes.NewReader(s))
  27. if err != nil {
  28. t.Errorf("m=%d, n=%d: NewReader: %v", m, n, err)
  29. continue
  30. }
  31. _, _, _, err0 := r.Next()
  32. // The total "ABCD" list length is 256 bytes, of which the first 12
  33. // bytes are "ABCDabcd" plus the 4-byte encoding of m. If the
  34. // "abcd" subchunk length (m) plus those 12 bytes is greater than
  35. // the total list length, we have an invalid RIFF, and we expect an
  36. // errListSubchunkTooLong error.
  37. if m+12 > 256 {
  38. if err0 != errListSubchunkTooLong {
  39. t.Errorf("m=%d, n=%d: Next #0: got %v, want %v", m, n, err0, errListSubchunkTooLong)
  40. }
  41. continue
  42. }
  43. // Otherwise, we expect a nil error.
  44. if err0 != nil {
  45. t.Errorf("m=%d, n=%d: Next #0: %v", m, n, err0)
  46. continue
  47. }
  48. _, _, _, err1 := r.Next()
  49. // If m > 0, then m > n, so that "abcd" subchunk doesn't have m
  50. // bytes of data. If m == 0, then that "abcd" subchunk is OK in
  51. // that it has 0 extra bytes of data, but the next subchunk (8 byte
  52. // header plus body) is missing, as we only have n < 8 more bytes.
  53. want := errShortChunkData
  54. if m == 0 {
  55. want = errShortChunkHeader
  56. }
  57. if err1 != want {
  58. t.Errorf("m=%d, n=%d: Next #1: got %v, want %v", m, n, err1, want)
  59. continue
  60. }
  61. }
  62. }
  63. }