File: TestFailUpdate.cpp

package info (click to toggle)
thunderbird 1%3A60.9.0-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,339,492 kB
  • sloc: cpp: 5,457,040; ansic: 2,360,385; python: 596,167; asm: 340,963; java: 326,296; xml: 258,830; sh: 84,445; makefile: 23,705; perl: 17,317; objc: 3,768; yacc: 1,766; ada: 1,681; lex: 1,364; pascal: 1,264; cs: 879; exp: 527; php: 436; lisp: 258; ruby: 153; awk: 152; sed: 53; csh: 27
file content (93 lines) | stat: -rw-r--r-- 2,758 bytes parent folder | download | duplicates (2)
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
#include "HashStore.h"
#include "nsPrintfCString.h"
#include "string.h"
#include "gtest/gtest.h"
#include "mozilla/Unused.h"

using namespace mozilla;
using namespace mozilla::safebrowsing;

static const char* kFilesInV2[] = {".pset", ".sbstore"};
static const char* kFilesInV4[] = {".pset", ".metadata"};

#define V2_TABLE "gtest-malware-simple"
#define V4_TABLE1 "goog-malware-proto"
#define V4_TABLE2 "goog-phish-proto"

#define ROOT_DIR NS_LITERAL_STRING("safebrowsing")
#define SB_FILE(x, y) NS_ConvertUTF8toUTF16(nsPrintfCString("%s%s", x, y))

template <typename T, size_t N>
void CheckFileExist(const char* table, const T (&files)[N], bool expectExists) {
  for (uint32_t i = 0; i < N; i++) {
    // This is just a quick way to know if this is v4 table
    NS_ConvertUTF8toUTF16 SUB_DIR(strstr(table, "-proto") ? "google4" : "");
    nsCOMPtr<nsIFile> file = GetFile(
        nsTArray<nsString>{ROOT_DIR, SUB_DIR, SB_FILE(table, files[i])});

    bool exists;
    file->Exists(&exists);

    ASSERT_EQ(expectExists, exists) << file->HumanReadablePath().get();
  }
}

TEST(FailUpdate, CheckTableReset) {
  const bool FULL_UPDATE = true;
  const bool PARTIAL_UPDATE = false;

  // Apply V2 update
  {
    auto update = new TableUpdateV2(NS_LITERAL_CSTRING(V2_TABLE));
    Unused << update->NewAddChunk(1);

    ApplyUpdate(update);

    // A successful V2 update should create .pset & .sbstore files
    CheckFileExist(V2_TABLE, kFilesInV2, true);
  }

  // Helper function to generate table update data
  auto func = [](TableUpdateV4* update, bool full, const char* str) {
    update->SetFullUpdate(full);
    std::string prefix(str);
    update->NewPrefixes(prefix.length(), prefix);
  };

  // Apply V4 update for table1
  {
    auto update = new TableUpdateV4(NS_LITERAL_CSTRING(V4_TABLE1));
    func(update, FULL_UPDATE, "test_prefix");

    ApplyUpdate(update);

    // A successful V4 update should create .pset & .metadata files
    CheckFileExist(V4_TABLE1, kFilesInV4, true);
  }

  // Apply V4 update for table2
  {
    auto update = new TableUpdateV4(NS_LITERAL_CSTRING(V4_TABLE2));
    func(update, FULL_UPDATE, "test_prefix");

    ApplyUpdate(update);

    CheckFileExist(V4_TABLE2, kFilesInV4, true);
  }

  // Apply V4 update with the same prefix in previous full udpate
  // This should cause an update error.
  {
    auto update = new TableUpdateV4(NS_LITERAL_CSTRING(V4_TABLE1));
    func(update, PARTIAL_UPDATE, "test_prefix");

    ApplyUpdate(update);

    // A fail update should remove files for that table
    CheckFileExist(V4_TABLE1, kFilesInV4, false);

    // A fail update should NOT remove files for the other tables
    CheckFileExist(V2_TABLE, kFilesInV2, true);
    CheckFileExist(V4_TABLE2, kFilesInV4, true);
  }
}