Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/url-classifier/tests/gtest/TestFailUpdate.cpp
Line
Count
Source (jump to first uncovered line)
1
#include "HashStore.h"
2
#include "nsPrintfCString.h"
3
#include "string.h"
4
#include "gtest/gtest.h"
5
#include "mozilla/Unused.h"
6
7
using namespace mozilla;
8
using namespace mozilla::safebrowsing;
9
10
static const char* kFilesInV2[] = {".pset", ".sbstore"};
11
static const char* kFilesInV4[] = {".pset", ".metadata"};
12
13
0
#define V2_TABLE  "gtest-malware-simple"
14
0
#define V4_TABLE1 "goog-malware-proto"
15
0
#define V4_TABLE2 "goog-phish-proto"
16
17
0
#define ROOT_DIR        NS_LITERAL_STRING("safebrowsing")
18
0
#define SB_FILE(x, y)   NS_ConvertUTF8toUTF16(nsPrintfCString("%s%s",x, y))
19
20
template<typename T, size_t N>
21
void CheckFileExist(const char* table, const T (&files)[N], bool expectExists)
22
0
{
23
0
  for (uint32_t i = 0; i < N; i++) {
24
0
    // This is just a quick way to know if this is v4 table
25
0
    NS_ConvertUTF8toUTF16 SUB_DIR(strstr(table, "-proto") ? "google4" : "");
26
0
    nsCOMPtr<nsIFile> file =
27
0
      GetFile(nsTArray<nsString> { ROOT_DIR, SUB_DIR, SB_FILE(table, files[i]) });
28
0
29
0
    bool exists;
30
0
    file->Exists(&exists);
31
0
32
0
    ASSERT_EQ(expectExists, exists) << file->HumanReadablePath().get();
33
0
  }
34
0
}
35
36
TEST(UrlClassifierFailUpdate, CheckTableReset)
37
0
{
38
0
  const bool FULL_UPDATE = true;
39
0
  const bool PARTIAL_UPDATE = false;
40
0
41
0
  // Apply V2 update
42
0
  {
43
0
    RefPtr<TableUpdateV2> update = new TableUpdateV2(NS_LITERAL_CSTRING(V2_TABLE));
44
0
    Unused << update->NewAddChunk(1);
45
0
46
0
    ApplyUpdate(update);
47
0
48
0
    // A successful V2 update should create .pset & .sbstore files
49
0
    CheckFileExist(V2_TABLE, kFilesInV2, true);
50
0
  }
51
0
52
0
  // Helper function to generate table update data
53
0
  auto func = [](RefPtr<TableUpdateV4> update, bool full, const char* str) {
54
0
    update->SetFullUpdate(full);
55
0
    nsCString prefix(str);
56
0
    update->NewPrefixes(prefix.Length(), prefix);
57
0
  };
58
0
59
0
  // Apply V4 update for table1
60
0
  {
61
0
    RefPtr<TableUpdateV4> update = new TableUpdateV4(NS_LITERAL_CSTRING(V4_TABLE1));
62
0
    func(update, FULL_UPDATE, "test_prefix");
63
0
64
0
    ApplyUpdate(update);
65
0
66
0
    // A successful V4 update should create .pset & .metadata files
67
0
    CheckFileExist(V4_TABLE1, kFilesInV4, true);
68
0
  }
69
0
70
0
  // Apply V4 update for table2
71
0
  {
72
0
    RefPtr<TableUpdateV4> update = new TableUpdateV4(NS_LITERAL_CSTRING(V4_TABLE2));
73
0
    func(update, FULL_UPDATE, "test_prefix");
74
0
75
0
    ApplyUpdate(update);
76
0
77
0
    CheckFileExist(V4_TABLE2, kFilesInV4, true);
78
0
  }
79
0
80
0
  // Apply V4 update with the same prefix in previous full udpate
81
0
  // This should cause an update error.
82
0
  {
83
0
    RefPtr<TableUpdateV4> update = new TableUpdateV4(NS_LITERAL_CSTRING(V4_TABLE1));
84
0
    func(update, PARTIAL_UPDATE, "test_prefix");
85
0
86
0
    ApplyUpdate(update);
87
0
88
0
    // A fail update should remove files for that table
89
0
    CheckFileExist(V4_TABLE1, kFilesInV4, false);
90
0
91
0
    // A fail update should NOT remove files for the other tables
92
0
    CheckFileExist(V2_TABLE, kFilesInV2, true);
93
0
    CheckFileExist(V4_TABLE2, kFilesInV4, true);
94
0
  }
95
0
}