Bug 1542744 - P3. Run the same prefixset testcases for different configuration. r=gcp
☠☠ backed out by 836dbcf0ffca ☠ ☠
authordlee <dlee@mozilla.com>
Fri, 10 May 2019 17:40:11 +0000
changeset 532656 f94b6f3a7fff9783dded2b404b84c0500c4182f2
parent 532655 c51b622bb1fe7e0d21df71a02c7f07f03f1e46fe
child 532657 1b3234069aba6667a34988719cca18bc18402fbb
push id11270
push userrgurzau@mozilla.com
push dateWed, 15 May 2019 15:07:19 +0000
treeherdermozilla-beta@571bc76da583 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp
bugs1542744
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1542744 - P3. Run the same prefixset testcases for different configuration. r=gcp This patch does the following: 1. Run the same prefixset tests when * browser.safebrowsing.prefixset.max_array_size = 0 * browser.safebrowsing.prefixset.max_array_size = UINT32_MAX This makes sure both of the methods to store prefixset are tested by existing testcases 2. Refine gtest with test fixture 3. Add TinySet and LargeSet testcases Differential Revision: https://phabricator.services.mozilla.com/D30338
toolkit/components/url-classifier/tests/gtest/TestVariableLengthPrefixSet.cpp
--- a/toolkit/components/url-classifier/tests/gtest/TestVariableLengthPrefixSet.cpp
+++ b/toolkit/components/url-classifier/tests/gtest/TestVariableLengthPrefixSet.cpp
@@ -4,35 +4,38 @@
 
 #include <mozilla/RefPtr.h>
 #include "nsString.h"
 #include "nsTArray.h"
 #include "nsClassHashtable.h"
 #include "VariableLengthPrefixSet.h"
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsIFile.h"
+#include "mozilla/Preferences.h"
 #include "gtest/gtest.h"
 
 using namespace mozilla::safebrowsing;
 
