...

Source file src/golang.org/x/image/riff/riff_test.go

Documentation: golang.org/x/image/riff

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

View as plain text