Bug 1551399 part 2. Stop using [array] in url-classifier's makeFindFullHashRequestV4. r=dimi,gcp
Differential Revision: https://phabricator.services.mozilla.com/D31022
This commit is contained in:
@@ -7,56 +7,36 @@ using namespace mozilla;
|
||||
using namespace mozilla::safebrowsing;
|
||||
|
||||
namespace {
|
||||
|
||||
// |Base64EncodedStringArray| and |MakeBase64EncodedStringArray|
|
||||
// works together to make us able to do things "literally" and easily.
|
||||
|
||||
// Given a nsCString array, construct an object which can be implicitly
|
||||
// casted to |const char**|, where all owning c-style strings have been
|
||||
// base64 encoded. The memory life cycle of what the "cast operator"
|
||||
// returns is just as the object itself.
|
||||
class Base64EncodedStringArray {
|
||||
public:
|
||||
Base64EncodedStringArray(nsCString aArray[], size_t N);
|
||||
operator const char**() const { return (const char**)&mArray[0]; }
|
||||
|
||||
private:
|
||||
// Since we can't guarantee the layout of nsCString (can we?),
|
||||
// an additional nsTArray<nsCString> is required to manage the
|
||||
// allocated string.
|
||||
nsTArray<const char*> mArray;
|
||||
nsTArray<nsCString> mStringStorage;
|
||||
};
|
||||
|
||||
// Simply used to infer the fixed-array size automatically.
|
||||
template <size_t N>
|
||||
Base64EncodedStringArray MakeBase64EncodedStringArray(nsCString (&aArray)[N]) {
|
||||
return Base64EncodedStringArray(aArray, N);
|
||||
}
|
||||
|
||||
void ToBase64EncodedStringArray(nsCString (&aInput)[N],
|
||||
nsTArray<nsCString>& aEncodedArray);
|
||||
} // end of unnamed namespace.
|
||||
|
||||
TEST(UrlClassifierFindFullHash, Request)
|
||||
{
|
||||
nsUrlClassifierUtils* urlUtil = nsUrlClassifierUtils::GetInstance();
|
||||
|
||||
const char* listNames[] = {"test-phish-proto", "test-unwanted-proto"};
|
||||
nsTArray<nsCString> listNames;
|
||||
listNames.AppendElement("test-phish-proto");
|
||||
listNames.AppendElement("test-unwanted-proto");
|
||||
|
||||
nsCString listStates[] = {nsCString("sta\x00te1", 7),
|
||||
nsCString("sta\x00te2", 7)};
|
||||
nsTArray<nsCString> listStateArray;
|
||||
ToBase64EncodedStringArray(listStates, listStateArray);
|
||||
|
||||
nsCString prefixes[] = {nsCString("\x00\x00\x00\x01", 4),
|
||||
nsCString("\x00\x00\x00\x00\x01", 5),
|
||||
nsCString("\x00\xFF\x00\x01", 4),
|
||||
nsCString("\x00\xFF\x00\x01\x11\x23\xAA\xBC", 8),
|
||||
nsCString("\x00\x00\x00\x01\x00\x01\x98", 7)};
|
||||
nsTArray<nsCString> prefixArray;
|
||||
ToBase64EncodedStringArray(prefixes, prefixArray);
|
||||
|
||||
nsCString requestBase64;
|
||||
nsresult rv;
|
||||
rv = urlUtil->MakeFindFullHashRequestV4(
|
||||
listNames, MakeBase64EncodedStringArray(listStates),
|
||||
MakeBase64EncodedStringArray(prefixes), ArrayLength(listNames),
|
||||
ArrayLength(prefixes), requestBase64);
|
||||
rv = urlUtil->MakeFindFullHashRequestV4(listNames, listStateArray,
|
||||
prefixArray, requestBase64);
|
||||
ASSERT_TRUE(NS_SUCCEEDED(rv));
|
||||
|
||||
// Base64 URL decode first.
|
||||
@@ -82,8 +62,8 @@ TEST(UrlClassifierFindFullHash, Request)
|
||||
ASSERT_EQ(threatInfo.threat_types_size(), (int)ArrayLength(listStates));
|
||||
for (int i = 0; i < threatInfo.threat_types_size(); i++) {
|
||||
uint32_t expectedThreatType;
|
||||
rv = urlUtil->ConvertListNameToThreatType(nsCString(listNames[i]),
|
||||
&expectedThreatType);
|
||||
rv =
|
||||
urlUtil->ConvertListNameToThreatType(listNames[i], &expectedThreatType);
|
||||
ASSERT_TRUE(NS_SUCCEEDED(rv));
|
||||
ASSERT_EQ(threatInfo.threat_types(i), expectedThreatType);
|
||||
}
|
||||
@@ -228,14 +208,14 @@ TEST(UrlClassifierFindFullHash, ParseRequest)
|
||||
/////////////////////////////////////////////////////////////
|
||||
namespace {
|
||||
|
||||
Base64EncodedStringArray::Base64EncodedStringArray(nsCString aArray[],
|
||||
size_t N) {
|
||||
template <size_t N>
|
||||
void ToBase64EncodedStringArray(nsCString (&aArray)[N],
|
||||
nsTArray<nsCString>& aEncodedArray) {
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
nsCString encoded;
|
||||
nsresult rv = Base64Encode(aArray[i], encoded);
|
||||
NS_ENSURE_SUCCESS_VOID(rv);
|
||||
mStringStorage.AppendElement(encoded);
|
||||
mArray.AppendElement(encoded.get());
|
||||
aEncodedArray.AppendElement(encoded);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -82,9 +82,7 @@ add_test(function test_update_v4() {
|
||||
add_test(function test_getHashRequestV4() {
|
||||
let request = gUrlUtil.makeFindFullHashRequestV4([TEST_TABLE_DATA_V4.tableName],
|
||||
[btoa(NEW_CLIENT_STATE)],
|
||||
[btoa("0123"), btoa("1234567"), btoa("1111")].sort(),
|
||||
1,
|
||||
3);
|
||||
[btoa("0123"), btoa("1234567"), btoa("1111")].sort());
|
||||
registerHandlerGethashV4("&$req=" + request);
|
||||
let completeFinishedCnt = 0;
|
||||
|
||||
@@ -187,9 +185,7 @@ add_test(function test_minWaitDuration() {
|
||||
|
||||
let request = gUrlUtil.makeFindFullHashRequestV4([TEST_TABLE_DATA_V4.tableName],
|
||||
[btoa(NEW_CLIENT_STATE)],
|
||||
[btoa("1234567")],
|
||||
1,
|
||||
1);
|
||||
[btoa("1234567")]);
|
||||
registerHandlerGethashV4("&$req=" + request);
|
||||
|
||||
// The last gethash response contained a min wait duration 12 secs 10 nano
|
||||
|
||||
@@ -31,14 +31,12 @@ function testMobileOnlyThreats() {
|
||||
let requestWithPHA =
|
||||
urlUtils.makeFindFullHashRequestV4(["goog-phish-proto", "goog-harmful-proto"],
|
||||
["", ""], // state.
|
||||
[btoa("0123")], // prefix.
|
||||
2, 1);
|
||||
[btoa("0123")]); // prefix.
|
||||
|
||||
let requestNoPHA =
|
||||
urlUtils.makeFindFullHashRequestV4(["goog-phish-proto"],
|
||||
[""], // state.
|
||||
[btoa("0123")], // prefix.
|
||||
1, 1);
|
||||
[btoa("0123")]); // prefix.
|
||||
|
||||
if (AppConstants.platform === "android") {
|
||||
notEqual(requestWithPHA, requestNoPHA,
|
||||
|
||||
Reference in New Issue
Block a user