Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(416)

Side by Side Diff: media/mp4/box_reader_unittest.cc

Issue 10823139: Set error on unrecognized top-level BMFF boxes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove FourCCs used only for testing Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 kTestBox[] = {
18 // Test box containing three children 18 // Test box containing three children. The FourCCs used for these boxes were
19 0x00, 0x00, 0x00, 0x40, 't', 'e', 's', 't', 19 // chosen arbitrarily.
acolwell GONE FROM CHROMIUM 2012/08/02 20:38:18 Why did these need to change if they are chosen ar
strobe_ 2012/08/02 21:27:50 They didn't, I was just removing the test variable
acolwell GONE FROM CHROMIUM 2012/08/02 21:40:25 Yeah. I think it is best to revert this text since
20 0x00, 0x00, 0x00, 0x40, 's', 'k', 'i', 'p',
20 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 21 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
21 0xf9, 0x0a, 0x0b, 0x0c, 0xfd, 0x0e, 0x0f, 0x10, 22 0xf9, 0x0a, 0x0b, 0x0c, 0xfd, 0x0e, 0x0f, 0x10,
22 // Ordinary child box 23 // Ordinary (8-byte header) child box
23 0x00, 0x00, 0x00, 0x0c, 'c', 'h', 'l', 'd', 0xde, 0xad, 0xbe, 0xef, 24 0x00, 0x00, 0x00, 0x0c, 'p', 's', 's', 'h', 0xde, 0xad, 0xbe, 0xef,
24 // Extended-size child box 25 // Extended-size header child box
25 0x00, 0x00, 0x00, 0x01, 'c', 'h', 'l', 'd', 26 0x00, 0x00, 0x00, 0x01, 'p', 's', 's', 'h',
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 27 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
27 0xfa, 0xce, 0xca, 0xfe, 28 0xfa, 0xce, 0xca, 0xfe,
28 // Empty box 29 // Empty free box
29 0x00, 0x00, 0x00, 0x08, 'm', 'p', 't', 'y', 30 0x00, 0x00, 0x00, 0x08, 'f', 'r', 'e', 'e',
30 // Trailing garbage 31 // Trailing garbage
31 0x00 }; 32 0x00 };
32 33
33 struct EmptyBox : Box { 34 struct EmptyBox : Box {
acolwell GONE FROM CHROMIUM 2012/08/02 20:38:18 Rename this and the ones below to match the new 4c
strobe_ 2012/08/02 21:27:50 Done.
34 virtual bool Parse(BoxReader* reader) OVERRIDE { 35 virtual bool Parse(BoxReader* reader) OVERRIDE {
35 return true; 36 return true;
36 } 37 }
37 virtual FourCC BoxType() const OVERRIDE { return FOURCC_MPTY; } 38 virtual FourCC BoxType() const OVERRIDE { return FOURCC_FREE; }
38 }; 39 };
39 40
40 struct ChildBox : Box { 41 struct ChildBox : Box {
41 uint32 val; 42 uint32 val;
42 43
43 virtual bool Parse(BoxReader* reader) OVERRIDE { 44 virtual bool Parse(BoxReader* reader) OVERRIDE {
44 return reader->Read4(&val); 45 return reader->Read4(&val);
45 } 46 }
46 virtual FourCC BoxType() const OVERRIDE { return FOURCC_CHLD; } 47 virtual FourCC BoxType() const OVERRIDE { return FOURCC_PSSH; }
47 }; 48 };
48 49
49 struct TestBox : Box { 50 struct TestBox : Box {
50 uint8 a, b; 51 uint8 a, b;
51 uint16 c; 52 uint16 c;
52 int32 d; 53 int32 d;
53 int64 e; 54 int64 e;
54 55
55 std::vector<ChildBox> kids; 56 std::vector<ChildBox> kids;
56 EmptyBox mpty; 57 EmptyBox mpty;
57 58
58 virtual bool Parse(BoxReader* reader) OVERRIDE { 59 virtual bool Parse(BoxReader* reader) OVERRIDE {
59 RCHECK(reader->ReadFullBoxHeader() && 60 RCHECK(reader->ReadFullBoxHeader() &&
60 reader->Read1(&a) && 61 reader->Read1(&a) &&
61 reader->Read1(&b) && 62 reader->Read1(&b) &&
62 reader->Read2(&c) && 63 reader->Read2(&c) &&
63 reader->Read4s(&d) && 64 reader->Read4s(&d) &&
64 reader->Read4sInto8s(&e)); 65 reader->Read4sInto8s(&e));
65 return reader->ScanChildren() && 66 return reader->ScanChildren() &&
66 reader->ReadChildren(&kids) && 67 reader->ReadChildren(&kids) &&
67 reader->MaybeReadChild(&mpty); 68 reader->MaybeReadChild(&mpty);
68 } 69 }
69 virtual FourCC BoxType() const OVERRIDE { return FOURCC_TEST; } 70 virtual FourCC BoxType() const OVERRIDE { return FOURCC_SKIP; }
70 71
71 TestBox(); 72 TestBox();
72 ~TestBox(); 73 ~TestBox();
73 }; 74 };
74 75
75 TestBox::TestBox() {} 76 TestBox::TestBox() {}
76 TestBox::~TestBox() {} 77 TestBox::~TestBox() {}
77 78
78 class BoxReaderTest : public testing::Test { 79 class BoxReaderTest : public testing::Test {
79 protected: 80 protected:
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); 131 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err));
131 132
132 TestBox box; 133 TestBox box;
133 EXPECT_FALSE(box.Parse(reader.get())); 134 EXPECT_FALSE(box.Parse(reader.get()));
134 } 135 }
135 136
136 TEST_F(BoxReaderTest, WrongFourCCTest) { 137 TEST_F(BoxReaderTest, WrongFourCCTest) {
137 std::vector<uint8> buf = GetBuf(); 138 std::vector<uint8> buf = GetBuf();
138 bool err; 139 bool err;
139 140
140 // Use an unknown FourCC both on an outer box and an inner one. 141 // Set an unrecognized top-level FourCC.
141 buf[5] = 1; 142 buf[5] = 1;
142 buf[28] = 1;
143 scoped_ptr<BoxReader> reader( 143 scoped_ptr<BoxReader> reader(
144 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); 144 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err));
145 145 EXPECT_FALSE(reader.get());
146 TestBox box; 146 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 } 147 }
158 148
159 TEST_F(BoxReaderTest, ChildrenTest) { 149 TEST_F(BoxReaderTest, ChildrenTest) {
160 std::vector<uint8> buf = GetBuf(); 150 std::vector<uint8> buf = GetBuf();
161 bool err; 151 bool err;
162 scoped_ptr<BoxReader> reader( 152 scoped_ptr<BoxReader> reader(
163 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err)); 153 BoxReader::ReadTopLevelBox(&buf[0], buf.size(), &err));
164 154
165 EXPECT_TRUE(reader->SkipBytes(16) && reader->ScanChildren()); 155 EXPECT_TRUE(reader->SkipBytes(16) && reader->ScanChildren());
166 156
167 EmptyBox mpty; 157 EmptyBox mpty;
168 EXPECT_TRUE(reader->ReadChild(&mpty)); 158 EXPECT_TRUE(reader->ReadChild(&mpty));
169 EXPECT_FALSE(reader->ReadChild(&mpty)); 159 EXPECT_FALSE(reader->ReadChild(&mpty));
170 EXPECT_TRUE(reader->MaybeReadChild(&mpty)); 160 EXPECT_TRUE(reader->MaybeReadChild(&mpty));
171 161
172 std::vector<ChildBox> kids; 162 std::vector<ChildBox> kids;
173 163
174 EXPECT_TRUE(reader->ReadAllChildren(&kids)); 164 EXPECT_TRUE(reader->ReadAllChildren(&kids));
175 EXPECT_EQ(2u, kids.size()); 165 EXPECT_EQ(2u, kids.size());
176 kids.clear(); 166 kids.clear();
177 EXPECT_FALSE(reader->ReadChildren(&kids)); 167 EXPECT_FALSE(reader->ReadChildren(&kids));
178 EXPECT_TRUE(reader->MaybeReadChildren(&kids)); 168 EXPECT_TRUE(reader->MaybeReadChildren(&kids));
179 } 169 }
180 170
181 } // namespace mp4 171 } // namespace mp4
182 } // namespace media 172 } // namespace media
OLDNEW
« media/mp4/box_reader.cc ('K') | « media/mp4/box_reader.cc ('k') | media/mp4/fourccs.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698