summaryrefslogtreecommitdiffstats
path: root/netwerk/dns/tests/unit/test_PublicSuffixList.js
blob: ad61abea8e59e9d0134a39845c7288a8f479b5d3 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Any copyright is dedicated to the Public Domain.
 * https://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { PublicSuffixList } = ChromeUtils.importESModule(
  "resource://gre/modules/netwerk-dns/PublicSuffixList.sys.mjs"
);
const { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);

const CLIENT = PublicSuffixList.CLIENT;
const SIGNAL = "public-suffix-list-updated";

const PAYLOAD_UPDATED_RECORDS = {
  current: [{ id: "tld-dafsa", "commit-hash": "current-commit-hash" }],
  created: [],
  updated: [
    {
      old: { id: "tld-dafsa", "commit-hash": "current-commit-hash" },
      new: { id: "tld-dafsa", "commit-hash": "new-commit-hash" },
    },
  ],
  deleted: [],
};
const PAYLOAD_CREATED_RECORDS = {
  current: [],
  created: [
    {
      id: "tld-dafsa",
      "commit-hash": "new-commit-hash",
      attachment: {},
    },
  ],
  updated: [],
  deleted: [],
};
const PAYLOAD_UPDATED_AND_CREATED_RECORDS = {
  current: [{ id: "tld-dafsa", "commit-hash": "current-commit-hash" }],
  created: [{ id: "tld-dafsa", "commit-hash": "another-commit-hash" }],
  updated: [
    {
      old: { id: "tld-dafsa", "commit-hash": "current-commit-hash" },
      new: { id: "tld-dafsa", "commit-hash": "new-commit-hash" },
    },
  ],
  deleted: [],
};

const fakeDafsaBinFile = do_get_file("data/fake_remote_dafsa.bin");
const mockedFilePath = fakeDafsaBinFile.path;

function setup() {
  Services.prefs.setBoolPref("network.psl.onUpdate_notify", true);
}
setup();
registerCleanupFunction(() => {
  Services.prefs.clearUserPref("network.psl.onUpdate_notify");
});

/**
 * downloadToDiskCalled is used by mockDownloadToDisk() and resetMockDownloadToDisk()
 * to keep track weather CLIENT.attachments.download is called or not
 * downloadToDiskBackup will help restore CLIENT.attachments.download to original definition
 * notifyUpdateBackup will help restore PublicSuffixList.notifyUpdate to original definition
 */
let downloadToDiskCalled = false;
const downloadToDiskBackup = CLIENT.attachments.downloadToDisk;

// returns a fake fileURI and sends a signal with filePath and no nsifile
function mockDownloadToDisk() {
  downloadToDiskCalled = false;
  CLIENT.attachments.downloadToDisk = async rec => {
    downloadToDiskCalled = true;
    return `file://${mockedFilePath}`; // Create a fake file URI
  };
}

// resetMockDownloadToDisk() must be run at the end of the test that uses mockDownloadToDisk()
const resetMockDownloadToDisk = () => {
  CLIENT.attachments.downloadToDisk = downloadToDiskBackup;
};

add_task(async () => {
  info("File path sent when record is in DB.");

  await CLIENT.db.clear(); // Make sure there's no record initially
  await CLIENT.db.create({
    id: "tld-dafsa",
    "commit-hash": "fake-commit-hash",
    attachment: {},
  });

  mockDownloadToDisk();

  const promiseSignal = TestUtils.topicObserved(SIGNAL);
  await PublicSuffixList.init();
  const observed = await promiseSignal;

  Assert.equal(
    observed[1],
    mockedFilePath,
    "File path sent when record is in DB."
  );
  await CLIENT.db.clear(); // Clean up the mockDownloaded record
  resetMockDownloadToDisk();
});

add_task(async () => {
  info("File path sent when record updated.");

  mockDownloadToDisk();

  const promiseSignal = TestUtils.topicObserved(SIGNAL);
  await PublicSuffixList.init();
  await CLIENT.emit("sync", { data: PAYLOAD_UPDATED_RECORDS });
  const observed = await promiseSignal;

  Assert.equal(
    observed[1],
    mockedFilePath,
    "File path sent when record updated."
  );
  resetMockDownloadToDisk();
});

add_task(async () => {
  info("Attachment downloaded when record created.");

  mockDownloadToDisk();

  await PublicSuffixList.init();
  await CLIENT.emit("sync", { data: PAYLOAD_CREATED_RECORDS });

  Assert.equal(
    downloadToDiskCalled,
    true,
    "Attachment downloaded when record created."
  );
  resetMockDownloadToDisk();
});

add_task(async () => {
  info("Attachment downloaded when record updated.");

  mockDownloadToDisk();

  await PublicSuffixList.init();
  await CLIENT.emit("sync", { data: PAYLOAD_UPDATED_RECORDS });

  Assert.equal(
    downloadToDiskCalled,
    true,
    "Attachment downloaded when record updated."
  );
  resetMockDownloadToDisk();
});

add_task(async () => {
  info("No download when more than one record is changed.");

  mockDownloadToDisk();

  await PublicSuffixList.init();
  await CLIENT.emit("sync", { data: PAYLOAD_UPDATED_AND_CREATED_RECORDS });

  Assert.equal(
    downloadToDiskCalled,
    false,
    "No download when more than one record is changed."
  );
  resetMockDownloadToDisk();
});