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

async function testSteps() {
  // ToDo: Replace storage and default with a getter function once we expose the
  // filenames of them to a IDL file.
  const basePath = `${storageDirName}/${defaultPersistenceDirName}/`;
  const principal = getPrincipal("https://example.com");
  const originDirName = "https+++example.com";

  // ToDo: Replace caches.sqlite with a getter function once we expose the
  // filename of it to a IDL file.
  const cachesDatabase = getRelativeFile(
    `${basePath}/${originDirName}/${cacheClientDirName}/caches.sqlite`
  );
  // ToDo: Replace .padding with a getter function once we expose the filename
  // of it to a IDL file.
  const paddingFile = getRelativeFile(
    `${basePath}/${originDirName}/${cacheClientDirName}/.padding`
  );
  // ToDo: Replace .padding-tmp with a getter function once we expose the
  // filename of it to a IDL file.
  const paddingTempFile = getRelativeFile(
    `${basePath}/${originDirName}/${cacheClientDirName}/.padding-tmp`
  );

  async function createTestOrigin() {
    async function sandboxScript() {
      const cache = await caches.open("myCache");
      const request = new Request("https://example.com/index.html");
      const response = new Response("hello world");
      await cache.put(request, response);
    }

    const sandbox = new Cu.Sandbox(principal, {
      wantGlobalProperties: ["caches", "fetch"],
    });

    const promise = new Promise(function(resolve, reject) {
      sandbox.resolve = resolve;
      sandbox.reject = reject;
    });

    Cu.evalInSandbox(
      sandboxScript.toSource() + " sandboxScript().then(resolve, reject);",
      sandbox
    );
    await promise;

    let request = reset();
    await requestFinished(request);
  }

  function removeFile(file) {
    file.remove(false);
  }

  function createEmptyFile(file) {
    file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644);
  }

  function checkFiles(
    expectCachesDatabase,
    expectPaddingFile,
    expectTempPaddingFile
  ) {
    let exists = cachesDatabase.exists();
    if (expectCachesDatabase) {
      ok(exists, "caches.sqlite does exist");
    } else {
      ok(!exists, "caches.sqlite doesn't exist");
    }

    exists = paddingFile.exists();
    if (expectPaddingFile) {
      ok(exists, ".padding does exist");
    } else {
      ok(!exists, ".padding doesn't exist");
    }

    exists = paddingTempFile.exists();
    if (expectTempPaddingFile) {
      ok(exists, ".padding-tmp does exist");
    } else {
      ok(!exists, ".padding-tmp doesn't exist");
    }
  }

  async function testInitFunctionality(
    hasCachesDatabase,
    hasPaddingFile,
    hasTempPaddingFile
  ) {
    info(
      `Testing init cache directory when caches.sqlite ` +
        `${hasCachesDatabase ? "exists" : "doesn't exist"}, .padding ` +
        `${hasPaddingFile ? "exists" : "doesn't exist"}, .padding-tmp ` +
        `${hasTempPaddingFile ? "exists" : "doesn't exist"}`
    );

    await createTestOrigin();

    checkFiles(true, true, false);

    if (hasTempPaddingFile) {
      createEmptyFile(paddingTempFile);
    }

    if (!hasPaddingFile) {
      removeFile(paddingFile);
    }

    if (!hasCachesDatabase) {
      removeFile(cachesDatabase);
    }

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

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

    request = initTemporaryOrigin(principal);
    await requestFinished(request);

    // After the origin is initialized, ".padding-tmp" should have always been
    // removed and ".padding" should only exist when "caches.sqlite" exists.
    checkFiles(hasCachesDatabase, hasCachesDatabase, false);

    request = clearOrigin(principal);
    await requestFinished(request);
  }

  await testInitFunctionality(false, false, false);
  // ToDo: .padding-tmp will be removed when the first cache operation is
  // executed, but we should remove it during InitOrigin in this case.
  //await testInitFunctionality(false, false, true);
  // ToDo: .padding should be removed when there is no caches.sqlite.
  //await testInitFunctionality(false, true, false);
  // ToDo: In this case, padding size should be counted as zero because there is
  // no database, but we don't.
  //await testInitFunctionality(false, true, true);
  await testInitFunctionality(true, false, false);
  await testInitFunctionality(true, false, true);
  await testInitFunctionality(true, true, false);
  await testInitFunctionality(true, true, true);
}