Bug 1364607 - Add a test for empty Safe Browsing updates. r?francois draft
authorDimiL <dlee@mozilla.com>
Wed, 17 May 2017 16:04:23 +0800
changeset 579914 61f3ef6a7f81e675519d4a2d84f1c18037811691
parent 579118 41958333867b0f537271dbd4cb4ba9e8a67a85a8
child 629151 c9d9a6230f1ba9ad4ccc22c47e461fec4d3be8ab
push id59405
push userbmo:dlee@mozilla.com
push dateThu, 18 May 2017 00:25:12 +0000
reviewersfrancois
bugs1364607
milestone55.0a1
Bug 1364607 - Add a test for empty Safe Browsing updates. r?francois This patch includes two test cases: 1. Apply an empty update through Classifier interface, which is the normal use case. 2. Apply an empty update through LookupCacheV4::ApplyUpdate, this ensure update algorithm is correct when applying an empty update. This scenario actually shouldn't happen in normal use case because it will be skipped by Classifier::CheckValidUpdate. MozReview-Commit-ID: 9khsuVatX0u
toolkit/components/url-classifier/tests/gtest/TestUrlClassifierTableUpdateV4.cpp
--- a/toolkit/components/url-classifier/tests/gtest/TestUrlClassifierTableUpdateV4.cpp
+++ b/toolkit/components/url-classifier/tests/gtest/TestUrlClassifierTableUpdateV4.cpp
@@ -753,8 +753,70 @@ TEST(UrlClassifierTableUpdateV4, ApplyUp
   testFullUpdate(fMap, &checksum);
 
   // Open lookup cache will load prefix set and verify the checksum
   testOpenLookupCache();
 
   Clear();
 }
 
+// This test ensure that an empty update works correctly. Empty update
+// should be skipped by CheckValidUpdate in Classifier::UpdateTableV4.
+TEST(UrlClassifierTableUpdateV4, EmptyUpdate)
+{
+  PrefixStringMap emptyAddition;
+  nsTArray<uint32_t> emptyRemoval;
+
+   _PrefixArray array;
+  PrefixStringMap map;
+  nsCString checksum;
+
+  CalculateCheckSum(array, checksum);
+
+  // Test apply empty full/partial update before we already
+  // have data in DB.
+  testFullUpdate(emptyAddition, &checksum);
+  testPartialUpdate(emptyAddition, &emptyRemoval, &checksum, map);
+
+  // Apply an full update.
+  CreateRandomSortedPrefixArray(100, 4, 4, array);
+  CreateRandomSortedPrefixArray(10, 5, 32, array);
+  PrefixArrayToPrefixStringMap(array, map);
+  CalculateCheckSum(array, checksum);
+
+  testFullUpdate(map, &checksum);
+
+  // Test apply empty full/partial update when we already
+  // have data in DB
+  testPartialUpdate(emptyAddition, &emptyRemoval, &checksum, map);
+  testFullUpdate(emptyAddition, &checksum);
+
+  Clear();
+}
+
+// This test ensure applying an empty update directly through update algorithm
+// should be correct.
+TEST(UrlClassifierTableUpdateV4, EmptyUpdate2)
+{
+  // Setup LookupCache with initial data
+   _PrefixArray array;
+  CreateRandomSortedPrefixArray(100, 4, 4, array);
+  CreateRandomSortedPrefixArray(10, 5, 32, array);
+  UniquePtr<LookupCacheV4> cache = SetupLookupCache<LookupCacheV4>(array);
+
+  // Setup TableUpdate object with only checksum from previous update(initial data).
+  nsCString checksum;
+  CalculateCheckSum(array, checksum);
+  std::string stdChecksum;
+  stdChecksum.assign(const_cast<char*>(checksum.BeginReading()), checksum.Length());
+
+  UniquePtr<TableUpdateV4> tableUpdate = MakeUnique<TableUpdateV4>(GTEST_TABLE);
+  tableUpdate->NewChecksum(stdChecksum);
+
+  // Apply update directly through LookupCache interface
+  PrefixStringMap input, output;
+  PrefixArrayToPrefixStringMap(array, input);
+  nsresult rv = cache->ApplyUpdate(tableUpdate.get(), input, output);
+
+  ASSERT_TRUE(rv == NS_OK);
+
+  Clear();
+}