summaryrefslogtreecommitdiffstats
path: root/dom/quota/test/xpcshell/test_createLocalStorage.js
blob: a99a458713663f4a242cce74ed92e9198877535c (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
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

async function testSteps() {
  const webAppsStoreFile = "webappsstore.sqlite";
  const lsArchiveFile = "storage/ls-archive.sqlite";
  const lsArchiveTmpFile = "storage/ls-archive-tmp.sqlite";

  function checkArchiveFileNotExists() {
    info("Checking archive tmp file");

    let archiveTmpFile = getRelativeFile(lsArchiveTmpFile);

    let exists = archiveTmpFile.exists();
    ok(!exists, "archive tmp file doesn't exist");

    info("Checking archive file");

    let archiveFile = getRelativeFile(lsArchiveFile);

    exists = archiveFile.exists();
    ok(!exists, "archive file doesn't exist");
  }

  function checkArchiveFileExists() {
    info("Checking archive tmp file");

    let archiveTmpFile = getRelativeFile(lsArchiveTmpFile);

    let exists = archiveTmpFile.exists();
    ok(!exists, "archive tmp file doesn't exist");

    info("Checking archive file");

    let archiveFile = getRelativeFile(lsArchiveFile);

    exists = archiveFile.exists();
    ok(exists, "archive file does exist");

    info("Checking archive file size");

    let fileSize = archiveFile.fileSize;
    ok(fileSize > 0, "archive file size is greater than zero");
  }

  // Profile 1 - Nonexistent apps store file.
  info("Clearing");

  let request = clear();
  await requestFinished(request);

  let appsStoreFile = getRelativeFile(webAppsStoreFile);

  let exists = appsStoreFile.exists();
  ok(!exists, "apps store file doesn't exist");

  checkArchiveFileNotExists();

  try {
    request = init();
    await requestFinished(request);

    ok(true, "Should not have thrown");
  } catch (ex) {
    ok(false, "Should not have thrown");
  }

  checkArchiveFileExists();

  // Profile 2 - apps store file is a directory.
  info("Clearing");

  request = clear();
  await requestFinished(request);

  appsStoreFile.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8));

  checkArchiveFileNotExists();

  try {
    request = init();
    await requestFinished(request);

    ok(true, "Should not have thrown");
  } catch (ex) {
    ok(false, "Should not have thrown");
  }

  checkArchiveFileExists();

  appsStoreFile.remove(true);

  // Profile 3 - Corrupted apps store file.
  info("Clearing");

  request = clear();
  await requestFinished(request);

  let ostream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
    Ci.nsIFileOutputStream
  );
  ostream.init(appsStoreFile, -1, parseInt("0644", 8), 0);
  ostream.write("foobar", 6);
  ostream.close();

  checkArchiveFileNotExists();

  try {
    request = init();
    await requestFinished(request);

    ok(true, "Should not have thrown");
  } catch (ex) {
    ok(false, "Should not have thrown");
  }

  checkArchiveFileExists();

  appsStoreFile.remove(false);

  // Profile 4 - Nonupdateable apps store file.
  info("Clearing");

  request = clear();
  await requestFinished(request);

  info("Installing package");

  // The profile contains storage.sqlite and webappsstore.sqlite
  // webappstore.sqlite was taken from FF 54 to force an upgrade.
  // There's just one record in the webappsstore2 table. The record was
  // modified by renaming the origin attribute userContextId to userContextKey.
  // This triggers an error during the upgrade.
  installPackage("createLocalStorage_profile");

  let fileSize = appsStoreFile.fileSize;
  ok(fileSize > 0, "apps store file size is greater than zero");

  checkArchiveFileNotExists();

  try {
    request = init();
    await requestFinished(request);

    ok(true, "Should not have thrown");
  } catch (ex) {
    ok(false, "Should not have thrown");
  }

  checkArchiveFileExists();

  appsStoreFile.remove(false);
}