Bug 1826049 - Make toolkit/components/url-classifier/tests/gtest buildable outside of a unified build environment r=andi

Depends on D174432

Differential Revision: https://phabricator.services.mozilla.com/D174433
This commit is contained in:
serge-sans-paille 2023-04-03 20:39:58 +00:00
Родитель aac9b6471d
Коммит acc75d964c
10 изменённых файлов: 77 добавлений и 76 удалений

Просмотреть файл

@ -28,6 +28,7 @@ IMAP
######
# Other
Checker Test # used only as part of tests for the thread-checker itself
Testing Thread # used only as part of tests for toolkit/components/url-classifier/tests/gtest/
######
# Unsorted thread names

Просмотреть файл

@ -7,7 +7,6 @@
#include "Classifier.h"
#include "HashStore.h"
#include "LookupCacheV4.h"
#include "mozilla/Components.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/Unused.h"
@ -20,19 +19,6 @@
using namespace mozilla;
using namespace mozilla::safebrowsing;
#define GTEST_SAFEBROWSING_DIR "safebrowsing"_ns
template <typename Function>
void RunTestInNewThread(Function&& aFunction) {
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
"RunTestInNewThread", std::forward<Function>(aFunction));
nsCOMPtr<nsIThread> testingThread;
nsresult rv =
NS_NewNamedThread("Testing Thread", getter_AddRefs(testingThread), r);
ASSERT_EQ(rv, NS_OK);
testingThread->Shutdown();
}
nsresult SyncApplyUpdates(TableUpdateArray& aUpdates) {
// We need to spin a new thread specifically because the callback
// will be on the caller thread. If we call Classifier::AsyncApplyUpdates
@ -194,56 +180,6 @@ void CheckContent(LookupCacheV4* aCache, const _PrefixArray& aPrefixArray) {
}
}
static nsresult BuildCache(LookupCacheV2* cache,
const _PrefixArray& aPrefixArray) {
AddPrefixArray prefixes;
AddCompleteArray completions;
nsresult rv = PrefixArrayToAddPrefixArray(aPrefixArray, prefixes);
if (NS_FAILED(rv)) {
return rv;
}
return cache->Build(prefixes, completions);
}
static nsresult BuildCache(LookupCacheV4* cache,
const _PrefixArray& aPrefixArray) {
PrefixStringMap map;
PrefixArrayToPrefixStringMap(aPrefixArray, map);
return cache->Build(map);
}
template <typename T>
RefPtr<T> SetupLookupCache(const _PrefixArray& aPrefixArray,
nsCOMPtr<nsIFile>& aFile) {
RefPtr<T> cache = new T(GTEST_TABLE_V4, ""_ns, aFile);
nsresult rv = cache->Init();
EXPECT_EQ(rv, NS_OK);
rv = BuildCache(cache, aPrefixArray);
EXPECT_EQ(rv, NS_OK);
return cache;
}
template <typename T>
RefPtr<T> SetupLookupCache(const _PrefixArray& aPrefixArray) {
nsCOMPtr<nsIFile> file;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));
file->AppendNative(GTEST_SAFEBROWSING_DIR);
RefPtr<T> cache = new T(GTEST_TABLE_V4, ""_ns, file);
nsresult rv = cache->Init();
EXPECT_EQ(rv, NS_OK);
rv = BuildCache(cache, aPrefixArray);
EXPECT_EQ(rv, NS_OK);
return cache;
}
nsresult BuildLookupCache(const RefPtr<Classifier>& classifier,
const nsACString& aTable,
_PrefixArray& aPrefixArray) {

Просмотреть файл

@ -7,11 +7,16 @@
#define nsUrlClassifierGTestCommon_h__
#include "Entries.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsIFile.h"
#include "nsTArray.h"
#include "nsIThread.h"
#include "nsThreadUtils.h"
#include "HashStore.h"
#include "gtest/gtest.h"
#include "mozilla/gtest/MozAssertions.h"
#include "LookupCacheV4.h"
using namespace mozilla::safebrowsing;
@ -23,15 +28,25 @@ class TableUpdate;
} // namespace safebrowsing
} // namespace mozilla
#define GTEST_SAFEBROWSING_DIR "safebrowsing"_ns
#define GTEST_TABLE_V4 "gtest-malware-proto"_ns
#define GTEST_TABLE_V2 "gtest-malware-simple"_ns
template <typename Function>
void RunTestInNewThread(Function&& aFunction);
void RunTestInNewThread(Function&& aFunction) {
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
"RunTestInNewThread", std::forward<Function>(aFunction));
nsCOMPtr<nsIThread> testingThread;
nsresult rv =
NS_NewNamedThread("Testing Thread", getter_AddRefs(testingThread), r);
ASSERT_EQ(rv, NS_OK);
testingThread->Shutdown();
}
// Synchronously apply updates by calling Classifier::AsyncApplyUpdates.
nsresult SyncApplyUpdates(Classifier* aClassifier,
nsTArray<TableUpdate*>* aUpdates);
nsresult SyncApplyUpdates(TableUpdateArray& aUpdates);
// Return nsIFile with root directory - NS_APP_USER_PROFILE_50_DIR
// Sub-directories are passed in path argument.
@ -70,13 +85,56 @@ void CheckContent(LookupCacheV4* cache, const _PrefixArray& aPrefixArray);
* Utility function to generate safebrowsing internal structure
*/
static inline nsresult BuildCache(LookupCacheV2* cache,
const _PrefixArray& aPrefixArray) {
AddPrefixArray prefixes;
AddCompleteArray completions;
nsresult rv = PrefixArrayToAddPrefixArray(aPrefixArray, prefixes);
if (NS_FAILED(rv)) {
return rv;
}
return cache->Build(prefixes, completions);
}
static inline nsresult BuildCache(LookupCacheV4* cache,
const _PrefixArray& aPrefixArray) {
PrefixStringMap map;
PrefixArrayToPrefixStringMap(aPrefixArray, map);
return cache->Build(map);
}
// Create a LookupCacheV4 object with sepecified prefix array.
template <typename T>
RefPtr<T> SetupLookupCache(const _PrefixArray& aPrefixArray);
RefPtr<T> SetupLookupCache(const _PrefixArray& aPrefixArray,
nsCOMPtr<nsIFile>& aFile) {
RefPtr<T> cache = new T(GTEST_TABLE_V4, ""_ns, aFile);
nsresult rv = cache->Init();
EXPECT_EQ(rv, NS_OK);
rv = BuildCache(cache, aPrefixArray);
EXPECT_EQ(rv, NS_OK);
return cache;
}
template <typename T>
RefPtr<T> SetupLookupCache(const _PrefixArray& aPrefixArray,
nsCOMPtr<nsIFile>& aFile);
RefPtr<T> SetupLookupCache(const _PrefixArray& aPrefixArray) {
nsCOMPtr<nsIFile> file;
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));
file->AppendNative(GTEST_SAFEBROWSING_DIR);
RefPtr<T> cache = new T(GTEST_TABLE_V4, ""_ns, file);
nsresult rv = cache->Init();
EXPECT_EQ(rv, NS_OK);
rv = BuildCache(cache, aPrefixArray);
EXPECT_EQ(rv, NS_OK);
return cache;
}
/**
* Retrieve Classifer class

Просмотреть файл

@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Common.h"
#include "LookupCacheV4.h"
#define EXPIRED_TIME_SEC (PR_Now() / PR_USEC_PER_SEC - 3600)
#define NOTEXPIRED_TIME_SEC (PR_Now() / PR_USEC_PER_SEC + 3600)
@ -22,10 +23,10 @@ static void SetupCacheEntry(LookupCacheV2* aLookupCache,
MissPrefixArray misses;
MissPrefixArray emptyMisses;
AddComplete* add = completes.AppendElement(fallible);
AddComplete* add = completes.AppendElement(mozilla::fallible);
add->complete.FromPlaintext(aCompletion);
Prefix* prefix = misses.AppendElement(fallible);
Prefix* prefix = misses.AppendElement(mozilla::fallible);
prefix->FromPlaintext(aCompletion);
// Setup positive cache first otherwise negative cache expiry will be
@ -231,7 +232,7 @@ TEST(UrlClassifierCaching, NegativeCacheExpireV2)
do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
MissPrefixArray misses;
Prefix* prefix = misses.AppendElement(fallible);
Prefix* prefix = misses.AppendElement(mozilla::fallible);
prefix->FromPlaintext(NEG_CACHE_EXPIRED_URL);
AddCompleteArray dummy;

Просмотреть файл

@ -49,7 +49,7 @@ TEST(UrlClassifierFailUpdate, CheckTableReset)
// Apply V2 update
{
RefPtr<TableUpdateV2> update = new TableUpdateV2(GTEST_TABLE_V2);
Unused << update->NewAddChunk(1);
mozilla::Unused << update->NewAddChunk(1);
ApplyUpdate(update);

Просмотреть файл

@ -9,6 +9,8 @@
#include "Common.h"
using namespace mozilla;
template <size_t N>
static void ToBase64EncodedStringArray(nsCString (&aInput)[N],
nsTArray<nsCString>& aEncodedArray) {

Просмотреть файл

@ -5,9 +5,12 @@
#include "Classifier.h"
#include "LookupCacheV4.h"
#include "nsAppDirectoryServiceDefs.h"
#include "Common.h"
#define GTEST_SAFEBROWSING_DIR "safebrowsing"_ns
static void TestHasPrefix(const nsCString& aURL, bool aExpectedHas,
bool aExpectedComplete) {
_PrefixArray array = {CreatePrefixFromURL("bravo.com/", 32),

Просмотреть файл

@ -32,7 +32,7 @@ static bool InitUpdateResponse(ListUpdateResponse* aUpdateResponse,
auto prefixes = rawHashes->mutable_raw_hashes();
for (auto p : aFixedLengthPrefixes) {
char buffer[4];
NativeEndian::copyAndSwapToBigEndian(buffer, &p, 1);
mozilla::NativeEndian::copyAndSwapToBigEndian(buffer, &p, 1);
prefixes->append(buffer, 4);
}
return true;
@ -102,7 +102,7 @@ TEST(UrlClassifierProtocolParser, SingleValueEncoding)
nsCString("check\x0sum", 9), true,
// As per spec, we should interpret the prefix as
// uint32 in little endian before encoding.
{LittleEndian::readUint32(expectedPrefix)},
{mozilla::LittleEndian::readUint32(expectedPrefix)},
true /* aDoPrefixEncoding */)) {
printf("Failed to initialize update response.");
ASSERT_TRUE(false);

Просмотреть файл

@ -11,6 +11,8 @@
#include "nsIFile.h"
#include "nsThreadUtils.h"
#include "string.h"
#include "LookupCacheV4.h"
#include "nsUrlClassifierUtils.h"
#include "Common.h"

Просмотреть файл

@ -38,5 +38,3 @@ if CONFIG["NIGHTLY_BUILD"] or CONFIG["MOZ_DEBUG"]:
DEFINES["MOZ_SAFEBROWSING_DUMP_FAILED_UPDATES"] = True
FINAL_LIBRARY = "xul-gtest"
REQUIRES_UNIFIED_BUILD = True