test_Chrome_formdata.js (3561B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { FormHistory } = ChromeUtils.importESModule( 7 "resource://gre/modules/FormHistory.sys.mjs" 8 ); 9 10 let rootDir = do_get_file("chromefiles/", true); 11 12 add_setup(async function setup_fakePaths() { 13 let pathId; 14 if (AppConstants.platform == "macosx") { 15 pathId = "ULibDir"; 16 } else if (AppConstants.platform == "win") { 17 pathId = "LocalAppData"; 18 } else { 19 pathId = "Home"; 20 } 21 registerFakePath(pathId, rootDir); 22 }); 23 24 /** 25 * This function creates a testing database in the default profile, 26 * populates it with 10 example data entries,migrates the database, 27 * and then searches for each entry to ensure it exists in the FormHistory. 28 * 29 * @async 30 * @param {string} migratorKey 31 * A string that identifies the type of migrator object to be retrieved. 32 * @param {Array<string>} subDirs 33 * An array of strings that specifies the subdirectories for the target profile directory. 34 * @returns {Promise<undefined>} 35 * A Promise that resolves when the migration is completed. 36 */ 37 async function testFormdata(migratorKey, subDirs) { 38 if (AppConstants.platform == "macosx") { 39 subDirs.unshift("Application Support"); 40 } else if (AppConstants.platform == "win") { 41 subDirs.push("User Data"); 42 } else { 43 subDirs.unshift(".config"); 44 } 45 46 let target = rootDir.clone(); 47 // Pretend this is the default profile 48 subDirs.push("Default"); 49 while (subDirs.length) { 50 target.append(subDirs.shift()); 51 } 52 53 await IOUtils.makeDirectory(target.path, { 54 createAncestor: true, 55 ignoreExisting: true, 56 }); 57 58 target.append("Web Data"); 59 await IOUtils.remove(target.path, { ignoreAbsent: true }); 60 61 // Clear any search history results 62 await FormHistory.update({ op: "remove" }); 63 64 let dbConn = await Sqlite.openConnection({ path: target.path }); 65 66 await dbConn.execute( 67 `CREATE TABLE "autofill" (name VARCHAR, value VARCHAR, value_lower VARCHAR, date_created INTEGER DEFAULT 0, date_last_used INTEGER DEFAULT 0, count INTEGER DEFAULT 1, PRIMARY KEY (name, value))` 68 ); 69 for (let i = 0; i < 10; i++) { 70 await dbConn.execute( 71 `INSERT INTO autofill VALUES (:name, :value, :value_lower, :date_created, :date_last_used, :count)`, 72 { 73 name: `name${i}`, 74 value: `example${i}`, 75 value_lower: `example${i}`, 76 date_created: Math.round(Date.now() / 1000) - i * 10000, 77 date_last_used: Date.now(), 78 count: i, 79 } 80 ); 81 } 82 await dbConn.close(); 83 84 let migrator = await MigrationUtils.getMigrator(migratorKey); 85 // Sanity check for the source. 86 Assert.ok(await migrator.isSourceAvailable()); 87 88 await promiseMigration(migrator, MigrationUtils.resourceTypes.FORMDATA, { 89 id: "Default", 90 name: "Person 1", 91 }); 92 93 for (let i = 0; i < 10; i++) { 94 let results = await FormHistory.search(["fieldname", "value"], { 95 fieldname: `name${i}`, 96 value: `example${i}`, 97 }); 98 Assert.ok(results.length, `Should have item${i} in FormHistory`); 99 } 100 } 101 102 add_task(async function test_Chrome() { 103 let subDirs = 104 AppConstants.platform == "linux" ? ["google-chrome"] : ["Google", "Chrome"]; 105 await testFormdata("chrome", subDirs); 106 }); 107 108 add_task(async function test_ChromiumEdge() { 109 if (AppConstants.platform == "linux") { 110 // Edge isn't available on Linux. 111 return; 112 } 113 let subDirs = 114 AppConstants.platform == "macosx" 115 ? ["Microsoft Edge"] 116 : ["Microsoft", "Edge"]; 117 await testFormdata("chromium-edge", subDirs); 118 });