+namespace {
+
 // Create fullhash by appending random characters.
-static nsCString CreateFullHash(const nsACString& in) {
+nsCString CreateFullHash(const nsACString& in) {
   nsCString out(in);
   out.SetLength(32);
   for (size_t i = in.Length(); i < 32; i++) {
     out.SetCharAt(char(rand() % 256), i);
   }
 
   return out;
 }
 
 // This function generate N prefixes with size between MIN and MAX.
 // The output array will not be cleared, random result will append to it
-static void RandomPrefixes(uint32_t N, uint32_t MIN, uint32_t MAX,
-                           _PrefixArray& array) {
+void RandomPrefixes(uint32_t N, uint32_t MIN, uint32_t MAX,
+                    _PrefixArray& array) {
   array.SetCapacity(array.Length() + N);
 
   uint32_t range = (MAX - MIN + 1);
 
   for (uint32_t i = 0; i < N; i++) {
     uint32_t prefixSize = (rand() % range) + MIN;
     _Prefix prefix;
     prefix.SetLength(prefixSize);
@@ -50,17 +53,17 @@ static void RandomPrefixes(uint32_t N, u
       }
     }
   }
 }
 
 // This test loops through all the prefixes and converts each prefix to
 // fullhash by appending random characters, each converted fullhash
 // should at least match its original length in the prefixSet.
-static void DoExpectedLookup(LookupCacheV4* cache, _PrefixArray& array) {
+void DoExpectedLookup(LookupCacheV4* cache, _PrefixArray& array) {
   uint32_t matchLength = 0;
   for (uint32_t i = 0; i < array.Length(); i++) {
     const nsCString& prefix = array[i];
     Completion complete;
     complete.Assign(CreateFullHash(prefix));
 
     // Find match for prefix-generated full hash
     bool has, confirmed;
@@ -83,18 +86,17 @@ static void DoExpectedLookup(LookupCache
           break;
         }
       }
       ASSERT_TRUE(found);
     }
   }
 }
 
-static void DoRandomLookup(LookupCacheV4* cache, uint32_t N,
-                           _PrefixArray& array) {
+void DoRandomLookup(LookupCacheV4* cache, uint32_t N, _PrefixArray& array) {
   for (uint32_t i = 0; i < N; i++) {
     // Random 32-bytes test fullhash
     char buf[32];
     for (uint32_t j = 0; j < 32; j++) {
       buf[j] = (char)(rand() % 256);
     }
 
     // Get the expected result.
@@ -112,284 +114,226 @@ static void DoRandomLookup(LookupCacheV4
     uint32_t matchLength = 0;
     cache->Has(complete, &has, &matchLength, &confirmed);
 
     ASSERT_TRUE(expected.IsEmpty() ? !matchLength
                                    : expected.Contains(matchLength));
   }
 }
 
-static already_AddRefed<LookupCacheV4> SetupLookupCache(
-    const nsACString& aName) {
+already_AddRefed<LookupCacheV4> SetupLookupCache(const nsACString& aName) {
   nsCOMPtr<nsIFile> rootDir;
   NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(rootDir));
 
   nsAutoCString provider("test");
   RefPtr<LookupCacheV4> lookup = new LookupCacheV4(aName, provider, rootDir);
   lookup->Init();
 
   return lookup.forget();
 }
 
-// Test setting prefix set with only 4-bytes prefixes
-TEST(UrlClassifierVLPrefixSet, FixedLengthSet)
-{
-  srand(time(nullptr));
+};  // namespace
 
-  RefPtr<LookupCacheV4> cache = SetupLookupCache(NS_LITERAL_CSTRING("test"));
+class UrlClassifierPrefixSetTest : public ::testing::TestWithParam<uint32_t> {
+ protected:
+  void SetUp() override {
+    // max_array_size to 0 means we are testing delta algorithm here.
+    static const char prefKey[] =
+        "browser.safebrowsing.prefixset.max_array_size";
+    Preferences::SetUint(prefKey, GetParam());
+
+    mCache = SetupLookupCache(NS_LITERAL_CSTRING("test"));
+  }
+
+  void TearDown() override {
+    mCache = nullptr;
+    mArray.Clear();
+    mMap.Clear();
+  }
 
-  PrefixStringMap map;
-  _PrefixArray array = {
-      _Prefix("alph"), _Prefix("brav"), _Prefix("char"),
-      _Prefix("delt"), _Prefix("echo"), _Prefix("foxt"),
-  };
+  nsresult SetupPrefixes(_PrefixArray&& aArray) {
+    mArray = std::move(aArray);
+    SetupPrefixMap(mArray, mMap);
+    return mCache->Build(mMap);
+  }
+
+  void SetupPrefixesAndVerify(_PrefixArray& aArray) {
+    mArray = aArray;
+    SetupPrefixMap(mArray, mMap);
 
-  SetupPrefixMap(array, map);
-  cache->Build(map);
+    ASSERT_TRUE(NS_SUCCEEDED(mCache->Build(mMap)));
+    Verify();
+  }
+
+  void SetupPrefixesAndVerify(_PrefixArray&& aArray) {
+    nsresult rv = SetupPrefixes(std::move(aArray));
+    ASSERT_TRUE(NS_SUCCEEDED(rv));
+    Verify();
+  }
 
-  DoExpectedLookup(cache, array);
-  DoRandomLookup(cache, 1000, array);
-  CheckContent(cache, array);
+  void SetupRandomPrefixesAndVerify(uint32_t N, uint32_t MIN, uint32_t MAX) {
+    srand(time(nullptr));
+    RandomPrefixes(N, MIN, MAX, mArray);
+    SetupPrefixMap(mArray, mMap);
+
+    ASSERT_TRUE(NS_SUCCEEDED(mCache->Build(mMap)));
+    Verify();
+  }
 
-  // Run random test
-  array.Clear();
-  map.Clear();
+  void Verify() {
+    DoExpectedLookup(mCache, mArray);
+    DoRandomLookup(mCache, 1000, mArray);
+    CheckContent(mCache, mArray);
+  }
+
+  RefPtr<LookupCacheV4> mCache;
+  _PrefixArray mArray;
+  PrefixStringMap mMap;
+};
 
-  RandomPrefixes(1500, 4, 4, array);
+// Test setting prefix set with only 4-bytes prefixes
+TEST_P(UrlClassifierPrefixSetTest, FixedLengthSet) {
+  SetupPrefixesAndVerify({
+      _Prefix("alph"),
+      _Prefix("brav"),
+      _Prefix("char"),
+      _Prefix("delt"),
+      _Prefix("echo"),
+      _Prefix("foxt"),
+  });
+}
 
-  SetupPrefixMap(array, map);
-  cache->Build(map);
+TEST_P(UrlClassifierPrefixSetTest, FixedLengthRandomSet) {
+  SetupRandomPrefixesAndVerify(1500, 4, 4);
+}
 
-  DoExpectedLookup(cache, array);
-  DoRandomLookup(cache, 1000, array);
-  CheckContent(cache, array);
+TEST_P(UrlClassifierPrefixSetTest, FixedLengthRandomLargeSet) {
+  SetupRandomPrefixesAndVerify(15000, 4, 4);
+}
+
+TEST_P(UrlClassifierPrefixSetTest, FixedLengthTinySet) {
+  SetupPrefixesAndVerify({
+      _Prefix("tiny"),
+  });
 }
 
 // Test setting prefix set with only 5~32 bytes prefixes
-TEST(UrlClassifierVLPrefixSet, VariableLengthSet)
-{
-  RefPtr<LookupCacheV4> cache = SetupLookupCache(NS_LITERAL_CSTRING("test"));
-
-  PrefixStringMap map;
-  _PrefixArray array = {
-      _Prefix("bravo"),   _Prefix("charlie"),
-      _Prefix("delta"),   _Prefix("EchoEchoEchoEchoEcho"),
-      _Prefix("foxtrot"), _Prefix("GolfGolfGolfGolfGolfGolfGolfGolf"),
-      _Prefix("hotel"),   _Prefix("november"),
-      _Prefix("oscar"),   _Prefix("quebec"),
-      _Prefix("romeo"),   _Prefix("sierrasierrasierrasierrasierra"),
-      _Prefix("Tango"),   _Prefix("whiskey"),
-      _Prefix("yankee"),  _Prefix("ZuluZuluZuluZulu")};
+TEST_P(UrlClassifierPrefixSetTest, VariableLengthSet) {
+  SetupPrefixesAndVerify(
+      {_Prefix("bravo"), _Prefix("charlie"), _Prefix("delta"),
+       _Prefix("EchoEchoEchoEchoEcho"), _Prefix("foxtrot"),
+       _Prefix("GolfGolfGolfGolfGolfGolfGolfGolf"), _Prefix("hotel"),
+       _Prefix("november"), _Prefix("oscar"), _Prefix("quebec"),
+       _Prefix("romeo"), _Prefix("sierrasierrasierrasierrasierra"),
+       _Prefix("Tango"), _Prefix("whiskey"), _Prefix("yankee"),
+       _Prefix("ZuluZuluZuluZulu")});
+}
 
-  SetupPrefixMap(array, map);
-  cache->Build(map);
-
-  DoExpectedLookup(cache, array);
-  DoRandomLookup(cache, 1000, array);
-  CheckContent(cache, array);
-
-  // Run random test
-  array.Clear();
-  map.Clear();
-
-  RandomPrefixes(1500, 5, 32, array);
-
-  SetupPrefixMap(array, map);
-  cache->Build(map);
-
-  DoExpectedLookup(cache, array);
-  DoRandomLookup(cache, 1000, array);
-  CheckContent(cache, array);
+TEST_P(UrlClassifierPrefixSetTest, VariableLengthRandomSet) {
+  SetupRandomPrefixesAndVerify(1500, 5, 32);
 }
 
 // Test setting prefix set with both 4-bytes prefixes and 5~32 bytes prefixes
-TEST(UrlClassifierVLPrefixSet, MixedPrefixSet)
-{
-  RefPtr<LookupCacheV4> cache = SetupLookupCache(NS_LITERAL_CSTRING("test"));
+TEST_P(UrlClassifierPrefixSetTest, MixedPrefixSet) {
+  SetupPrefixesAndVerify(
+      {_Prefix("enus"), _Prefix("apollo"), _Prefix("mars"),
+       _Prefix("Hecatonchires cyclopes"), _Prefix("vesta"), _Prefix("neptunus"),
+       _Prefix("jupiter"), _Prefix("diana"), _Prefix("minerva"),
+       _Prefix("ceres"), _Prefix("Aidos,Adephagia,Adikia,Aletheia"),
+       _Prefix("hecatonchires"), _Prefix("alcyoneus"), _Prefix("hades"),
+       _Prefix("vulcanus"), _Prefix("juno"), _Prefix("mercury"),
+       _Prefix("Stheno, Euryale and Medusa")});
+}
 
-  PrefixStringMap map;
-  _PrefixArray array = {_Prefix("enus"),
-                        _Prefix("apollo"),
-                        _Prefix("mars"),
-                        _Prefix("Hecatonchires cyclopes"),
-                        _Prefix("vesta"),
-                        _Prefix("neptunus"),
-                        _Prefix("jupiter"),
-                        _Prefix("diana"),
-                        _Prefix("minerva"),
-                        _Prefix("ceres"),
-                        _Prefix("Aidos,Adephagia,Adikia,Aletheia"),
-                        _Prefix("hecatonchires"),
-                        _Prefix("alcyoneus"),
-                        _Prefix("hades"),
-                        _Prefix("vulcanus"),
-                        _Prefix("juno"),
-                        _Prefix("mercury"),
-                        _Prefix("Stheno, Euryale and Medusa")};
-
-  SetupPrefixMap(array, map);
-  cache->Build(map);
-
-  DoExpectedLookup(cache, array);
-  DoRandomLookup(cache, 1000, array);
-  CheckContent(cache, array);
-
-  // Run random test
-  array.Clear();
-  map.Clear();
-
-  RandomPrefixes(1500, 4, 32, array);
-
-  SetupPrefixMap(array, map);
-  cache->Build(map);
-
-  DoExpectedLookup(cache, array);
-  DoRandomLookup(cache, 1000, array);
-  CheckContent(cache, array);
+TEST_P(UrlClassifierPrefixSetTest, MixedRandomPrefixSet) {
+  SetupRandomPrefixesAndVerify(1500, 4, 32);
 }
 
 // Test resetting prefix set
-TEST(UrlClassifierVLPrefixSet, ResetPrefix)
-{
-  RefPtr<LookupCacheV4> cache = SetupLookupCache(NS_LITERAL_CSTRING("test"));
-
-  // First prefix set
-  _PrefixArray array1 = {
+TEST_P(UrlClassifierPrefixSetTest, ResetPrefix) {
+  // Base prefix set
+  _PrefixArray oldArray = {
       _Prefix("Iceland"),   _Prefix("Peru"),    _Prefix("Mexico"),
       _Prefix("Australia"), _Prefix("Japan"),   _Prefix("Egypt"),
       _Prefix("America"),   _Prefix("Finland"), _Prefix("Germany"),
       _Prefix("Italy"),     _Prefix("France"),  _Prefix("Taiwan"),
   };
-  {
-    PrefixStringMap map;
-
-    SetupPrefixMap(array1, map);
-    cache->Build(map);
+  SetupPrefixesAndVerify(oldArray);
 
-    DoExpectedLookup(cache, array1);
-  }
-
-  // Second
-  _PrefixArray array2 = {
+  // New prefix set
+  _PrefixArray newArray = {
       _Prefix("Pikachu"),    _Prefix("Bulbasaur"), _Prefix("Charmander"),
       _Prefix("Blastoise"),  _Prefix("Pidgey"),    _Prefix("Mewtwo"),
       _Prefix("Jigglypuff"), _Prefix("Persian"),   _Prefix("Tentacool"),
       _Prefix("Onix"),       _Prefix("Eevee"),     _Prefix("Jynx"),
   };
-  {
-    PrefixStringMap map;
-
-    SetupPrefixMap(array2, map);
-    cache->Build(map);
-
-    DoExpectedLookup(cache, array2);
-  }
+  SetupPrefixesAndVerify(newArray);
 
   // Should not match any of the first prefix set
   uint32_t matchLength = 0;
-  for (uint32_t i = 0; i < array1.Length(); i++) {
+  for (uint32_t i = 0; i < oldArray.Length(); i++) {
     Completion complete;
-    complete.Assign(CreateFullHash(array1[i]));
+    complete.Assign(CreateFullHash(oldArray[i]));
 
     // Find match for prefix-generated full hash
     bool has, confirmed;
-    cache->Has(complete, &has, &matchLength, &confirmed);
+    mCache->Has(complete, &has, &matchLength, &confirmed);
 
     ASSERT_TRUE(matchLength == 0);
   }
 }
 
 // Test only set one 4-bytes prefix and one full-length prefix
-TEST(UrlClassifierVLPrefixSet, TinyPrefixSet)
-{
-  RefPtr<LookupCacheV4> cache = SetupLookupCache(NS_LITERAL_CSTRING("test"));
-
-  PrefixStringMap map;
-  _PrefixArray array = {
+TEST_P(UrlClassifierPrefixSetTest, TinyPrefixSet) {
+  SetupPrefixesAndVerify({
       _Prefix("AAAA"),
       _Prefix("11112222333344445555666677778888"),
-  };
-
-  SetupPrefixMap(array, map);
-  cache->Build(map);
-
-  DoExpectedLookup(cache, array);
-  DoRandomLookup(cache, 1000, array);
-  CheckContent(cache, array);
+  });
 }
 
 // Test empty prefix set and IsEmpty function
-TEST(UrlClassifierVLPrefixSet, EmptyPrefixSet)
-{
-  RefPtr<LookupCacheV4> cache = SetupLookupCache(NS_LITERAL_CSTRING("test"));
+TEST_P(UrlClassifierPrefixSetTest, EmptyFixedPrefixSet) {
+  ASSERT_TRUE(mCache->IsEmpty());
 
-  bool empty = cache->IsEmpty();
-  ASSERT_TRUE(empty);
-
-  PrefixStringMap map;
-  _PrefixArray array1;
-
-  // Lookup an empty array should never match
-  DoRandomLookup(cache, 100, array1);
+  SetupPrefixesAndVerify({});
 
   // Insert an 4-bytes prefix, then IsEmpty should return false
-  _PrefixArray array2 = {_Prefix("test")};
-  SetupPrefixMap(array2, map);
-  cache->Build(map);
+  SetupPrefixesAndVerify({_Prefix("test")});
+
+  ASSERT_TRUE(!mCache->IsEmpty());
+}
 
-  empty = cache->IsEmpty();
-  ASSERT_TRUE(!empty);
+TEST_P(UrlClassifierPrefixSetTest, EmptyVariableLengthPrefixSet) {
+  ASSERT_TRUE(mCache->IsEmpty());
 
-  _PrefixArray array3 = {_Prefix("test variable length")};
+  SetupPrefixesAndVerify({});
 
   // Insert an 5~32 bytes prefix, then IsEmpty should return false
-  SetupPrefixMap(array3, map);
-  cache->Build(map);
+  SetupPrefixesAndVerify({_Prefix("test variable length")});
 
-  empty = cache->IsEmpty();
-  ASSERT_TRUE(!empty);
+  ASSERT_TRUE(!mCache->IsEmpty());
 }
 
 // Test prefix size should only between 4~32 bytes
-TEST(UrlClassifierVLPrefixSet, MinMaxPrefixSet)
-{
-  RefPtr<LookupCacheV4> cache = SetupLookupCache(NS_LITERAL_CSTRING("test"));
-
-  PrefixStringMap map;
-  {
-    _PrefixArray array = {_Prefix("1234"), _Prefix("ABCDEFGHIJKKMNOP"),
-                          _Prefix("1aaa2bbb3ccc4ddd5eee6fff7ggg8hhh")};
-
-    SetupPrefixMap(array, map);
-    nsresult rv = cache->Build(map);
-    ASSERT_TRUE(rv == NS_OK);
-  }
+TEST_P(UrlClassifierPrefixSetTest, MinMaxPrefixSet) {
+  // Test prefix set between 4-32 bytes, should success
+  SetupPrefixesAndVerify({_Prefix("1234"), _Prefix("ABCDEFGHIJKKMNOP"),
+                          _Prefix("1aaa2bbb3ccc4ddd5eee6fff7ggg8hhh")});
 
   // Prefix size less than 4-bytes should fail
-  {
-    _PrefixArray array = {_Prefix("123")};
-
-    SetupPrefixMap(array, map);
-    nsresult rv = cache->Build(map);
-    ASSERT_TRUE(NS_FAILED(rv));
-  }
+  nsresult rv = SetupPrefixes({_Prefix("123")});
+  ASSERT_TRUE(NS_FAILED(rv));
 
   // Prefix size greater than 32-bytes should fail
-  {
-    _PrefixArray array = {_Prefix("1aaa2bbb3ccc4ddd5eee6fff7ggg8hhh9")};
-
-    SetupPrefixMap(array, map);
-    nsresult rv = cache->Build(map);
-    ASSERT_TRUE(NS_FAILED(rv));
-  }
+  rv = SetupPrefixes({_Prefix("1aaa2bbb3ccc4ddd5eee6fff7ggg8hhh9")});
+  ASSERT_TRUE(NS_FAILED(rv));
 }
 
 // Test save then load prefix set with only 4-bytes prefixes
-TEST(UrlClassifierVLPrefixSet, LoadSaveFixedLengthPrefixSet)
-{
+TEST_P(UrlClassifierPrefixSetTest, LoadSaveFixedLengthPrefixSet) {
   nsCOMPtr<nsIFile> file;
   _PrefixArray array;
   PrefixStringMap map;
 
   // Save
   {
     RefPtr<LookupCacheV4> save =
         SetupLookupCache(NS_LITERAL_CSTRING("test-save"));
@@ -418,18 +362,17 @@ TEST(UrlClassifierVLPrefixSet, LoadSaveF
     DoRandomLookup(load, 1000, array);
     CheckContent(load, array);
   }
 
   file->Remove(false);
 }
 
 // Test save then load prefix set with only 5~32 bytes prefixes
-TEST(UrlClassifierVLPrefixSet, LoadSaveVariableLengthPrefixSet)
-{
+TEST_P(UrlClassifierPrefixSetTest, LoadSaveVariableLengthPrefixSet) {
   nsCOMPtr<nsIFile> file;
   _PrefixArray array;
   PrefixStringMap map;
 
   // Save
   {
     RefPtr<LookupCacheV4> save =
         SetupLookupCache(NS_LITERAL_CSTRING("test-save"));
@@ -458,18 +401,17 @@ TEST(UrlClassifierVLPrefixSet, LoadSaveV
     DoRandomLookup(load, 1000, array);
     CheckContent(load, array);
   }
 
   file->Remove(false);
 }
 
 // Test save then load prefix with both 4 bytes prefixes and 5~32 bytes prefixes
-TEST(UrlClassifierVLPrefixSet, LoadSavePrefixSet)
-{
+TEST_P(UrlClassifierPrefixSetTest, LoadSavePrefixSet) {
   nsCOMPtr<nsIFile> file;
   _PrefixArray array;
   PrefixStringMap map;
 
   // Save
   {
     RefPtr<LookupCacheV4> save =
         SetupLookupCache(NS_LITERAL_CSTRING("test-save"));
@@ -500,18 +442,17 @@ TEST(UrlClassifierVLPrefixSet, LoadSaveP
     DoRandomLookup(load, 1000, array);
     CheckContent(load, array);
   }
 
   file->Remove(false);
 }
 
 // This is for fixed-length prefixset
-TEST(UrlClassifierVLPrefixSet, LoadSaveNoDelta)
-{
+TEST_P(UrlClassifierPrefixSetTest, LoadSaveNoDelta) {
   nsCOMPtr<nsIFile> file;
   _PrefixArray array;
   PrefixStringMap map;
 
   for (uint32_t i = 0; i < 100; i++) {
     // construct a tree without deltas by making the distance
     // between entries larger than 16 bits
     uint32_t v = ((1 << 16) + 1) * i;
@@ -544,8 +485,11 @@ TEST(UrlClassifierVLPrefixSet, LoadSaveN
 
     DoExpectedLookup(load, array);
     DoRandomLookup(load, 1000, array);
     CheckContent(load, array);
   }
 
   file->Remove(false);
 }
+
+// To run the same test for different configurations of "browser_safebrowsing_prefixset_max_array_size"
+INSTANTIATE_TEST_CASE_P(UrlClassifierPrefixSetTest, UrlClassifierPrefixSetTest, ::testing::Values(0, UINT32_MAX));