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

Side by Side Diff: chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader_unittest.cc

Issue 17101030: Media Galleries API - Picasa: Change PMP Parsing to deal with PlatformFile. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 6 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 <algorithm> 5 #include <algorithm>
6 #include <vector> 6 #include <vector>
7 7
8 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h" 8 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_column_reader.h"
9 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h" 9 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_constants.h"
10 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_test_helper.h" 10 #include "chrome/browser/media_galleries/fileapi/picasa/pmp_test_helper.h"
(...skipping 26 matching lines...) Expand all
37 } 37 }
38 38
39 // TestValid 39 // TestValid
40 template<class T> 40 template<class T>
41 void TestValid(const picasa::PmpFieldType field_type, 41 void TestValid(const picasa::PmpFieldType field_type,
42 const std::vector<T>& elems) { 42 const std::vector<T>& elems) {
43 PmpTestHelper test_helper("test"); 43 PmpTestHelper test_helper("test");
44 ASSERT_TRUE(test_helper.Init()); 44 ASSERT_TRUE(test_helper.Init());
45 45
46 PmpColumnReader reader; 46 PmpColumnReader reader;
47
48 uint32 rows_read = 0xFF;
49
50 std::vector<uint8> data = 47 std::vector<uint8> data =
51 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); 48 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
52 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes( 49 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(
53 &reader, data, field_type, &rows_read)); 50 &reader, data, field_type));
54 EXPECT_EQ(elems.size(), rows_read); 51 EXPECT_EQ(elems.size(), reader.rows());
55 52
56 for (uint32 i = 0; i < elems.size() && i < rows_read; i++) { 53 for (uint32 i = 0; i < elems.size() && i < reader.rows(); i++) {
57 T target; 54 T target;
58 EXPECT_TRUE(DoRead(&reader, i, &target)); 55 EXPECT_TRUE(DoRead(&reader, i, &target));
59 EXPECT_EQ(elems[i], target); 56 EXPECT_EQ(elems[i], target);
60 } 57 }
61 } 58 }
62 59
63 template<class T> 60 template<class T>
64 void TestMalformed(const picasa::PmpFieldType field_type, 61 void TestMalformed(const picasa::PmpFieldType field_type,
65 const std::vector<T>& elems) { 62 const std::vector<T>& elems) {
66 PmpTestHelper test_helper("test"); 63 PmpTestHelper test_helper("test");
67 ASSERT_TRUE(test_helper.Init()); 64 ASSERT_TRUE(test_helper.Init());
68 65
69 PmpColumnReader reader_too_few_declared_rows; 66 PmpColumnReader reader_too_few_declared_rows;
70 std::vector<uint8> data_too_few_declared_rows = 67 std::vector<uint8> data_too_few_declared_rows =
71 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems); 68 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems);
72 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 69 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
73 &reader_too_few_declared_rows, 70 &reader_too_few_declared_rows,
74 data_too_few_declared_rows, 71 data_too_few_declared_rows,
75 field_type, 72 field_type));
76 NULL));
77 73
78 PmpColumnReader reader_too_many_declared_rows; 74 PmpColumnReader reader_too_many_declared_rows;
79 std::vector<uint8> data_too_many_declared_rows = 75 std::vector<uint8> data_too_many_declared_rows =
80 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems); 76 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems);
81 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 77 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
82 &reader_too_many_declared_rows, 78 &reader_too_many_declared_rows,
83 data_too_many_declared_rows, 79 data_too_many_declared_rows,
84 field_type, 80 field_type));
85 NULL));
86 81
87 PmpColumnReader reader_truncated; 82 PmpColumnReader reader_truncated;
88 std::vector<uint8> data_truncated = 83 std::vector<uint8> data_truncated =
89 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); 84 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
90 data_truncated.resize(data_truncated.size()-10); 85 data_truncated.resize(data_truncated.size()-10);
91 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 86 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
92 &reader_truncated, data_truncated, field_type, NULL)); 87 &reader_truncated, data_truncated, field_type));
93 88
94 PmpColumnReader reader_padded; 89 PmpColumnReader reader_padded;
95 std::vector<uint8> data_padded = 90 std::vector<uint8> data_padded =
96 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); 91 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems);
97 data_padded.resize(data_padded.size()+10); 92 data_padded.resize(data_padded.size()+10);
98 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 93 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
99 &reader_padded, data_padded, field_type, NULL)); 94 &reader_padded, data_padded, field_type));
100 } 95 }
101 96
102 template<class T> 97 template<class T>
103 void TestPrimitive(const picasa::PmpFieldType field_type) { 98 void TestPrimitive(const picasa::PmpFieldType field_type) {
104 // Make an ascending vector of the primitive. 99 // Make an ascending vector of the primitive.
105 uint32 n = 100; 100 uint32 n = 100;
106 std::vector<T> data(n, 0); 101 std::vector<T> data(n, 0);
107 for (uint32 i = 0; i < n; i++) { 102 for (uint32 i = 0; i < n; i++) {
108 data[i] = i*3; 103 data[i] = i*3;
109 } 104 }
110 105
111 TestValid<T>(field_type, data); 106 TestValid<T>(field_type, data);
112 TestMalformed<T>(field_type, data); 107 TestMalformed<T>(field_type, data);
113 } 108 }
114 109
115 110
116 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) { 111 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) {
117 PmpTestHelper test_helper("test"); 112 PmpTestHelper test_helper("test");
118 ASSERT_TRUE(test_helper.Init()); 113 ASSERT_TRUE(test_helper.Init());
119 114
120 PmpColumnReader reader_good_header; 115 PmpColumnReader reader_good_header;
121 uint32 rows_read = 0xFF;
122 std::vector<uint8> good_header = 116 std::vector<uint8> good_header =
123 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0); 117 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
124 EXPECT_TRUE(test_helper.InitColumnReaderFromBytes( 118 EXPECT_TRUE(test_helper.InitColumnReaderFromBytes(
125 &reader_good_header, 119 &reader_good_header,
126 good_header, 120 good_header,
127 picasa::PMP_TYPE_STRING, 121 picasa::PMP_TYPE_STRING));
128 &rows_read)); 122 EXPECT_EQ(0U, reader_good_header.rows()) <<
129 EXPECT_EQ(0U, rows_read) << "Read non-zero rows from header-only data."; 123 "Read non-zero rows from header-only data.";
130 124
131 PmpColumnReader reader_bad_magic_bytes; 125 PmpColumnReader reader_bad_magic_bytes;
132 std::vector<uint8> bad_magic_bytes = 126 std::vector<uint8> bad_magic_bytes =
133 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0); 127 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
134 bad_magic_bytes[0] = 0xff; 128 bad_magic_bytes[0] = 0xff;
135 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 129 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
136 &reader_bad_magic_bytes, 130 &reader_bad_magic_bytes,
137 bad_magic_bytes, 131 bad_magic_bytes,
138 picasa::PMP_TYPE_STRING, 132 picasa::PMP_TYPE_STRING));
139 NULL));
140 133
141 PmpColumnReader reader_inconsistent_types; 134 PmpColumnReader reader_inconsistent_types;
142 std::vector<uint8> inconsistent_type = 135 std::vector<uint8> inconsistent_type =
143 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0); 136 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
144 inconsistent_type[picasa::kPmpFieldType1Offset] = 0xff; 137 inconsistent_type[picasa::kPmpFieldType1Offset] = 0xff;
145 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 138 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
146 &reader_inconsistent_types, 139 &reader_inconsistent_types,
147 inconsistent_type, 140 inconsistent_type,
148 picasa::PMP_TYPE_STRING, 141 picasa::PMP_TYPE_STRING));
149 NULL));
150 142
151 PmpColumnReader reader_invalid_type; 143 PmpColumnReader reader_invalid_type;
152 std::vector<uint8> invalid_type = 144 std::vector<uint8> invalid_type =
153 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0); 145 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
154 invalid_type[picasa::kPmpFieldType1Offset] = 0xff; 146 invalid_type[picasa::kPmpFieldType1Offset] = 0xff;
155 invalid_type[picasa::kPmpFieldType2Offset] = 0xff; 147 invalid_type[picasa::kPmpFieldType2Offset] = 0xff;
156 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 148 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
157 &reader_invalid_type, 149 &reader_invalid_type,
158 invalid_type, 150 invalid_type,
159 picasa::PMP_TYPE_STRING, 151 picasa::PMP_TYPE_STRING));
160 NULL));
161 152
162 PmpColumnReader reader_incomplete_header; 153 PmpColumnReader reader_incomplete_header;
163 std::vector<uint8> incomplete_header = 154 std::vector<uint8> incomplete_header =
164 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0); 155 PmpTestHelper::MakeHeader(picasa::PMP_TYPE_STRING, 0);
165 incomplete_header.resize(10); 156 incomplete_header.resize(10);
166 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( 157 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes(
167 &reader_incomplete_header, 158 &reader_incomplete_header,
168 incomplete_header, 159 incomplete_header,
169 picasa::PMP_TYPE_STRING, 160 picasa::PMP_TYPE_STRING));
170 NULL));
171 } 161 }
172 162
173 TEST(PmpColumnReaderTest, StringParsing) { 163 TEST(PmpColumnReaderTest, StringParsing) {
174 std::vector<std::string> empty_strings(100, ""); 164 std::vector<std::string> empty_strings(100, "");
175 165
176 // Test empty strings read okay. 166 // Test empty strings read okay.
177 TestValid(picasa::PMP_TYPE_STRING, empty_strings); 167 TestValid(picasa::PMP_TYPE_STRING, empty_strings);
178 168
179 std::vector<std::string> mixed_strings; 169 std::vector<std::string> mixed_strings;
180 mixed_strings.push_back(""); 170 mixed_strings.push_back("");
(...skipping 12 matching lines...) Expand all
193 } 183 }
194 184
195 TEST(PmpColumnReaderTest, PrimitiveParsing) { 185 TEST(PmpColumnReaderTest, PrimitiveParsing) {
196 TestPrimitive<uint32>(picasa::PMP_TYPE_UINT32); 186 TestPrimitive<uint32>(picasa::PMP_TYPE_UINT32);
197 TestPrimitive<double>(picasa::PMP_TYPE_DOUBLE64); 187 TestPrimitive<double>(picasa::PMP_TYPE_DOUBLE64);
198 TestPrimitive<uint8>(picasa::PMP_TYPE_UINT8); 188 TestPrimitive<uint8>(picasa::PMP_TYPE_UINT8);
199 TestPrimitive<uint64>(picasa::PMP_TYPE_UINT64); 189 TestPrimitive<uint64>(picasa::PMP_TYPE_UINT64);
200 } 190 }
201 191
202 } // namespace 192 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698