Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / child / blob_storage / blob_consolidation_unittest.cc
blob77f78733fd48f95a0232d71baea4d5d300ff7b3c
1 // Copyright 2015 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.
5 #include "content/child/blob_storage/blob_consolidation.h"
7 #include "base/files/file_path.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using ReadStatus = content::BlobConsolidation::ReadStatus;
12 using storage::DataElement;
14 namespace content {
15 namespace {
17 static blink::WebThreadSafeData CreateData(const std::string& str) {
18 return blink::WebThreadSafeData(str.c_str(), str.size());
21 TEST(BlobConsolidationTest, TestSegmentation) {
22 BlobConsolidation consolidation;
23 consolidation.AddDataItem(CreateData("12345"));
24 EXPECT_EQ(5lu, consolidation.total_memory());
26 const auto& items = consolidation.consolidated_items();
27 EXPECT_EQ(1u, items.size());
28 EXPECT_EQ(5lu, items[0].length);
29 EXPECT_EQ(DataElement::TYPE_BYTES, items[0].type);
30 EXPECT_EQ(0lu, items[0].offset);
32 char memory[] = {'E'};
33 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 0, 1, memory));
34 EXPECT_EQ('1', memory[0]);
35 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 1, 1, memory));
36 EXPECT_EQ('2', memory[0]);
37 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 2, 1, memory));
38 EXPECT_EQ('3', memory[0]);
39 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 3, 1, memory));
40 EXPECT_EQ('4', memory[0]);
41 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 4, 1, memory));
42 EXPECT_EQ('5', memory[0]);
45 TEST(BlobConsolidationTest, TestConsolidation) {
46 BlobConsolidation consolidation;
47 consolidation.AddDataItem(CreateData("1"));
48 consolidation.AddDataItem(CreateData("23"));
49 consolidation.AddDataItem(CreateData("4"));
50 EXPECT_EQ(4u, consolidation.total_memory());
52 const auto& items = consolidation.consolidated_items();
53 EXPECT_EQ(1u, items.size());
54 EXPECT_EQ(4lu, items[0].length);
55 EXPECT_EQ(DataElement::TYPE_BYTES, items[0].type);
56 EXPECT_EQ(0lu, items[0].offset);
58 char memory[4];
59 EXPECT_EQ(ReadStatus::ERROR_OUT_OF_BOUNDS,
60 consolidation.ReadMemory(0, 0, 5, memory));
61 EXPECT_EQ(ReadStatus::ERROR_OUT_OF_BOUNDS,
62 consolidation.ReadMemory(1, 0, 4, memory));
63 EXPECT_EQ(ReadStatus::ERROR_OUT_OF_BOUNDS,
64 consolidation.ReadMemory(0, 1, 4, memory));
65 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 0, 4, memory));
67 char expected_memory[] = {'1', '2', '3', '4'};
68 EXPECT_THAT(memory, testing::ElementsAreArray(expected_memory));
71 TEST(BlobConsolidationTest, TestMassiveConsolidation) {
72 BlobConsolidation consolidation;
73 const size_t kNumParts = 300;
74 const size_t kPartSize = 5;
75 const size_t kTotalMemory = kNumParts * kPartSize;
76 const size_t kReadSegmentSize = 6; // Must be factor of kTotalMemory
77 const size_t kNumReadSegments = kTotalMemory / kReadSegmentSize;
79 char current_value = 0;
80 for (size_t i = 0; i < kNumParts; i++) {
81 char data[kPartSize];
82 for (size_t j = 0; j < kPartSize; j++) {
83 data[j] = current_value;
84 ++current_value;
86 consolidation.AddDataItem(blink::WebThreadSafeData(data, kPartSize));
88 EXPECT_EQ(kTotalMemory, consolidation.total_memory());
90 const auto& items = consolidation.consolidated_items();
91 EXPECT_EQ(1u, items.size());
92 EXPECT_EQ(kTotalMemory, items[0].length);
94 char expected_value = 0;
95 char read_buffer[kReadSegmentSize];
96 for (size_t i = 0; i < kNumReadSegments; i++) {
97 EXPECT_EQ(ReadStatus::OK,
98 consolidation.ReadMemory(0, i * kReadSegmentSize,
99 kReadSegmentSize, read_buffer));
100 for (size_t j = 0; j < kReadSegmentSize; j++) {
101 EXPECT_EQ(expected_value, read_buffer[j]);
102 ++expected_value;
107 TEST(BlobConsolidationTest, TestPartialRead) {
108 BlobConsolidation consolidation;
109 consolidation.AddDataItem(CreateData("1"));
110 consolidation.AddDataItem(CreateData("23"));
111 consolidation.AddDataItem(CreateData("45"));
112 EXPECT_EQ(5u, consolidation.total_memory());
114 const auto& items = consolidation.consolidated_items();
115 EXPECT_EQ(1u, items.size());
116 EXPECT_EQ(5lu, items[0].length);
117 EXPECT_EQ(0lu, items[0].offset);
119 char memory_part1[] = {'X', 'X'};
120 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 0, 2, memory_part1));
121 char expected_memory_part1[] = {'1', '2'};
122 EXPECT_THAT(memory_part1, testing::ElementsAreArray(expected_memory_part1));
124 char memory_part2[] = {'X', 'X'};
125 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 2, 2, memory_part2));
126 char expected_memory_part2[] = {'3', '4'};
127 EXPECT_THAT(memory_part2, testing::ElementsAreArray(expected_memory_part2));
129 char memory_part3[] = {'X'};
130 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 4, 1, memory_part3));
131 char expected_memory_part3[] = {'5'};
132 EXPECT_THAT(memory_part3, testing::ElementsAreArray(expected_memory_part3));
135 TEST(BlobConsolidationTest, TestBoundaries) {
136 BlobConsolidation consolidation;
137 consolidation.AddDataItem(CreateData("1"));
138 consolidation.AddFileItem(base::FilePath(FILE_PATH_LITERAL("testPath")), 1,
139 10, 5.0);
140 consolidation.AddDataItem(CreateData("2"));
141 consolidation.AddDataItem(CreateData("3"));
142 consolidation.AddBlobItem("testUUID", 1, 2);
143 consolidation.AddDataItem(CreateData("45"));
144 EXPECT_EQ(5u, consolidation.total_memory());
146 const auto& items = consolidation.consolidated_items();
147 EXPECT_EQ(5u, items.size());
149 EXPECT_EQ(1lu, items[0].length);
150 EXPECT_EQ(DataElement::TYPE_BYTES, items[0].type);
152 EXPECT_EQ(10lu, items[1].length);
153 EXPECT_EQ(DataElement::TYPE_FILE, items[1].type);
155 EXPECT_EQ(2lu, items[2].length);
156 EXPECT_EQ(DataElement::TYPE_BYTES, items[2].type);
158 EXPECT_EQ(2lu, items[3].length);
159 EXPECT_EQ(DataElement::TYPE_BLOB, items[3].type);
161 EXPECT_EQ(2lu, items[4].length);
162 EXPECT_EQ(DataElement::TYPE_BYTES, items[4].type);
164 char test_memory[5];
165 EXPECT_EQ(ReadStatus::ERROR_WRONG_TYPE,
166 consolidation.ReadMemory(1, 0, 1, test_memory));
167 EXPECT_EQ(ReadStatus::ERROR_WRONG_TYPE,
168 consolidation.ReadMemory(3, 0, 1, test_memory));
170 char memory_part1[1];
171 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(0, 0, 1, memory_part1));
172 char expected_memory_part1[] = {'1'};
173 EXPECT_THAT(memory_part1, testing::ElementsAreArray(expected_memory_part1));
175 char memory_part2[2];
176 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(2, 0, 2, memory_part2));
177 char expected_memory_part2[] = {'2', '3'};
178 EXPECT_THAT(memory_part2, testing::ElementsAreArray(expected_memory_part2));
180 char memory_part3[2];
181 EXPECT_EQ(ReadStatus::OK, consolidation.ReadMemory(4, 0, 2, memory_part3));
182 char expected_memory_part3[] = {'4', '5'};
183 EXPECT_THAT(memory_part3, testing::ElementsAreArray(expected_memory_part3));
186 } // namespace
187 } // namespace content