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

Side by Side Diff: ui/base/resource/data_pack_unittest.cc

Issue 394313002: Add support for loading pak files from arbitrary file regions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move Region to MemoryMappedFile Created 6 years, 4 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/files/file.h" 6 #include "base/files/file.h"
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/strings/string_piece.h" 10 #include "base/strings/string_piece.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 ASSERT_TRUE(pack.GetStringPiece(1, &data)); 85 ASSERT_TRUE(pack.GetStringPiece(1, &data));
86 EXPECT_EQ(0U, data.length()); 86 EXPECT_EQ(0U, data.length());
87 ASSERT_TRUE(pack.GetStringPiece(10, &data)); 87 ASSERT_TRUE(pack.GetStringPiece(10, &data));
88 EXPECT_EQ(0U, data.length()); 88 EXPECT_EQ(0U, data.length());
89 89
90 // Try looking up an invalid key. 90 // Try looking up an invalid key.
91 ASSERT_FALSE(pack.HasResource(140)); 91 ASSERT_FALSE(pack.HasResource(140));
92 ASSERT_FALSE(pack.GetStringPiece(140, &data)); 92 ASSERT_FALSE(pack.GetStringPiece(140, &data));
93 } 93 }
94 94
95 TEST(DataPackTest, LoadFromFileRegion) {
96 base::ScopedTempDir dir;
97 ASSERT_TRUE(dir.CreateUniqueTempDir());
98 base::FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak"));
99
100 // Construct a file which has a non page-aligned zero-filled header followed
101 // by the actual pak file content.
102 const char kPadding[5678] = {0};
103 ASSERT_EQ(static_cast<int>(sizeof(kPadding)),
104 base::WriteFile(data_path, kPadding, sizeof(kPadding)));
105 ASSERT_EQ(static_cast<int>(kSamplePakSize),
106 base::AppendToFile(data_path, kSamplePakContents, kSamplePakSize));
107
108 base::File file(data_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
109 ASSERT_TRUE(file.IsValid());
110
111 // Load the file through the data pack API.
112 DataPack pack(SCALE_FACTOR_100P);
113 base::MemoryMappedFile::Region region(sizeof(kPadding), kSamplePakSize);
114 ASSERT_TRUE(pack.LoadFromFileRegion(file.Pass(), region));
115
116 base::StringPiece data;
117 ASSERT_TRUE(pack.HasResource(4));
118 ASSERT_TRUE(pack.GetStringPiece(4, &data));
119 EXPECT_EQ("this is id 4", data);
120 ASSERT_TRUE(pack.HasResource(6));
121 ASSERT_TRUE(pack.GetStringPiece(6, &data));
122 EXPECT_EQ("this is id 6", data);
123
124 // Try reading zero-length data blobs, just in case.
125 ASSERT_TRUE(pack.GetStringPiece(1, &data));
126 EXPECT_EQ(0U, data.length());
127 ASSERT_TRUE(pack.GetStringPiece(10, &data));
128 EXPECT_EQ(0U, data.length());
129
130 // Try looking up an invalid key.
131 ASSERT_FALSE(pack.HasResource(140));
132 ASSERT_FALSE(pack.GetStringPiece(140, &data));
133 }
134
95 INSTANTIATE_TEST_CASE_P(WriteBINARY, DataPackTest, ::testing::Values( 135 INSTANTIATE_TEST_CASE_P(WriteBINARY, DataPackTest, ::testing::Values(
96 DataPack::BINARY)); 136 DataPack::BINARY));
97 INSTANTIATE_TEST_CASE_P(WriteUTF8, DataPackTest, ::testing::Values( 137 INSTANTIATE_TEST_CASE_P(WriteUTF8, DataPackTest, ::testing::Values(
98 DataPack::UTF8)); 138 DataPack::UTF8));
99 INSTANTIATE_TEST_CASE_P(WriteUTF16, DataPackTest, ::testing::Values( 139 INSTANTIATE_TEST_CASE_P(WriteUTF16, DataPackTest, ::testing::Values(
100 DataPack::UTF16)); 140 DataPack::UTF16));
101 141
102 TEST(DataPackTest, LoadFileWithTruncatedHeader) { 142 TEST(DataPackTest, LoadFileWithTruncatedHeader) {
103 base::FilePath data_path; 143 base::FilePath data_path;
104 ASSERT_TRUE(PathService::Get(UI_DIR_TEST_DATA, &data_path)); 144 ASSERT_TRUE(PathService::Get(UI_DIR_TEST_DATA, &data_path));
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 kSampleCorruptPakSize), 210 kSampleCorruptPakSize),
171 static_cast<int>(kSampleCorruptPakSize)); 211 static_cast<int>(kSampleCorruptPakSize));
172 212
173 // Reading asset #10 should now fail as it extends past the end of the file. 213 // Reading asset #10 should now fail as it extends past the end of the file.
174 ASSERT_TRUE(pack.HasResource(10)); 214 ASSERT_TRUE(pack.HasResource(10));
175 ASSERT_FALSE(pack.GetStringPiece(10, &data)); 215 ASSERT_FALSE(pack.GetStringPiece(10, &data));
176 } 216 }
177 #endif 217 #endif
178 218
179 } // namespace ui 219 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698