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

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

Issue 12704024: Simple PMP reader to parse Picasa's metadata (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Missed a change. 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 fileapi::PmpColumnReader;
17 using fileapi::PmpTestHelper;
18
19 // Writes memory into a temporary file for use in test
20 bool InitColumnReaderFromMemory(PmpColumnReader* const reader,
21 std::vector<uint8> data, uint32* rows_read) {
22 base::FilePath temppath;
23
24 if (!file_util::CreateTemporaryFile(&temppath) ||
25 !PmpTestHelper::WriteToFile(temppath, data)) {
26 return false;
27 }
28
29 bool success = reader->Init(temppath, rows_read);
30
31 file_util::Delete(temppath, true);
32
33 return success;
34 }
35
36 // Overridden version of Read method to make test code templatable.
37 bool DoRead(const PmpColumnReader* reader, uint32 row, std::string* target) {
38 return reader->ReadString(row, target);
39 }
40
41 bool DoRead(const PmpColumnReader* reader, uint32 row, uint32* target) {
42 return reader->ReadUInt32(row, target);
43 }
44
45 bool DoRead(const PmpColumnReader* reader, uint32 row, double* target) {
46 return reader->ReadDouble64(row, target);
47 }
48
49 bool DoRead(const PmpColumnReader* reader, uint32 row, uint8* target) {
50 return reader->ReadUInt8(row, target);
51 }
52
53 bool DoRead(const PmpColumnReader* reader, uint32 row, uint64* target) {
54 return reader->ReadUInt64(row, target);
55 }
56
57 // TestValid
58 template<class T>
59 void TestValid(const uint16 field_type, const std::vector<T>& elems) {
60 PmpColumnReader reader;
61
62 uint32 rows_read = 0xFF;
63
64 std::vector<uint8> data =
65 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
66 ASSERT_TRUE(InitColumnReaderFromMemory(&reader, data, &rows_read));
67 EXPECT_EQ(elems.size(), rows_read);
68
69 for(uint32 i = 0; i < elems.size() && i < rows_read; i++) {
70 T target;
71 EXPECT_TRUE(DoRead(&reader, i, &target));
72 EXPECT_EQ(elems[i], target);
73 }
74 }
75
76 template<class T>
77 void TestMalformed(const uint16 field_type,
78 const std::vector<T>& elems) {
79 PmpColumnReader reader1, reader2, reader3, reader4;
80
81 std::vector<uint8> data_too_few_declared_rows =
82 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems);
83 EXPECT_FALSE(InitColumnReaderFromMemory(
84 &reader1, data_too_few_declared_rows, NULL));
85
86 std::vector<uint8> data_too_many_declared_rows =
87 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems);
88 EXPECT_FALSE(InitColumnReaderFromMemory(
89 &reader2, data_too_many_declared_rows, NULL));
90
91 std::vector<uint8> data_truncated =
92 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
93 data_truncated.resize(data_truncated.size()-10);
94 EXPECT_FALSE(InitColumnReaderFromMemory(&reader3, data_truncated, NULL));
95
96 std::vector<uint8> data_padded =
97 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
98 data_padded.resize(data_padded.size()+10);
99 EXPECT_FALSE(InitColumnReaderFromMemory(&reader4, data_padded, NULL));
100 }
101
102 template<class T>
103 void TestPrimitives(const uint16 field_type) {
vandebo (ex-Chrome) 2013/04/02 18:10:55 nit: TestPrimitive
tommycli 2013/04/02 19:05:55 Done.
104 // Make an ascending vector of the primitive.
105 uint32 n = 100;
106 std::vector<T> data(n, 0);
107 for(uint32 i = 0; i < n; i++) {
108 data[i] = i*3;
109 }
110
111 TestValid<T>(field_type, data);
112 TestMalformed<T>(field_type, data);
113 }
114
115
116 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) {
117 PmpColumnReader reader1, reader2, reader3, reader4, reader5;
118
119 // Good header.
120 uint32 rows_read = 0xFF;
121 std::vector<uint8> good_header = PmpTestHelper::MakeHeader(0x00, 0);
122 ASSERT_TRUE(InitColumnReaderFromMemory(&reader1, good_header, &rows_read));
123 EXPECT_EQ(0U, rows_read) << "Read non-zero rows from header-only data.";
124
125 // Botch up elements of the header.
126 std::vector<uint8> bad_magic_byte = PmpTestHelper::MakeHeader(0x00, 0);
127 bad_magic_byte[0] = 0xff;
128 EXPECT_FALSE(InitColumnReaderFromMemory(&reader2, bad_magic_byte, NULL));
129
130 // Corrupt means the type fields don't agree.
131 std::vector<uint8> corrupt_type = PmpTestHelper::MakeHeader(0x00, 0);
132 corrupt_type[fileapi::kPmpFieldType1Offset] = 0xff;
133 EXPECT_FALSE(InitColumnReaderFromMemory(&reader3, corrupt_type, NULL));
134
135 std::vector<uint8> invalid_type = PmpTestHelper::MakeHeader(0x00, 0);
136 invalid_type[fileapi::kPmpFieldType1Offset] = 0xff;
137 invalid_type[fileapi::kPmpFieldType2Offset] = 0xff;
138 EXPECT_FALSE(InitColumnReaderFromMemory(&reader4, invalid_type, NULL));
139
140 std::vector<uint8> incomplete_header = PmpTestHelper::MakeHeader(0x00, 0);
141 incomplete_header.resize(10);
142 EXPECT_FALSE(InitColumnReaderFromMemory(&reader5, incomplete_header, NULL));
143 }
144
145 TEST(PmpColumnReaderTest, StringParsing) {
146 std::vector<std::string> empty_strings(100, "");
147
148 // Test empty strings read okay.
149 TestValid(fileapi::kPmpFieldTypeString, empty_strings);
150
151 std::vector<std::string> mixed_strings;
152 mixed_strings.push_back("");
153 mixed_strings.push_back("Hello");
154 mixed_strings.push_back("World");
155 mixed_strings.push_back("");
156 mixed_strings.push_back("123123");
157 mixed_strings.push_back("Q");
158 mixed_strings.push_back("");
159
160 // Test that a mixed set of strings read correctly.
161 TestValid(fileapi::kPmpFieldTypeString, mixed_strings);
162
163 // Test with the data messed up in a variety of ways.
164 TestMalformed(fileapi::kPmpFieldTypeString, mixed_strings);
165 }
166
167 TEST(PmpColumnReaderTest, PrimitiveParsing) {
168 TestPrimitives<uint32>(fileapi::kPmpFieldTypeUInt32);
169 TestPrimitives<double>(fileapi::kPmpFieldTypeDouble64);
170 TestPrimitives<uint8>(fileapi::kPmpFieldTypeUInt8);
171 TestPrimitives<uint64>(fileapi::kPmpFieldTypeUInt64);
172 }
173
174 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698