Bug 1353956 - P7. Add gtest to ensure .pset is correctly loaded and removed. r=gcp
authordlee <dlee@mozilla.com>
Thu, 07 Mar 2019 14:43:16 +0000
changeset 462961 9bac094cec23aa298e04e8326cbd8d9b558575f0
parent 462960 a87bd3f9b87d37bfb3b2ee5137c58bfa669ce1cb
child 462962 9aabc3ad2470032f34bf20cafcb5c0bc2c61d64e
push id79946
push userdlee@mozilla.com
push dateThu, 07 Mar 2019 20:29:26 +0000
treeherderautoland@9bac094cec23 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgcp
bugs1353956
milestone67.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 1353956 - P7. Add gtest to ensure .pset is correctly loaded and removed. r=gcp Differential Revision: https://phabricator.services.mozilla.com/D22490
toolkit/components/url-classifier/tests/gtest/Common.cpp
toolkit/components/url-classifier/tests/gtest/Common.h
toolkit/components/url-classifier/tests/gtest/TestLookupCacheV4.cpp
toolkit/components/url-classifier/tests/gtest/TestVariableLengthPrefixSet.cpp
--- a/toolkit/components/url-classifier/tests/gtest/Common.cpp
+++ b/toolkit/components/url-classifier/tests/gtest/Common.cpp
@@ -142,16 +142,28 @@ nsCString GeneratePrefix(const nsCString
   Completion complete;
   complete.FromPlaintext(aFragment);
 
   nsCString hash;
   hash.Assign((const char*)complete.buf, aLength);
   return hash;
 }
 
