Chromium Code Reviews| Index: media/mp4/box_reader_unittest.cc |
| diff --git a/media/mp4/box_reader_unittest.cc b/media/mp4/box_reader_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d9e0a05da0f2dfc9a78afbc4f557c7cfc464980b |
| --- /dev/null |
| +++ b/media/mp4/box_reader_unittest.cc |
| @@ -0,0 +1,184 @@ |
| +// Copyright (c) 2012 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 <string.h> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "media/mp4/box_reader.h" |
| +#include "media/mp4/rcheck.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace media { |
| +namespace mp4 { |
| + |
| +const static uint8 kTestBox[] = { |
| + // Test box containing three children |
| + 0x00, 0x00, 0x00, 0x40, 't', 'e', 's', 't', |
| + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| + 0xf9, 0x0a, 0x0b, 0x0c, 0xfd, 0x0e, 0x0f, 0x10, |
| + // Ordinary child box |
| + 0x00, 0x00, 0x00, 0x0c, 'c', 'h', 'l', 'd', 0xde, 0xad, 0xbe, 0xef, |
| + // Extended-size child box |
| + 0x00, 0x00, 0x00, 0x01, 'c', 'h', 'l', 'd', |
| + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, |
| + 0xfa, 0xce, 0xca, 0xfe, |
| + // Empty box |
| + 0x00, 0x00, 0x00, 0x08, 'm', 'p', 't', 'y', |
| + // Trailing garbage |
| + 0x00 }; |
| + |
| +struct EmptyBox { |
| +}; |
| + |
| +struct ChildBox { |
| + uint32 val; |
| +}; |
| + |
| +struct TestBox { |
| + uint8 a, b; |
| + uint16 c; |
| + int32 d; |
| + int64 e; |
| + |
| + std::vector<ChildBox> kids; |
| + EmptyBox mpty; |
| + |
| + TestBox(); |
| + ~TestBox(); |
| +}; |
| + |
| +TestBox::TestBox() {} |
| +TestBox::~TestBox() {} |
| + |
| +bool Parse(BoxReader* r, EmptyBox* mpty) { |
| + return true; |
| +} |
| + |
| +bool Parse(BoxReader* r, ChildBox* chld) { |
| + return r->Read(&chld->val); |
| +} |
| + |
| +bool Parse(BoxReader* r, TestBox* test) { |
| + RCHECK(r->ReadFullBoxHeader() && |
| + r->Read(&test->a) && |
| + r->Read(&test->b) && |
| + r->Read(&test->c) && |
| + r->Read(&test->d) && |
| + r->Read4(&test->e)); |
| + return r->ScanChildren() && |
| + r->ReadChildren(&test->kids) && |
| + r->MaybeReadChild(&test->mpty); |
| +} |
| + |
| +template<> FourCC GetBoxType<EmptyBox>() { return FOURCC_MPTY; } |
| +template<> FourCC GetBoxType<ChildBox>() { return FOURCC_CHLD; } |
| +template<> FourCC GetBoxType<TestBox>() { return FOURCC_TEST; } |
| + |
| +class BoxReaderTest : public testing::Test { |
| + protected: |
| + std::vector<uint8> GetBuf() { |
| + return std::vector<uint8>(kTestBox, kTestBox + sizeof(kTestBox)); |
| + } |
| +}; |
| + |
| +TEST_F(BoxReaderTest, BoringTest) { |
|
acolwell GONE FROM CHROMIUM
2012/06/07 19:44:02
Please use a more descriptive name
strobe_
2012/06/07 22:04:22
Done.
|
| + std::vector<uint8> buf = GetBuf(); |
| + bool err; |
| + scoped_ptr<BoxReader> r( |
| + BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
| + EXPECT_FALSE(err); |
| + EXPECT_TRUE(r.get()); |
| + |
| + TestBox b; |
| + EXPECT_TRUE(Parse(r.get(), &b)); |
| + EXPECT_EQ(0x01, r->version()); |
| + EXPECT_EQ(0x020304u, r->flags()); |
| + EXPECT_EQ(0x05, b.a); |
| + EXPECT_EQ(0x06, b.b); |
| + EXPECT_EQ(0x0708, b.c); |
| + EXPECT_EQ((int32) 0xf90a0b0c, b.d); |
| + EXPECT_EQ((int32) 0xfd0e0f10, b.e); |
| + |
| + EXPECT_EQ(2u, b.kids.size()); |
| + EXPECT_EQ(0xdeadbeef, b.kids[0].val); |
| + EXPECT_EQ(0xfacecafe, b.kids[1].val); |
| + |
| + // Accounting for the extra byte outside of the box above |
| + EXPECT_EQ(buf.size(), r->size() + 1); |
| +} |
| + |
| +TEST_F(BoxReaderTest, OuterTooShortTest) { |
| + std::vector<uint8> buf = GetBuf(); |
| + bool err; |
| + |
| + // Create a soft failure by truncating the outer box. |
| + scoped_ptr<BoxReader> r( |
| + BoxReader::ReadTopLevelBox(&buf[0], buf.size() - 2, &err)); |
| + |
| + EXPECT_FALSE(err); |
| + EXPECT_FALSE(r.get()); |
| +} |
| + |
| +TEST_F(BoxReaderTest, InnerTooLongTest) { |
| + std::vector<uint8> buf = GetBuf(); |
| + bool err; |
| + |
| + // Make an inner box too big for its outer box. |
| + buf[25] = 1; |
| + scoped_ptr<BoxReader> r( |
| + BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
| + |
| + TestBox b; |
| + EXPECT_FALSE(Parse(r.get(), &b)); |
| +} |
| + |
| +TEST_F(BoxReaderTest, WrongFourCCTest) { |
| + std::vector<uint8> buf = GetBuf(); |
| + bool err; |
| + |
| + // Use an unknown FourCC both on an outer box and an inner one. |
| + buf[5] = 1; |
| + buf[28] = 1; |
| + scoped_ptr<BoxReader> r( |
| + BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
| + |
| + TestBox b; |
| + std::vector<ChildBox> kids; |
| + // This should still work; the outer box reader doesn't care about the FourCC, |
| + // since it assumes you've already examined it before deciding what to parse. |
| + EXPECT_TRUE(Parse(r.get(), &b)); |
| + EXPECT_EQ(0x74017374u, r->type()); |
| + // Parsing the TestBox should have left the modified inner box unread, which |
| + // we collect here. |
| + EXPECT_TRUE(r->ReadAllChildren(&kids)); |
| + EXPECT_EQ(1u, kids.size()); |
| + EXPECT_EQ(0xdeadbeef, kids[0].val); |
| +} |
| + |
| +TEST_F(BoxReaderTest, ChildrenTest) { |
| + std::vector<uint8> buf = GetBuf(); |
| + bool err; |
| + scoped_ptr<BoxReader> r( |
| + BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
| + |
| + EXPECT_TRUE(r->SkipBytes(16) && r->ScanChildren()); |
| + |
| + EmptyBox mpty; |
| + EXPECT_TRUE(r->ReadChild(&mpty)); |
| + EXPECT_FALSE(r->ReadChild(&mpty)); |
| + EXPECT_TRUE(r->MaybeReadChild(&mpty)); |
| + |
| + std::vector<ChildBox> kids; |
| + |
| + EXPECT_TRUE(r->ReadAllChildren(&kids)); |
| + EXPECT_EQ(2u, kids.size()); |
| + kids.clear(); |
| + EXPECT_FALSE(r->ReadChildren(&kids)); |
| + EXPECT_TRUE(r->MaybeReadChildren(&kids)); |
| +} |
| + |
| +} // namespace mp4 |
| +} // namespace media |