test_BackupService_recoverFromSnapshotFolder.js (4446B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { ArchiveUtils } = ChromeUtils.importESModule( 7 "resource:///modules/backup/ArchiveUtils.sys.mjs" 8 ); 9 const { JsonSchema } = ChromeUtils.importESModule( 10 "resource://gre/modules/JsonSchema.sys.mjs" 11 ); 12 13 let currentProfile; 14 add_setup(() => { 15 currentProfile = setupProfile(); 16 }); 17 18 /** 19 * Tests that if the backup-manifest.json provides an appName different from 20 * AppConstants.MOZ_APP_NAME of the currently running application, then 21 * recoverFromSnapshotFolder should throw an exception. 22 */ 23 add_task(async function test_different_appName() { 24 let testRecoveryPath = await IOUtils.createUniqueDirectory( 25 PathUtils.tempDir, 26 "testDifferentAppName" 27 ); 28 29 let meta = Object.assign({}, FAKE_METADATA); 30 meta.appName = "Some other application"; 31 Assert.notEqual( 32 meta.appName, 33 AppConstants.MOZ_APP_NAME, 34 "Set up a different appName in the manifest correctly." 35 ); 36 37 let manifest = { 38 version: ArchiveUtils.SCHEMA_VERSION, 39 meta, 40 resources: {}, 41 }; 42 let schema = await BackupService.MANIFEST_SCHEMA; 43 let validationResult = JsonSchema.validate(manifest, schema); 44 Assert.ok(validationResult.valid, "Schema matches manifest"); 45 46 await IOUtils.writeJSON( 47 PathUtils.join(testRecoveryPath, BackupService.MANIFEST_FILE_NAME), 48 manifest 49 ); 50 51 let bs = new BackupService(); 52 // This should reject and mention the invalid appName from the manifest. 53 await Assert.rejects( 54 bs.recoverFromSnapshotFolder(testRecoveryPath), 55 new RegExp(`${meta.appName}`) 56 ); 57 58 await IOUtils.remove(testRecoveryPath, { recursive: true }); 59 }); 60 61 /** 62 * Tests that if the backup-manifest.json provides an appVersion greater than 63 * AppConstants.MOZ_APP_VERSION of the currently running application, then 64 * recoverFromSnapshotFolder should throw an exception. 65 */ 66 add_task(async function test_newer_appVersion() { 67 let testRecoveryPath = await IOUtils.createUniqueDirectory( 68 PathUtils.tempDir, 69 "testNewerAppVersion" 70 ); 71 72 let meta = Object.assign({}, FAKE_METADATA); 73 // Hopefully this static version number will do for now. 74 meta.appVersion = "999.0.0"; 75 Assert.equal( 76 Services.vc.compare(AppConstants.MOZ_APP_VERSION, meta.appVersion), 77 -1, 78 "The current application version is less than 999.0.0." 79 ); 80 81 let manifest = { 82 version: ArchiveUtils.SCHEMA_VERSION, 83 meta, 84 resources: {}, 85 }; 86 let schema = await BackupService.MANIFEST_SCHEMA; 87 let validationResult = JsonSchema.validate(manifest, schema); 88 Assert.ok(validationResult.valid, "Schema matches manifest"); 89 90 await IOUtils.writeJSON( 91 PathUtils.join(testRecoveryPath, BackupService.MANIFEST_FILE_NAME), 92 manifest 93 ); 94 95 let bs = new BackupService(); 96 // This should reject and mention the invalid appVersion from the manifest. 97 await Assert.rejects( 98 bs.recoverFromSnapshotFolder(testRecoveryPath), 99 new RegExp(`${meta.appVersion}`) 100 ); 101 102 await IOUtils.remove(testRecoveryPath, { recursive: true }); 103 }); 104 105 add_task(async function test_profile_naming() { 106 let testRecoveryPath = await IOUtils.createUniqueDirectory( 107 PathUtils.tempDir, 108 "testProfileNaming" 109 ); 110 111 let meta = Object.assign({}, FAKE_METADATA); 112 let manifest = { 113 version: ArchiveUtils.SCHEMA_VERSION, 114 meta, 115 resources: {}, 116 }; 117 let schema = await BackupService.MANIFEST_SCHEMA; 118 let validationResult = JsonSchema.validate(manifest, schema); 119 Assert.ok(validationResult.valid, "Schema matches manifest"); 120 121 await IOUtils.writeJSON( 122 PathUtils.join(testRecoveryPath, BackupService.MANIFEST_FILE_NAME), 123 manifest 124 ); 125 126 let currentName = `original-profile-that-was-backed-up`; 127 currentProfile.name = currentName; 128 129 let bs = new BackupService(); 130 131 await bs.recoverFromSnapshotFolder(testRecoveryPath, false); 132 133 Assert.equal( 134 currentProfile.name, 135 `old-${currentName}`, 136 "The old profile prefix was added" 137 ); 138 currentName = currentProfile.name; 139 140 // Let's make sure that we don't end up chaining the prefix 141 await bs.recoverFromSnapshotFolder(testRecoveryPath, false); 142 Assert.notEqual( 143 currentProfile.name, 144 `old-${currentName}`, 145 "The old profile prefix was not added again" 146 ); 147 Assert.equal( 148 currentProfile.name, 149 currentName, 150 "The name of the profile did not change" 151 ); 152 153 await IOUtils.remove(testRecoveryPath, { recursive: true }); 154 });