summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_corruptfile.js
blob: 92b375850faa3425189b071120cc07de8452a25a (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

// Tests that attempting to install a corrupt XPI file doesn't break the universe

const profileDir = gProfD.clone();
profileDir.append("extensions");

function run_test() {
  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");

  startupManager();

  if (TEST_UNPACKED)
    run_test_unpacked();
  else
    run_test_packed();
}

// When installing packed we won't detect corruption in the XPI until we attempt
// to load bootstrap.js so everything will look normal from the outside.
function run_test_packed() {
  do_test_pending();

  prepare_test({
    "corrupt@tests.mozilla.org": [
      ["onInstalling", false],
      ["onInstalled", false]
    ]
  }, [
    "onNewInstall",
    "onInstallStarted",
    "onInstallEnded"
  ]);

  installAllFiles([do_get_file("data/corruptfile.xpi")], function() {
    ensure_test_completed();

    AddonManager.getAddonByID("corrupt@tests.mozilla.org", function(addon) {
      do_check_neq(addon, null);

      do_test_finished();
    });
  });
}

// When extracting the corruption will be detected and the add-on fails to
// install
function run_test_unpacked() {
  do_test_pending();

  prepare_test({
    "corrupt@tests.mozilla.org": [
      ["onInstalling", false],
      "onOperationCancelled"
    ]
  }, [
    "onNewInstall",
    "onInstallStarted",
    "onInstallFailed"
  ]);

  installAllFiles([do_get_file("data/corruptfile.xpi")], function() {
    ensure_test_completed();

    // Check the add-on directory isn't left over
    var addonDir = profileDir.clone();
    addonDir.append("corrupt@tests.mozilla.org");
    pathShouldntExist(addonDir);

    // Check the staging directory isn't left over
    var stageDir = profileDir.clone();
    stageDir.append("staged");
    pathShouldntExist(stageDir);

    AddonManager.getAddonByID("corrupt@tests.mozilla.org", function(addon) {
      do_check_eq(addon, null);

      do_test_finished();
    });
  });
}