From 1032932288ae1095a3c5eccd78c0ffdcb66d2c05 Mon Sep 17 00:00:00 2001 From: Dimi Lee Date: Thu, 28 Feb 2019 08:12:36 +0000 Subject: [PATCH] Bug 1353956 - P1. Rename checksum used in SafeBrowsing V4 to SHA256. r=gcp SafeBrowsing V4 protocol use SHA-256 as the checksum to check integrity of update data and also the integrity of prefix files. SafeBrowsing V2 HashStore use MD5 as the checksum to check integrity of .sbstore Since we are going to use CRC32 as the integrity check of V4 prefix files, I think rename V4 "checksum" to SHA256 can improve readability. Differential Revision: https://phabricator.services.mozilla.com/D21460 --HG-- extra : moz-landing-system : lando --- .../components/url-classifier/Classifier.cpp | 7 +- .../components/url-classifier/HashStore.cpp | 4 +- toolkit/components/url-classifier/HashStore.h | 6 +- .../url-classifier/LookupCacheV4.cpp | 57 +++--- .../components/url-classifier/LookupCacheV4.h | 2 +- .../url-classifier/ProtocolParser.cpp | 2 +- .../gtest/TestUrlClassifierTableUpdateV4.cpp | 191 +++++++++--------- 7 files changed, 133 insertions(+), 136 deletions(-) diff --git a/toolkit/components/url-classifier/Classifier.cpp b/toolkit/components/url-classifier/Classifier.cpp index 13899b9fe8c3..cbf4c310c36f 100644 --- a/toolkit/components/url-classifier/Classifier.cpp +++ b/toolkit/components/url-classifier/Classifier.cpp @@ -1577,9 +1577,8 @@ nsresult Classifier::LoadMetadata(nsIFile* aDirectory, nsACString& aResult) { continue; } - nsCString state; - nsCString checksum; - rv = lookupCacheV4->LoadMetadata(state, checksum); + nsCString state, sha256; + rv = lookupCacheV4->LoadMetadata(state, sha256); if (NS_FAILED(rv)) { LOG(("Failed to get metadata for table %s", tableName.get())); continue; @@ -1591,7 +1590,7 @@ nsresult Classifier::LoadMetadata(nsIFile* aDirectory, nsACString& aResult) { NS_ENSURE_SUCCESS(rv, rv); nsAutoCString checksumBase64; - rv = Base64Encode(checksum, checksumBase64); + rv = Base64Encode(sha256, checksumBase64); NS_ENSURE_SUCCESS(rv, rv); LOG(("Appending state '%s' and checksum '%s' for table %s", diff --git a/toolkit/components/url-classifier/HashStore.cpp b/toolkit/components/url-classifier/HashStore.cpp index 21287286974c..aa8c11186983 100644 --- a/toolkit/components/url-classifier/HashStore.cpp +++ b/toolkit/components/url-classifier/HashStore.cpp @@ -206,8 +206,8 @@ nsresult TableUpdateV4::NewRemovalIndices(const uint32_t* aIndices, return NS_OK; } -void TableUpdateV4::NewChecksum(const std::string& aChecksum) { - mChecksum.Assign(aChecksum.data(), aChecksum.size()); +void TableUpdateV4::SetSHA256(const std::string& aSHA256) { + mSHA256.Assign(aSHA256.data(), aSHA256.size()); } nsresult TableUpdateV4::NewFullHashResponse( diff --git a/toolkit/components/url-classifier/HashStore.h b/toolkit/components/url-classifier/HashStore.h index f9cf6d208bdf..2df2ecedc377 100644 --- a/toolkit/components/url-classifier/HashStore.h +++ b/toolkit/components/url-classifier/HashStore.h @@ -159,7 +159,7 @@ class TableUpdateV4 : public TableUpdate { return mRemovalIndiceArray; } const nsACString& ClientState() const { return mClientState; } - const nsACString& Checksum() const { return mChecksum; } + const nsACString& SHA256() const { return mSHA256; } const FullHashResponseMap& FullHashResponse() const { return mFullHashResponseMap; } @@ -170,7 +170,7 @@ class TableUpdateV4 : public TableUpdate { void SetFullUpdate(bool aIsFullUpdate) { mFullUpdate = aIsFullUpdate; } void NewPrefixes(int32_t aSize, const nsACString& aPrefixes); void SetNewClientState(const nsACString& aState) { mClientState = aState; } - void NewChecksum(const std::string& aChecksum); + void SetSHA256(const std::string& aSHA256); nsresult NewRemovalIndices(const uint32_t* aIndices, size_t aNumOfIndices); nsresult NewFullHashResponse(const Prefix& aPrefix, @@ -183,7 +183,7 @@ class TableUpdateV4 : public TableUpdate { PrefixStringMap mPrefixesMap; RemovalIndiceArray mRemovalIndiceArray; nsCString mClientState; - nsCString mChecksum; + nsCString mSHA256; // This is used to store response from fullHashes.find. FullHashResponseMap mFullHashResponseMap; diff --git a/toolkit/components/url-classifier/LookupCacheV4.cpp b/toolkit/components/url-classifier/LookupCacheV4.cpp index 8aafe06f7d03..992901d962c3 100644 --- a/toolkit/components/url-classifier/LookupCacheV4.cpp +++ b/toolkit/components/url-classifier/LookupCacheV4.cpp @@ -173,15 +173,15 @@ nsresult LookupCacheV4::LoadFromFile(nsCOMPtr& aFile) { return rv; } - nsCString state, checksum; - rv = LoadMetadata(state, checksum); + nsCString state, sha256; + rv = LoadMetadata(state, sha256); Telemetry::Accumulate(Telemetry::URLCLASSIFIER_VLPS_METADATA_CORRUPT, rv == NS_ERROR_FILE_CORRUPTED); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } - rv = VerifyChecksum(checksum); + rv = VerifySHA256(sha256); Telemetry::Accumulate(Telemetry::URLCLASSIFIER_VLPS_LOAD_CORRUPT, rv == NS_ERROR_FILE_CORRUPTED); Unused << NS_WARN_IF(NS_FAILED(rv)); @@ -222,8 +222,8 @@ static nsresult InitCrypto(nsCOMPtr& aCrypto) { } // Read prefix into a buffer and also update the hash which -// keeps track of the checksum -static void UpdateChecksum(nsICryptoHash* aCrypto, const nsACString& aPrefix) { +// keeps track of the sha256 hash +static void UpdateSHA256(nsICryptoHash* aCrypto, const nsACString& aPrefix) { MOZ_ASSERT(aCrypto); aCrypto->Update( reinterpret_cast(const_cast(aPrefix.BeginReading())), @@ -315,7 +315,7 @@ nsresult LookupCacheV4::ApplyUpdate(RefPtr aTableUpdate, return rv; } - UpdateChecksum(crypto, smallestOldPrefix); + UpdateSHA256(crypto, smallestOldPrefix); } smallestOldPrefix.SetLength(0); } else { @@ -324,7 +324,7 @@ nsresult LookupCacheV4::ApplyUpdate(RefPtr aTableUpdate, return rv; } - UpdateChecksum(crypto, smallestAddPrefix); + UpdateSHA256(crypto, smallestAddPrefix); smallestAddPrefix.SetLength(0); } } @@ -343,20 +343,20 @@ nsresult LookupCacheV4::ApplyUpdate(RefPtr aTableUpdate, return NS_ERROR_UC_UPDATE_WRONG_REMOVAL_INDICES; } - nsAutoCString checksum; - crypto->Finish(false, checksum); - if (aTableUpdate->Checksum().IsEmpty()) { - LOG(("Update checksum missing.")); + nsAutoCString sha256; + crypto->Finish(false, sha256); + if (aTableUpdate->SHA256().IsEmpty()) { + LOG(("Update sha256 hash missing.")); Telemetry::Accumulate( Telemetry::URLCLASSIFIER_UPDATE_ERROR, mProvider, NS_ERROR_GET_CODE(NS_ERROR_UC_UPDATE_MISSING_CHECKSUM)); - // Generate our own checksum to tableUpdate to ensure there is always + // Generate our own sha256 to tableUpdate to ensure there is always // checksum in .metadata - std::string stdChecksum(checksum.BeginReading(), checksum.Length()); - aTableUpdate->NewChecksum(stdChecksum); - } else if (aTableUpdate->Checksum() != checksum) { - LOG(("Checksum mismatch after applying partial update")); + std::string stdSha256(sha256.BeginReading(), sha256.Length()); + aTableUpdate->SetSHA256(stdSha256); + } else if (aTableUpdate->SHA256() != sha256) { + LOG(("SHA256 hash mismatch after applying partial update")); return NS_ERROR_UC_UPDATE_CHECKSUM_MISMATCH; } @@ -370,7 +370,7 @@ nsresult LookupCacheV4::AddFullHashResponseToCache( return NS_OK; } -nsresult LookupCacheV4::VerifyChecksum(const nsACString& aChecksum) { +nsresult LookupCacheV4::VerifySHA256(const nsACString& aSHA256) { nsCOMPtr crypto; nsresult rv = InitCrypto(crypto); if (NS_FAILED(rv)) { @@ -387,14 +387,14 @@ nsresult LookupCacheV4::VerifyChecksum(const nsACString& aChecksum) { if (!loadPSet.GetSmallestPrefix(prefix)) { break; } - UpdateChecksum(crypto, prefix); + UpdateSHA256(crypto, prefix); } - nsAutoCString checksum; - crypto->Finish(false, checksum); + nsAutoCString sha256; + crypto->Finish(false, sha256); - if (checksum != aChecksum) { - LOG(("Checksum mismatch when loading prefixes from file.")); + if (sha256 != aSHA256) { + LOG(("Sha256 hash mismatch when loading prefixes from file.")); return NS_ERROR_FILE_CORRUPTED; } @@ -512,15 +512,14 @@ nsresult LookupCacheV4::WriteMetadata( rv = WriteValue(outputStream, aTableUpdate->ClientState()); NS_ENSURE_SUCCESS(rv, rv); - // Write the checksum. - rv = WriteValue(outputStream, aTableUpdate->Checksum()); + // Write the SHA256 hash. + rv = WriteValue(outputStream, aTableUpdate->SHA256()); NS_ENSURE_SUCCESS(rv, rv); return rv; } -nsresult LookupCacheV4::LoadMetadata(nsACString& aState, - nsACString& aChecksum) { +nsresult LookupCacheV4::LoadMetadata(nsACString& aState, nsACString& aSHA256) { nsCOMPtr metaFile; nsresult rv = mStoreDirectory->Clone(getter_AddRefs(metaFile)); NS_ENSURE_SUCCESS(rv, rv); @@ -543,10 +542,10 @@ nsresult LookupCacheV4::LoadMetadata(nsACString& aState, return rv; } - // Read the checksum. - rv = ReadValue(localInFile, aChecksum); + // Read the SHA256 hash. + rv = ReadValue(localInFile, aSHA256); if (NS_FAILED(rv)) { - LOG(("Failed to read checksum.")); + LOG(("Failed to read SHA256 hash.")); return rv; } diff --git a/toolkit/components/url-classifier/LookupCacheV4.h b/toolkit/components/url-classifier/LookupCacheV4.h index e3ef80c67fcb..dfb970b2d15b 100644 --- a/toolkit/components/url-classifier/LookupCacheV4.h +++ b/toolkit/components/url-classifier/LookupCacheV4.h @@ -56,7 +56,7 @@ class LookupCacheV4 final : public LookupCache { virtual int Ver() const override { return VER; } - nsresult VerifyChecksum(const nsACString& aChecksum); + nsresult VerifySHA256(const nsACString& aSHA256); RefPtr mVLPrefixSet; }; diff --git a/toolkit/components/url-classifier/ProtocolParser.cpp b/toolkit/components/url-classifier/ProtocolParser.cpp index e0f28191ecac..91318aa7ff34 100644 --- a/toolkit/components/url-classifier/ProtocolParser.cpp +++ b/toolkit/components/url-classifier/ProtocolParser.cpp @@ -829,7 +829,7 @@ nsresult ProtocolParserProtobuf::ProcessOneResponse( tuV4->SetNewClientState(state); if (aResponse.has_checksum()) { - tuV4->NewChecksum(aResponse.checksum().sha256()); + tuV4->SetSHA256(aResponse.checksum().sha256()); } PARSER_LOG( diff --git a/toolkit/components/url-classifier/tests/gtest/TestUrlClassifierTableUpdateV4.cpp b/toolkit/components/url-classifier/tests/gtest/TestUrlClassifierTableUpdateV4.cpp index 383c14b6bb19..5bfd8891f2d6 100644 --- a/toolkit/components/url-classifier/tests/gtest/TestUrlClassifierTableUpdateV4.cpp +++ b/toolkit/components/url-classifier/tests/gtest/TestUrlClassifierTableUpdateV4.cpp @@ -54,7 +54,7 @@ static void MergeAndSortArray(const _PrefixArray& array1, output.Sort(); } -static void CalculateCheckSum(_PrefixArray& prefixArray, nsCString& checksum) { +static void CalculateSHA256(_PrefixArray& prefixArray, nsCString& sha256) { prefixArray.Sort(); nsresult rv; @@ -68,7 +68,7 @@ static void CalculateCheckSum(_PrefixArray& prefixArray, nsCString& checksum) { reinterpret_cast(const_cast(prefix.get())), prefix.Length()); } - cryptoHash->Finish(false, checksum); + cryptoHash->Finish(false, sha256); } // N: Number of prefixes, MIN/MAX: minimum/maximum prefix size @@ -114,7 +114,7 @@ static void CreateRandomRemovalIndices(uint32_t N, uint32_t MAX, // Function to generate TableUpdateV4. static void GenerateUpdateData(bool fullUpdate, PrefixStringMap& add, - nsTArray* removal, nsCString* checksum, + nsTArray* removal, nsCString* sha256, TableUpdateArray& tableUpdates) { RefPtr tableUpdate = new TableUpdateV4(GTEST_TABLE); tableUpdate->SetFullUpdate(fullUpdate); @@ -128,12 +128,12 @@ static void GenerateUpdateData(bool fullUpdate, PrefixStringMap& add, tableUpdate->NewRemovalIndices(removal->Elements(), removal->Length()); } - if (checksum) { - std::string stdChecksum; - stdChecksum.assign(const_cast(checksum->BeginReading()), - checksum->Length()); + if (sha256) { + std::string stdSHA256; + stdSHA256.assign(const_cast(sha256->BeginReading()), + sha256->Length()); - tableUpdate->NewChecksum(stdChecksum); + tableUpdate->SetSHA256(stdSHA256); } tableUpdates.AppendElement(tableUpdate); @@ -200,18 +200,18 @@ static void testUpdate(TableUpdateArray& tableUpdates, VerifyPrefixSet(expected); } -static void testFullUpdate(PrefixStringMap& add, nsCString* checksum) { +static void testFullUpdate(PrefixStringMap& add, nsCString* sha256) { TableUpdateArray tableUpdates; - GenerateUpdateData(true, add, nullptr, checksum, tableUpdates); + GenerateUpdateData(true, add, nullptr, sha256, tableUpdates); testUpdate(tableUpdates, add); } static void testPartialUpdate(PrefixStringMap& add, nsTArray* removal, - nsCString* checksum, PrefixStringMap& expected) { + nsCString* sha256, PrefixStringMap& expected) { TableUpdateArray tableUpdates; - GenerateUpdateData(false, add, removal, checksum, tableUpdates); + GenerateUpdateData(false, add, removal, sha256, tableUpdates); testUpdate(tableUpdates, expected); } @@ -238,13 +238,13 @@ TEST(UrlClassifierTableUpdateV4, FixLenghtPSetFullUpdate) { _PrefixArray array; PrefixStringMap map; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(5000, 4, 4, array); PrefixArrayToPrefixStringMap(array, map); - CalculateCheckSum(array, checksum); + CalculateSHA256(array, sha256); - testFullUpdate(map, &checksum); + testFullUpdate(map, &sha256); Clear(); } @@ -252,13 +252,13 @@ TEST(UrlClassifierTableUpdateV4, FixLenghtPSetFullUpdate) { TEST(UrlClassifierTableUpdateV4, VariableLenghtPSetFullUpdate) { _PrefixArray array; PrefixStringMap map; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(5000, 5, 32, array); PrefixArrayToPrefixStringMap(array, map); - CalculateCheckSum(array, checksum); + CalculateSHA256(array, sha256); - testFullUpdate(map, &checksum); + testFullUpdate(map, &sha256); Clear(); } @@ -267,14 +267,14 @@ TEST(UrlClassifierTableUpdateV4, VariableLenghtPSetFullUpdate) { TEST(UrlClassifierTableUpdateV4, MixedPSetFullUpdate) { _PrefixArray array; PrefixStringMap map; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(5000, 4, 4, array); CreateRandomSortedPrefixArray(1000, 5, 32, array); PrefixArrayToPrefixStringMap(array, map); - CalculateCheckSum(array, checksum); + CalculateSHA256(array, sha256); - testFullUpdate(map, &checksum); + testFullUpdate(map, &sha256); Clear(); } @@ -285,21 +285,21 @@ TEST(UrlClassifierTableUpdateV4, PartialUpdateWithRemoval) { // Apply a full update first. { PrefixStringMap fMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(10000, 4, 4, fArray); CreateRandomSortedPrefixArray(2000, 5, 32, fArray); PrefixArrayToPrefixStringMap(fArray, fMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - testFullUpdate(fMap, &checksum); + testFullUpdate(fMap, &sha256); } // Apply a partial update with removal. { _PrefixArray pArray, mergedArray; PrefixStringMap pMap, mergedMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(5000, 4, 4, pArray); CreateRandomSortedPrefixArray(1000, 5, 32, pArray); @@ -314,9 +314,9 @@ TEST(UrlClassifierTableUpdateV4, PartialUpdateWithRemoval) { // Calculate the expected prefix map. MergeAndSortArray(fArray, pArray, mergedArray); PrefixArrayToPrefixStringMap(mergedArray, mergedMap); - CalculateCheckSum(mergedArray, checksum); + CalculateSHA256(mergedArray, sha256); - testPartialUpdate(pMap, &removal, &checksum, mergedMap); + testPartialUpdate(pMap, &removal, &sha256, mergedMap); } Clear(); @@ -328,21 +328,21 @@ TEST(UrlClassifierTableUpdateV4, PartialUpdateWithoutRemoval) { // Apply a full update first. { PrefixStringMap fMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(10000, 4, 4, fArray); CreateRandomSortedPrefixArray(2000, 5, 32, fArray); PrefixArrayToPrefixStringMap(fArray, fMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - testFullUpdate(fMap, &checksum); + testFullUpdate(fMap, &sha256); } // Apply a partial update without removal { _PrefixArray pArray, mergedArray; PrefixStringMap pMap, mergedMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(5000, 4, 4, pArray); CreateRandomSortedPrefixArray(1000, 5, 32, pArray); @@ -352,9 +352,9 @@ TEST(UrlClassifierTableUpdateV4, PartialUpdateWithoutRemoval) { // Calculate the expected prefix map. MergeAndSortArray(fArray, pArray, mergedArray); PrefixArrayToPrefixStringMap(mergedArray, mergedMap); - CalculateCheckSum(mergedArray, checksum); + CalculateSHA256(mergedArray, sha256); - testPartialUpdate(pMap, nullptr, &checksum, mergedMap); + testPartialUpdate(pMap, nullptr, &sha256, mergedMap); } Clear(); @@ -368,13 +368,13 @@ TEST(UrlClassifierTableUpdateV4, PartialUpdatePrefixAlreadyExist) { // Apply a full update fist. { PrefixStringMap fMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(1000, 4, 32, fArray); PrefixArrayToPrefixStringMap(fArray, fMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - testFullUpdate(fMap, &checksum); + testFullUpdate(fMap, &sha256); } // Apply a partial update which contains a prefix in previous full update. @@ -401,14 +401,14 @@ TEST(UrlClassifierTableUpdateV4, PartialUpdatePrefixAlreadyExist) { TEST(UrlClassifierTableUpdateV4, OnlyPartialUpdate) { _PrefixArray pArray; PrefixStringMap pMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(5000, 4, 4, pArray); CreateRandomSortedPrefixArray(1000, 5, 32, pArray); PrefixArrayToPrefixStringMap(pArray, pMap); - CalculateCheckSum(pArray, checksum); + CalculateSHA256(pArray, sha256); - testPartialUpdate(pMap, nullptr, &checksum, pMap); + testPartialUpdate(pMap, nullptr, &sha256, pMap); Clear(); } @@ -420,21 +420,21 @@ TEST(UrlClassifierTableUpdateV4, PartialUpdateOnlyRemoval) { // Apply a full update first. { PrefixStringMap fMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(5000, 4, 4, fArray); CreateRandomSortedPrefixArray(1000, 5, 32, fArray); PrefixArrayToPrefixStringMap(fArray, fMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - testFullUpdate(fMap, &checksum); + testFullUpdate(fMap, &sha256); } // Apply a partial update without add prefix, only contain removal indices. { _PrefixArray pArray; PrefixStringMap pMap, mergedMap; - nsCString checksum; + nsCString sha256; // Remove 1/5 of elements of original prefix set. nsTArray removal; @@ -442,9 +442,9 @@ TEST(UrlClassifierTableUpdateV4, PartialUpdateOnlyRemoval) { RemoveElements(removal, fArray); PrefixArrayToPrefixStringMap(fArray, mergedMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - testPartialUpdate(pMap, &removal, &checksum, mergedMap); + testPartialUpdate(pMap, &removal, &sha256, mergedMap); } Clear(); @@ -454,7 +454,7 @@ TEST(UrlClassifierTableUpdateV4, PartialUpdateOnlyRemoval) { TEST(UrlClassifierTableUpdateV4, MultipleTableUpdates) { _PrefixArray fArray, pArray, mergedArray; PrefixStringMap fMap, pMap, mergedMap; - nsCString checksum; + nsCString sha256; TableUpdateArray tableUpdates; @@ -462,9 +462,9 @@ TEST(UrlClassifierTableUpdateV4, MultipleTableUpdates) { CreateRandomSortedPrefixArray(10000, 4, 4, fArray); CreateRandomSortedPrefixArray(2000, 5, 32, fArray); PrefixArrayToPrefixStringMap(fArray, fMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - GenerateUpdateData(true, fMap, nullptr, &checksum, tableUpdates); + GenerateUpdateData(true, fMap, nullptr, &sha256, tableUpdates); // Generate second partial update CreateRandomSortedPrefixArray(3000, 4, 4, pArray); @@ -473,9 +473,9 @@ TEST(UrlClassifierTableUpdateV4, MultipleTableUpdates) { PrefixArrayToPrefixStringMap(pArray, pMap); MergeAndSortArray(fArray, pArray, mergedArray); - CalculateCheckSum(mergedArray, checksum); + CalculateSHA256(mergedArray, sha256); - GenerateUpdateData(false, pMap, nullptr, &checksum, tableUpdates); + GenerateUpdateData(false, pMap, nullptr, &sha256, tableUpdates); // Generate thrid partial update fArray.AppendElements(pArray); @@ -493,9 +493,9 @@ TEST(UrlClassifierTableUpdateV4, MultipleTableUpdates) { MergeAndSortArray(fArray, pArray, mergedArray); PrefixArrayToPrefixStringMap(mergedArray, mergedMap); - CalculateCheckSum(mergedArray, checksum); + CalculateSHA256(mergedArray, sha256); - GenerateUpdateData(false, pMap, &removal, &checksum, tableUpdates); + GenerateUpdateData(false, pMap, &removal, &sha256, tableUpdates); testUpdate(tableUpdates, mergedMap); @@ -510,22 +510,22 @@ TEST(UrlClassifierTableUpdateV4, MultiplePartialUpdateTableUpdates) { // Apply a full update first { PrefixStringMap fMap; - nsCString checksum; + nsCString sha256; // Generate first full udpate CreateRandomSortedPrefixArray(10000, 4, 4, fArray); CreateRandomSortedPrefixArray(3000, 5, 32, fArray); PrefixArrayToPrefixStringMap(fArray, fMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - testFullUpdate(fMap, &checksum); + testFullUpdate(fMap, &sha256); } // Apply multiple partial updates in one table update { _PrefixArray pArray, mergedArray; PrefixStringMap pMap, mergedMap; - nsCString checksum; + nsCString sha256; nsTArray removal; TableUpdateArray tableUpdates; @@ -540,9 +540,9 @@ TEST(UrlClassifierTableUpdateV4, MultiplePartialUpdateTableUpdates) { RemoveElements(removal, fArray); MergeAndSortArray(fArray, pArray, mergedArray); - CalculateCheckSum(mergedArray, checksum); + CalculateSHA256(mergedArray, sha256); - GenerateUpdateData(false, pMap, &removal, &checksum, tableUpdates); + GenerateUpdateData(false, pMap, &removal, &sha256, tableUpdates); fArray.AppendElements(pArray); fArray.Sort(); @@ -561,9 +561,9 @@ TEST(UrlClassifierTableUpdateV4, MultiplePartialUpdateTableUpdates) { MergeAndSortArray(fArray, pArray, mergedArray); PrefixArrayToPrefixStringMap(mergedArray, mergedMap); - CalculateCheckSum(mergedArray, checksum); + CalculateSHA256(mergedArray, sha256); - GenerateUpdateData(false, pMap, &removal, &checksum, tableUpdates); + GenerateUpdateData(false, pMap, &removal, &sha256, tableUpdates); testUpdate(tableUpdates, mergedMap); } @@ -578,13 +578,13 @@ TEST(UrlClassifierTableUpdateV4, RemovalIndexTooLarge) { // Apply a full update first { PrefixStringMap fMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(1000, 4, 32, fArray); PrefixArrayToPrefixStringMap(fArray, fMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - testFullUpdate(fMap, &checksum); + testFullUpdate(fMap, &sha256); } // Apply a partial update with removal indice array larger than @@ -615,31 +615,31 @@ TEST(UrlClassifierTableUpdateV4, ChecksumMismatch) { { _PrefixArray fArray; PrefixStringMap fMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(1000, 4, 32, fArray); PrefixArrayToPrefixStringMap(fArray, fMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - testFullUpdate(fMap, &checksum); + testFullUpdate(fMap, &sha256); } - // Apply a partial update with incorrect checksum + // Apply a partial update with incorrect sha256 { _PrefixArray pArray; PrefixStringMap pMap; - nsCString checksum; + nsCString sha256; TableUpdateArray tableUpdates; CreateRandomSortedPrefixArray(200, 4, 32, pArray); PrefixArrayToPrefixStringMap(pArray, pMap); - // Checksum should be calculated with both old prefix set and add prefix - // set, here we only calculate checksum with add prefix set to check if + // sha256 should be calculated with both old prefix set and add prefix + // set, here we only calculate sha256 with add prefix set to check if // applyUpdate will return failure. - CalculateCheckSum(pArray, checksum); + CalculateSHA256(pArray, sha256); - GenerateUpdateData(false, pMap, nullptr, &checksum, tableUpdates); + GenerateUpdateData(false, pMap, nullptr, &sha256, tableUpdates); testUpdateFail(tableUpdates); } @@ -647,25 +647,25 @@ TEST(UrlClassifierTableUpdateV4, ChecksumMismatch) { } TEST(UrlClassifierTableUpdateV4, ApplyUpdateThenLoad) { - // Apply update with checksum + // Apply update with sha256 { _PrefixArray fArray; PrefixStringMap fMap; - nsCString checksum; + nsCString sha256; CreateRandomSortedPrefixArray(1000, 4, 32, fArray); PrefixArrayToPrefixStringMap(fArray, fMap); - CalculateCheckSum(fArray, checksum); + CalculateSHA256(fArray, sha256); - testFullUpdate(fMap, &checksum); + testFullUpdate(fMap, &sha256); - // Open lookup cache will load prefix set and verify the checksum + // Open lookup cache will load prefix set and verify the sha256 testOpenLookupCache(); } Clear(); - // Apply update without checksum + // Apply update without sha256 { _PrefixArray fArray; PrefixStringMap fMap; @@ -706,15 +706,15 @@ TEST(UrlClassifierTableUpdateV4, ApplyUpdateWithFixedChecksum) { PrefixStringMap fMap; PrefixArrayToPrefixStringMap(fArray, fMap); - nsCString checksum( + nsCString sha256( "\xae\x18\x94\xd7\xd0\x83\x5f\xc1" "\x58\x59\x5c\x2c\x72\xb9\x6e\x5e" "\xf4\xe8\x0a\x6b\xff\x5e\x6b\x81" "\x65\x34\x06\x16\x06\x59\xa0\x67"); - testFullUpdate(fMap, &checksum); + testFullUpdate(fMap, &sha256); - // Open lookup cache will load prefix set and verify the checksum + // Open lookup cache will load prefix set and verify the sha256 testOpenLookupCache(); Clear(); @@ -728,27 +728,27 @@ TEST(UrlClassifierTableUpdateV4, EmptyUpdate) { _PrefixArray array; PrefixStringMap map; - nsCString checksum; + nsCString sha256; - CalculateCheckSum(array, checksum); + CalculateSHA256(array, sha256); // Test apply empty full/partial update before we already // have data in DB. - testFullUpdate(emptyAddition, &checksum); - testPartialUpdate(emptyAddition, &emptyRemoval, &checksum, map); + testFullUpdate(emptyAddition, &sha256); + testPartialUpdate(emptyAddition, &emptyRemoval, &sha256, map); // Apply an full update. CreateRandomSortedPrefixArray(100, 4, 4, array); CreateRandomSortedPrefixArray(10, 5, 32, array); PrefixArrayToPrefixStringMap(array, map); - CalculateCheckSum(array, checksum); + CalculateSHA256(array, sha256); - testFullUpdate(map, &checksum); + testFullUpdate(map, &sha256); // Test apply empty full/partial update when we already // have data in DB - testPartialUpdate(emptyAddition, &emptyRemoval, &checksum, map); - testFullUpdate(emptyAddition, &checksum); + testPartialUpdate(emptyAddition, &emptyRemoval, &sha256, map); + testFullUpdate(emptyAddition, &sha256); Clear(); } @@ -762,16 +762,15 @@ TEST(UrlClassifierTableUpdateV4, EmptyUpdate2) { CreateRandomSortedPrefixArray(10, 5, 32, array); RefPtr cache = SetupLookupCache(array); - // Setup TableUpdate object with only checksum from previous update(initial + // Setup TableUpdate object with only sha256 from previous update(initial // data). - nsCString checksum; - CalculateCheckSum(array, checksum); - std::string stdChecksum; - stdChecksum.assign(const_cast(checksum.BeginReading()), - checksum.Length()); + nsCString sha256; + CalculateSHA256(array, sha256); + std::string stdSHA256; + stdSHA256.assign(const_cast(sha256.BeginReading()), sha256.Length()); RefPtr tableUpdate = new TableUpdateV4(GTEST_TABLE); - tableUpdate->NewChecksum(stdChecksum); + tableUpdate->SetSHA256(stdSHA256); // Apply update directly through LookupCache interface PrefixStringMap input, output;