summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/common_audio/wav_header_unittest.cc
blob: 95721dac656f7e2a9f4ad7a865dc7edcf6542b1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "common_audio/wav_header.h"

#include <string.h>

#include <limits>

#include "test/gtest.h"

namespace webrtc {

// Doesn't take ownership of the buffer.
class WavHeaderBufferReader : public WavHeaderReader {
 public:
  WavHeaderBufferReader(const uint8_t* buf, size_t size, bool check_read_size)
      : buf_(buf),
        size_(size),
        pos_(0),
        buf_exhausted_(false),
        check_read_size_(check_read_size) {}

  ~WavHeaderBufferReader() override {
    // Verify the entire buffer has been read.
    if (check_read_size_)
      EXPECT_EQ(size_, pos_);
  }

  size_t Read(void* buf, size_t num_bytes) override {
    EXPECT_FALSE(buf_exhausted_);

    const size_t bytes_remaining = size_ - pos_;
    if (num_bytes > bytes_remaining) {
      // The caller is signalled about an exhausted buffer when we return fewer
      // bytes than requested. There should not be another read attempt after
      // this point.
      buf_exhausted_ = true;
      num_bytes = bytes_remaining;
    }
    memcpy(buf, &buf_[pos_], num_bytes);
    pos_ += num_bytes;
    return num_bytes;
  }

  bool SeekForward(uint32_t num_bytes) override {
    // Verify we don't try to read outside of a properly sized header.
    if (size_ >= kPcmWavHeaderSize)
      EXPECT_GE(size_, pos_ + num_bytes);
    EXPECT_FALSE(buf_exhausted_);

    const size_t bytes_remaining = size_ - pos_;
    if (num_bytes > bytes_remaining) {
      // Error: cannot seek beyond EOF.
      return false;
    }
    if (num_bytes == bytes_remaining) {
      // There should not be another read attempt after this point.
      buf_exhausted_ = true;
    }
    pos_ += num_bytes;
    return true;
  }

  int64_t GetPosition() override { return pos_; }

