Add domain request detection to incident reporting service.
[chromium-blink-merge.git] / chrome / browser / safe_browsing / chunk_range_unittest.cc
blobc0960df792fc59e22cfbf1eb93fb16a12f556f16
1 // Copyright (c) 2006-2008 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.
4 //
5 // Test program to convert lists of integers into ranges, and vice versa.
7 #include "chrome/browser/safe_browsing/chunk_range.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 // Test various configurations of chunk numbers.
12 TEST(SafeBrowsingChunkRangeTest, TestChunksToRangeString) {
13 std::vector<int> chunks;
14 std::string range_string;
16 // Test one chunk range and one single value.
17 chunks.push_back(1);
18 chunks.push_back(2);
19 chunks.push_back(3);
20 chunks.push_back(4);
21 chunks.push_back(7);
22 ChunksToRangeString(chunks, &range_string);
23 EXPECT_EQ(range_string, std::string("1-4,7"));
25 chunks.clear();
26 range_string.clear();
28 // Test all chunk numbers in one range.
29 chunks.push_back(3);
30 chunks.push_back(4);
31 chunks.push_back(5);
32 chunks.push_back(6);
33 chunks.push_back(7);
34 chunks.push_back(8);
35 chunks.push_back(9);
36 chunks.push_back(10);
37 ChunksToRangeString(chunks, &range_string);
38 EXPECT_EQ(range_string, std::string("3-10"));
40 chunks.clear();
41 range_string.clear();
43 // Test no chunk numbers in contiguous ranges.
44 chunks.push_back(3);
45 chunks.push_back(5);
46 chunks.push_back(7);
47 chunks.push_back(9);
48 chunks.push_back(11);
49 chunks.push_back(13);
50 chunks.push_back(15);
51 chunks.push_back(17);
52 ChunksToRangeString(chunks, &range_string);
53 EXPECT_EQ(range_string, std::string("3,5,7,9,11,13,15,17"));
55 chunks.clear();
56 range_string.clear();
58 // Test a single chunk number.
59 chunks.push_back(17);
60 ChunksToRangeString(chunks, &range_string);
61 EXPECT_EQ(range_string, std::string("17"));
63 chunks.clear();
64 range_string.clear();
66 // Test duplicates.
67 chunks.push_back(1);
68 chunks.push_back(2);
69 chunks.push_back(2);
70 chunks.push_back(2);
71 chunks.push_back(3);
72 chunks.push_back(7);
73 chunks.push_back(7);
74 chunks.push_back(7);
75 chunks.push_back(7);
76 ChunksToRangeString(chunks, &range_string);
77 EXPECT_EQ(range_string, std::string("1-3,7"));
79 // Test unsorted chunks.
80 chunks.push_back(4);
81 chunks.push_back(1);
82 chunks.push_back(7);
83 chunks.push_back(3);
84 chunks.push_back(2);
85 ChunksToRangeString(chunks, &range_string);
86 EXPECT_EQ(range_string, std::string("1-4,7"));
88 chunks.clear();
89 range_string.clear();
92 TEST(SafeBrowsingChunkRangeTest, TestStringToRanges) {
93 std::vector<ChunkRange> ranges;
95 std::string input = "1-100,398,415,1138-2001,2019";
96 EXPECT_TRUE(StringToRanges(input, &ranges));
97 EXPECT_EQ(ranges.size(), static_cast<size_t>(5));
98 EXPECT_EQ(ranges[0].start(), 1);
99 EXPECT_EQ(ranges[0].stop(), 100);
100 EXPECT_EQ(ranges[1].start(), 398);
101 EXPECT_EQ(ranges[1].stop(), 398);
102 EXPECT_EQ(ranges[3].start(), 1138);
103 EXPECT_EQ(ranges[3].stop(), 2001);
105 ranges.clear();
107 input = "1,2,3,4,5,6,7";
108 EXPECT_TRUE(StringToRanges(input, &ranges));
109 EXPECT_EQ(ranges.size(), static_cast<size_t>(7));
111 ranges.clear();
113 input = "300-3001";
114 EXPECT_TRUE(StringToRanges(input, &ranges));
115 EXPECT_EQ(ranges.size(), static_cast<size_t>(1));
116 EXPECT_EQ(ranges[0].start(), 300);
117 EXPECT_EQ(ranges[0].stop(), 3001);
119 ranges.clear();
121 input = "17";
122 EXPECT_TRUE(StringToRanges(input, &ranges));
123 EXPECT_EQ(ranges.size(), static_cast<size_t>(1));
124 EXPECT_EQ(ranges[0].start(), 17);
125 EXPECT_EQ(ranges[0].stop(), 17);
127 ranges.clear();
129 input = "x-y";
130 EXPECT_FALSE(StringToRanges(input, &ranges));
134 TEST(SafeBrowsingChunkRangeTest, TestRangesToChunks) {
135 std::vector<ChunkRange> ranges;
136 ranges.push_back(ChunkRange(1, 4));
137 ranges.push_back(ChunkRange(17));
139 std::vector<int> chunks;
140 RangesToChunks(ranges, &chunks);
142 EXPECT_EQ(chunks.size(), static_cast<size_t>(5));
143 EXPECT_EQ(chunks[0], 1);
144 EXPECT_EQ(chunks[1], 2);
145 EXPECT_EQ(chunks[2], 3);
146 EXPECT_EQ(chunks[3], 4);
147 EXPECT_EQ(chunks[4], 17);
151 TEST(SafeBrowsingChunkRangeTest, TestSearchChunkRanges) {
152 std::string range_str("1-10,15-17,21-410,555,991-1000");
153 std::vector<ChunkRange> ranges;
154 StringToRanges(range_str, &ranges);
156 EXPECT_TRUE(IsChunkInRange(7, ranges));
157 EXPECT_TRUE(IsChunkInRange(300, ranges));
158 EXPECT_TRUE(IsChunkInRange(555, ranges));
159 EXPECT_TRUE(IsChunkInRange(1, ranges));
160 EXPECT_TRUE(IsChunkInRange(1000, ranges));
162 EXPECT_FALSE(IsChunkInRange(11, ranges));
163 EXPECT_FALSE(IsChunkInRange(990, ranges));
164 EXPECT_FALSE(IsChunkInRange(2000, ranges));