toolkit/components/places/tests/unit/test_utils_backups_create.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * Check for correct functionality of bookmarks backups
michael@0 9 */
michael@0 10
michael@0 11 const NUMBER_OF_BACKUPS = 10;
michael@0 12
michael@0 13 function run_test() {
michael@0 14 run_next_test();
michael@0 15 }
michael@0 16
michael@0 17 add_task(function () {
michael@0 18 // Generate random dates.
michael@0 19 let dateObj = new Date();
michael@0 20 let dates = [];
michael@0 21 while (dates.length < NUMBER_OF_BACKUPS) {
michael@0 22 // Use last year to ensure today's backup is the newest.
michael@0 23 let randomDate = new Date(dateObj.getFullYear() - 1,
michael@0 24 Math.floor(12 * Math.random()),
michael@0 25 Math.floor(28 * Math.random()));
michael@0 26 let dateString = randomDate.toLocaleFormat("%Y-%m-%d");
michael@0 27 if (dates.indexOf(dateString) == -1)
michael@0 28 dates.push(dateString);
michael@0 29 }
michael@0 30 // Sort dates from oldest to newest.
michael@0 31 dates.sort();
michael@0 32
michael@0 33 // Get and cleanup the backups folder.
michael@0 34 let backupFolderPath = yield PlacesBackups.getBackupFolder();
michael@0 35 let bookmarksBackupDir = new FileUtils.File(backupFolderPath);
michael@0 36
michael@0 37 // Fake backups are created backwards to ensure we won't consider file
michael@0 38 // creation time.
michael@0 39 // Create fake backups for the newest dates.
michael@0 40 for (let i = dates.length - 1; i >= 0; i--) {
michael@0 41 let backupFilename = PlacesBackups.getFilenameForDate(new Date(dates[i]));
michael@0 42 let backupFile = bookmarksBackupDir.clone();
michael@0 43 backupFile.append(backupFilename);
michael@0 44 backupFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0666", 8));
michael@0 45 do_log_info("Creating fake backup " + backupFile.leafName);
michael@0 46 if (!backupFile.exists())
michael@0 47 do_throw("Unable to create fake backup " + backupFile.leafName);
michael@0 48 }
michael@0 49
michael@0 50 yield PlacesBackups.create(NUMBER_OF_BACKUPS);
michael@0 51 // Add today's backup.
michael@0 52 dates.push(dateObj.toLocaleFormat("%Y-%m-%d"));
michael@0 53
michael@0 54 // Check backups. We have 11 dates but we the max number is 10 so the
michael@0 55 // oldest backup should have been removed.
michael@0 56 for (let i = 0; i < dates.length; i++) {
michael@0 57 let backupFilename;
michael@0 58 let shouldExist;
michael@0 59 let backupFile;
michael@0 60 if (i > 0) {
michael@0 61 let files = bookmarksBackupDir.directoryEntries;
michael@0 62 while (files.hasMoreElements()) {
michael@0 63 let entry = files.getNext().QueryInterface(Ci.nsIFile);
michael@0 64 if (PlacesBackups.filenamesRegex.test(entry.leafName)) {
michael@0 65 backupFilename = entry.leafName;
michael@0 66 backupFile = entry;
michael@0 67 break;
michael@0 68 }
michael@0 69 }
michael@0 70 shouldExist = true;
michael@0 71 }
michael@0 72 else {
michael@0 73 backupFilename = PlacesBackups.getFilenameForDate(new Date(dates[i]));
michael@0 74 backupFile = bookmarksBackupDir.clone();
michael@0 75 backupFile.append(backupFilename);
michael@0 76 shouldExist = false;
michael@0 77 }
michael@0 78 if (backupFile.exists() != shouldExist)
michael@0 79 do_throw("Backup should " + (shouldExist ? "" : "not") + " exist: " + backupFilename);
michael@0 80 }
michael@0 81
michael@0 82 // Cleanup backups folder.
michael@0 83 // XXX: Can't use bookmarksBackupDir.remove(true) because file lock happens
michael@0 84 // on WIN XP.
michael@0 85 let files = bookmarksBackupDir.directoryEntries;
michael@0 86 while (files.hasMoreElements()) {
michael@0 87 let entry = files.getNext().QueryInterface(Ci.nsIFile);
michael@0 88 entry.remove(false);
michael@0 89 }
michael@0 90 do_check_false(bookmarksBackupDir.directoryEntries.hasMoreElements());
michael@0 91 });

mercurial