 private:
  const uint8_t* buf_;
  const size_t size_;
  size_t pos_;
  bool buf_exhausted_;
  const bool check_read_size_;
};

// Try various choices of WAV header parameters, and make sure that the good
// ones are accepted and the bad ones rejected.
TEST(WavHeaderTest, CheckWavParameters) {
  // Try some really stupid values for one parameter at a time.
  EXPECT_TRUE(CheckWavParameters(1, 8000, WavFormat::kWavFormatPcm, 0));
  EXPECT_FALSE(CheckWavParameters(0, 8000, WavFormat::kWavFormatPcm, 0));
  EXPECT_FALSE(CheckWavParameters(0x10000, 8000, WavFormat::kWavFormatPcm, 0));
  EXPECT_FALSE(CheckWavParameters(1, 0, WavFormat::kWavFormatPcm, 0));

  // Too large values.
  EXPECT_FALSE(
      CheckWavParameters(1 << 20, 1 << 20, WavFormat::kWavFormatPcm, 0));
  EXPECT_FALSE(CheckWavParameters(1, 8000, WavFormat::kWavFormatPcm,
                                  std::numeric_limits<uint32_t>::max()));

  // Not the same number of samples for each channel.
  EXPECT_FALSE(CheckWavParameters(3, 8000, WavFormat::kWavFormatPcm, 5));
}

TEST(WavHeaderTest, ReadWavHeaderWithErrors) {
  size_t num_channels = 0;
  int sample_rate = 0;
  WavFormat format = WavFormat::kWavFormatPcm;
  size_t bytes_per_sample = 0;
  size_t num_samples = 0;
  int64_t data_start_pos = 0;

  // Test a few ways the header can be invalid. We start with the valid header
  // used in WriteAndReadWavHeader, and invalidate one field per test. The
  // invalid field is indicated in the array name, and in the comments with
  // *BAD*.
  {
    constexpr uint8_t kBadRiffID[] = {
        // clang-format off
        // clang formatting doesn't respect inline comments.
      'R', 'i', 'f', 'f',  // *BAD*
      0xbd, 0xd0, 0x5b, 0x07,  // size of whole file - 8: 123457689 + 44 - 8
      'W', 'A', 'V', 'E',
      'f', 'm', 't', ' ',
      16, 0, 0, 0,  // size of fmt block - 8: 24 - 8
      1, 0,  // format: PCM (1)
      17, 0,  // channels: 17
      0x39, 0x30, 0, 0,  // sample rate: 12345
      0xc9, 0x33, 0x03, 0,  // byte rate: 1 * 17 * 12345
      17, 0,  // block align: NumChannels * BytesPerSample
      8, 0,  // bits per sample: 1 * 8
      'd', 'a', 't', 'a',
      0x99, 0xd0, 0x5b, 0x07,  // size of payload: 123457689
        // clang-format on
    };
    WavHeaderBufferReader r(kBadRiffID, sizeof(kBadRiffID),
                            /*check_read_size=*/false);
    EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                               &bytes_per_sample, &num_samples,
                               &data_start_pos));
  }
  {
    constexpr uint8_t kBadBitsPerSample[] = {
        // clang-format off
        // clang formatting doesn't respect inline comments.
      'R', 'I', 'F', 'F',
      0xbd, 0xd0, 0x5b, 0x07,  // size of whole file - 8: 123457689 + 44 - 8
      'W', 'A', 'V', 'E',
      'f', 'm', 't', ' ',
      16, 0, 0, 0,  // size of fmt block - 8: 24 - 8
      1, 0,  // format: PCM (1)
      17, 0,  // channels: 17
      0x39, 0x30, 0, 0,  // sample rate: 12345
      0xc9, 0x33, 0x03, 0,  // byte rate: 1 * 17 * 12345
      17, 0,  // block align: NumChannels * BytesPerSample
      1, 0,  // bits per sample: *BAD*
      'd', 'a', 't', 'a',
      0x99, 0xd0, 0x5b, 0x07,  // size of payload: 123457689
        // clang-format on
    };
    WavHeaderBufferReader r(kBadBitsPerSample, sizeof(kBadBitsPerSample),
                            /*check_read_size=*/true);
    EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                               &bytes_per_sample, &num_samples,
                               &data_start_pos));
  }
  {
    constexpr uint8_t kBadByteRate[] = {
        // clang-format off
        // clang formatting doesn't respect inline comments.
      'R', 'I', 'F', 'F',
      0xbd, 0xd0, 0x5b, 0x07,  // size of whole file - 8: 123457689 + 44 - 8
      'W', 'A', 'V', 'E',
      'f', 'm', 't', ' ',
      16, 0, 0, 0,  // size of fmt block - 8: 24 - 8
      1, 0,  // format: PCM (1)
      17, 0,  // channels: 17
      0x39, 0x30, 0, 0,  // sample rate: 12345
      0x00, 0x33, 0x03, 0,  // byte rate: *BAD*
      17, 0,  // block align: NumChannels * BytesPerSample
      8, 0,  // bits per sample: 1 * 8
      'd', 'a', 't', 'a',
      0x99, 0xd0, 0x5b, 0x07,  // size of payload: 123457689
        // clang-format on
    };
    WavHeaderBufferReader r(kBadByteRate, sizeof(kBadByteRate),
                            /*check_read_size=*/true);
    EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                               &bytes_per_sample, &num_samples,
                               &data_start_pos));
  }
  {
    constexpr uint8_t kBadFmtHeaderSize[] = {
        // clang-format off
        // clang formatting doesn't respect inline comments.
      'R', 'I', 'F', 'F',
      0xbd, 0xd0, 0x5b, 0x07,  // size of whole file - 8: 123457689 + 44 - 8
      'W', 'A', 'V', 'E',
      'f', 'm', 't', ' ',
      17, 0, 0, 0,  // size of fmt block *BAD*. Only 16 and 18 permitted.
      1, 0,  // format: PCM (1)
      17, 0,  // channels: 17
      0x39, 0x30, 0, 0,  // sample rate: 12345
      0xc9, 0x33, 0x03, 0,  // byte rate: 1 * 17 * 12345
      17, 0,  // block align: NumChannels * BytesPerSample
      8, 0,  // bits per sample: 1 * 8
      0,  // extra (though invalid) header byte
      'd', 'a', 't', 'a',
      0x99, 0xd0, 0x5b, 0x07,  // size of payload: 123457689
        // clang-format on
    };
    WavHeaderBufferReader r(kBadFmtHeaderSize, sizeof(kBadFmtHeaderSize),
                            /*check_read_size=*/false);
    EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                               &bytes_per_sample, &num_samples,
                               &data_start_pos));
  }
  {
    constexpr uint8_t kNonZeroExtensionField[] = {
        // clang-format off
        // clang formatting doesn't respect inline comments.
      'R', 'I', 'F', 'F',
      0xbd, 0xd0, 0x5b, 0x07,  // size of whole file - 8: 123457689 + 44 - 8
      'W', 'A', 'V', 'E',
      'f', 'm', 't', ' ',
      18, 0, 0, 0,  // size of fmt block - 8: 24 - 8
      1, 0,  // format: PCM (1)
      17, 0,  // channels: 17
      0x39, 0x30, 0, 0,  // sample rate: 12345
      0xc9, 0x33, 0x03, 0,  // byte rate: 1 * 17 * 12345
      17, 0,  // block align: NumChannels * BytesPerSample
      8, 0,  // bits per sample: 1 * 8
      1, 0,  // non-zero extension field *BAD*
      'd', 'a', 't', 'a',
      0x99, 0xd0, 0x5b, 0x07,  // size of payload: 123457689
        // clang-format on
    };
    WavHeaderBufferReader r(kNonZeroExtensionField,
                            sizeof(kNonZeroExtensionField),
                            /*check_read_size=*/false);
    EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                               &bytes_per_sample, &num_samples,
                               &data_start_pos));
  }
  {
    constexpr uint8_t kMissingDataChunk[] = {
        // clang-format off
        // clang formatting doesn't respect inline comments.
      'R', 'I', 'F', 'F',
      0xbd, 0xd0, 0x5b, 0x07,  // size of whole file - 8: 123457689 + 44 - 8
      'W', 'A', 'V', 'E',
      'f', 'm', 't', ' ',
      16, 0, 0, 0,  // size of fmt block - 8: 24 - 8
      1, 0,  // format: PCM (1)
      17, 0,  // channels: 17
      0x39, 0x30, 0, 0,  // sample rate: 12345
      0xc9, 0x33, 0x03, 0,  // byte rate: 1 * 17 * 12345
      17, 0,  // block align: NumChannels * BytesPerSample
      8, 0,  // bits per sample: 1 * 8
        // clang-format on
    };
    WavHeaderBufferReader r(kMissingDataChunk, sizeof(kMissingDataChunk),
                            /*check_read_size=*/true);
    EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                               &bytes_per_sample, &num_samples,
                               &data_start_pos));
  }
  {
    constexpr uint8_t kMissingFmtAndDataChunks[] = {
        // clang-format off
        // clang formatting doesn't respect inline comments.
      'R', 'I', 'F', 'F',
      0xbd, 0xd0, 0x5b, 0x07,  // size of whole file - 8: 123457689 + 44 - 8
      'W', 'A', 'V', 'E',
        // clang-format on
    };
    WavHeaderBufferReader r(kMissingFmtAndDataChunks,
                            sizeof(kMissingFmtAndDataChunks),
                            /*check_read_size=*/true);
    EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                               &bytes_per_sample, &num_samples,
                               &data_start_pos));
  }
}

