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

add_task(async function testSteps() {
  const principal = getPrincipal("http://example.com");

  const dataFile = getRelativeFile(
    "storage/default/http+++example.com/ls/data.sqlite"
  );

  const usageJournalFile = getRelativeFile(
    "storage/default/http+++example.com/ls/usage-journal"
  );

  const usageFile = getRelativeFile(
    "storage/default/http+++example.com/ls/usage"
  );

  const data = {};
  data.key = "foo";
  data.value = "bar";
  data.usage = data.key.length + data.value.length;

  async function createStorageForMigration(createUsageDir) {
    info("Clearing");

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

    info("Installing package");

    // The profile contains storage.sqlite and webappsstore.sqlite. The file
    // create_db.js in the package was run locally, specifically it was
    // temporarily added to xpcshell.ini and then executed:
    // mach xpcshell-test --interactive dom/localstorage/test/unit/create_db.js
    installPackage("usageAfterMigration_profile");

    if (createUsageDir) {
      // Origin must be initialized before the usage dir is created.

      info("Initializing storage");

      request = initStorage();
      await requestFinished(request);

      info("Initializing temporary storage");

      request = initTemporaryStorage();
      await requestFinished(request);

      info("Initializing origin");

      request = initTemporaryOrigin("default", principal);
      await requestFinished(request);

      info("Creating usage as a directory");

      // This will cause a failure during migration.
      usageFile.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8));
    }
  }

  function verifyData() {
    ok(dataFile.exists(), "Data file does exist");
  }

  async function verifyUsage(success) {
    info("Verifying usage in memory");

    let request = getOriginUsage(principal, /* fromMemory */ true);
    await requestFinished(request);

    if (success) {
      is(request.result.usage, data.usage, "Correct usage");
    } else {
      is(request.result.usage, 0, "Zero usage");
    }

    info("Verifying usage on disk");

    if (success) {
      ok(!usageJournalFile.exists(), "Usage journal file doesn't exist");
      ok(usageFile.exists(), "Usage file does exist");
      let usage = await readUsageFromUsageFile(usageFile);
      is(usage, data.usage, "Correct usage");
    } else {
      ok(usageJournalFile.exists(), "Usage journal file does exist");
      ok(usageFile.exists(), "Usage file does exist");
    }
  }

  info("Setting prefs");

  Services.prefs.setBoolPref(
    "dom.storage.enable_unsupported_legacy_implementation",
    false
  );

  info("Stage 1 - Testing usage after successful data migration");

  await createStorageForMigration(/* createUsageDir */ false);

  info("Getting storage");

  let storage = getLocalStorage(principal);

  info("Opening");

  storage.open();

  verifyData();

  await verifyUsage(/* success */ true);

  info("Stage 2 - Testing usage after unsuccessful data migration");

  await createStorageForMigration(/* createUsageDir */ true);

  info("Getting storage");

  storage = getLocalStorage(principal);

  info("Opening");

  try {
    storage.open();
    ok(false, "Should have thrown");
  } catch (ex) {
    ok(true, "Did throw");
  }

  verifyData();

  await verifyUsage(/* success */ false);

  info("Stage 3 - Testing usage after unsuccessful/successful data migration");

  await createStorageForMigration(/* createUsageDir */ true);

  info("Getting storage");

  storage = getLocalStorage(principal);

  info("Opening");

  try {
    storage.open();
    ok(false, "Should have thrown");
  } catch (ex) {
    ok(true, "Did throw");
  }

  usageFile.remove(true);

  info("Opening");

  storage.open();

  verifyData();

  await verifyUsage(/* success */ true);
});