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

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: Address comments 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 std::string data(kTestAudioData, arraysize(kTestAudioData) - 1);
60 data[kChannelIndex] = '\x00';
61 data[kChannelIndex + 1] = '\x00';
62 auto handler = WavAudioHandler::Create(data);
63 EXPECT_FALSE(handler);
64 }
65
66 TEST(WavAudioHandlerTest, TestZeroBitsPerSampleIsNotValid) {
67 // Read in the sample data and modify the bits_per_sample field to hold
68 // |00|00|.
69 std::string data(kTestAudioData, arraysize(kTestAudioData) - 1);
70 data[kBitsPerSampleIndex] = '\x00';
71 data[kBitsPerSampleIndex + 1] = '\x00';
72 auto handler = WavAudioHandler::Create(data);
73 EXPECT_FALSE(handler);
74 }
75
76 TEST(WavAudioHandlerTest, TestZeroSamplesPerSecondIsNotValid) {
77 // Read in the sample data and modify the bits_per_sample field to hold
78 // |00|00|.
79 std::string data(kTestAudioData, arraysize(kTestAudioData) - 1);
80 data[kSampleRateIndex] = '\x00';
81 data[kSampleRateIndex + 1] = '\x00';
82 data[kSampleRateIndex + 2] = '\x00';
83 data[kSampleRateIndex + 3] = '\x00';
84 auto handler = WavAudioHandler::Create(data);
85 EXPECT_FALSE(handler);
86 }
87
88 TEST(WavAudioHandlerTest, TestTooBigTotalSizeIsOkay) {
89 // The size filed in the header should hold a very big number.
90 std::string data(kTestAudioData, arraysize(kTestAudioData) - 1);
91 data[kWavDataSizeIndex] = '\x00';
92 data[kWavDataSizeIndex + 1] = '\xFF';
93 data[kWavDataSizeIndex + 2] = '\xFF';
94 data[kWavDataSizeIndex + 3] = '\x00';
95 auto handler = WavAudioHandler::Create(data);
96 EXPECT_TRUE(handler);
97 ASSERT_EQ(2u, handler->num_channels());
98 ASSERT_EQ(16u, handler->bits_per_sample());
99 ASSERT_EQ(48000u, handler->sample_rate());
100 ASSERT_EQ(1u, handler->total_frames());
101 ASSERT_EQ(20u, handler->GetDuration().InMicroseconds());
102
103 ASSERT_EQ(4U, handler->data().size());
104 const char kData[] = "\x01\x00\x01\x00";
105 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler->data());
106 }
107
108 TEST(WavAudioHandlerTest, TestTooBigDataChunkSizeIsOkay) {
109 // If the |data| chunk size is last and it indicates it has more than it
110 // actually does, that's okay. Just consume the rest of the string. If it
111 // is not the last subsection, this file will parse badly.
112 std::string data(kTestAudioData, arraysize(kTestAudioData) - 1);
113 data[kDataHeaderIndex + 4] = '\x00';
114 data[kDataHeaderIndex + 5] = '\xFF';
115 data[kDataHeaderIndex + 6] = '\xFF';
116 data[kDataHeaderIndex + 7] = '\x00';
117 auto handler = WavAudioHandler::Create(data);
118 EXPECT_TRUE(handler);
119 ASSERT_EQ(2u, handler->num_channels());
120 ASSERT_EQ(16u, handler->bits_per_sample());
121 ASSERT_EQ(48000u, handler->sample_rate());
122 ASSERT_EQ(1u, handler->total_frames());
123 ASSERT_EQ(20u, handler->GetDuration().InMicroseconds());
124
125 ASSERT_EQ(4U, handler->data().size());
126 const char kData[] = "\x01\x00\x01\x00";
127 ASSERT_EQ(base::StringPiece(kData, arraysize(kData) - 1), handler->data());
128 }
129
130 TEST(WavAudioHandlerTest, TestTooSmallFormatSizeIsNotValid) {
131 // If the |data| chunk size is last and it indicates it has more than it
132 // actually does, that's okay. Just consume the rest of the string. If it
133 // is not the last subsection, this file will parse badly.
134 std::string data(kTestAudioData, arraysize(kTestAudioData) - 1);
135 data[kFormatHeaderIndex + 4] = '\x04';
136 data[kFormatHeaderIndex + 5] = '\x00';
137 data[kFormatHeaderIndex + 6] = '\x00';
138 data[kFormatHeaderIndex + 7] = '\x00';
139 auto handler = WavAudioHandler::Create(data);
140 EXPECT_FALSE(handler);
141 }
142
143 TEST(WavAudioHandlerTest, TestOtherSectionTypesIsOkay) {
144 // Append some other subsection header "abcd", the class should just consume
145 // and keep going.
146 std::string data(kTestAudioData, arraysize(kTestAudioData) - 1);
147 data.append("abcd\x04\x00\x00\x00\x01\x02\x03\x04");
148 data[kWavDataSizeIndex] += 4; // This should not overflow.
149 auto handler = WavAudioHandler::Create(data);
150 EXPECT_TRUE(handler);
151 }
152
153 TEST(WavAudioHandlerTest, TestNoFmtSectionIsNotValid) {
154 // Write over the "fmt " header. No valid handler should be returned.
155 std::string data(kTestAudioData, arraysize(kTestAudioData) - 1);
156 data[kFormatHeaderIndex] = 'a';
157 data[kFormatHeaderIndex + 1] = 'b';
158 data[kFormatHeaderIndex + 2] = 'c';
159 data[kFormatHeaderIndex + 3] = 'd';
160 auto handler = WavAudioHandler::Create(data);
161 EXPECT_FALSE(handler);
162 }
163
164 TEST(WavAudioHandlerTest, TestNoDataSectionIsOkay) {
165 // This one could go both ways. But for now, let's say that it's okay not
166 // to have a "data" section - just make sure everything is zeroed out as it
167 // should be.
168 std::string data(kTestAudioData, arraysize(kTestAudioData) - 1);
169 data[kDataHeaderIndex] = 'a';
170 data[kDataHeaderIndex + 1] = 'b';
171 data[kDataHeaderIndex + 2] = 'c';
172 data[kDataHeaderIndex + 3] = 'd';
173 auto handler = WavAudioHandler::Create(data);
174 EXPECT_TRUE(handler);
175 ASSERT_EQ(2u, handler->num_channels());
176 ASSERT_EQ(16u, handler->bits_per_sample());
177 ASSERT_EQ(48000u, handler->sample_rate());
178 ASSERT_EQ(0u, handler->total_frames());
179 ASSERT_EQ(0u, handler->GetDuration().InMicroseconds());
180 ASSERT_EQ(0u, handler->data().size());
35 } 181 }
36 182
37 } // namespace media 183 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698