// Try writing and reading a valid WAV header and make sure it looks OK.
TEST(WavHeaderTest, WriteAndReadWavHeader) {
  constexpr int kSize = 4 + kPcmWavHeaderSize + 4;
  uint8_t buf[kSize];
  size_t header_size;
  memset(buf, 0xa4, sizeof(buf));
  WriteWavHeader(17, 12345, WavFormat::kWavFormatPcm, 123457689, buf + 4,
                 &header_size);
  constexpr uint8_t kExpectedBuf[] = {
      // clang-format off
    // clang formatting doesn't respect inline comments.
    0xa4, 0xa4, 0xa4, 0xa4,  // untouched bytes before header
    'R', 'I', 'F', 'F',
    0x56, 0xa1, 0xb7, 0x0e,  // size of whole file - 8: 123457689 + 44 - 8
    'W', 'A', 'V', 'E',
    'f', 'm', 't', ' ',
    16, 0, 0, 0,  // size of fmt block - 8: 24 - 8
    1, 0,  // format: PCM (1)
    17, 0,  // channels: 17
    0x39, 0x30, 0, 0,  // sample rate: 12345
    0x92, 0x67, 0x06, 0,  // byte rate: 2 * 17 * 12345
    34, 0,  // block align: NumChannels * BytesPerSample
    16, 0,  // bits per sample: 2 * 8
    'd', 'a', 't', 'a',
    0x32, 0xa1, 0xb7, 0x0e,  // size of payload: 2 * 123457689
    0xa4, 0xa4, 0xa4, 0xa4,  // untouched bytes after header
      // clang-format on
  };
  static_assert(sizeof(kExpectedBuf) == kSize, "buffer size");
  EXPECT_EQ(0, memcmp(kExpectedBuf, buf, kSize));

  size_t num_channels = 0;
  int sample_rate = 0;
  WavFormat format = WavFormat::kWavFormatPcm;
  size_t bytes_per_sample = 0;
  size_t num_samples = 0;
  int64_t data_start_pos = 0;
  WavHeaderBufferReader r(buf + 4, sizeof(buf) - 8,
                          /*check_read_size=*/true);
  EXPECT_TRUE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                            &bytes_per_sample, &num_samples, &data_start_pos));
  EXPECT_EQ(17u, num_channels);
  EXPECT_EQ(12345, sample_rate);
  EXPECT_EQ(WavFormat::kWavFormatPcm, format);
  EXPECT_EQ(2u, bytes_per_sample);
  EXPECT_EQ(123457689u, num_samples);
}

