| Index: webkit/fileapi/media/picasa/pmp_column_reader_unittest.cc
|
| diff --git a/webkit/fileapi/media/picasa/pmp_column_reader_unittest.cc b/webkit/fileapi/media/picasa/pmp_column_reader_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..f783808846ef569005cd8b213654c987aac8dc73
|
| --- /dev/null
|
| +++ b/webkit/fileapi/media/picasa/pmp_column_reader_unittest.cc
|
| @@ -0,0 +1,202 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include <algorithm>
|
| +#include <vector>
|
| +
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "webkit/fileapi/media/picasa/pmp_column_reader.h"
|
| +#include "webkit/fileapi/media/picasa/pmp_constants.h"
|
| +
|
| +namespace {
|
| +
|
| +using fileapi::PmpColumnReader;
|
| +
|
| +// Return a vector so we don't have to worry about memory management
|
| +std::vector<uint8> MakeHeader(const uint16 field_type, const uint32 row_count) {
|
| + std::vector<uint8> header(fileapi::kPmpHeaderSize);
|
| +
|
| + // Copy in magic bytes
|
| + memcpy(&header[0], fileapi::kPmpMagicO0S4, 4);
|
| + memcpy(&header[6], fileapi::kPmpMagicO6S2, 2);
|
| + memcpy(&header[8], fileapi::kPmpMagicO8S4, 4);
|
| + memcpy(&header[14], fileapi::kPmpMagicO14S2, 2);
|
| +
|
| + // Copy in field type
|
| + memcpy(&header[fileapi::kPmpFieldTypeFirstOffset], &field_type, 2);
|
| + memcpy(&header[fileapi::kPmpFieldTypeSecondOffset], &field_type, 2);
|
| +
|
| + // Copy in row count
|
| + memcpy(&header[fileapi::kPmpRowCountOffset], &row_count, 4);
|
| +
|
| + return header;
|
| +}
|
| +
|
| +// Flatten a vector of elements into an array of bytes
|
| +template<class T>
|
| +std::vector<uint8> Flatten(const std::vector<T>& elems) {
|
| + const uint8* elems0 = reinterpret_cast<const uint8*>(&elems[0]);
|
| + std::vector<uint8> data_body(elems0, elems0 + sizeof(T)*elems.size());
|
| +
|
| + return data_body;
|
| +}
|
| +
|
| +// Custom specialization for strings
|
| +// Given a vector of strings, returns a vector of all the characters in strings
|
| +template<>
|
| +std::vector<uint8> Flatten(const std::vector<std::string>& strings) {
|
| + std::vector<uint8> totalchars;
|
| +
|
| + for(std::vector<std::string>::const_iterator it = strings.begin();
|
| + it != strings.end(); it++) {
|
| + std::copy(it->begin(), it->end(), std::back_inserter(totalchars));
|
| + totalchars.push_back('\0'); // add the null termination too
|
| + }
|
| +
|
| + return totalchars;
|
| +}
|
| +
|
| +std::vector<uint8> Combined(const std::vector<uint8>& a,
|
| + const std::vector<uint8>& b) {
|
| + std::vector<uint8> total;
|
| +
|
| + std::copy(a.begin(), a.end(), std::back_inserter(total));
|
| + std::copy(b.begin(), b.end(), std::back_inserter(total));
|
| +
|
| + return total;
|
| +}
|
| +
|
| +// TestValid
|
| +template<class T>
|
| +void TestValid(PmpColumnReader* reader, const uint16 field_type,
|
| + const std::vector<T>& elems) {
|
| + uint32 rows_read = 0xFF;
|
| +
|
| + std::vector<uint8> data = Combined(MakeHeader(field_type, elems.size()),
|
| + Flatten(elems));
|
| + EXPECT_TRUE(reader->ReadFromMemory(&rows_read, &data[0], data.size()));
|
| + EXPECT_EQ(rows_read, elems.size());
|
| +
|
| + for(uint32 i = 0; i < elems.size(); i++) {
|
| + T target;
|
| + EXPECT_TRUE(reader->Read(i, &target));
|
| + EXPECT_EQ(target, elems[i]);
|
| + }
|
| +}
|
| +
|
| +template<class T>
|
| +void TestMalformed(PmpColumnReader* reader, const uint16 field_type,
|
| + const std::vector<T>& elems) {
|
| +
|
| + std::vector<uint8> data_too_few_declared_rows =
|
| + Combined(MakeHeader(field_type, elems.size()-1), Flatten(elems));
|
| + EXPECT_FALSE(reader->ReadFromMemory(NULL,
|
| + &data_too_few_declared_rows[0],
|
| + data_too_few_declared_rows.size()));
|
| +
|
| + std::vector<uint8> data_too_many_declared_rows =
|
| + Combined(MakeHeader(field_type, elems.size()+1), Flatten(elems));
|
| + EXPECT_FALSE(reader->ReadFromMemory(NULL,
|
| + &data_too_many_declared_rows[0],
|
| + data_too_many_declared_rows.size()));
|
| +
|
| + std::vector<uint8> data_truncated =
|
| + Combined(MakeHeader(field_type, elems.size()), Flatten(elems));
|
| + data_truncated.resize(data_truncated.size()-10);
|
| +
|
| + EXPECT_FALSE(reader->ReadFromMemory(NULL, &data_truncated[0],
|
| + data_truncated.size()));
|
| +
|
| + std::vector<uint8> data_padded =
|
| + Combined(MakeHeader(field_type, elems.size()), Flatten(elems));
|
| + data_padded.resize(data_padded.size()+10);
|
| + EXPECT_FALSE(reader->ReadFromMemory(NULL, &data_padded[0],
|
| + data_padded.size()));
|
| +}
|
| +
|
| +template<class T>
|
| +void TestPrimitives(const uint16 field_type) {
|
| + PmpColumnReader reader("testcolumn");
|
| +
|
| + // Make an ascending vector of the primitive
|
| + uint32 n = 100;
|
| + std::vector<T> data(n, 0);
|
| + for(uint32 i = 0; i < n; i++) {
|
| + data[i] = i*3;
|
| + }
|
| +
|
| + TestValid(&reader, field_type, data);
|
| + TestMalformed(&reader, field_type, data);
|
| +}
|
| +
|
| +
|
| +TEST(PmpColumnReaderTest, HeaderParsingAndValidation) {
|
| + PmpColumnReader reader("testcolumn");
|
| +
|
| + // Good header
|
| + uint32 rows_read = 0xFF;
|
| + std::vector<uint8> good_header = MakeHeader(0x00, 0);
|
| + EXPECT_TRUE(reader.ReadFromMemory(&rows_read, &good_header[0],
|
| + good_header.size()));
|
| + EXPECT_EQ(rows_read, 0U) << "Read non-zero rows from header-only data.";
|
| +
|
| + // Botch up elements of the header
|
| + std::vector<uint8> bad_magic_byte = MakeHeader(0x00, 0);
|
| + bad_magic_byte[0] = 0xff;
|
| + EXPECT_FALSE(reader.ReadFromMemory(NULL, &bad_magic_byte[0],
|
| + bad_magic_byte.size()));
|
| +
|
| + // Where corrupt means the type fields don't agree
|
| + std::vector<uint8> corrupt_type = MakeHeader(0x00, 0);
|
| + corrupt_type[fileapi::kPmpFieldTypeFirstOffset] = 0xff;
|
| + EXPECT_FALSE(reader.ReadFromMemory(NULL, &corrupt_type[0],
|
| + corrupt_type.size()));
|
| +
|
| + std::vector<uint8> invalid_type = MakeHeader(0x00, 0);
|
| + invalid_type[fileapi::kPmpFieldTypeFirstOffset] = 0xff;
|
| + invalid_type[fileapi::kPmpFieldTypeSecondOffset] = 0xff;
|
| + EXPECT_FALSE(reader.ReadFromMemory(NULL, &invalid_type[0],
|
| + invalid_type.size()));
|
| +
|
| + std::vector<uint8> incomplete_header = MakeHeader(0x00, 0);
|
| + incomplete_header.resize(10);
|
| + EXPECT_FALSE(reader.ReadFromMemory(NULL, &incomplete_header[0],
|
| + incomplete_header.size()));
|
| +}
|
| +
|
| +TEST(PmpColumnReaderTest, StringParsing) {
|
| + uint16 string_type = 0x00;
|
| + PmpColumnReader reader("testcolumn");
|
| +
|
| + std::vector<std::string> empty_strings(100, "");
|
| +
|
| + // Test empty strings read okay
|
| + TestValid(&reader, string_type, empty_strings);
|
| +
|
| + std::vector<std::string> mixed_strings;
|
| + mixed_strings.push_back("");
|
| + mixed_strings.push_back("Hello");
|
| + mixed_strings.push_back("World");
|
| + mixed_strings.push_back("");
|
| + mixed_strings.push_back("123123");
|
| + mixed_strings.push_back("Q");
|
| + mixed_strings.push_back("");
|
| +
|
| + // Test that a mixed set of strings read correctly
|
| + TestValid(&reader, string_type, mixed_strings);
|
| +
|
| + // Test with the data messed up in a variety of ways
|
| + TestMalformed(&reader, string_type, mixed_strings);
|
| +}
|
| +
|
| +TEST(PmpColumnReaderTest, PrimitiveParsing) {
|
| + TestPrimitives<uint32>(0x01);
|
| + TestPrimitives<double>(0x02);
|
| + TestPrimitives<uint8>(0x03);
|
| + TestPrimitives<uint64>(0x04);
|
| + TestPrimitives<uint16>(0x05);
|
| + TestPrimitives<uint32>(0x07);
|
| +}
|
| +
|
| +} // namespace
|
|
|