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

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

mercurial