OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "media/mp4/box_reader.h" | 10 #include "media/mp4/box_reader.h" |
11 #include "media/mp4/rcheck.h" | 11 #include "media/mp4/rcheck.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace media { | 14 namespace media { |
15 namespace mp4 { | 15 namespace mp4 { |
16 | 16 |
17 static const uint8 kTestBox[] = { | 17 static const uint8 kSkipBox[] = { |
18 // Test box containing three children | 18 // Top-level test box containing three children |
19 0x00, 0x00, 0x00, 0x40, 't', 'e', 's', 't', | 19 0x00, 0x00, 0x00, 0x40, 's', 'k', 'i', 'p', |
20 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | 20 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
21 0xf9, 0x0a, 0x0b, 0x0c, 0xfd, 0x0e, 0x0f, 0x10, | 21 0xf9, 0x0a, 0x0b, 0x0c, 0xfd, 0x0e, 0x0f, 0x10, |
22 // Ordinary child box | 22 // Ordinary (8-byte header) child box |
23 0x00, 0x00, 0x00, 0x0c, 'c', 'h', 'l', 'd', 0xde, 0xad, 0xbe, 0xef, | 23 0x00, 0x00, 0x00, 0x0c, 'p', 's', 's', 'h', 0xde, 0xad, 0xbe, 0xef, |
24 // Extended-size child box | 24 // Extended-size header child box |
25 0x00, 0x00, 0x00, 0x01, 'c', 'h', 'l', 'd', | 25 0x00, 0x00, 0x00, 0x01, 'p', 's', 's', 'h', |
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, | 26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, |
27 0xfa, 0xce, 0xca, 0xfe, | 27 0xfa, 0xce, 0xca, 0xfe, |
28 // Empty box | 28 // Empty free box |
29 0x00, 0x00, 0x00, 0x08, 'm', 'p', 't', 'y', | 29 0x00, 0x00, 0x00, 0x08, 'f', 'r', 'e', 'e', |
30 // Trailing garbage | 30 // Trailing garbage |
31 0x00 }; | 31 0x00 }; |
32 | 32 |
33 struct EmptyBox : Box { | 33 struct FreeBox : Box { |
34 virtual bool Parse(BoxReader* reader) OVERRIDE { | 34 virtual bool Parse(BoxReader* reader) OVERRIDE { |
35 return true; | 35 return true; |
36 } | 36 } |
37 virtual FourCC BoxType() const OVERRIDE { return FOURCC_MPTY; } | 37 virtual FourCC BoxType() const OVERRIDE { return FOURCC_FREE; } |
38 }; | 38 }; |
39 | 39 |
40 struct ChildBox : Box { | 40 struct PsshBox : Box { |
41 uint32 val; | 41 uint32 val; |
42 | 42 |
43 virtual bool Parse(BoxReader* reader) OVERRIDE { | 43 virtual bool Parse(BoxReader* reader) OVERRIDE { |
44 return reader->Read4(&val); | 44 return reader->Read4(&val); |
45 } | 45 } |
46 virtual FourCC BoxType() const OVERRIDE { return FOURCC_CHLD; } | 46 virtual FourCC BoxType() const OVERRIDE { return FOURCC_PSSH; } |
47 }; | 47 }; |
48 | 48 |
49 struct TestBox : Box { | 49 struct SkipBox : Box { |
50 uint8 a, b; | 50 uint8 a, b; |
51 uint16 c; | 51 uint16 c; |
52 int32 d; | 52 int32 d; |
53 int64 e; | 53 int64 e; |
54 | 54 |
55 std::vector<ChildBox> kids; | 55 std::vector<PsshBox> kids; |
56 EmptyBox mpty; | 56 FreeBox mpty; |
57 | 57 |
58 virtual bool Parse(BoxReader* reader) OVERRIDE { | 58 virtual bool Parse(BoxReader* reader) OVERRIDE { |
59 RCHECK(reader->ReadFullBoxHeader() && | 59 RCHECK(reader->ReadFullBoxHeader() && |
60 reader->Read1(&a) && | 60 reader->Read1(&a) && |
61 reader->Read1(&b) && | 61 reader->Read1(&b) && |
62 reader->Read2(&c) && | 62 reader->Read2(&c) && |
63 reader->Read4s(&d) && | 63 reader->Read4s(&d) && |
64 reader->Read4sInto8s(&e)); | 64 reader->Read4sInto8s(&e)); |
65 return reader->ScanChildren() && | 65 return reader->ScanChildren() && |
66 reader->ReadChildren(&kids) && | 66 reader->ReadChildren(&kids) && |
67 reader->MaybeReadChild(&mpty); | 67 reader->MaybeReadChild(&mpty); |
68 } | 68 } |
69 virtual FourCC BoxType() const OVERRIDE { return FOURCC_TEST; } | 69 virtual FourCC BoxType() const OVERRIDE { return FOURCC_SKIP; } |
70 | 70 |
71 TestBox(); | 71 SkipBox(); |
72 ~TestBox(); | 72 ~SkipBox(); |
73 }; | 73 }; |
74 | 74 |
75 TestBox::TestBox() {} | 75 SkipBox::SkipBox() {} |
76 TestBox::~TestBox() {} | 76 SkipBox::~SkipBox() {} |
77 | 77 |
78 class BoxReaderTest : public testing::Test { | 78 class BoxReaderTest : public testing::Test { |
79 protected: | 79 protected: |
80 std::vector<uint8> GetBuf() { | 80 std::vector<uint8> GetBuf() { |
81 return std::vector<uint8>(kTestBox, kTestBox + sizeof(kTestBox)); | 81 return std::vector<uint8>(kSkipBox, kSkipBox + sizeof(kSkipBox)); |
82 } | 82 } |
83 }; | 83 }; |
84 | 84 |
85 TEST_F(BoxReaderTest, ExpectedOperationTest) { | 85 TEST_F(BoxReaderTest, ExpectedOperationTest) { |
86 std::vector<uint8> buf = GetBuf(); | 86 std::vector<uint8> buf = GetBuf(); |
87 bool err; | 87 bool err; |
88 scoped_ptr<BoxReader> reader( | 88 scoped_ptr<BoxReader> reader( |
89 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); | 89 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
90 EXPECT_FALSE(err); | 90 EXPECT_FALSE(err); |
91 EXPECT_TRUE(reader.get()); | 91 EXPECT_TRUE(reader.get()); |
92 | 92 |
93 TestBox box; | 93 SkipBox box; |
94 EXPECT_TRUE(box.Parse(reader.get())); | 94 EXPECT_TRUE(box.Parse(reader.get())); |
95 EXPECT_EQ(0x01, reader->version()); | 95 EXPECT_EQ(0x01, reader->version()); |
96 EXPECT_EQ(0x020304u, reader->flags()); | 96 EXPECT_EQ(0x020304u, reader->flags()); |
97 EXPECT_EQ(0x05, box.a); | 97 EXPECT_EQ(0x05, box.a); |
98 EXPECT_EQ(0x06, box.b); | 98 EXPECT_EQ(0x06, box.b); |
99 EXPECT_EQ(0x0708, box.c); | 99 EXPECT_EQ(0x0708, box.c); |
100 EXPECT_EQ(static_cast<int32>(0xf90a0b0c), box.d); | 100 EXPECT_EQ(static_cast<int32>(0xf90a0b0c), box.d); |
101 EXPECT_EQ(static_cast<int32>(0xfd0e0f10), box.e); | 101 EXPECT_EQ(static_cast<int32>(0xfd0e0f10), box.e); |
102 | 102 |
103 EXPECT_EQ(2u, box.kids.size()); | 103 EXPECT_EQ(2u, box.kids.size()); |
(...skipping 18 matching lines...) Expand all Loading... |
122 | 122 |
123 TEST_F(BoxReaderTest, InnerTooLongTest) { | 123 TEST_F(BoxReaderTest, InnerTooLongTest) { |
124 std::vector<uint8> buf = GetBuf(); | 124 std::vector<uint8> buf = GetBuf(); |
125 bool err; | 125 bool err; |
126 | 126 |
127 // Make an inner box too big for its outer box. | 127 // Make an inner box too big for its outer box. |
128 buf[25] = 1; | 128 buf[25] = 1; |
129 scoped_ptr<BoxReader> reader( | 129 scoped_ptr<BoxReader> reader( |
130 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); | 130 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
131 | 131 |
132 TestBox box; | 132 SkipBox box; |
133 EXPECT_FALSE(box.Parse(reader.get())); | 133 EXPECT_FALSE(box.Parse(reader.get())); |
134 } | 134 } |
135 | 135 |
136 TEST_F(BoxReaderTest, WrongFourCCTest) { | 136 TEST_F(BoxReaderTest, WrongFourCCTest) { |
137 std::vector<uint8> buf = GetBuf(); | 137 std::vector<uint8> buf = GetBuf(); |
138 bool err; | 138 bool err; |
139 | 139 |
140 // Use an unknown FourCC both on an outer box and an inner one. | 140 // Set an unrecognized top-level FourCC. |
141 buf[5] = 1; | 141 buf[5] = 1; |
142 buf[28] = 1; | |
143 scoped_ptr<BoxReader> reader( | 142 scoped_ptr<BoxReader> reader( |
144 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); | 143 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
145 | 144 EXPECT_FALSE(reader.get()); |
146 TestBox box; | 145 EXPECT_TRUE(err); |
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 } | 146 } |
158 | 147 |
159 TEST_F(BoxReaderTest, ChildrenTest) { | 148 TEST_F(BoxReaderTest, ChildrenTest) { |
160 std::vector<uint8> buf = GetBuf(); | 149 std::vector<uint8> buf = GetBuf(); |
161 bool err; | 150 bool err; |
162 scoped_ptr<BoxReader> reader( | 151 scoped_ptr<BoxReader> reader( |
163 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); | 152 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); |
164 | 153 |
165 EXPECT_TRUE(reader->SkipBytes(16) && reader->ScanChildren()); | 154 EXPECT_TRUE(reader->SkipBytes(16) && reader->ScanChildren()); |
166 | 155 |
167 EmptyBox mpty; | 156 FreeBox free; |
168 EXPECT_TRUE(reader->ReadChild(&mpty)); | 157 EXPECT_TRUE(reader->ReadChild(&free)); |
169 EXPECT_FALSE(reader->ReadChild(&mpty)); | 158 EXPECT_FALSE(reader->ReadChild(&free)); |
170 EXPECT_TRUE(reader->MaybeReadChild(&mpty)); | 159 EXPECT_TRUE(reader->MaybeReadChild(&free)); |
171 | 160 |
172 std::vector<ChildBox> kids; | 161 std::vector<PsshBox> kids; |
173 | 162 |
174 EXPECT_TRUE(reader->ReadAllChildren(&kids)); | 163 EXPECT_TRUE(reader->ReadAllChildren(&kids)); |
175 EXPECT_EQ(2u, kids.size()); | 164 EXPECT_EQ(2u, kids.size()); |
176 kids.clear(); | 165 kids.clear(); |
177 EXPECT_FALSE(reader->ReadChildren(&kids)); | 166 EXPECT_FALSE(reader->ReadChildren(&kids)); |
178 EXPECT_TRUE(reader->MaybeReadChildren(&kids)); | 167 EXPECT_TRUE(reader->MaybeReadChildren(&kids)); |
179 } | 168 } |
180 | 169 |
181 } // namespace mp4 | 170 } // namespace mp4 |
182 } // namespace media | 171 } // namespace media |
OLD | NEW |