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

const SEC_PER_MONTH = 60 * 60 * 24 * 30;

async function testSteps() {
  function getHostname(index) {
    return "www.example" + index + ".com";
  }

  function getOrigin(index) {
    return "https://" + getHostname(index);
  }

  function getOriginDir(index) {
    return getRelativeFile("storage/default/https+++" + getHostname(index));
  }

  function updateOriginLastAccessTime(index, deltaSec) {
    let originDir = getOriginDir(index);

    let metadataFile = originDir.clone();
    metadataFile.append(".metadata-v2");

    let fileRandomAccessStream = Cc[
      "@mozilla.org/network/file-random-access-stream;1"
    ].createInstance(Ci.nsIFileRandomAccessStream);
    fileRandomAccessStream.init(metadataFile, -1, -1, 0);

    let binaryInputStream = Cc[
      "@mozilla.org/binaryinputstream;1"
    ].createInstance(Ci.nsIBinaryInputStream);
    binaryInputStream.setInputStream(fileRandomAccessStream);

    let lastAccessTime = binaryInputStream.read64();

    let seekableStream = fileRandomAccessStream.QueryInterface(
      Ci.nsISeekableStream
    );
    seekableStream.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);

    binaryOutputStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
      Ci.nsIBinaryOutputStream
    );
    binaryOutputStream.setOutputStream(fileRandomAccessStream);

    binaryOutputStream.write64(lastAccessTime + deltaSec * PR_USEC_PER_SEC);

    binaryOutputStream.close();

    binaryInputStream.close();
  }

  function verifyOriginDir(index, shouldExist) {
    let originDir = getOriginDir(index);
    let exists = originDir.exists();
    if (shouldExist) {
      ok(exists, "Origin directory does exist");
    } else {
      ok(!exists, "Origin directory doesn't exist");
    }
  }

  info("Setting prefs");

  Services.prefs.setBoolPref("dom.quotaManager.loadQuotaFromCache", false);
  Services.prefs.setBoolPref("dom.quotaManager.checkQuotaInfoLoadTime", true);
  Services.prefs.setIntPref(
    "dom.quotaManager.longQuotaInfoLoadTimeThresholdMs",
    0
  );

  info("Initializing");

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

  info("Initializing temporary storage");

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

  info("Initializing origins");

  for (let index = 0; index < 30; index++) {
    request = initTemporaryOrigin("default", getPrincipal(getOrigin(index)));
    await requestFinished(request);
  }

  info("Updating last access time of selected origins");

  for (let index = 0; index < 10; index++) {
    updateOriginLastAccessTime(index, -14 * SEC_PER_MONTH);
  }

  for (let index = 10; index < 20; index++) {
    updateOriginLastAccessTime(index, -7 * SEC_PER_MONTH);
  }

  info("Resetting");

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

  info("Setting pref");

  Services.prefs.setIntPref(
    "dom.quotaManager.unaccessedForLongTimeThresholdSec",
    13 * SEC_PER_MONTH
  );

  info("Initializing");

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

  info("Initializing temporary storage");

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

  info("Verifying origin directories");

  for (let index = 0; index < 10; index++) {
    verifyOriginDir(index, false);
  }
  for (let index = 10; index < 30; index++) {
    verifyOriginDir(index, true);
  }

  info("Resetting");

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

  info("Setting pref");

  Services.prefs.setIntPref(
    "dom.quotaManager.unaccessedForLongTimeThresholdSec",
    6 * SEC_PER_MONTH
  );

  info("Initializing");

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

  info("Initializing temporary storage");

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

  info("Verifying origin directories");

  for (let index = 0; index < 20; index++) {
    verifyOriginDir(index, false);
  }
  for (let index = 20; index < 30; index++) {
    verifyOriginDir(index, true);
  }

  info("Resetting");

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