| 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 static const 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 : Box { |
| 34 virtual bool Parse(BoxReader* reader) OVERRIDE { |
| 35 return true; |
| 36 } |
| 37 virtual FourCC BoxType() const OVERRIDE { return FOURCC_MPTY; } |
| 38 }; |
| 39 |
| 40 struct ChildBox : Box { |
| 41 uint32 val; |
| 42 |
| 43 virtual bool Parse(BoxReader* reader) OVERRIDE { |
| 44 return reader->Read4(&val); |
| 45 } |
| 46 virtual FourCC BoxType() const OVERRIDE { return FOURCC_CHLD; } |
| 47 }; |
| 48 |
| 49 struct TestBox : Box { |
| 50 uint8 a, b; |
| 51 uint16 c; |
| 52 int32 d; |
| 53 int64 e; |
| 54 |
| 55 std::vector<ChildBox> kids; |
| 56 EmptyBox mpty; |
| 57 |
| 58 virtual bool Parse(BoxReader* reader) OVERRIDE { |
| 59 RCHECK(reader->ReadFullBoxHeader() && |
| 60 reader->Read1(&a) && |
| 61 reader->Read1(&b) && |
| 62 reader->Read2(&c) && |
| 63 reader->Read4s(&d) && |
| 64 reader->Read4sInto8s(&e)); |
| 65 return reader->ScanChildren() && |
| 66 reader->ReadChildren(&kids) && |
| 67 reader->MaybeReadChild(&mpty); |
| 68 } |
| 69 virtual FourCC BoxType() const OVERRIDE { return FOURCC_TEST; } |
| 70 |
| 71 TestBox(); |
| 72 ~TestBox(); |
| 73 }; |
| 74 |
| 75 TestBox::TestBox() {} |
| 76 TestBox::~TestBox() {} |
| 77 |
| 78 class BoxReaderTest : public testing::Test { |
| 79 protected: |
| 80 std::vector<uint8> GetBuf() { |
| 81 return std::vector<uint8>(kTestBox, kTestBox + sizeof(kTestBox)); |
| 82 } |
| 83 }; |
| 84 |
| 85 TEST_F(BoxReaderTest, ExpectedOperationTest) { |
| 86 std::vector<uint8> buf = GetBuf(); |
| 87 bool err; |
| 88 scoped_ptr<BoxReader> reader( |
| 89 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
| 90 EXPECT_FALSE(err); |
| 91 EXPECT_TRUE(reader.get()); |
| 92 |
| 93 TestBox box; |
| 94 EXPECT_TRUE(box.Parse(reader.get())); |
| 95 EXPECT_EQ(0x01, reader->version()); |
| 96 EXPECT_EQ(0x020304u, reader->flags()); |
| 97 EXPECT_EQ(0x05, box.a); |
| 98 EXPECT_EQ(0x06, box.b); |
| 99 EXPECT_EQ(0x0708, box.c); |
| 100 EXPECT_EQ(static_cast<int32>(0xf90a0b0c), box.d); |
| 101 EXPECT_EQ(static_cast<int32>(0xfd0e0f10), box.e); |
| 102 |
| 103 EXPECT_EQ(2u, box.kids.size()); |
| 104 EXPECT_EQ(0xdeadbeef, box.kids[0].val); |
| 105 EXPECT_EQ(0xfacecafe, box.kids[1].val); |
| 106 |
| 107 // Accounting for the extra byte outside of the box above |
| 108 EXPECT_EQ(buf.size(), static_cast<uint64>(reader->size() + 1)); |
| 109 } |
| 110 |
| 111 TEST_F(BoxReaderTest, OuterTooShortTest) { |
| 112 std::vector<uint8> buf = GetBuf(); |
| 113 bool err; |
| 114 |
| 115 // Create a soft failure by truncating the outer box. |
| 116 scoped_ptr<BoxReader> r( |
| 117 BoxReader::ReadTopLevelBox(&buf[0], buf.size() - 2, &err)); |
| 118 |
| 119 EXPECT_FALSE(err); |
| 120 EXPECT_FALSE(r.get()); |
| 121 } |
| 122 |
| 123 TEST_F(BoxReaderTest, InnerTooLongTest) { |
| 124 std::vector<uint8> buf = GetBuf(); |
| 125 bool err; |
| 126 |
| 127 // Make an inner box too big for its outer box. |
| 128 buf[25] = 1; |
| 129 scoped_ptr<BoxReader> reader( |
| 130 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
| 131 |
| 132 TestBox box; |
| 133 EXPECT_FALSE(box.Parse(reader.get())); |
| 134 } |
| 135 |
| 136 TEST_F(BoxReaderTest, WrongFourCCTest) { |
| 137 std::vector<uint8> buf = GetBuf(); |
| 138 bool err; |
| 139 |
| 140 // Use an unknown FourCC both on an outer box and an inner one. |
| 141 buf[5] = 1; |
| 142 buf[28] = 1; |
| 143 scoped_ptr<BoxReader> reader( |
| 144 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
| 145 |
| 146 TestBox box; |
| 147 std::vector<ChildBox> kids; |
| 148 // This should still work; the outer box reader doesn't care about the FourCC, |
| 149 // since it assumes you've already examined it before deciding what to parse. |
| 150 EXPECT_TRUE(box.Parse(reader.get())); |
| 151 EXPECT_EQ(0x74017374, reader->type()); |
| 152 // Parsing the TestBox should have left the modified inner box unread, which |
| 153 // we collect here. |
| 154 EXPECT_TRUE(reader->ReadAllChildren(&kids)); |
| 155 EXPECT_EQ(1u, kids.size()); |
| 156 EXPECT_EQ(0xdeadbeef, kids[0].val); |
| 157 } |
| 158 |
| 159 TEST_F(BoxReaderTest, ChildrenTest) { |
| 160 std::vector<uint8> buf = GetBuf(); |
| 161 bool err; |
| 162 scoped_ptr<BoxReader> reader( |
| 163 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
| 164 |
| 165 EXPECT_TRUE(reader->SkipBytes(16) && reader->ScanChildren()); |
| 166 |
| 167 EmptyBox mpty; |
| 168 EXPECT_TRUE(reader->ReadChild(&mpty)); |
| 169 EXPECT_FALSE(reader->ReadChild(&mpty)); |
| 170 EXPECT_TRUE(reader->MaybeReadChild(&mpty)); |
| 171 |
| 172 std::vector<ChildBox> kids; |
| 173 |
| 174 EXPECT_TRUE(reader->ReadAllChildren(&kids)); |
| 175 EXPECT_EQ(2u, kids.size()); |
| 176 kids.clear(); |
| 177 EXPECT_FALSE(reader->ReadChildren(&kids)); |
| 178 EXPECT_TRUE(reader->MaybeReadChildren(&kids)); |
| 179 } |
| 180 |
| 181 } // namespace mp4 |
| 182 } // namespace media |
| OLD | NEW |