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
media / parsers / webp_parser_unittest.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include "base/base_paths.h"
#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/files/memory_mapped_file.h"
#include "base/path_service.h"
#include "media/parsers/vp8_parser.h"
#include "media/parsers/webp_parser.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
namespace {
constexpr size_t kWebPFileAndVp8ChunkHeaderSizeInBytes = 20u;
// clang-format off
constexpr uint8_t kLossyWebPFileHeader[] = {
'R', 'I', 'F', 'F',
0x0c, 0x00, 0x00, 0x00, // == 12 (little endian)
'W', 'E', 'B', 'P',
'V', 'P', '8', ' ',
0x00, 0x00, 0x00, 0x00 // == 0
};
constexpr base::span<const uint8_t> kLossyWebPEncodedData(
kLossyWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes);
constexpr base::span<const uint8_t> kInvalidWebPEncodedDataSize(
kLossyWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes - 5u);
constexpr uint8_t kLosslessWebPFileHeader[] = {
'R', 'I', 'F', 'F',
0x0c, 0x00, 0x00, 0x00,
'W', 'E', 'B', 'P',
'V', 'P', '8', 'L',
0x00, 0x00, 0x00, 0x00
};
constexpr base::span<const uint8_t> kLosslessWebPEncodedData(
kLosslessWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes);
constexpr uint8_t kExtendedWebPFileHeader[] = {
'R', 'I', 'F', 'F',
0x0c, 0x00, 0x00, 0x00,
'W', 'E', 'B', 'P',
'V', 'P', '8', 'X',
0x00, 0x00, 0x00, 0x00
};
constexpr base::span<const uint8_t> kExtendedWebPEncodedData(
kExtendedWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes);
constexpr uint8_t kUnknownWebPFileHeader[] = {
'R', 'I', 'F', 'F',
0x0c, 0x00, 0x00, 0x00,
'W', 'E', 'B', 'P',
'V', 'P', '8', '~',
0x00, 0x00, 0x00, 0x00
};
constexpr base::span<const uint8_t> kUnknownWebPEncodedData(
kUnknownWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes);
constexpr uint8_t kInvalidRiffWebPFileHeader[] = {
'X', 'I', 'F', 'F',
0x0c, 0x00, 0x00, 0x00,
'W', 'E', 'B', 'P',
'V', 'P', '8', ' ',
0x00, 0x00, 0x00, 0x00
};
constexpr base::span<const uint8_t> kInvalidRiffWebPEncodedData(
kInvalidRiffWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes);
constexpr uint8_t kInvalidOddFileSizeInWebPFileHeader[] = {
'R', 'I', 'F', 'F',
0x0d, 0x00, 0x00, 0x00, // == 13 (Invalid: should be even)
'W', 'E', 'B', 'P',
'V', 'P', '8', ' ',
0x00, 0x00, 0x00, 0x00,
0x00
};
constexpr base::span<const uint8_t> kInvalidOddFileSizeInHeaderWebPEncodedData(
kInvalidOddFileSizeInWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes + 1u); // Match the reported size
constexpr uint8_t kInvalidLargerThanLimitFileSizeInWebPFileHeader[] = {
'R', 'I', 'F', 'F',
0xfe, 0xff, 0xff, 0xff, // == 2^32 - 2 (Invalid: should be <= 2^32 - 10)
'W', 'E', 'B', 'P',
'V', 'P', '8', ' ',
0x00, 0x00, 0x00, 0x00
};
constexpr base::span<const uint8_t>
kInvalidLargerThanLimitFileSizeInHeaderWebPEncodedData(
kInvalidLargerThanLimitFileSizeInWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes);
constexpr uint8_t kInvalidLargerFileSizeInWebPFileHeader[] = {
'R', 'I', 'F', 'F',
0x10, 0x00, 0x00, 0x00, // == 16 (Invalid: should be 12)
'W', 'E', 'B', 'P',
'V', 'P', '8', ' ',
0x00, 0x00, 0x00, 0x00
};
constexpr base::span<const uint8_t>
kInvalidLargerFileSizeInHeaderWebPEncodedData(
kInvalidLargerFileSizeInWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes);
constexpr uint8_t kInvalidKeyFrameSizeInWebPFileHeader[] = {
'R', 'I', 'F', 'F',
0x0c, 0x00, 0x00, 0x00, // == 12
'W', 'E', 'B', 'P',
'V', 'P', '8', ' ',
0xc8, 0x00, 0x00, 0x00 // == 200 (Invalid: should be 0)
};
constexpr base::span<const uint8_t> kInvalidKeyFrameSizeInWebPEncodedData(
kInvalidKeyFrameSizeInWebPFileHeader,
kWebPFileAndVp8ChunkHeaderSizeInBytes);
constexpr uint8_t kMismatchingOddVp8FrameSizeAndDataSize[] = {
'R', 'I', 'F', 'F',
0x12, 0x00, 0x00, 0x00, // == 18
'W', 'E', 'B', 'P',
'V', 'P', '8', ' ',
0x03, 0x00, 0x00, 0x00, // == 3
0x11, 0xa0, 0x23, 0x00, // Valid padding byte
0xfa, 0xcc // Should not exist.
};
constexpr base::span<const uint8_t>
kMismatchingOddVp8FrameSizeAndDataSizeEncodedData(
kMismatchingOddVp8FrameSizeAndDataSize,
kWebPFileAndVp8ChunkHeaderSizeInBytes + 6u);
constexpr uint8_t kMismatchingEvenVp8FrameSizeAndDataSize[] = {
'R', 'I', 'F', 'F',
0x12, 0x00, 0x00, 0x00, // == 18
'W', 'E', 'B', 'P',
'V', 'P', '8', ' ',
0x04, 0x00, 0x00, 0x00, // == 4
0x11, 0xa0, 0x23, 0x12,
0xfc, 0xcd // Should not exist.
};
constexpr base::span<const uint8_t>
kMismatchingEvenVp8FrameSizeAndDataSizeEncodedData(
kMismatchingEvenVp8FrameSizeAndDataSize,
kWebPFileAndVp8ChunkHeaderSizeInBytes + 6u);
constexpr uint8_t kInvalidPaddingByteInVp8DataChunk[] = {
'R', 'I', 'F', 'F',
0x10, 0x00, 0x00, 0x00, // == 16
'W', 'E', 'B', 'P',
'V', 'P', '8', ' ',
0x03, 0x00, 0x00, 0x00, // == 3
0x11, 0xa0, 0x23, 0xff // Invalid: last byte should be 0
};
constexpr base::span<const uint8_t>
kInvalidPaddingByteInVp8DataChunkEncodedData(
kInvalidPaddingByteInVp8DataChunk,
kWebPFileAndVp8ChunkHeaderSizeInBytes + 4u);
// clang-format on
} // namespace
TEST(WebPParserTest, WebPImageFileValidator) {
// Verify that only lossy WebP formats pass.
ASSERT_TRUE(IsLossyWebPImage(kLossyWebPEncodedData));
// Verify that lossless, extended, and unknown WebP formats fail.
ASSERT_FALSE(IsLossyWebPImage(kLosslessWebPEncodedData));
ASSERT_FALSE(IsLossyWebPImage(kExtendedWebPEncodedData));
ASSERT_FALSE(IsLossyWebPImage(kUnknownWebPEncodedData));
// Verify that invalid WebP file headers and sizes fail.
ASSERT_FALSE(IsLossyWebPImage(kInvalidRiffWebPEncodedData));
ASSERT_FALSE(IsLossyWebPImage(kInvalidWebPEncodedDataSize));
}
TEST(WebPParserTest, ParseLossyWebP) {
base::FilePath data_dir;
ASSERT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &data_dir));
base::FilePath file_path = data_dir.AppendASCII("media")
.AppendASCII("test")
.AppendASCII("data")
.AppendASCII("red_green_gradient_lossy.webp");
base::MemoryMappedFile stream;
ASSERT_TRUE(stream.Initialize(file_path))
<< "Couldn't open stream file: " << file_path.MaybeAsASCII();
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(base::span<const uint8_t>(stream.data(), stream.length()));
ASSERT_TRUE(result);
ASSERT_TRUE(result->IsKeyframe());
ASSERT_TRUE(result->data);
// Original image is 3000x3000.
ASSERT_EQ(3000u, result->width);
ASSERT_EQ(3000u, result->height);
}
TEST(WebPParserTest, ParseLosslessWebP) {
base::FilePath data_dir;
ASSERT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &data_dir));
base::FilePath file_path =
data_dir.AppendASCII("media")
.AppendASCII("test")
.AppendASCII("data")
.AppendASCII("yellow_pink_gradient_lossless.webp");
base::MemoryMappedFile stream;
ASSERT_TRUE(stream.Initialize(file_path))
<< "Couldn't open stream file: " << file_path.MaybeAsASCII();
// Should fail because WebP parser does not parse lossless webp images.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(base::span<const uint8_t>(stream.data(), stream.length()));
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseExtendedWebP) {
base::FilePath data_dir;
ASSERT_TRUE(base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &data_dir));
base::FilePath file_path = data_dir.AppendASCII("media")
.AppendASCII("test")
.AppendASCII("data")
.AppendASCII("bouncy_ball.webp");
base::MemoryMappedFile stream;
ASSERT_TRUE(stream.Initialize(file_path))
<< "Couldn't open stream file: " << file_path.MaybeAsASCII();
// Should fail because WebP parser does not parse extended webp images.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(base::span<const uint8_t>(stream.data(), stream.length()));
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseWebPWithUnknownFormat) {
// Should fail when the specifier byte at position 16 holds anything but ' '.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(kUnknownWebPEncodedData);
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseWebPWithInvalidHeaders) {
// Should fail because the header is an invalid WebP container.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(kInvalidRiffWebPEncodedData);
ASSERT_FALSE(result);
// Should fail because the header has an invalid size.
result = ParseWebPImage(kInvalidWebPEncodedDataSize);
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseWebPWithInvalidOddSizeInHeader) {
// Should fail because the size reported in the header is odd.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(kInvalidOddFileSizeInHeaderWebPEncodedData);
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseWebPWithInvalidLargerThanLimitSizeInHeader) {
// Should fail because the size reported in the header is larger than
// 2^32 - 10 per the WebP spec.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(kInvalidLargerThanLimitFileSizeInHeaderWebPEncodedData);
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseWebPWithInvalidFileSizeInHeader) {
// Should fail because the size reported in the header does not match the
// actual data size.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(kInvalidLargerFileSizeInHeaderWebPEncodedData);
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseWebPWithEmptyVp8KeyFrameAndIncorrectKeyFrameSize) {
// Should fail because the reported VP8 key frame size is larger than the
// the existing data.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(kInvalidKeyFrameSizeInWebPEncodedData);
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseWebPWithMismatchingVp8FrameAndDataSize) {
// Should fail because the reported VP8 key frame size (even or odd) does not
// match the encoded data's size.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(kMismatchingOddVp8FrameSizeAndDataSizeEncodedData);
ASSERT_FALSE(result);
result = ParseWebPImage(kMismatchingEvenVp8FrameSizeAndDataSizeEncodedData);
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseWebPWithInvalidPaddingByteInVp8DataChunk) {
// Should fail because the reported VP8 key frame size is odd and the added
// padding byte is not 0.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(kInvalidPaddingByteInVp8DataChunkEncodedData);
ASSERT_FALSE(result);
}
TEST(WebPParserTest, ParseWebPWithEmptyVp8KeyFrame) {
// Should fail because the VP8 parser is passed a data chunk of size 0.
std::unique_ptr<Vp8FrameHeader> result =
ParseWebPImage(kLossyWebPEncodedData);
ASSERT_FALSE(result);
}
} // namespace media