Chromium Code Reviews| Index: media/base/container_names_unittest.cc |
| diff --git a/media/base/container_names_unittest.cc b/media/base/container_names_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..789b9e21c78246b518cb00ecd5d016bdec60c4cc |
| --- /dev/null |
| +++ b/media/base/container_names_unittest.cc |
| @@ -0,0 +1,215 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/file_util.h" |
| +#include "base/stringprintf.h" |
| +#include "media/base/container_names.h" |
| +#include "media/base/test_data_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace container_names { |
| + |
| +// Verify the container name matches the result |
|
xhwang
2013/05/06 23:51:27
nit: wrapping too early?
jrummell
2013/05/16 23:48:01
Done.
|
| +// based on the contents of a buffer. |
| +void ExpectContainer(FFmpegContainerName name, |
|
xhwang
2013/05/06 23:51:27
nit: make this function static.
jrummell
2013/05/16 23:48:01
Done.
|
| + char* buffer, |
| + size_t buffer_size) { |
| + EXPECT_EQ( |
| + name, |
| + DetermineContainer(reinterpret_cast<uint8_t*>(buffer), buffer_size)); |
| +} |
| + |
| +// Test that the names are in the correct sorted order. |
| +TEST(ContainerNamesTest, CheckSortOrder) { |
| + EXPECT_GT(container_names::ContainerNameMappingSize(), 0); |
| + for (int i = 1; i < ContainerNameMappingSize(); ++i) { |
| + EXPECT_LT(strcasecmp(ContainerNameMappingItem(i - 1).name, |
| + ContainerNameMappingItem(i).name), |
| + 0) |
| + << ContainerNameMappingItem(i - 1).name << " and " |
| + << ContainerNameMappingItem(i).name << " are in the wrong order."; |
| + } |
| +} |
| + |
| +// Test that string lookup works for all names in the list. |
| +TEST(ContainerNamesTest, NameLookup) { |
| + // Search for all strings. |
| + for (int i = 0; i < ContainerNameMappingSize(); ++i) { |
| + const char* name = ContainerNameMappingItem(i).name; |
| + EXPECT_EQ(ContainerNameMappingItem(i).id, LookupContainer(name)) |
| + << "Unable to lookup " << name; |
| + } |
| + |
| + // Check that some name not in the list fails. |
| + EXPECT_EQ(CONTAINER_UNKNOWN, LookupContainer("foo")); |
| + // A name before the first one in the list. |
| + EXPECT_EQ(CONTAINER_UNKNOWN, LookupContainer(" ")); |
| + // A name after the last one in the list. |
| + EXPECT_EQ(CONTAINER_UNKNOWN, LookupContainer("ZZZ")); |
| +} |
| + |
| +#define BYTE_ORDER_MARK "\xef\xbb\xbf" |
| + |
| +// Test that small buffers are handled correctly. |
| +TEST(ContainerNamesTest, CheckSmallBufferSize) { |
| + // Empty buffer. |
| + char buffer[0]; |
| + ExpectContainer(CONTAINER_UNKNOWN, buffer, 0); |
| + |
| + // Try a simple string |
| + char buffer1[] = "******** START SCRIPT ********"; |
| + ExpectContainer(CONTAINER_SUBVIEWER1, buffer1, strlen(buffer1)); |
| + |
| + // Try adding byte order mark at beginning. |
| + char buffer2[] = BYTE_ORDER_MARK "Interplay MVE File\x1A\x00\x1A"; |
| + ExpectContainer(CONTAINER_IPMOVIE, buffer2, 24); |
| + // Repeat but leave out a character (so it won't match) |
| + ExpectContainer(CONTAINER_UNKNOWN, buffer2, 23); |
| + |
| + // Try a simple SRT file. |
| + char buffer3[] = |
| + "1\n" |
| + "00:03:23,550 --> 00:03:24,375\n" |
| + "You always had a hard time finding your place in this world.\n" |
| + "\n" |
| + "2\n" |
| + "00:03:24,476 --> 00:03:25,175\n" |
| + "What are you talking about?\n"; |
| + ExpectContainer(CONTAINER_SRT, buffer3, strlen(buffer3)); |
| + |
| + // Try a simple tag. Note that the tag checking requires |
| + // at least 12 characters. |
| + char buffer4[12] = ".snd"; |
| + ExpectContainer(CONTAINER_AU, buffer4, sizeof(buffer4)); |
| + |
| + // HLS has it's own loop. |
| + char buffer5[] = "#EXTM3U" |
| + "some other random stuff" |
| + "#EXT-X-MEDIA-SEQUENCE:"; |
| + ExpectContainer(CONTAINER_HLS, buffer5, sizeof(buffer5)); |
| + |
| + // PJS has several loops. |
| + char buffer6[] = "1234567890, 123456, \"My subtitle.\"\n"; |
| + ExpectContainer(CONTAINER_PJS, buffer6, sizeof(buffer6)); |
| + // Repeat but leave out \n\0 at end. |
| + ExpectContainer(CONTAINER_PJS, buffer6, sizeof(buffer6) - 2); |
| + // Repeat but leave out \"\n\0 at end. |
| + ExpectContainer(CONTAINER_UNKNOWN, buffer6, sizeof(buffer6) - 3); |
| + |
| + // Try a large buffer all zeros. |
| + char buffer7[4096]; |
| + memset(buffer7, 0, sizeof(buffer7)); |
| + ExpectContainer(CONTAINER_UNKNOWN, buffer7, sizeof(buffer7)); |
| + |
| + // Reuse buffer, but all \n this time. |
| + memset(buffer7, '\n', sizeof(buffer7)); |
| + ExpectContainer(CONTAINER_UNKNOWN, buffer7, sizeof(buffer7)); |
| +} |
| + |
| +// Define the test data as string which should return the specfied |
| +// FFmpegContainerName. Length needs to be specified if the string |
| +// contains embedded \0 characters. |
| +struct Mapping { |
| + FFmpegContainerName id; |
| + int length; |
| + const char* name; |
| +}; |
| + |
| +// The following are the fixed strings compared in ContainerNames. |
| +// Since the first 4 characters are used as a TAG, this checks that the |
| +// TAG is defined correctly. |
| +const Mapping kFixedStrings[] = { |
| + { CONTAINER_AMR, 0, "#!AMR" }, |
| + { CONTAINER_APC, 0, "CRYO_APC" }, |
| + { CONTAINER_AQTITLE, 0, "-->> 23" }, |
| + { CONTAINER_ASF, 16, "\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00" |
| + "\xaa\x00\x62\xce\x6c" }, |
| + { CONTAINER_ASS, 0, "[Script Info]" }, |
| + { CONTAINER_ASS, 0, BYTE_ORDER_MARK "[Script Info]" }, |
| + { CONTAINER_CONCAT, 0, "ffconcat version 1.0" }, |
| + { CONTAINER_DNXHD, 43, "\x00\x00\x02\x80\x01 789*123456789*123456789" |
| + "*123456789*\x04\xd3" }, |
| + { CONTAINER_FFMETADATA, 0, ";FFMETADATA" }, |
| + { CONTAINER_IDF, 12, "\x04\x31\x2e\x34\x00\x00\x00\x00\x4f\x00\x15" |
| + "\x00" }, |
| + { CONTAINER_ILBC, 0, "#!iLBC" }, |
| + { CONTAINER_ISS, 0, "IMA_ADPCM_Sound" }, |
| + { CONTAINER_IV8, 0, "\x01\x01\x03\xb8\x80\x60" }, |
| + { CONTAINER_JV, 0, "JVxx Compression by John M Phillips Copyright " |
| + "(C) 1995 The Bitmap Brothers Ltd." }, |
| + { CONTAINER_LIBNUT, 0, "nut/multimedia container" }, |
| + { CONTAINER_LXF, 8, "LEITCH\x00\x00" }, |
| + { CONTAINER_NUV, 0, "NuppelVideo" }, |
| + { CONTAINER_NUV, 0, "MythTVVideo" }, |
| + { CONTAINER_PAF, 0, "Packed Animation File V1.0\n(c) 1992-96 " |
| + "Amazing Studio\x0a\x1a" }, |
| + { CONTAINER_REALTEXT, 0, "<window" }, |
| + { CONTAINER_REALTEXT, 0, BYTE_ORDER_MARK "<window" }, |
| + { CONTAINER_RPL, 0, "ARMovie\x0A" }, |
| + { CONTAINER_SAMI, 0, "<SAMI>" }, |
| + { CONTAINER_SAMI, 0, BYTE_ORDER_MARK "<SAMI>" }, |
| + { CONTAINER_SMJPEG, 8, "\x00\x0aSMJPEG" }, |
| + { CONTAINER_VPLAYER, 0, "12:34:56.789=" }, |
| + { CONTAINER_VOBSUB, 0, "# VobSub index file," }, |
| + { CONTAINER_VOC, 0, "Creative Voice File\x1A" }, |
| + { CONTAINER_W64, 42, "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1" |
| + "\x00\x00 89*1234wave\xf3\xac\xd3\x11\x8c\xd1" |
| + "\x00\xc0\x4f\x8e\xdb\x8a" }, |
| + { CONTAINER_WEBVTT, 0, "WEBVTT" }, |
| + { CONTAINER_WEBVTT, 0, BYTE_ORDER_MARK "WEBVTT" }, |
| + { CONTAINER_WTV, 16, "\xb7\xd8\x00\x20\x37\x49\xda\x11\xa6\x4e\x00" |
| + "\x07\xe9\x5e\xad\x8d" }, |
| + { CONTAINER_YUV4MPEGPIPE, 0, "YUV4MPEG2" } |
| +}; |
| + |
| +// Test that containers that start with fixed strings are handled correctly. |
| +// This is to verify that the TAG matches the first 4 characters of the string. |
| +TEST(ContainerNamesTest, CheckFixedStrings) { |
| + for (int i = 0; i < static_cast<int>(arraysize(kFixedStrings)); ++i) { |
| + char buffer[256]; |
| + int length = kFixedStrings[i].length; |
| + if (length == 0) |
| + length = strlen(kFixedStrings[i].name); |
| + memcpy(buffer, kFixedStrings[i].name, length); |
| + // Put some random characters after the string. |
| + buffer[length] = 'a'; |
| + buffer[length + 1] = 'b'; |
| + ExpectContainer(kFixedStrings[i].id, buffer, sizeof(buffer)); |
| + } |
| +} |
| + |
| +// Determine the container type of a specified file. |
| +void TestFile(FFmpegContainerName expected, const base::FilePath& filename) { |
| + char buffer[8192]; |
| + size_t read = file_util::ReadFile(filename, buffer, sizeof(buffer)); |
| + |
| + // Now verify the type. |
| + ExpectContainer(expected, buffer, read); |
| +} |
| + |
| +// Test several files to ensure that the container is detected properly. |
| +TEST(ContainerNamesTest, FileCheck) { |
| + TestFile(CONTAINER_OGG, media::GetTestDataFilePath("bear.ogv")); |
| + TestFile(CONTAINER_OGG, media::GetTestDataFilePath("9ch.ogg")); |
| + TestFile(CONTAINER_WAV, media::GetTestDataFilePath("4ch.wav")); |
| + TestFile(CONTAINER_WAV, media::GetTestDataFilePath("sfx_f32le.wav")); |
| + TestFile(CONTAINER_WAV, media::GetTestDataFilePath("sfx_s16le.wav")); |
| + TestFile(CONTAINER_MOV, media::GetTestDataFilePath("bear-1280x720.mp4")); |
| + TestFile(CONTAINER_MOV, media::GetTestDataFilePath("sfx.m4a")); |
| + TestFile(CONTAINER_WEBM, media::GetTestDataFilePath("bear-320x240.webm")); |
| + TestFile(CONTAINER_MP3, media::GetTestDataFilePath("id3_test.mp3")); |
| + TestFile(CONTAINER_MP3, media::GetTestDataFilePath("sfx.mp3")); |
| + |
| + // now try a few non containers |
| + TestFile(CONTAINER_UNKNOWN, media::GetTestDataFilePath("ten_byte_file")); |
| + TestFile(CONTAINER_UNKNOWN, media::GetTestDataFilePath("README")); |
| + TestFile(CONTAINER_UNKNOWN, |
| + media::GetTestDataFilePath("bali_640x360_P422.yuv")); |
| + TestFile(CONTAINER_UNKNOWN, |
| + media::GetTestDataFilePath("bali_640x360_RGB24.rgb")); |
| + TestFile(CONTAINER_UNKNOWN, |
| + media::GetTestDataFilePath("webm_vp8_track_entry")); |
| +} |
| + |
| +} // namespace container_names |