| OLD | NEW |
| (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 uint16 field_type, const std::vector<T>& elems) { |
| 43 PmpTestHelper test_helper; |
| 44 ASSERT_TRUE(test_helper.Init()); |
| 45 |
| 46 PmpColumnReader reader; |
| 47 |
| 48 uint32 rows_read = 0xFF; |
| 49 |
| 50 std::vector<uint8> data = |
| 51 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); |
| 52 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes(&reader, data, &rows_read)); |
| 53 EXPECT_EQ(elems.size(), rows_read); |
| 54 |
| 55 for(uint32 i = 0; i < elems.size() && i < rows_read; i++) { |
| 56 T target; |
| 57 EXPECT_TRUE(DoRead(&reader, i, &target)); |
| 58 EXPECT_EQ(elems[i], target); |
| 59 } |
| 60 } |
| 61 |
| 62 template<class T> |
| 63 void TestMalformed(const uint16 field_type, const std::vector<T>& elems) { |
| 64 PmpTestHelper test_helper; |
| 65 ASSERT_TRUE(test_helper.Init()); |
| 66 |
| 67 PmpColumnReader reader1, reader2, reader3, reader4; |
| 68 |
| 69 std::vector<uint8> data_too_few_declared_rows = |
| 70 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()-1, elems); |
| 71 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( |
| 72 &reader1, data_too_few_declared_rows, NULL)); |
| 73 |
| 74 std::vector<uint8> data_too_many_declared_rows = |
| 75 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size()+1, elems); |
| 76 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( |
| 77 &reader2, data_too_many_declared_rows, NULL)); |
| 78 |
| 79 std::vector<uint8> data_truncated = |
| 80 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); |
| 81 data_truncated.resize(data_truncated.size()-10); |
| 82 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( |
| 83 &reader3, data_truncated, NULL)); |
| 84 |
| 85 std::vector<uint8> data_padded = |
| 86 PmpTestHelper::MakeHeaderAndBody(field_type, elems.size(), elems); |
| 87 data_padded.resize(data_padded.size()+10); |
| 88 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( |
| 89 &reader4, data_padded, NULL)); |
| 90 } |
| 91 |
| 92 template<class T> |
| 93 void TestPrimitive(const uint16 field_type) { |
| 94 // Make an ascending vector of the primitive. |
| 95 uint32 n = 100; |
| 96 std::vector<T> data(n, 0); |
| 97 for(uint32 i = 0; i < n; i++) { |
| 98 data[i] = i*3; |
| 99 } |
| 100 |
| 101 TestValid<T>(field_type, data); |
| 102 TestMalformed<T>(field_type, data); |
| 103 } |
| 104 |
| 105 |
| 106 TEST(PmpColumnReaderTest, HeaderParsingAndValidation) { |
| 107 PmpTestHelper test_helper; |
| 108 ASSERT_TRUE(test_helper.Init()); |
| 109 |
| 110 PmpColumnReader reader1, reader2, reader3, reader4, reader5; |
| 111 |
| 112 // Good header. |
| 113 uint32 rows_read = 0xFF; |
| 114 std::vector<uint8> good_header = PmpTestHelper::MakeHeader(0x00, 0); |
| 115 ASSERT_TRUE(test_helper.InitColumnReaderFromBytes( |
| 116 &reader1, good_header, &rows_read)); |
| 117 EXPECT_EQ(0U, rows_read) << "Read non-zero rows from header-only data."; |
| 118 |
| 119 // Botch up elements of the header. |
| 120 std::vector<uint8> bad_magic_byte = PmpTestHelper::MakeHeader(0x00, 0); |
| 121 bad_magic_byte[0] = 0xff; |
| 122 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( |
| 123 &reader2, bad_magic_byte, NULL)); |
| 124 |
| 125 // Corrupt means the type fields don't agree. |
| 126 std::vector<uint8> corrupt_type = PmpTestHelper::MakeHeader(0x00, 0); |
| 127 corrupt_type[picasaimport::kPmpFieldType1Offset] = 0xff; |
| 128 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( |
| 129 &reader3, corrupt_type, NULL)); |
| 130 |
| 131 std::vector<uint8> invalid_type = PmpTestHelper::MakeHeader(0x00, 0); |
| 132 invalid_type[picasaimport::kPmpFieldType1Offset] = 0xff; |
| 133 invalid_type[picasaimport::kPmpFieldType2Offset] = 0xff; |
| 134 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( |
| 135 &reader4, invalid_type, NULL)); |
| 136 |
| 137 std::vector<uint8> incomplete_header = PmpTestHelper::MakeHeader(0x00, 0); |
| 138 incomplete_header.resize(10); |
| 139 EXPECT_FALSE(test_helper.InitColumnReaderFromBytes( |
| 140 &reader5, incomplete_header, NULL)); |
| 141 } |
| 142 |
| 143 TEST(PmpColumnReaderTest, StringParsing) { |
| 144 std::vector<std::string> empty_strings(100, ""); |
| 145 |
| 146 // Test empty strings read okay. |
| 147 TestValid(picasaimport::STRING_TYPE, empty_strings); |
| 148 |
| 149 std::vector<std::string> mixed_strings; |
| 150 mixed_strings.push_back(""); |
| 151 mixed_strings.push_back("Hello"); |
| 152 mixed_strings.push_back("World"); |
| 153 mixed_strings.push_back(""); |
| 154 mixed_strings.push_back("123123"); |
| 155 mixed_strings.push_back("Q"); |
| 156 mixed_strings.push_back(""); |
| 157 |
| 158 // Test that a mixed set of strings read correctly. |
| 159 TestValid(picasaimport::STRING_TYPE, mixed_strings); |
| 160 |
| 161 // Test with the data messed up in a variety of ways. |
| 162 TestMalformed(picasaimport::STRING_TYPE, mixed_strings); |
| 163 } |
| 164 |
| 165 TEST(PmpColumnReaderTest, PrimitiveParsing) { |
| 166 TestPrimitive<uint32>(picasaimport::UINT32_TYPE); |
| 167 TestPrimitive<double>(picasaimport::DOUBLE64_TYPE); |
| 168 TestPrimitive<uint8>(picasaimport::UINT8_TYPE); |
| 169 TestPrimitive<uint64>(picasaimport::UINT64_TYPE); |
| 170 } |
| 171 |
| 172 } // namespace |
| OLD | NEW |