summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/webextensions/test/xpcshell/test_ProductAddonChecker.js
blob: 6c562db6591ba92ec71bd9de2810d0a916b84e8c (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"use strict";

Components.utils.import("resource://gre/modules/addons/ProductAddonChecker.jsm");
Components.utils.import("resource://testing-common/httpd.js");
Components.utils.import("resource://gre/modules/osfile.jsm");

const LocalFile = new Components.Constructor("@mozilla.org/file/local;1", AM_Ci.nsIFile, "initWithPath");

var testserver = new HttpServer();
testserver.registerDirectory("/data/", do_get_file("data/productaddons"));
testserver.start();
var root = testserver.identity.primaryScheme + "://" +
           testserver.identity.primaryHost + ":" +
           testserver.identity.primaryPort + "/data/"

/**
 * Compares binary data of 2 arrays and returns true if they are the same
 *
 * @param arr1 The first array to compare
 * @param arr2 The second array to compare
*/
function compareBinaryData(arr1, arr2) {
  do_check_eq(arr1.length, arr2.length);
  for (let i = 0; i < arr1.length; i++) {
    if (arr1[i] != arr2[i]) {
      do_print("Data differs at index " + i +
               ", arr1: " + arr1[i] + ", arr2: " + arr2[i]);
      return false;
    }
  }
  return true;
}

/**
 * Reads a file's data and returns it
 *
 * @param file The file to read the data from
 * @return array of bytes for the data in the file.
*/
function getBinaryFileData(file) {
  let fileStream = AM_Cc["@mozilla.org/network/file-input-stream;1"].
                   createInstance(AM_Ci.nsIFileInputStream);
  // Open as RD_ONLY with default permissions.
  fileStream.init(file, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0);

  let stream = AM_Cc["@mozilla.org/binaryinputstream;1"].
               createInstance(AM_Ci.nsIBinaryInputStream);
  stream.setInputStream(fileStream);
  let bytes = stream.readByteArray(stream.available());
  fileStream.close();
  return bytes;
}

/**
 * Compares binary data of 2 files and returns true if they are the same
 *
 * @param file1 The first file to compare
 * @param file2 The second file to compare
*/
function compareFiles(file1, file2) {
  return compareBinaryData(getBinaryFileData(file1), getBinaryFileData(file2));
}

add_task(function* test_404() {
  let res = yield ProductAddonChecker.getProductAddonList(root + "404.xml");
  do_check_true(res.usedFallback);
});

add_task(function* test_not_xml() {
  let res = yield ProductAddonChecker.getProductAddonList(root + "bad.txt");
  do_check_true(res.usedFallback);
});

add_task(function* test_invalid_xml() {
  let res = yield ProductAddonChecker.getProductAddonList(root + "bad.xml");
  do_check_true(res.usedFallback);
});

add_task(function* test_wrong_xml() {
  let res = yield ProductAddonChecker.getProductAddonList(root + "bad2.xml");
  do_check_true(res.usedFallback);
});

add_task(function* test_missing() {
  let addons = yield ProductAddonChecker.getProductAddonList(root + "missing.xml");
  do_check_eq(addons, null);
});

add_task(function* test_empty() {
  let res = yield ProductAddonChecker.getProductAddonList(root + "empty.xml");
  do_check_true(Array.isArray(res.gmpAddons));
  do_check_eq(res.gmpAddons.length, 0);
});

add_task(function* test_good_xml() {
  let res = yield ProductAddonChecker.getProductAddonList(root + "good.xml");
  do_check_true(Array.isArray(res.gmpAddons));

  // There are three valid entries in the XML
  do_check_eq(res.gmpAddons.length, 5);

  let addon = res.gmpAddons[0];
  do_check_eq(addon.id, "test1");
  do_check_eq(addon.URL, "http://example.com/test1.xpi");
  do_check_eq(addon.hashFunction, undefined);
  do_check_eq(addon.hashValue, undefined);
  do_check_eq(addon.version, undefined);
  do_check_eq(addon.size, undefined);

  addon = res.gmpAddons[1];
  do_check_eq(addon.id, "test2");
  do_check_eq(addon.URL, "http://example.com/test2.xpi");
  do_check_eq(addon.hashFunction, "md5");
  do_check_eq(addon.hashValue, "djhfgsjdhf");
  do_check_eq(addon.version, undefined);
  do_check_eq(addon.size, undefined);

  addon = res.gmpAddons[2];
  do_check_eq(addon.id, "test3");
  do_check_eq(addon.URL, "http://example.com/test3.xpi");
  do_check_eq(addon.hashFunction, undefined);
  do_check_eq(addon.hashValue, undefined);
  do_check_eq(addon.version, "1.0");
  do_check_eq(addon.size, 45);

  addon = res.gmpAddons[3];
  do_check_eq(addon.id, "test4");
  do_check_eq(addon.URL, undefined);
  do_check_eq(addon.hashFunction, undefined);
  do_check_eq(addon.hashValue, undefined);
  do_check_eq(addon.version, undefined);
  do_check_eq(addon.size, undefined);

  addon = res.gmpAddons[4];
  do_check_eq(addon.id, undefined);
  do_check_eq(addon.URL, "http://example.com/test5.xpi");
  do_check_eq(addon.hashFunction, undefined);
  do_check_eq(addon.hashValue, undefined);
  do_check_eq(addon.version, undefined);
  do_check_eq(addon.size, undefined);
});

add_task(function* test_download_nourl() {
  try {
    let path = yield ProductAddonChecker.downloadAddon({});

    yield OS.File.remove(path);
    do_throw("Should not have downloaded a file with a missing url");
  }
  catch (e) {
    do_check_true(true, "Should have thrown when downloading a file with a missing url.");
  }
});

add_task(function* test_download_missing() {
  try {
    let path = yield ProductAddonChecker.downloadAddon({
      URL: root + "nofile.xpi",
    });

    yield OS.File.remove(path);
    do_throw("Should not have downloaded a missing file");
  }
  catch (e) {
    do_check_true(true, "Should have thrown when downloading a missing file.");
  }
});

add_task(function* test_download_noverify() {
  let path = yield ProductAddonChecker.downloadAddon({
    URL: root + "unsigned.xpi",
  });

  let stat = yield OS.File.stat(path);
  do_check_false(stat.isDir);
  do_check_eq(stat.size, 452)

  do_check_true(compareFiles(do_get_file("data/productaddons/unsigned.xpi"), new LocalFile(path)));

  yield OS.File.remove(path);
});

add_task(function* test_download_badsize() {
  try {
    let path = yield ProductAddonChecker.downloadAddon({
      URL: root + "unsigned.xpi",
      size: 400,
    });

    yield OS.File.remove(path);
    do_throw("Should not have downloaded a file with a bad size");
  }
  catch (e) {
    do_check_true(true, "Should have thrown when downloading a file with a bad size.");
  }
});

add_task(function* test_download_badhashfn() {
  try {
    let path = yield ProductAddonChecker.downloadAddon({
      URL: root + "unsigned.xpi",
      hashFunction: "sha2567",
      hashValue: "9b9abf7ddfc1a6d7ffc7e0247481dcc202363e4445ad3494fb22036f1698c7f3",
    });

    yield OS.File.remove(path);
    do_throw("Should not have downloaded a file with a bad hash function");
  }
  catch (e) {
    do_check_true(true, "Should have thrown when downloading a file with a bad hash function.");
  }
});

add_task(function* test_download_badhash() {
  try {
    let path = yield ProductAddonChecker.downloadAddon({
      URL: root + "unsigned.xpi",
      hashFunction: "sha256",
      hashValue: "8b9abf7ddfc1a6d7ffc7e0247481dcc202363e4445ad3494fb22036f1698c7f3",
    });

    yield OS.File.remove(path);
    do_throw("Should not have downloaded a file with a bad hash");
  }
  catch (e) {
    do_check_true(true, "Should have thrown when downloading a file with a bad hash.");
  }
});

add_task(function* test_download_works() {
  let path = yield ProductAddonChecker.downloadAddon({
    URL: root + "unsigned.xpi",
    size: 452,
    hashFunction: "sha256",
    hashValue: "9b9abf7ddfc1a6d7ffc7e0247481dcc202363e4445ad3494fb22036f1698c7f3",
  });

  let stat = yield OS.File.stat(path);
  do_check_false(stat.isDir);

  do_check_true(compareFiles(do_get_file("data/productaddons/unsigned.xpi"), new LocalFile(path)));

  yield OS.File.remove(path);
});