test_getRecords.js (7454B)
1 /* 2 * Test for make sure getRecords can retrieve right collection from storage. 3 */ 4 5 "use strict"; 6 7 const { CreditCard } = ChromeUtils.importESModule( 8 "resource://gre/modules/CreditCard.sys.mjs" 9 ); 10 11 let FormAutofillParent, FormAutofillStatus; 12 let OSKeyStore; 13 add_setup(async () => { 14 ({ FormAutofillParent, FormAutofillStatus } = ChromeUtils.importESModule( 15 "resource://autofill/FormAutofillParent.sys.mjs" 16 )); 17 ({ OSKeyStore } = ChromeUtils.importESModule( 18 "resource://gre/modules/OSKeyStore.sys.mjs" 19 )); 20 }); 21 22 const TEST_ADDRESS_1 = { 23 "given-name": "Timothy", 24 "additional-name": "John", 25 "family-name": "Berners-Lee", 26 organization: "World Wide Web Consortium", 27 "street-address": "32 Vassar Street\nMIT Room 32-G524", 28 "address-level2": "Cambridge", 29 "address-level1": "MA", 30 "postal-code": "02139", 31 country: "US", 32 tel: "+16172535702", 33 email: "timbl@w3.org", 34 }; 35 36 const TEST_ADDRESS_2 = { 37 "street-address": "Some Address", 38 country: "US", 39 }; 40 41 let TEST_CREDIT_CARD_1 = { 42 "cc-name": "John Doe", 43 "cc-number": "4111111111111111", 44 "cc-exp-month": 4, 45 "cc-exp-year": 2017, 46 "cc-type": "visa", 47 }; 48 49 let TEST_CREDIT_CARD_2 = { 50 "cc-name": "John Dai", 51 "cc-number": "4929001587121045", 52 "cc-exp-month": 2, 53 "cc-exp-year": 2017, 54 "cc-type": "visa", 55 }; 56 57 add_task(async function test_getRecords() { 58 FormAutofillStatus.init(); 59 60 await FormAutofillStatus.formAutofillStorage.initialize(); 61 let fakeResult = { 62 addresses: [ 63 { 64 "given-name": "Timothy", 65 "additional-name": "John", 66 "family-name": "Berners-Lee", 67 organization: "World Wide Web Consortium", 68 }, 69 ], 70 creditCards: [ 71 { 72 "cc-name": "John Doe", 73 "cc-number": "4111111111111111", 74 "cc-exp-month": 4, 75 "cc-exp-year": 2017, 76 }, 77 ], 78 }; 79 80 for (let collectionName of ["addresses", "creditCards", "nonExisting"]) { 81 let collection = FormAutofillStatus.formAutofillStorage[collectionName]; 82 let expectedResult = fakeResult[collectionName] || []; 83 84 if (collection) { 85 sinon.stub(collection, "getAll"); 86 collection.getAll.returns(Promise.resolve(expectedResult)); 87 } 88 const fap = new FormAutofillParent(); 89 sinon.stub(fap, "browsingContext").get(() => { 90 return {}; 91 }); 92 93 await fap.getRecords({ collectionName }); 94 if (collection) { 95 Assert.equal(collection.getAll.called, true); 96 collection.getAll.restore(); 97 } 98 } 99 }); 100 101 add_task(async function test_getRecords_addresses() { 102 await FormAutofillStatus.formAutofillStorage.initialize(); 103 let mockAddresses = [TEST_ADDRESS_1, TEST_ADDRESS_2]; 104 let collection = FormAutofillStatus.formAutofillStorage.addresses; 105 sinon.stub(collection, "getAll"); 106 collection.getAll.returns(Promise.resolve(mockAddresses)); 107 108 let testCases = [ 109 { 110 description: "If the search string could match 1 address", 111 filter: { 112 collectionName: "addresses", 113 fieldName: "street-address", 114 searchString: "Some", 115 }, 116 expectedResult: [TEST_ADDRESS_2], 117 }, 118 { 119 description: "If the search string could match multiple addresses", 120 filter: { 121 collectionName: "addresses", 122 fieldName: "country", 123 searchString: "u", 124 }, 125 expectedResult: [TEST_ADDRESS_1, TEST_ADDRESS_2], 126 }, 127 { 128 description: "If the search string could not match any address", 129 filter: { 130 collectionName: "addresses", 131 fieldName: "street-address", 132 searchString: "test", 133 }, 134 expectedResult: [], 135 }, 136 { 137 description: "If the search string is empty", 138 filter: { 139 collectionName: "addresses", 140 fieldName: "street-address", 141 searchString: "", 142 }, 143 expectedResult: [TEST_ADDRESS_1, TEST_ADDRESS_2], 144 }, 145 { 146 description: 147 "Check if the filtering logic is free from searching special chars", 148 filter: { 149 collectionName: "addresses", 150 fieldName: "street-address", 151 searchString: ".*", 152 }, 153 expectedResult: [], 154 }, 155 { 156 description: 157 "Prevent broken while searching the property that does not exist", 158 filter: { 159 collectionName: "addresses", 160 fieldName: "tel", 161 searchString: "1", 162 }, 163 expectedResult: [], 164 }, 165 ]; 166 167 for (let testCase of testCases) { 168 info("Starting testcase: " + testCase.description); 169 const fap = new FormAutofillParent(); 170 sinon.stub(fap, "browsingContext").get(() => { 171 return {}; 172 }); 173 let result = await fap.getRecords(testCase.filter); 174 Assert.deepEqual(result, testCase.expectedResult); 175 } 176 }); 177 178 add_task(async function test_getRecords_creditCards() { 179 await FormAutofillStatus.formAutofillStorage.initialize(); 180 let collection = FormAutofillStatus.formAutofillStorage.creditCards; 181 let encryptedCCRecords = await Promise.all( 182 [TEST_CREDIT_CARD_1, TEST_CREDIT_CARD_2].map(async record => { 183 let clonedRecord = Object.assign({}, record); 184 clonedRecord["cc-number"] = CreditCard.getLongMaskedNumber( 185 record["cc-number"] 186 ); 187 clonedRecord["cc-number-encrypted"] = await OSKeyStore.encrypt( 188 record["cc-number"] 189 ); 190 return clonedRecord; 191 }) 192 ); 193 sinon 194 .stub(collection, "getAll") 195 .callsFake(() => 196 Promise.resolve([ 197 Object.assign({}, encryptedCCRecords[0]), 198 Object.assign({}, encryptedCCRecords[1]), 199 ]) 200 ); 201 202 let testCases = [ 203 { 204 description: "If the search string could match multiple creditCards", 205 filter: { 206 collectionName: "creditCards", 207 fieldName: "cc-name", 208 searchString: "John", 209 }, 210 expectedResult: encryptedCCRecords, 211 }, 212 { 213 description: "If the search string could not match any creditCard", 214 filter: { 215 collectionName: "creditCards", 216 fieldName: "cc-name", 217 searchString: "T", 218 }, 219 expectedResult: [], 220 }, 221 { 222 description: 223 "Return all creditCards if focused field is cc number; " + 224 "if the search string could match multiple creditCards", 225 filter: { 226 collectionName: "creditCards", 227 fieldName: "cc-number", 228 searchString: "4", 229 }, 230 expectedResult: encryptedCCRecords, 231 }, 232 { 233 description: "If the search string could match 1 creditCard", 234 filter: { 235 collectionName: "creditCards", 236 fieldName: "cc-name", 237 searchString: "John Doe", 238 }, 239 mpEnabled: true, 240 expectedResult: encryptedCCRecords.slice(0, 1), 241 }, 242 { 243 description: "Return all creditCards if focused field is cc number", 244 filter: { 245 collectionName: "creditCards", 246 fieldName: "cc-number", 247 searchString: "411", 248 }, 249 mpEnabled: true, 250 expectedResult: encryptedCCRecords, 251 }, 252 ]; 253 254 for (let testCase of testCases) { 255 info("Starting testcase: " + testCase.description); 256 if (testCase.mpEnabled) { 257 let tokendb = Cc["@mozilla.org/security/pk11tokendb;1"].createInstance( 258 Ci.nsIPK11TokenDB 259 ); 260 let token = tokendb.getInternalKeyToken(); 261 token.reset(); 262 token.initPassword("password"); 263 } 264 const fap = new FormAutofillParent(); 265 sinon.stub(fap, "browsingContext").get(() => { 266 return {}; 267 }); 268 let result = await fap.getRecords(testCase.filter); 269 Assert.deepEqual(result, testCase.expectedResult); 270 } 271 });