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
175
176
|
/* Any copyright is dedicated to the Public Domain.
* https://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { PublicSuffixList } = ChromeUtils.import(
"resource://gre/modules/netwerk-dns/PublicSuffixList.jsm"
);
const { TestUtils } = ChromeUtils.import(
"resource://testing-common/TestUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
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");
});
/**
* downloadCalled is used by mockDownload() and resetMockDownload()
* to keep track weather CLIENT.attachments.download is called or not
* downloadBackup will help restore CLIENT.attachments.download to original definition
* notifyUpdateBackup will help restore PublicSuffixList.notifyUpdate to original definition
*/
let downloadCalled = false;
const downloadBackup = CLIENT.attachments.download;
// returns a fake fileURI and sends a signal with filePath and no nsifile
const mockDownload = () => {
downloadCalled = false;
CLIENT.attachments.download = async rec => {
downloadCalled = true;
return `file://${mockedFilePath}`; // Create a fake file URI
};
};
// resetMockDownload() must be run at the end of the test that uses mockDownload()
const resetMockDownload = () => {
CLIENT.attachments.download = downloadBackup;
};
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: {},
});
mockDownload();
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
resetMockDownload();
});
add_task(async () => {
info("File path sent when record updated.");
mockDownload();
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."
);
resetMockDownload();
});
add_task(async () => {
info("Attachment downloaded when record created.");
mockDownload();
await PublicSuffixList.init();
await CLIENT.emit("sync", { data: PAYLOAD_CREATED_RECORDS });
Assert.equal(
downloadCalled,
true,
"Attachment downloaded when record created."
);
resetMockDownload();
});
add_task(async () => {
info("Attachment downloaded when record updated.");
mockDownload();
await PublicSuffixList.init();
await CLIENT.emit("sync", { data: PAYLOAD_UPDATED_RECORDS });
Assert.equal(
downloadCalled,
true,
"Attachment downloaded when record updated."
);
resetMockDownload();
});
add_task(async () => {
info("No download when more than one record is changed.");
mockDownload();
await PublicSuffixList.init();
await CLIENT.emit("sync", { data: PAYLOAD_UPDATED_AND_CREATED_RECORDS });
Assert.equal(
downloadCalled,
false,
"No download when more than one record is changed."
);
resetMockDownload();
});
|