Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string.h> | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "media/mp4/box_reader.h" | |
| 11 #include "media/mp4/rcheck.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace media { | |
| 15 namespace mp4 { | |
| 16 | |
| 17 const static uint8 kTestBox[] = { | |
| 18 // Test box containing three children | |
| 19 0x00, 0x00, 0x00, 0x40, 't', 'e', 's', 't', | |
| 20 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | |
| 21 0xf9, 0x0a, 0x0b, 0x0c, 0xfd, 0x0e, 0x0f, 0x10, | |
| 22 // Ordinary child box | |
| 23 0x00, 0x00, 0x00, 0x0c, 'c', 'h', 'l', 'd', 0xde, 0xad, 0xbe, 0xef, | |
| 24 // Extended-size child box | |
| 25 0x00, 0x00, 0x00, 0x01, 'c', 'h', 'l', 'd', | |
| 26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, | |
| 27 0xfa, 0xce, 0xca, 0xfe, | |
| 28 // Empty box | |
| 29 0x00, 0x00, 0x00, 0x08, 'm', 'p', 't', 'y', | |
| 30 // Trailing garbage | |
| 31 0x00 }; | |
| 32 | |
| 33 struct EmptyBox { | |
| 34 }; | |
| 35 | |
| 36 struct ChildBox { | |
| 37 uint32 val; | |
| 38 }; | |
| 39 | |
| 40 struct TestBox { | |
| 41 uint8 a, b; | |
| 42 uint16 c; | |
| 43 int32 d; | |
| 44 int64 e; | |
| 45 | |
| 46 std::vector<ChildBox> kids; | |
| 47 EmptyBox mpty; | |
| 48 | |
| 49 TestBox(); | |
| 50 ~TestBox(); | |
| 51 }; | |
| 52 | |
| 53 TestBox::TestBox() {} | |
| 54 TestBox::~TestBox() {} | |
| 55 | |
| 56 bool Parse(BoxReader* r, EmptyBox* mpty) { | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 bool Parse(BoxReader* r, ChildBox* chld) { | |
| 61 return r->Read(&chld->val); | |
| 62 } | |
| 63 | |
| 64 bool Parse(BoxReader* r, TestBox* test) { | |
| 65 RCHECK(r->ReadFullBoxHeader() && | |
| 66 r->Read(&test->a) && | |
| 67 r->Read(&test->b) && | |
| 68 r->Read(&test->c) && | |
| 69 r->Read(&test->d) && | |
| 70 r->Read4(&test->e)); | |
| 71 return r->ScanChildren() && | |
| 72 r->ReadChildren(&test->kids) && | |
| 73 r->MaybeReadChild(&test->mpty); | |
| 74 } | |
| 75 | |
| 76 template<> FourCC GetBoxType<EmptyBox>() { return FOURCC_MPTY; } | |
| 77 template<> FourCC GetBoxType<ChildBox>() { return FOURCC_CHLD; } | |
| 78 template<> FourCC GetBoxType<TestBox>() { return FOURCC_TEST; } | |
| 79 | |
| 80 class BoxReaderTest : public testing::Test { | |
| 81 protected: | |
| 82 std::vector<uint8> GetBuf() { | |
| 83 return std::vector<uint8>(kTestBox, kTestBox + sizeof(kTestBox)); | |
| 84 } | |
| 85 }; | |
| 86 | |
| 87 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.
| |
| 88 std::vector<uint8> buf = GetBuf(); | |
| 89 bool err; | |
| 90 scoped_ptr<BoxReader> r( | |
| 91 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); | |
| 92 EXPECT_FALSE(err); | |
| 93 EXPECT_TRUE(r.get()); | |
| 94 | |
| 95 TestBox b; | |
| 96 EXPECT_TRUE(Parse(r.get(), &b)); | |
| 97 EXPECT_EQ(0x01, r->version()); | |
| 98 EXPECT_EQ(0x020304u, r->flags()); | |
| 99 EXPECT_EQ(0x05, b.a); | |
| 100 EXPECT_EQ(0x06, b.b); | |
| 101 EXPECT_EQ(0x0708, b.c); | |
| 102 EXPECT_EQ((int32) 0xf90a0b0c, b.d); | |
| 103 EXPECT_EQ((int32) 0xfd0e0f10, b.e); | |
| 104 | |
| 105 EXPECT_EQ(2u, b.kids.size()); | |
| 106 EXPECT_EQ(0xdeadbeef, b.kids[0].val); | |
| 107 EXPECT_EQ(0xfacecafe, b.kids[1].val); | |
| 108 | |
| 109 // Accounting for the extra byte outside of the box above | |
| 110 EXPECT_EQ(buf.size(), r->size() + 1); | |
| 111 } | |
| 112 | |
| 113 TEST_F(BoxReaderTest, OuterTooShortTest) { | |
| 114 std::vector<uint8> buf = GetBuf(); | |
| 115 bool err; | |
| 116 | |
| 117 // Create a soft failure by truncating the outer box. | |
| 118 scoped_ptr<BoxReader> r( | |
| 119 BoxReader::ReadTopLevelBox(&buf[0], buf.size() - 2, &err)); | |
| 120 | |
| 121 EXPECT_FALSE(err); | |
| 122 EXPECT_FALSE(r.get()); | |
| 123 } | |
| 124 | |
| 125 TEST_F(BoxReaderTest, InnerTooLongTest) { | |
| 126 std::vector<uint8> buf = GetBuf(); | |
| 127 bool err; | |
| 128 | |
| 129 // Make an inner box too big for its outer box. | |
| 130 buf[25] = 1; | |
| 131 scoped_ptr<BoxReader> r( | |
| 132 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); | |
| 133 | |
| 134 TestBox b; | |
| 135 EXPECT_FALSE(Parse(r.get(), &b)); | |
| 136 } | |
| 137 | |
| 138 TEST_F(BoxReaderTest, WrongFourCCTest) { | |
| 139 std::vector<uint8> buf = GetBuf(); | |
| 140 bool err; | |
| 141 | |
| 142 // Use an unknown FourCC both on an outer box and an inner one. | |
| 143 buf[5] = 1; | |
| 144 buf[28] = 1; | |
| 145 scoped_ptr<BoxReader> r( | |
| 146 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); | |
| 147 | |
| 148 TestBox b; | |
| 149 std::vector<ChildBox> kids; | |
| 150 // This should still work; the outer box reader doesn't care about the FourCC, | |
| 151 // since it assumes you've already examined it before deciding what to parse. | |
| 152 EXPECT_TRUE(Parse(r.get(), &b)); | |
| 153 EXPECT_EQ(0x74017374u, r->type()); | |
| 154 // Parsing the TestBox should have left the modified inner box unread, which | |
| 155 // we collect here. | |
| 156 EXPECT_TRUE(r->ReadAllChildren(&kids)); | |
| 157 EXPECT_EQ(1u, kids.size()); | |
| 158 EXPECT_EQ(0xdeadbeef, kids[0].val); | |
| 159 } | |
| 160 | |
| 161 TEST_F(BoxReaderTest, ChildrenTest) { | |
| 162 std::vector<uint8> buf = GetBuf(); | |
| 163 bool err; | |
| 164 scoped_ptr<BoxReader> r( | |
| 165 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); | |
| 166 | |
| 167 EXPECT_TRUE(r->SkipBytes(16) && r->ScanChildren()); | |
| 168 | |
| 169 EmptyBox mpty; | |
| 170 EXPECT_TRUE(r->ReadChild(&mpty)); | |
| 171 EXPECT_FALSE(r->ReadChild(&mpty)); | |
| 172 EXPECT_TRUE(r->MaybeReadChild(&mpty)); | |
| 173 | |
| 174 std::vector<ChildBox> kids; | |
| 175 | |
| 176 EXPECT_TRUE(r->ReadAllChildren(&kids)); | |
| 177 EXPECT_EQ(2u, kids.size()); | |
| 178 kids.clear(); | |
| 179 EXPECT_FALSE(r->ReadChildren(&kids)); | |
| 180 EXPECT_TRUE(r->MaybeReadChildren(&kids)); | |
| 181 } | |
| 182 | |
| 183 } // namespace mp4 | |
| 184 } // namespace media | |
| OLD | NEW |