test_backupService_findBackupsInWellKnownLocations.js (3981B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 add_task( 7 async function test_findBackupsInWellKnownLocations_and_multipleFiles() { 8 // make an isolated temp folder to act as the “default backup dir” 9 const TEST_ROOT = await IOUtils.createUniqueDirectory( 10 PathUtils.tempDir, 11 "test-findBackupsInWellKnownLocations" 12 ); 13 const BACKUP_DIR = PathUtils.join(TEST_ROOT, "Backups"); 14 await IOUtils.makeDirectory(BACKUP_DIR, { createAncestors: true }); 15 16 // A helper to write an empty (but “valid-looking”) backup file 17 async function touch(fileName) { 18 const p = PathUtils.join(BACKUP_DIR, fileName); 19 await IOUtils.writeUTF8(p, "<!-- stub backup -->", { 20 tmpPath: p + ".tmp", 21 }); 22 return p; 23 } 24 Services.prefs.setStringPref("browser.backup.location", BACKUP_DIR); 25 26 // Create the service and force it to search our temp folder instead of the real default 27 let bs = new BackupService(); 28 let sandbox = sinon.createSandbox(); 29 30 // getBackupFileInfo should return without throwing to simulate 31 // what happens when a valid backup file's validity is checked 32 sandbox.stub(bs, "getBackupFileInfo").callsFake(async _filePath => {}); 33 34 // Sanity: the directory exists and is empty 35 Assert.ok(await IOUtils.exists(BACKUP_DIR), "Backup directory exists"); 36 Assert.equal( 37 (await IOUtils.getChildren(BACKUP_DIR)).length, 38 0, 39 "Folder is empty" 40 ); 41 42 // 1) Single valid file -> findBackupsInWellKnownLocations should find it 43 const ONE = "FirefoxBackup_one_20241201-1200.html"; 44 await touch(ONE); 45 46 let result = await bs.findBackupsInWellKnownLocations(); 47 Assert.ok(result.found, "Found should be true with one candidate"); 48 Assert.equal( 49 result.multipleBackupsFound, 50 false, 51 "multipleBackupsFound should be false" 52 ); 53 // The service stores whatever IOUtils.getChildren returns; assert path ends with our file 54 Assert.ok( 55 result.backupFileToRestore && result.backupFileToRestore.endsWith(ONE), 56 "backupFileToRestore should point at the single html file" 57 ); 58 59 // 2) Add a second matching file -> well-known search should refuse to pick (validateFile=false) 60 const TWO = "FirefoxBackup_two_20241202-1300.html"; 61 await touch(TWO); 62 63 let result2 = await bs.findBackupsInWellKnownLocations(); 64 Assert.ok( 65 !result2.found, 66 "Found should be false when multiple candidates exist and validateFile=false" 67 ); 68 Assert.equal( 69 result2.multipleBackupsFound, 70 true, 71 "Should signal multipleBackupsFound" 72 ); 73 Assert.equal( 74 result2.backupFileToRestore, 75 null, 76 "No file chosen if multiple & not allowed" 77 ); 78 79 // 3) Call the lower-level API with multipleFiles=true (still no validation) 80 let { multipleBackupsFound } = await bs.findIfABackupFileExists({ 81 validateFile: false, 82 multipleFiles: true, // allow choosing even if we see more than one 83 }); 84 Assert.ok(!multipleBackupsFound, "Should not report multiple when allowed"); 85 86 // 4) With validateFile=true and multipleFiles=true, should select newest file, 87 // but still report multipleBackupsFound=true 88 let result3 = await bs.findBackupsInWellKnownLocations({ 89 validateFile: true, 90 multipleFiles: true, 91 }); 92 Assert.ok( 93 result3.found, 94 "Found should be true when validateFile=true and multiple files exist" 95 ); 96 Assert.equal( 97 result3.multipleBackupsFound, 98 true, 99 "Should signal multipleBackupsFound when validateFile=true and multipleFiles=true and multiple files exist" 100 ); 101 Assert.ok( 102 result3.backupFileToRestore && result3.backupFileToRestore.endsWith(TWO), 103 "Should select the newest file when validateFile=true" 104 ); 105 106 // Cleanup 107 sandbox.restore(); 108 await IOUtils.remove(TEST_ROOT, { recursive: true }); 109 } 110 );