+void CheckContent(LookupCacheV4* cache, PrefixStringMap& expected) {
+  PrefixStringMap vlPSetMap;
+  cache->GetPrefixes(vlPSetMap);
+
+  for (auto iter = vlPSetMap.Iter(); !iter.Done(); iter.Next()) {
+    nsCString* expectedPrefix = expected.Get(iter.Key());
+    nsCString* resultPrefix = iter.Data();
+
+    ASSERT_TRUE(resultPrefix->Equals(*expectedPrefix));
+  }
+}
+
 static nsresult BuildCache(LookupCacheV2* cache,
                            const _PrefixArray& prefixArray) {
   AddPrefixArray prefixes;
   AddCompleteArray completions;
   nsresult rv = PrefixArrayToAddPrefixArrayV2(prefixArray, prefixes);
   if (NS_FAILED(rv)) {
     return rv;
   }
--- a/toolkit/components/url-classifier/tests/gtest/Common.h
+++ b/toolkit/components/url-classifier/tests/gtest/Common.h
@@ -5,16 +5,17 @@
 #include "gtest/gtest.h"
 
 using namespace mozilla;
 using namespace mozilla::safebrowsing;
 
 namespace mozilla {
 namespace safebrowsing {
 class Classifier;
+class LookupCacheV4;
 }
 }  // namespace mozilla
 
 typedef nsCString _Fragment;
 typedef nsTArray<nsCString> _PrefixArray;
 
 template <typename Function>
 void RunTestInNewThread(Function&& aFunction);
@@ -38,11 +39,14 @@ void PrefixArrayToPrefixStringMap(const 
                                   PrefixStringMap& out);
 
 nsresult PrefixArrayToAddPrefixArrayV2(const nsTArray<nsCString>& prefixArray,
                                        AddPrefixArray& out);
 
 // Generate a hash prefix from string
 nsCString GeneratePrefix(const nsCString& aFragment, uint8_t aLength);
 
+// To test if the content is equal
+void CheckContent(LookupCacheV4* cache, PrefixStringMap& expected);
+
 // Create a LookupCacheV4 object with sepecified prefix array.
 template <typename T>
 RefPtr<T> SetupLookupCache(const _PrefixArray& prefixArray);
--- a/toolkit/components/url-classifier/tests/gtest/TestLookupCacheV4.cpp
+++ b/toolkit/components/url-classifier/tests/gtest/TestLookupCacheV4.cpp
@@ -38,8 +38,90 @@ TEST(UrlClassifierLookupCacheV4, HasComp
 
 TEST(UrlClassifierLookupCacheV4, HasPrefix) {
   TestHasPrefix(_Fragment("browsing.com/"), true, false);
 }
 
 TEST(UrlClassifierLookupCacheV4, Nomatch) {
   TestHasPrefix(_Fragment("nomatch.com/"), false, false);
 }
+
+// Test an existing .pset should be removed after .vlpset is written
+TEST(UrlClassifierLookupCacheV4, RemoveOldPset) {
+  nsCOMPtr<nsIFile> oldPsetFile;
+  NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
+                         getter_AddRefs(oldPsetFile));
+  oldPsetFile->AppendNative(NS_LITERAL_CSTRING("safebrowsing"));
+  oldPsetFile->AppendNative(GTEST_TABLE + NS_LITERAL_CSTRING(".pset"));
+
+  nsCOMPtr<nsIFile> newPsetFile;
+  NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
+                         getter_AddRefs(newPsetFile));
+  newPsetFile->AppendNative(NS_LITERAL_CSTRING("safebrowsing"));
+  newPsetFile->AppendNative(GTEST_TABLE + NS_LITERAL_CSTRING(".vlpset"));
+
+  // Create the legacy .pset file
+  nsresult rv = oldPsetFile->Create(nsIFile::NORMAL_FILE_TYPE, 0666);
+  EXPECT_EQ(rv, NS_OK);
+
+  bool exists;
+  rv = oldPsetFile->Exists(&exists);
+  EXPECT_EQ(rv, NS_OK);
+  EXPECT_EQ(exists, true);
+
+  // Setup the data in lookup cache and write its data to disk
+  RefPtr<Classifier> classifier = GetClassifier();
+  _PrefixArray array = {GeneratePrefix(_Fragment("entry.com/"), 4)};
+  rv = SetupLookupCacheV4(classifier, array, GTEST_TABLE);
+  EXPECT_EQ(rv, NS_OK);
+
+  RefPtr<LookupCache> cache = classifier->GetLookupCache(GTEST_TABLE, false);
+  rv = cache->WriteFile();
+  EXPECT_EQ(rv, NS_OK);
+
+  // .vlpset should exist while .pset should be removed
+  rv = newPsetFile->Exists(&exists);
+  EXPECT_EQ(rv, NS_OK);
+  EXPECT_EQ(exists, true);
+
+  rv = oldPsetFile->Exists(&exists);
+  EXPECT_EQ(rv, NS_OK);
+  EXPECT_EQ(exists, false);
+
+  newPsetFile->Remove(false);
+}
+
+// Test the legacy load
+TEST(UrlClassifierLookupCacheV4, LoadOldPset) {
+  nsCOMPtr<nsIFile> oldPsetFile;
+
+  _PrefixArray array = {GeneratePrefix(_Fragment("entry.com/"), 4)};
+  PrefixStringMap map;
+  PrefixArrayToPrefixStringMap(array, map);
+
+  // Prepare .pset file on disk
+  {
+    NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
+                           getter_AddRefs(oldPsetFile));
+    oldPsetFile->AppendNative(NS_LITERAL_CSTRING("safebrowsing"));
+    oldPsetFile->AppendNative(GTEST_TABLE + NS_LITERAL_CSTRING(".pset"));
+
+    RefPtr<VariableLengthPrefixSet> pset = new VariableLengthPrefixSet;
+    pset->SetPrefixes(map);
+
+    nsCOMPtr<nsIOutputStream> stream;
+    nsresult rv =
+        NS_NewLocalFileOutputStream(getter_AddRefs(stream), oldPsetFile);
+    EXPECT_EQ(rv, NS_OK);
+
+    rv = pset->WritePrefixes(stream);
+    EXPECT_EQ(rv, NS_OK);
+  }
+
+  // Load data from disk
+  RefPtr<Classifier> classifier = GetClassifier();
+  RefPtr<LookupCache> cache = classifier->GetLookupCache(GTEST_TABLE, false);
+
+  RefPtr<LookupCacheV4> cacheV4 = LookupCache::Cast<LookupCacheV4>(cache);
+  CheckContent(cacheV4, map);
+
+  oldPsetFile->Remove(false);
+}
--- a/toolkit/components/url-classifier/tests/gtest/TestVariableLengthPrefixSet.cpp
+++ b/toolkit/components/url-classifier/tests/gtest/TestVariableLengthPrefixSet.cpp
@@ -50,28 +50,16 @@ static void RandomPrefixes(uint32_t N, u
       if (!array.Contains(prefix)) {
         array.AppendElement(prefix);
         added = true;
       }
     }
   }
 }
 
-static void CheckContent(LookupCacheV4* cache, PrefixStringMap& expected) {
-  PrefixStringMap vlPSetMap;
-  cache->GetPrefixes(vlPSetMap);
-
-  for (auto iter = vlPSetMap.Iter(); !iter.Done(); iter.Next()) {
-    nsCString* expectedPrefix = expected.Get(iter.Key());
-    nsCString* resultPrefix = iter.Data();
-
-    ASSERT_TRUE(resultPrefix->Equals(*expectedPrefix));
-  }
-}
-
 // 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) {
   uint32_t matchLength = 0;
   for (uint32_t i = 0; i < array.Length(); i++) {
     const nsCString& prefix = array[i];
     Completion complete;