1c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Copyright (c) 2008 The Chromium Authors. All rights reserved. 2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be 3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file. 4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/data_pack.h" 6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/file_path.h" 8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/file_util.h" 9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/path_service.h" 10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/scoped_temp_dir.h" 11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/string_piece.h" 12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "testing/gtest/include/gtest/gtest.h" 13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(DataPackTest, Load) { 15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott FilePath data_path; 16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott PathService::Get(base::DIR_SOURCE_ROOT, &data_path); 17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott data_path = data_path.Append( 18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott FILE_PATH_LITERAL("base/data/data_pack_unittest/sample.pak")); 19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::DataPack pack; 21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.Load(data_path)); 22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::StringPiece data; 24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.GetStringPiece(4, &data)); 25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("this is id 4", data); 26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.GetStringPiece(6, &data)); 27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ("this is id 6", data); 28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Try reading zero-length data blobs, just in case. 30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.GetStringPiece(1, &data)); 31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ(0U, data.length()); 32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.GetStringPiece(10, &data)); 33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ(0U, data.length()); 34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Try looking up an invalid key. 36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_FALSE(pack.GetStringPiece(140, &data)); 37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick ScottTEST(DataPackTest, Write) { 40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ScopedTempDir dir; 41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(dir.CreateUniqueTempDir()); 42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott FilePath file = dir.path().Append(FILE_PATH_LITERAL("data.pak")); 43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string one("one"); 45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string two("two"); 46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string three("three"); 47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string four("four"); 48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::string fifteen("fifteen"); 49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott std::map<uint32, base::StringPiece> resources; 51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott resources.insert(std::make_pair(1, base::StringPiece(one))); 52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott resources.insert(std::make_pair(2, base::StringPiece(two))); 53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott resources.insert(std::make_pair(15, base::StringPiece(fifteen))); 54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott resources.insert(std::make_pair(3, base::StringPiece(three))); 55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott resources.insert(std::make_pair(4, base::StringPiece(four))); 56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(base::DataPack::WritePack(file, resources)); 57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott // Now try to read the data back in. 59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::DataPack pack; 60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.Load(file)); 61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott 62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott base::StringPiece data; 63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.GetStringPiece(1, &data)); 64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ(one, data); 65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.GetStringPiece(2, &data)); 66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ(two, data); 67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.GetStringPiece(3, &data)); 68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ(three, data); 69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.GetStringPiece(4, &data)); 70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ(four, data); 71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott ASSERT_TRUE(pack.GetStringPiece(15, &data)); 72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott EXPECT_EQ(fifteen, data); 73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott} 74