Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(899)

Unified Diff: media/audio/sounds/wav_audio_handler_unittest.cc

Issue 1453233002: Improve input handling for WaveAudioHandler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: More comments, more test cases. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/audio/sounds/wav_audio_handler_unittest.cc
diff --git a/media/audio/sounds/wav_audio_handler_unittest.cc b/media/audio/sounds/wav_audio_handler_unittest.cc
index d4ec83a5838243c5df7d488a7146780a0f8d29b4..903e9780a7c0ebbb6d5382b6d3839175046e2859 100644
--- a/media/audio/sounds/wav_audio_handler_unittest.cc
+++ b/media/audio/sounds/wav_audio_handler_unittest.cc
@@ -12,26 +12,181 @@
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
+namespace {
+// WAV header comes first in the test data.
+const size_t kWavHeaderSize = 12;
+const size_t kWavDataSizeIndex = 4;
+
+// "fmt " header comes next.
+const size_t kFormatHeaderIndex = kWavHeaderSize;
+const size_t kFormatHeaderSize = 8;
+const size_t kFormatPayloadSize = 16;
+const size_t kChannelIndex = kWavHeaderSize + kFormatHeaderSize + 2;
+const size_t kBitsPerSampleIndex = kWavHeaderSize + kFormatHeaderSize + 14;
+const size_t kSampleRateIndex = kWavHeaderSize + kFormatHeaderSize + 4;
+
+// "data" header comes last.
+const size_t kDataHeaderIndex =
+ kWavHeaderSize + kFormatHeaderSize + kFormatPayloadSize;
+
+} // namespace
TEST(WavAudioHandlerTest, SampleDataTest) {
- WavAudioHandler handler(base::StringPiece(kTestAudioData,
- arraysize(kTestAudioData)));
- ASSERT_EQ(2u, handler.num_channels());
- ASSERT_EQ(16u, handler.bits_per_sample());
- ASSERT_EQ(48000u, handler.sample_rate());
- ASSERT_EQ(1u, handler.total_frames());
- ASSERT_EQ(20u, handler.GetDuration().InMicroseconds());
-
- ASSERT_EQ(4U, handler.data().size());
+ auto handler = WavAudioHandler::Create(
+ base::StringPiece(kTestAudioData, arraysize(kTestAudioData) - 1));
+ ASSERT_TRUE(handler);
+ ASSERT_EQ(2u, handler->num_channels());
+ ASSERT_EQ(16u, handler->bits_per_sample());
+ ASSERT_EQ(48000u, handler->sample_rate());
+ ASSERT_EQ(1u, handler->total_frames());
+ ASSERT_EQ(20u, handler->GetDuration().InMicroseconds());
+
+ ASSERT_EQ(4U, handler->data().size());
+ const char kData[] = "\x01\x00\x01\x00";
+ ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler->data());
+
+ scoped_ptr<AudioBus> bus =
+ AudioBus::Create(handler->num_channels(),
+ handler->data().size() / handler->num_channels());
+
+ size_t bytes_written = 0u;
+ ASSERT_TRUE(handler->CopyTo(bus.get(), 0, &bytes_written));
+ ASSERT_EQ(static_cast<size_t>(handler->data().size()), bytes_written);
+}
+
+TEST(WavAudioHandlerTest, TestZeroChannelsIsNotValid) {
+ // Read in the sample data and modify the channel field to hold |00|00|.
+ auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
tommi (sloooow) - chröme 2015/11/19 09:44:06 please fix these to not use auto. It's not needed
slan 2015/11/19 18:57:16 Done. Also relied on implicit casting when passing
tommi (sloooow) - chröme 2015/11/20 09:45:23 Acknowledged.
+ data[kChannelIndex] = '\x00';
+ data[kChannelIndex + 1] = '\x00';
+ auto handler =
+ WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
+ EXPECT_FALSE(handler);
+}
+
+TEST(WavAudioHandlerTest, TestZeroBitsPerSampleIsNotValid) {
+ // Read in the sample data and modify the bits_per_sample field to hold
+ // |00|00|.
+ auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
+ data[kBitsPerSampleIndex] = '\x00';
+ data[kBitsPerSampleIndex + 1] = '\x00';
+ auto handler =
+ WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
+ EXPECT_FALSE(handler);
+}
+
+TEST(WavAudioHandlerTest, TestZeroSamplesPerSecondIsNotValid) {
+ // Read in the sample data and modify the bits_per_sample field to hold
+ // |00|00|.
+ auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
+ data[kSampleRateIndex] = '\x00';
+ data[kSampleRateIndex + 1] = '\x00';
+ data[kSampleRateIndex + 2] = '\x00';
+ data[kSampleRateIndex + 3] = '\x00';
+ auto handler =
+ WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
+ EXPECT_FALSE(handler);
+}
+
+TEST(WavAudioHandlerTest, TestTooBigTotalSizeIsOkay) {
+ // The size filed in the header should hold a very big number.
+ auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
+ data[kWavDataSizeIndex] = '\x00';
+ data[kWavDataSizeIndex + 1] = '\xFF';
+ data[kWavDataSizeIndex + 2] = '\xFF';
+ data[kWavDataSizeIndex + 3] = '\x00';
+ auto handler =
+ WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
+ EXPECT_TRUE(handler);
+ ASSERT_EQ(2u, handler->num_channels());
+ ASSERT_EQ(16u, handler->bits_per_sample());
+ ASSERT_EQ(48000u, handler->sample_rate());
+ ASSERT_EQ(1u, handler->total_frames());
+ ASSERT_EQ(20u, handler->GetDuration().InMicroseconds());
+
+ ASSERT_EQ(4U, handler->data().size());
const char kData[] = "\x01\x00\x01\x00";
- ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler.data());
+ ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler->data());
+}
- scoped_ptr<AudioBus> bus = AudioBus::Create(
- handler.num_channels(), handler.data().size() / handler.num_channels());
+TEST(WavAudioHandlerTest, TestTooBigDataChunkSizeIsOkay) {
+ // If the |data| chunk size is last and it indicates it has more than it
+ // actually does, that's okay. Just consume the rest of the string. If it
+ // is not the last subsection, this file will parse badly.
+ auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
+ data[kDataHeaderIndex + 4] = '\x00';
+ data[kDataHeaderIndex + 5] = '\xFF';
+ data[kDataHeaderIndex + 6] = '\xFF';
+ data[kDataHeaderIndex + 7] = '\x00';
+ auto handler =
+ WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
+ EXPECT_TRUE(handler);
+ ASSERT_EQ(2u, handler->num_channels());
+ ASSERT_EQ(16u, handler->bits_per_sample());
+ ASSERT_EQ(48000u, handler->sample_rate());
+ ASSERT_EQ(1u, handler->total_frames());
+ ASSERT_EQ(20u, handler->GetDuration().InMicroseconds());
+
+ ASSERT_EQ(4U, handler->data().size());
+ const char kData[] = "\x01\x00\x01\x00";
+ ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler->data());
+}
+
+TEST(WavAudioHandlerTest, TestTooSmallFormatSizeIsNotValid) {
+ // If the |data| chunk size is last and it indicates it has more than it
+ // actually does, that's okay. Just consume the rest of the string. If it
+ // is not the last subsection, this file will parse badly.
+ auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
+ data[kFormatHeaderIndex + 4] = '\x04';
+ data[kFormatHeaderIndex + 5] = '\x00';
+ data[kFormatHeaderIndex + 6] = '\x00';
+ data[kFormatHeaderIndex + 7] = '\x00';
+ auto handler =
+ WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
+ EXPECT_FALSE(handler);
+}
+
+TEST(WavAudioHandlerTest, TestOtherSectionTypesIsOkay) {
+ // Append some other subsection header "abcd", the class should just consume
+ // and keep going.
+ auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
+ data.append("abcd\x04\x00\x00\x00\x01\x02\x03\x04");
+ data[kWavDataSizeIndex] += 4; // This should not overflow.
+ auto handler =
+ WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
+ EXPECT_TRUE(handler);
+}
+
+TEST(WavAudioHandlerTest, TestNoFmtSectionIsNotValid) {
+ // Write over the "fmt " header. No valid handler should be returned.
+ auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
+ data[kFormatHeaderIndex] = 'a';
+ data[kFormatHeaderIndex + 1] = 'b';
+ data[kFormatHeaderIndex + 2] = 'c';
+ data[kFormatHeaderIndex + 3] = 'd';
+ auto handler =
+ WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
+ EXPECT_FALSE(handler);
+}
- size_t bytes_written;
- ASSERT_TRUE(handler.CopyTo(bus.get(), 0, &bytes_written));
- ASSERT_EQ(static_cast<size_t>(handler.data().size()), bytes_written);
+TEST(WavAudioHandlerTest, TestNoDataSectionIsOkay) {
+ // This one could go both ways. But for now, let's say that it's okay not
+ // to have a "data" section - just make sure everything is zeroed out as it
+ // should be.
+ auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
+ data[kDataHeaderIndex] = 'a';
+ data[kDataHeaderIndex + 1] = 'b';
+ data[kDataHeaderIndex + 2] = 'c';
+ data[kDataHeaderIndex + 3] = 'd';
+ auto handler =
+ WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
+ EXPECT_TRUE(handler);
+ ASSERT_EQ(2u, handler->num_channels());
+ ASSERT_EQ(16u, handler->bits_per_sample());
+ ASSERT_EQ(48000u, handler->sample_rate());
+ ASSERT_EQ(0u, handler->total_frames());
+ ASSERT_EQ(0u, handler->GetDuration().InMicroseconds());
+ ASSERT_EQ(0u, handler->data().size());
}
} // namespace media

Powered by Google App Engine
This is Rietveld 408576698