1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef nsUrlClassifierGTestCommon_h__
#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;
namespace mozilla {
namespace safebrowsing {
class Classifier;
class LookupCacheV4;
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) {
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.
already_AddRefed<nsIFile> GetFile(const nsTArray<nsString>& aPath);
// ApplyUpdate will call |ApplyUpdates| of Classifier within a new thread
void ApplyUpdate(nsTArray<TableUpdate*>& aUpdates);
void ApplyUpdate(TableUpdate* aUpdate);
/**
* Prefix processing utility functions
*/
typedef nsCString _Prefix;
typedef nsTArray<nsCString> _PrefixArray;
// This function converts a lexigraphic-sorted prefixes array
// to a hash table keyed by prefix size(PrefixStringMap is defined in Entries.h)
nsresult PrefixArrayToPrefixStringMap(const _PrefixArray& aPrefixArray,
PrefixStringMap& aOut);
// This function converts a lexigraphic-sorted prefixes array
// to an array containing AddPrefix(AddPrefix is defined in Entries.h)
nsresult PrefixArrayToAddPrefixArray(const _PrefixArray& aPrefixArray,
AddPrefixArray& aOut);
_Prefix CreatePrefixFromURL(const char* aURL, uint8_t aPrefixSize);
_Prefix CreatePrefixFromURL(const nsCString& aURL, uint8_t aPrefixSize);
// To test if the content is equal
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,
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;
}
/**
* Retrieve Classifer class
*/
RefPtr<Classifier> GetClassifier();
nsresult BuildLookupCache(const RefPtr<Classifier>& aClassifier,
const nsACString& aTable, _PrefixArray& aPrefixArray);
#endif // nsUrlClassifierGTestCommon_h__
|