// Try reading an atypical but valid WAV header and make sure it's parsed OK.
TEST(WavHeaderTest, ReadAtypicalWavHeader) {
  constexpr uint8_t kBuf[] = {
      // clang-format off
      // clang formatting doesn't respect inline comments.
    'R', 'I', 'F', 'F',
    0xbf, 0xd0, 0x5b, 0x07,  // Size of whole file - 8 + extra 2 bytes of zero
                             // extension: 123457689 + 44 - 8 + 2 (atypical).
    'W', 'A', 'V', 'E',
    'f', 'm', 't', ' ',
    18, 0, 0, 0,             // Size of fmt block (with an atypical extension
                             // size field).
    1, 0,                    // Format: PCM (1).
    17, 0,                   // Channels: 17.
    0x39, 0x30, 0, 0,        // Sample rate: 12345.
    0xc9, 0x33, 0x03, 0,     // Byte rate: 1 * 17 * 12345.
    17, 0,                   // Block align: NumChannels * BytesPerSample.
    8, 0,                    // Bits per sample: 1 * 8.
    0, 0,                    // Zero extension size field (atypical).
    'd', 'a', 't', 'a',
    0x99, 0xd0, 0x5b, 0x07,  // Size of payload: 123457689.
      // clang-format on
  };

  size_t num_channels = 0;
  int sample_rate = 0;
  WavFormat format = WavFormat::kWavFormatPcm;
  size_t bytes_per_sample = 0;
  size_t num_samples = 0;
  int64_t data_start_pos = 0;
  WavHeaderBufferReader r(kBuf, sizeof(kBuf), /*check_read_size=*/true);
  EXPECT_TRUE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                            &bytes_per_sample, &num_samples, &data_start_pos));
  EXPECT_EQ(17u, num_channels);
  EXPECT_EQ(12345, sample_rate);
  EXPECT_EQ(WavFormat::kWavFormatPcm, format);
  EXPECT_EQ(1u, bytes_per_sample);
  EXPECT_EQ(123457689u, num_samples);
}

