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