test_BackupService_onedrive.js (4449B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 // This is mostly taken from test_windows_onedrive_registry.js. 7 // Each "test suite" is a set of tests run on a specific registry 8 // configuration. 9 // NB: Backup only needs the location of the personal OneDrive folder. It 10 // does an existence check, so that folder must exist. We use PathUtils.tempDir 11 // for that. 12 const tempDirPath = PathUtils.tempDir; 13 14 const testSuites = [ 15 { 16 registryMap: { 17 "HKEY_CURRENT_USER\\Software\\Microsoft\\OneDrive\\Accounts\\Personal\\UserFolder": 18 tempDirPath, 19 }, 20 personalFolder: tempDirPath, 21 businessFolders: [], 22 }, 23 { 24 registryMap: { 25 "HKEY_CURRENT_USER\\Software\\Microsoft\\OneDrive\\Accounts\\Business1\\UserFolder": 26 "Q:\\Me\\OneDrive - MyOrg", 27 }, 28 personalFolder: null, 29 businessFolders: ["Q:\\Me\\OneDrive - MyOrg"], 30 }, 31 { 32 registryMap: { 33 "HKEY_CURRENT_USER\\Software\\Microsoft\\OneDrive\\Accounts\\Personal\\UserFolder": 34 tempDirPath, 35 "HKEY_CURRENT_USER\\Software\\Microsoft\\OneDrive\\Accounts\\Business5\\UserFolder": 36 "Q:\\Me\\OneDrive - Org1", 37 "HKEY_CURRENT_USER\\Software\\Microsoft\\OneDrive\\Accounts\\Business6\\UserFolder": 38 "Q:\\Me\\OneDrive - Org2", 39 "HKEY_CURRENT_USER\\Software\\Microsoft\\OneDrive\\Accounts\\Business10\\UserFolder": 40 "Q:\\Me\\OneDrive - Org2(2)", 41 }, 42 personalFolder: tempDirPath, 43 businessFolders: [ 44 "Q:\\Me\\OneDrive - Org1", 45 "Q:\\Me\\OneDrive - Org2", 46 "Q:\\Me\\OneDrive - Org2(2)", 47 ], 48 }, 49 ]; 50 51 // value of registryMap from currently-running test suite 52 let currentRegistryContents; 53 // The registry won't be opened for more than one key at a time. 54 let currentRegistryPath; 55 // Un-mock the registry. We need to do this before test end (i.e. 56 // registerCleanupFunction) because cleanup involves the registry. 57 let do_cleanup; 58 59 let mockRegistry = { 60 open: (root, path, mode) => { 61 Assert.equal( 62 root, 63 Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, 64 "reg key is in HKEY_CURRENT_USER" 65 ); 66 let isPersonal = path.match( 67 /Software\\Microsoft\\OneDrive\\Accounts\\Personal/ 68 ); 69 let isBusiness = path.match( 70 /Software\\Microsoft\\OneDrive\\Accounts\\Business(\d+)/ 71 ); 72 Assert.ok(isPersonal || isBusiness, "opening correct root path"); 73 Assert.equal(mode, Ci.nsIWindowsRegKey.ACCESS_READ, "mode was ACCESS_READ"); 74 currentRegistryPath = "HKEY_CURRENT_USER\\" + path; 75 }, 76 hasValue: value => { 77 Assert.equal(value, "UserFolder", "value is UserFolder"); 78 return currentRegistryPath + "\\" + value in currentRegistryContents; 79 }, 80 readStringValue: value => { 81 if (!(currentRegistryPath + "\\" + value in currentRegistryContents)) { 82 // This should never happen. 83 Assert.ok( 84 false, 85 `${currentRegistryPath + "\\" + value} not found in registry` 86 ); 87 throw new Error("read nonexistent value"); 88 } 89 return currentRegistryContents[currentRegistryPath + "\\" + value]; 90 }, 91 92 setRegistryContents: newRegistryMap => { 93 info(`setting new registry map: ${JSON.stringify(newRegistryMap)}`); 94 currentRegistryContents = newRegistryMap; 95 }, 96 97 QueryInterface: ChromeUtils.generateQI(["nsIWindowsRegKey"]), 98 }; 99 100 function setupMockRegistryComponent() { 101 const { MockRegistrar } = ChromeUtils.importESModule( 102 "resource://testing-common/MockRegistrar.sys.mjs" 103 ); 104 let cid = MockRegistrar.registerEx( 105 "@mozilla.org/windows-registry-key;1", 106 { shouldCreateInstance: false }, 107 mockRegistry 108 ); 109 do_cleanup = () => { 110 MockRegistrar.unregister(cid); 111 }; 112 } 113 114 add_task(async function runTests() { 115 setupMockRegistryComponent(); 116 117 const docsFolder = Services.dirsvc.get("Docs", Ci.nsIFile).path; 118 for (let test of testSuites) { 119 mockRegistry.setRegistryContents(test.registryMap); 120 let personalFolder = BackupService.oneDriveFolderPath; 121 Assert.equal( 122 personalFolder?.path || null, 123 test.personalFolder, 124 "got correct personal OneDrive root" 125 ); 126 127 Assert.equal( 128 BackupService.DEFAULT_PARENT_DIR_PATH, 129 test.personalFolder ? test.personalFolder : docsFolder, 130 "BackupService.DEFAULT_PARENT_DIR_PATH reflects correct folder" 131 ); 132 } 133 134 do_cleanup(); 135 });