Home | History | Annotate | Download | only in safe_browsing
      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.
      6 
      7 #include "chunk_range.h"
      8 #include "testing/gtest/include/gtest/gtest.h"
      9 
     10 // Test various configurations of chunk numbers.
     11 TEST(SafeBrowsingChunkRangeTest, TestChunksToRangeString) {
     12   std::vector<int> chunks;
     13   std::string range_string;
     14 
     15   // Test one chunk range and one single value.
     16   chunks.push_back(1);
     17   chunks.push_back(2);
     18   chunks.push_back(3);
     19   chunks.push_back(4);
     20   chunks.push_back(7);
     21   ChunksToRangeString(chunks, &range_string);
     22   EXPECT_EQ(range_string, std::string("1-4,7"));
     23 
     24   chunks.clear();
     25   range_string.clear();
     26 
     27   // Test all chunk numbers in one range.
     28   chunks.push_back(3);
     29   chunks.push_back(4);
     30   chunks.push_back(5);
     31   chunks.push_back(6);
     32   chunks.push_back(7);
     33   chunks.push_back(8);
     34   chunks.push_back(9);
     35   chunks.push_back(10);
     36   ChunksToRangeString(chunks, &range_string);
     37   EXPECT_EQ(range_string, std::string("3-10"));
     38 
     39   chunks.clear();
     40   range_string.clear();
     41 
     42   // Test no chunk numbers in contiguous ranges.
     43   chunks.push_back(3);
     44   chunks.push_back(5);
     45   chunks.push_back(7);
     46   chunks.push_back(9);
     47   chunks.push_back(11);
     48   chunks.push_back(13);
     49   chunks.push_back(15);
     50   chunks.push_back(17);
     51   ChunksToRangeString(chunks, &range_string);
     52   EXPECT_EQ(range_string, std::string("3,5,7,9,11,13,15,17"));
     53 
     54   chunks.clear();
     55   range_string.clear();
     56 
     57   // Test a single chunk number.
     58   chunks.push_back(17);
     59   ChunksToRangeString(chunks, &range_string);
     60   EXPECT_EQ(range_string, std::string("17"));
     61 
     62   chunks.clear();
     63   range_string.clear();
     64 
     65   // Test duplicates.
     66   chunks.push_back(1);
     67   chunks.push_back(2);
     68   chunks.push_back(2);
     69   chunks.push_back(2);
     70   chunks.push_back(3);
     71   chunks.push_back(7);
     72   chunks.push_back(7);
     73   chunks.push_back(7);
     74   chunks.push_back(7);
     75   ChunksToRangeString(chunks, &range_string);
     76   EXPECT_EQ(range_string, std::string("1-3,7"));
     77 
     78   // Test unsorted chunks.
     79   chunks.push_back(4);
     80   chunks.push_back(1);
     81   chunks.push_back(7);
     82   chunks.push_back(3);
     83   chunks.push_back(2);
     84   ChunksToRangeString(chunks, &range_string);
     85   EXPECT_EQ(range_string, std::string("1-4,7"));
     86 
     87   chunks.clear();
     88   range_string.clear();
     89 }
     90 
     91 TEST(SafeBrowsingChunkRangeTest, TestStringToRanges) {
     92   std::vector<ChunkRange> ranges;
     93 
     94   std::string input = "1-100,398,415,1138-2001,2019";
     95   EXPECT_TRUE(StringToRanges(input, &ranges));
     96   EXPECT_EQ(ranges.size(), static_cast<size_t>(5));
     97   EXPECT_EQ(ranges[0].start(), 1);
     98   EXPECT_EQ(ranges[0].stop(),  100);
     99   EXPECT_EQ(ranges[1].start(), 398);
    100   EXPECT_EQ(ranges[1].stop(),  398);
    101   EXPECT_EQ(ranges[3].start(), 1138);
    102   EXPECT_EQ(ranges[3].stop(),  2001);
    103 
    104   ranges.clear();
    105 
    106   input = "1,2,3,4,5,6,7";
    107   EXPECT_TRUE(StringToRanges(input, &ranges));
    108   EXPECT_EQ(ranges.size(), static_cast<size_t>(7));
    109 
    110   ranges.clear();
    111 
    112   input = "300-3001";
    113   EXPECT_TRUE(StringToRanges(input, &ranges));
    114   EXPECT_EQ(ranges.size(), static_cast<size_t>(1));
    115   EXPECT_EQ(ranges[0].start(),  300);
    116   EXPECT_EQ(ranges[0].stop(),  3001);
    117 
    118   ranges.clear();
    119 
    120   input = "17";
    121   EXPECT_TRUE(StringToRanges(input, &ranges));
    122   EXPECT_EQ(ranges.size(), static_cast<size_t>(1));
    123   EXPECT_EQ(ranges[0].start(), 17);
    124   EXPECT_EQ(ranges[0].stop(),  17);
    125 
    126   ranges.clear();
    127 
    128   input = "x-y";
    129   EXPECT_FALSE(StringToRanges(input, &ranges));
    130 }
    131 
    132 
    133 TEST(SafeBrowsingChunkRangeTest, TestRangesToChunks) {
    134   std::vector<ChunkRange> ranges;
    135   ranges.push_back(ChunkRange(1, 4));
    136   ranges.push_back(ChunkRange(17));
    137 
    138   std::vector<int> chunks;
    139   RangesToChunks(ranges, &chunks);
    140 
    141   EXPECT_EQ(chunks.size(), static_cast<size_t>(5));
    142   EXPECT_EQ(chunks[0], 1);
    143   EXPECT_EQ(chunks[1], 2);
    144   EXPECT_EQ(chunks[2], 3);
    145   EXPECT_EQ(chunks[3], 4);
    146   EXPECT_EQ(chunks[4], 17);
    147 }
    148 
    149 
    150 TEST(SafeBrowsingChunkRangeTest, TestSearchChunkRanges) {
    151   std::string range_str("1-10,15-17,21-410,555,991-1000");
    152   std::vector<ChunkRange> ranges;
    153   StringToRanges(range_str, &ranges);
    154 
    155   EXPECT_TRUE(IsChunkInRange(7, ranges));
    156   EXPECT_TRUE(IsChunkInRange(300, ranges));
    157   EXPECT_TRUE(IsChunkInRange(555, ranges));
    158   EXPECT_TRUE(IsChunkInRange(1, ranges));
    159   EXPECT_TRUE(IsChunkInRange(1000, ranges));
    160 
    161   EXPECT_FALSE(IsChunkInRange(11, ranges));
    162   EXPECT_FALSE(IsChunkInRange(990, ranges));
    163   EXPECT_FALSE(IsChunkInRange(2000, ranges));
    164 }
    165