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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_piece.h" 8 #include "base/strings/string_piece.h"
9 #include "media/audio/sounds/test_data.h" 9 #include "media/audio/sounds/test_data.h"
10 #include "media/audio/sounds/wav_audio_handler.h" 10 #include "media/audio/sounds/wav_audio_handler.h"
11 #include "media/base/audio_bus.h" 11 #include "media/base/audio_bus.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace media { 14 namespace media {
15 namespace {
16 // WAV header comes first in the test data.
17 const size_t kWavHeaderSize = 12;
18 const size_t kWavDataSizeIndex = 4;
19
20 // "fmt " header comes next.
21 const size_t kFormatHeaderIndex = kWavHeaderSize;
22 const size_t kFormatHeaderSize = 8;
23 const size_t kFormatPayloadSize = 16;
24 const size_t kChannelIndex = kWavHeaderSize + kFormatHeaderSize + 2;
25 const size_t kBitsPerSampleIndex = kWavHeaderSize + kFormatHeaderSize + 14;
26 const size_t kSampleRateIndex = kWavHeaderSize + kFormatHeaderSize + 4;
27
28 // "data" header comes last.
29 const size_t kDataHeaderIndex =
30 kWavHeaderSize + kFormatHeaderSize + kFormatPayloadSize;
31
32 } // namespace
15 33
16 TEST(WavAudioHandlerTest, SampleDataTest) { 34 TEST(WavAudioHandlerTest, SampleDataTest) {
17 WavAudioHandler handler(base::StringPiece(kTestAudioData, 35 auto handler = WavAudioHandler::Create(
18 arraysize(kTestAudioData))); 36 base::StringPiece(kTestAudioData, arraysize(kTestAudioData) - 1));
19 ASSERT_EQ(2u, handler.num_channels()); 37 ASSERT_TRUE(handler);
20 ASSERT_EQ(16u, handler.bits_per_sample()); 38 ASSERT_EQ(2u, handler->num_channels());
21 ASSERT_EQ(48000u, handler.sample_rate()); 39 ASSERT_EQ(16u, handler->bits_per_sample());
22 ASSERT_EQ(1u, handler.total_frames()); 40 ASSERT_EQ(48000u, handler->sample_rate());
23 ASSERT_EQ(20u, handler.GetDuration().InMicroseconds()); 41 ASSERT_EQ(1u, handler->total_frames());
42 ASSERT_EQ(20u, handler->GetDuration().InMicroseconds());
24 43
25 ASSERT_EQ(4U, handler.data().size()); 44 ASSERT_EQ(4U, handler->data().size());
26 const char kData[] = "\x01\x00\x01\x00"; 45 const char kData[] = "\x01\x00\x01\x00";
27 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler.data()); 46 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler->data());
28 47
29 scoped_ptr<AudioBus> bus = AudioBus::Create( 48 scoped_ptr<AudioBus> bus =
30 handler.num_channels(), handler.data().size() / handler.num_channels()); 49 AudioBus::Create(handler->num_channels(),
50 handler->data().size() / handler->num_channels());
31 51
32 size_t bytes_written; 52 size_t bytes_written = 0u;
33 ASSERT_TRUE(handler.CopyTo(bus.get(), 0, &bytes_written)); 53 ASSERT_TRUE(handler->CopyTo(bus.get(), 0, &bytes_written));
34 ASSERT_EQ(static_cast<size_t>(handler.data().size()), bytes_written); 54 ASSERT_EQ(static_cast<size_t>(handler->data().size()), bytes_written);
55 }
56
57 TEST(WavAudioHandlerTest, TestZeroChannelsIsNotValid) {
58 // Read in the sample data and modify the channel field to hold |00|00|.
59 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.
60 data[kChannelIndex] = '\x00';
61 data[kChannelIndex + 1] = '\x00';
62 auto handler =
63 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
64 EXPECT_FALSE(handler);
65 }
66
67 TEST(WavAudioHandlerTest, TestZeroBitsPerSampleIsNotValid) {
68 // Read in the sample data and modify the bits_per_sample field to hold
69 // |00|00|.
70 auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
71 data[kBitsPerSampleIndex] = '\x00';
72 data[kBitsPerSampleIndex + 1] = '\x00';
73 auto handler =
74 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
75 EXPECT_FALSE(handler);
76 }
77
78 TEST(WavAudioHandlerTest, TestZeroSamplesPerSecondIsNotValid) {
79 // Read in the sample data and modify the bits_per_sample field to hold
80 // |00|00|.
81 auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
82 data[kSampleRateIndex] = '\x00';
83 data[kSampleRateIndex + 1] = '\x00';
84 data[kSampleRateIndex + 2] = '\x00';
85 data[kSampleRateIndex + 3] = '\x00';
86 auto handler =
87 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
88 EXPECT_FALSE(handler);
89 }
90
91 TEST(WavAudioHandlerTest, TestTooBigTotalSizeIsOkay) {
92 // The size filed in the header should hold a very big number.
93 auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
94 data[kWavDataSizeIndex] = '\x00';
95 data[kWavDataSizeIndex + 1] = '\xFF';
96 data[kWavDataSizeIndex + 2] = '\xFF';
97 data[kWavDataSizeIndex + 3] = '\x00';
98 auto handler =
99 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
100 EXPECT_TRUE(handler);
101 ASSERT_EQ(2u, handler->num_channels());
102 ASSERT_EQ(16u, handler->bits_per_sample());
103 ASSERT_EQ(48000u, handler->sample_rate());
104 ASSERT_EQ(1u, handler->total_frames());
105 ASSERT_EQ(20u, handler->GetDuration().InMicroseconds());
106
107 ASSERT_EQ(4U, handler->data().size());
108 const char kData[] = "\x01\x00\x01\x00";
109 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler->data());
110 }
111
112 TEST(WavAudioHandlerTest, TestTooBigDataChunkSizeIsOkay) {
113 // If the |data| chunk size is last and it indicates it has more than it
114 // actually does, that's okay. Just consume the rest of the string. If it
115 // is not the last subsection, this file will parse badly.
116 auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
117 data[kDataHeaderIndex + 4] = '\x00';
118 data[kDataHeaderIndex + 5] = '\xFF';
119 data[kDataHeaderIndex + 6] = '\xFF';
120 data[kDataHeaderIndex + 7] = '\x00';
121 auto handler =
122 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
123 EXPECT_TRUE(handler);
124 ASSERT_EQ(2u, handler->num_channels());
125 ASSERT_EQ(16u, handler->bits_per_sample());
126 ASSERT_EQ(48000u, handler->sample_rate());
127 ASSERT_EQ(1u, handler->total_frames());
128 ASSERT_EQ(20u, handler->GetDuration().InMicroseconds());
129
130 ASSERT_EQ(4U, handler->data().size());
131 const char kData[] = "\x01\x00\x01\x00";
132 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler->data());
133 }
134
135 TEST(WavAudioHandlerTest, TestTooSmallFormatSizeIsNotValid) {
136 // If the |data| chunk size is last and it indicates it has more than it
137 // actually does, that's okay. Just consume the rest of the string. If it
138 // is not the last subsection, this file will parse badly.
139 auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
140 data[kFormatHeaderIndex + 4] = '\x04';
141 data[kFormatHeaderIndex + 5] = '\x00';
142 data[kFormatHeaderIndex + 6] = '\x00';
143 data[kFormatHeaderIndex + 7] = '\x00';
144 auto handler =
145 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
146 EXPECT_FALSE(handler);
147 }
148
149 TEST(WavAudioHandlerTest, TestOtherSectionTypesIsOkay) {
150 // Append some other subsection header "abcd", the class should just consume
151 // and keep going.
152 auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
153 data.append("abcd\x04\x00\x00\x00\x01\x02\x03\x04");
154 data[kWavDataSizeIndex] += 4; // This should not overflow.
155 auto handler =
156 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
157 EXPECT_TRUE(handler);
158 }
159
160 TEST(WavAudioHandlerTest, TestNoFmtSectionIsNotValid) {
161 // Write over the "fmt " header. No valid handler should be returned.
162 auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
163 data[kFormatHeaderIndex] = 'a';
164 data[kFormatHeaderIndex + 1] = 'b';
165 data[kFormatHeaderIndex + 2] = 'c';
166 data[kFormatHeaderIndex + 3] = 'd';
167 auto handler =
168 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
169 EXPECT_FALSE(handler);
170 }
171
172 TEST(WavAudioHandlerTest, TestNoDataSectionIsOkay) {
173 // This one could go both ways. But for now, let's say that it's okay not
174 // to have a "data" section - just make sure everything is zeroed out as it
175 // should be.
176 auto data = std::string(kTestAudioData, arraysize(kTestAudioData) - 1);
177 data[kDataHeaderIndex] = 'a';
178 data[kDataHeaderIndex + 1] = 'b';
179 data[kDataHeaderIndex + 2] = 'c';
180 data[kDataHeaderIndex + 3] = 'd';
181 auto handler =
182 WavAudioHandler::Create(base::StringPiece(data.c_str(), data.size()));
183 EXPECT_TRUE(handler);
184 ASSERT_EQ(2u, handler->num_channels());
185 ASSERT_EQ(16u, handler->bits_per_sample());
186 ASSERT_EQ(48000u, handler->sample_rate());
187 ASSERT_EQ(0u, handler->total_frames());
188 ASSERT_EQ(0u, handler->GetDuration().InMicroseconds());
189 ASSERT_EQ(0u, handler->data().size());
35 } 190 }
36 191
37 } // namespace media 192 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698