// Try reading a valid WAV header which contains an optional chunk and make sure
// it's parsed OK.
TEST(WavHeaderTest, ReadWavHeaderWithOptionalChunk) {
  constexpr uint8_t kBuf[] = {
      // clang-format off
      // clang formatting doesn't respect inline comments.
    'R', 'I', 'F', 'F',
    0xcd, 0xd0, 0x5b, 0x07,  // Size of whole file - 8 + an extra 16 bytes of
                             // "metadata" (8 bytes header, 16 bytes payload):
                             // 123457689 + 44 - 8 + 16.
    'W', 'A', 'V', 'E',
    'f', 'm', 't', ' ',
    16, 0, 0, 0,             // Size of fmt block.
    1, 0,                    // Format: PCM (1).
    17, 0,                   // Channels: 17.
    0x39, 0x30, 0, 0,        // Sample rate: 12345.
    0xc9, 0x33, 0x03, 0,     // Byte rate: 1 * 17 * 12345.
    17, 0,                   // Block align: NumChannels * BytesPerSample.
    8, 0,                    // Bits per sample: 1 * 8.
    'L', 'I', 'S', 'T',      // Metadata chunk ID.
    16, 0, 0, 0,             // Metadata chunk payload size.
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // Metadata (16 bytes).
    'd', 'a', 't', 'a',
    0x99, 0xd0, 0x5b, 0x07,  // Size of payload: 123457689.
      // clang-format on
  };

  size_t num_channels = 0;
  int sample_rate = 0;
  WavFormat format = WavFormat::kWavFormatPcm;
  size_t bytes_per_sample = 0;
  size_t num_samples = 0;
  int64_t data_start_pos = 0;
  WavHeaderBufferReader r(kBuf, sizeof(kBuf), /*check_read_size=*/true);
  EXPECT_TRUE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                            &bytes_per_sample, &num_samples, &data_start_pos));
  EXPECT_EQ(17u, num_channels);
  EXPECT_EQ(12345, sample_rate);
  EXPECT_EQ(WavFormat::kWavFormatPcm, format);
  EXPECT_EQ(1u, bytes_per_sample);
  EXPECT_EQ(123457689u, num_samples);
}

// Try reading an invalid WAV header which has the the data chunk before the
// format one and make sure it's not parsed.
TEST(WavHeaderTest, ReadWavHeaderWithDataBeforeFormat) {
  constexpr uint8_t kBuf[] = {
      // clang-format off
      // clang formatting doesn't respect inline comments.
    'R', 'I', 'F', 'F',
    52,  0,   0,   0,    // Size of whole file - 8: 16 + 44 - 8.
    'W', 'A', 'V', 'E',
    'd', 'a', 't', 'a',
    16, 0, 0, 0,         // Data chunk payload size.
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // Data 16 bytes.
    'f', 'm', 't', ' ',
    16,  0,   0,   0,    // Size of fmt block.
    1,   0,              // Format: Pcm (1).
    1,   0,              // Channels: 1.
    60,  0,   0,   0,    // Sample rate: 60.
    60,  0,   0,   0,    // Byte rate: 1 * 1 * 60.
    1,   0,              // Block align: NumChannels * BytesPerSample.
    8,   0,              // Bits per sample: 1 * 8.
      // clang-format on
  };

  size_t num_channels = 0;
  int sample_rate = 0;
  WavFormat format = WavFormat::kWavFormatPcm;
  size_t bytes_per_sample = 0;
  size_t num_samples = 0;
  int64_t data_start_pos = 0;
  WavHeaderBufferReader r(kBuf, sizeof(kBuf), /*check_read_size=*/false);
  EXPECT_FALSE(ReadWavHeader(&r, &num_channels, &sample_rate, &format,
                             &bytes_per_sample, &num_samples, &data_start_pos));
}

}  // namespace webrtc