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

Side by Side Diff: webkit/fileapi/media/picasa/pmp_column_reader_unittest.cc

Issue 14247034: Move Media Galleries FileAPI code out of webkit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cr-14352004
Patch Set: Add android ifdef. Created 7 years, 8 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
OLDNEW
(Empty)
1 // Copyright 2013 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 <algorithm>
6 #include <vector>
7
8 #include "base/file_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "webkit/fileapi/media/picasa/pmp_column_reader.h"
11 #include "webkit/fileapi/media/picasa/pmp_constants.h"
12 #include "webkit/fileapi/media/picasa/pmp_test_helper.h"
13
14 namespace {
15
16 using picasaimport::PmpColumnReader;
17 using picasaimport::PmpTestHelper;
18
19 // Overridden version of Read method to make test code templatable.
20 bool DoRead(const PmpColumnReader* reader, uint32 row, std::string* target) {
21 return reader->ReadString(row, target);
22 }
23
24 bool DoRead(const PmpColumnReader* reader, uint32 row, uint32* target) {
25 return reader->ReadUInt32(row, target);
26 }
27
28 bool DoRead(const PmpColumnReader* reader, uint32 row, double* target) {
29 return reader->ReadDouble64(row, target);
30 }
31
32 bool DoRead(const PmpColumnReader* reader, uint32 row, uint8* target) {
33 return reader->ReadUInt8(row, target);
34 }
35
36 bool DoRead(const PmpColumnReader* reader, uint32 row, uint64* target) {
37 return reader->ReadUInt64(row, target);
38 }
39
40 // TestValid
41 template<class T>
42 void TestValid(const picasaimport::PmpFieldType field_type,
43 const std::vector<T>& elems) {
44 PmpTestHelper test_helper;
45 ASSERT_TRUE(test_helper.Init());
46
47 PmpColumnReader reader;
48
49 uint32 rows_read = 0xFF;
50
51 std::vector<uint8> data =
52 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
53 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(&reader, data, &rows_read));
54 EXPECT_EQ(elems.size(), rows_read);
55
56 for (uint32 i = 0; i < elems.size() && i < rows_read; i++) {
57 T target;
58 EXPECT_TRUE(DoRead(&reader, i, &target));
59 EXPECT_EQ(elems[i], target);
60 }
61 }
62
63 template<class T>
64 void TestMalformed(const picasaimport::PmpFieldType field_type,
65 const std::vector<T>& elems) {
66 PmpTestHelper test_helper;
67 ASSERT_TRUE(test_helper.Init());
68
69 PmpColumnReader reader1, reader2, reader3, reader4;
70
71 std::vector<uint8> data_too_few_declared_rows =
72 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems);
73 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
74 &reader1, data_too_few_declared_rows, NULL));
75
76 std::vector<uint8> data_too_many_declared_rows =
77 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems);
78 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
79 &reader2, data_too_many_declared_rows, NULL));
80
81 std::vector<uint8> data_truncated =
82 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
83 data_truncated.resize(data_truncated.size()-10);
84 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
85 &reader3, data_truncated, NULL));
86
87 std::vector<uint8> data_padded =
88 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
89 data_padded.resize(data_padded.size()+10);
90 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
91 &reader4, data_padded, NULL));
92 }
93
94 template<class T>
95 void TestPrimitive(const picasaimport::PmpFieldType field_type) {
96 // Make an ascending vector of the primitive.
97 uint32 n = 100;
98 std::vector<T> data(n, 0);
99 for (uint32 i = 0; i < n; i++) {
100 data[i] = i*3;
101 }
102
103 TestValid<T>(field_type, data);
104 TestMalformed<T>(field_type, data);
105 }
106
107
108 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) {
109 PmpTestHelper test_helper;
110 ASSERT_TRUE(test_helper.Init());
111
112 PmpColumnReader reader1, reader2, reader3, reader4, reader5;
113
114 // Good header.
115 uint32 rows_read = 0xFF;
116 std::vector<uint8> good_header = PmpTestHelper::MakeHeader(
117 picasaimport::PMP_TYPE_STRING, 0);
118 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(
119 &reader1, good_header, &rows_read));
120 EXPECT_EQ(0U, rows_read) << "Read non-zero rows from header-only data.";
121
122 // Botch up elements of the header.
123 std::vector<uint8> bad_magic_byte = PmpTestHelper::MakeHeader(
124 picasaimport::PMP_TYPE_STRING, 0);
125 bad_magic_byte[0] = 0xff;
126 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
127 &reader2, bad_magic_byte, NULL));
128
129 // Corrupt means the type fields don't agree.
130 std::vector<uint8> corrupt_type = PmpTestHelper::MakeHeader(
131 picasaimport::PMP_TYPE_STRING, 0);
132 corrupt_type[picasaimport::kPmpFieldType1Offset] = 0xff;
133 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
134 &reader3, corrupt_type, NULL));
135
136 std::vector<uint8> invalid_type = PmpTestHelper::MakeHeader(
137 picasaimport::PMP_TYPE_STRING, 0);
138 invalid_type[picasaimport::kPmpFieldType1Offset] = 0xff;
139 invalid_type[picasaimport::kPmpFieldType2Offset] = 0xff;
140 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
141 &reader4, invalid_type, NULL));
142
143 std::vector<uint8> incomplete_header = PmpTestHelper::MakeHeader(
144 picasaimport::PMP_TYPE_STRING, 0);
145 incomplete_header.resize(10);
146 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
147 &reader5, incomplete_header, NULL));
148 }
149
150 TEST(PmpColumnReaderTest, StringParsing) {
151 std::vector<std::string> empty_strings(100, "");
152
153 // Test empty strings read okay.
154 TestValid(picasaimport::PMP_TYPE_STRING, empty_strings);
155
156 std::vector<std::string> mixed_strings;
157 mixed_strings.push_back("");
158 mixed_strings.push_back("Hello");
159 mixed_strings.push_back("World");
160 mixed_strings.push_back("");
161 mixed_strings.push_back("123123");
162 mixed_strings.push_back("Q");
163 mixed_strings.push_back("");
164
165 // Test that a mixed set of strings read correctly.
166 TestValid(picasaimport::PMP_TYPE_STRING, mixed_strings);
167
168 // Test with the data messed up in a variety of ways.
169 TestMalformed(picasaimport::PMP_TYPE_STRING, mixed_strings);
170 }
171
172 TEST(PmpColumnReaderTest, PrimitiveParsing) {
173 TestPrimitive<uint32>(picasaimport::PMP_TYPE_UINT32);
174 TestPrimitive<double>(picasaimport::PMP_TYPE_DOUBLE64);
175 TestPrimitive<uint8>(picasaimport::PMP_TYPE_UINT8);
176 TestPrimitive<uint64>(picasaimport::PMP_TYPE_UINT64);
177 }
178
179 } // namespace
OLDNEW
« no previous file with comments | « webkit/fileapi/media/picasa/pmp_column_reader.cc ('k') | webkit/fileapi/media/picasa/pmp_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698