test_unaccessedOrigins.js (4180B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 const SEC_PER_MONTH = 60 * 60 * 24 * 30; 7 8 async function testSteps() { 9 function getHostname(index) { 10 return "www.example" + index + ".com"; 11 } 12 13 function getOrigin(index) { 14 return "https://" + getHostname(index); 15 } 16 17 function getOriginDir(index) { 18 return getRelativeFile("storage/default/https+++" + getHostname(index)); 19 } 20 21 function updateOriginLastAccessTime(index, deltaSec) { 22 let originDir = getOriginDir(index); 23 24 let metadataFile = originDir.clone(); 25 metadataFile.append(".metadata-v2"); 26 27 let fileRandomAccessStream = Cc[ 28 "@mozilla.org/network/file-random-access-stream;1" 29 ].createInstance(Ci.nsIFileRandomAccessStream); 30 fileRandomAccessStream.init(metadataFile, -1, -1, 0); 31 32 let binaryInputStream = Cc[ 33 "@mozilla.org/binaryinputstream;1" 34 ].createInstance(Ci.nsIBinaryInputStream); 35 binaryInputStream.setInputStream(fileRandomAccessStream); 36 37 let lastAccessTime = binaryInputStream.read64(); 38 39 let seekableStream = fileRandomAccessStream.QueryInterface( 40 Ci.nsISeekableStream 41 ); 42 seekableStream.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0); 43 44 binaryOutputStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance( 45 Ci.nsIBinaryOutputStream 46 ); 47 binaryOutputStream.setOutputStream(fileRandomAccessStream); 48 49 binaryOutputStream.write64(lastAccessTime + deltaSec * PR_USEC_PER_SEC); 50 51 binaryOutputStream.close(); 52 53 binaryInputStream.close(); 54 } 55 56 function verifyOriginDir(index, shouldExist) { 57 let originDir = getOriginDir(index); 58 let exists = originDir.exists(); 59 if (shouldExist) { 60 ok(exists, "Origin directory does exist"); 61 } else { 62 ok(!exists, "Origin directory doesn't exist"); 63 } 64 } 65 66 info("Setting prefs"); 67 68 Services.prefs.setBoolPref("dom.quotaManager.loadQuotaFromCache", false); 69 Services.prefs.setBoolPref("dom.quotaManager.checkQuotaInfoLoadTime", true); 70 Services.prefs.setIntPref( 71 "dom.quotaManager.longQuotaInfoLoadTimeThresholdMs", 72 0 73 ); 74 75 info("Initializing"); 76 77 request = init(); 78 await requestFinished(request); 79 80 info("Initializing temporary storage"); 81 82 request = initTemporaryStorage(); 83 await requestFinished(request); 84 85 info("Initializing origins"); 86 87 for (let index = 0; index < 30; index++) { 88 request = initTemporaryOrigin( 89 "default", 90 getPrincipal(getOrigin(index)), 91 /* createIfNonExistent */ true 92 ); 93 await requestFinished(request); 94 } 95 96 info("Updating last access time of selected origins"); 97 98 for (let index = 0; index < 10; index++) { 99 updateOriginLastAccessTime(index, -14 * SEC_PER_MONTH); 100 } 101 102 for (let index = 10; index < 20; index++) { 103 updateOriginLastAccessTime(index, -7 * SEC_PER_MONTH); 104 } 105 106 info("Resetting"); 107 108 request = reset(); 109 await requestFinished(request); 110 111 info("Setting pref"); 112 113 Services.prefs.setIntPref( 114 "dom.quotaManager.unaccessedForLongTimeThresholdSec", 115 13 * SEC_PER_MONTH 116 ); 117 118 info("Initializing"); 119 120 request = init(); 121 await requestFinished(request); 122 123 info("Initializing temporary storage"); 124 125 request = initTemporaryStorage(); 126 await requestFinished(request); 127 128 info("Verifying origin directories"); 129 130 for (let index = 0; index < 10; index++) { 131 verifyOriginDir(index, false); 132 } 133 for (let index = 10; index < 30; index++) { 134 verifyOriginDir(index, true); 135 } 136 137 info("Resetting"); 138 139 request = reset(); 140 await requestFinished(request); 141 142 info("Setting pref"); 143 144 Services.prefs.setIntPref( 145 "dom.quotaManager.unaccessedForLongTimeThresholdSec", 146 6 * SEC_PER_MONTH 147 ); 148 149 info("Initializing"); 150 151 request = init(); 152 await requestFinished(request); 153 154 info("Initializing temporary storage"); 155 156 request = initTemporaryStorage(); 157 await requestFinished(request); 158 159 info("Verifying origin directories"); 160 161 for (let index = 0; index < 20; index++) { 162 verifyOriginDir(index, false); 163 } 164 for (let index = 20; index < 30; index++) { 165 verifyOriginDir(index, true); 166 } 167 168 info("Resetting"); 169 170 request = reset(); 171 await requestFinished(request); 172 }