summaryrefslogtreecommitdiffstats
path: root/toolkit/components/url-classifier/tests/gtest/TestLookupCacheV4.cpp
blob: e1482c1416ce30d5fbb566cdfe1a30165942b0e6 (plain)
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
148
149
150
151
152
/* -*- 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/. */

#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),
                        CreatePrefixFromURL("browsing.com/", 8),
                        CreatePrefixFromURL("gound.com/", 5),
                        CreatePrefixFromURL("small.com/", 4)};

  nsCOMPtr<nsIFile> file;
  NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));
  file->AppendNative(GTEST_SAFEBROWSING_DIR);

  RunTestInNewThread([&]() -> void {
    RefPtr<LookupCache> cache = SetupLookupCache<LookupCacheV4>(array, file);

    Completion lookupHash;
    lookupHash.FromPlaintext(aURL);

    bool has, confirmed;
    uint32_t matchLength;
    // Freshness is not used in V4 so we just put dummy values here.
    TableFreshnessMap dummy;
    nsresult rv = cache->Has(lookupHash, &has, &matchLength, &confirmed);

    EXPECT_EQ(rv, NS_OK);
    EXPECT_EQ(has, aExpectedHas);
    EXPECT_EQ(matchLength == COMPLETE_SIZE, aExpectedComplete);
    EXPECT_EQ(confirmed, false);

    cache->ClearAll();
  });
}

TEST(UrlClassifierLookupCacheV4, HasComplete)
{ TestHasPrefix("bravo.com/"_ns, true, true); }

TEST(UrlClassifierLookupCacheV4, HasPrefix)
{ TestHasPrefix("browsing.com/"_ns, true, false); }

TEST(UrlClassifierLookupCacheV4, Nomatch)
{ TestHasPrefix("nomatch.com/"_ns, false, false); }

// Test an existing .pset should be removed after .vlpset is written
TEST(UrlClassifierLookupCacheV4, RemoveOldPset)
{
  nsCOMPtr<nsIFile> oldPsetFile;
  NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
                         getter_AddRefs(oldPsetFile));
  oldPsetFile->AppendNative("safebrowsing"_ns);
  oldPsetFile->AppendNative(GTEST_TABLE_V4 + ".pset"_ns);

  nsCOMPtr<nsIFile> newPsetFile;
  NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
                         getter_AddRefs(newPsetFile));
  newPsetFile->AppendNative("safebrowsing"_ns);
  newPsetFile->AppendNative(GTEST_TABLE_V4 + ".vlpset"_ns);

  // Create the legacy .pset file
  nsresult rv = oldPsetFile->Create(nsIFile::NORMAL_FILE_TYPE, 0666);
  EXPECT_EQ(rv, NS_OK);

  bool exists;
  rv = oldPsetFile->Exists(&exists);
  EXPECT_EQ(rv, NS_OK);
  EXPECT_EQ(exists, true);

  // Setup the data in lookup cache and write its data to disk
  RefPtr<Classifier> classifier = GetClassifier();
  _PrefixArray array = {CreatePrefixFromURL("entry.com/", 4)};
  rv = BuildLookupCache(classifier, GTEST_TABLE_V4, array);
  EXPECT_EQ(rv, NS_OK);

  RefPtr<LookupCache> cache = classifier->GetLookupCache(GTEST_TABLE_V4, false);
  rv = cache->WriteFile();
  EXPECT_EQ(rv, NS_OK);

  // .vlpset should exist while .pset should be removed
  rv = newPsetFile->Exists(&exists);
  EXPECT_EQ(rv, NS_OK);
  EXPECT_EQ(exists, true);

  rv = oldPsetFile->Exists(&exists);
  EXPECT_EQ(rv, NS_OK);
  EXPECT_EQ(exists, false);

  newPsetFile->Remove(false);
}

// Test the legacy load
TEST(UrlClassifierLookupCacheV4, LoadOldPset)
{
  nsCOMPtr<nsIFile> oldPsetFile;

  _PrefixArray array = {CreatePrefixFromURL("entry.com/", 4)};
  PrefixStringMap map;
  PrefixArrayToPrefixStringMap(array, map);

  // Prepare .pset file on disk
  {
    NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
                           getter_AddRefs(oldPsetFile));
    oldPsetFile->AppendNative("safebrowsing"_ns);
    oldPsetFile->AppendNative(GTEST_TABLE_V4 + ".pset"_ns);

    RefPtr<VariableLengthPrefixSet> pset = new VariableLengthPrefixSet;
    pset->SetPrefixes(map);

    nsCOMPtr<nsIOutputStream> stream;
    nsresult rv =
        NS_NewLocalFileOutputStream(getter_AddRefs(stream), oldPsetFile);
    EXPECT_EQ(rv, NS_OK);

    rv = pset->WritePrefixes(stream);
    EXPECT_EQ(rv, NS_OK);
  }

  // Load data from disk
  RefPtr<Classifier> classifier = GetClassifier();
  RefPtr<LookupCache> cache = classifier->GetLookupCache(GTEST_TABLE_V4, false);

  RefPtr<LookupCacheV4> cacheV4 = LookupCache::Cast<LookupCacheV4>(cache);
  CheckContent(cacheV4, array);

  oldPsetFile->Remove(false);
}

TEST(UrlClassifierLookupCacheV4, BuildAPI)
{
  _PrefixArray init = {_Prefix("alph")};
  RefPtr<LookupCacheV4> cache = SetupLookupCache<LookupCacheV4>(init);

  _PrefixArray update = {_Prefix("beta")};
  PrefixStringMap map;
  PrefixArrayToPrefixStringMap(update, map);

  cache->Build(map);
  EXPECT_TRUE(map.IsEmpty());

  CheckContent(cache, update);
}