Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import logging | 6 import logging |
| 7 import os | 7 import os |
| 8 import pickle | 8 import pickle |
| 9 import re | |
| 9 | 10 |
| 10 import autofill_dataset_converter | 11 import autofill_dataset_converter |
| 11 import autofill_dataset_generator | 12 import autofill_dataset_generator |
| 12 import pyauto_functional # Must be imported before pyauto | 13 import pyauto_functional # Must be imported before pyauto |
| 13 import pyauto | 14 import pyauto |
| 14 | 15 |
| 15 | 16 |
| 16 class AutofillTest(pyauto.PyUITest): | 17 class AutofillTest(pyauto.PyUITest): |
| 17 """Tests that autofill works correctly""" | 18 """Tests that autofill works correctly""" |
| 18 | 19 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 'ADDRESS_HOME_ZIP': 'my_zip', | 84 'ADDRESS_HOME_ZIP': 'my_zip', |
| 84 'ADDRESS_HOME_COUNTRY': 'United States'} | 85 'ADDRESS_HOME_COUNTRY': 'United States'} |
| 85 # Add some invalid fields. | 86 # Add some invalid fields. |
| 86 with_invalid = without_invalid.copy() | 87 with_invalid = without_invalid.copy() |
| 87 with_invalid['PHONE_HOME_WHOLE_NUMBER'] = 'Invalid_Phone_Number' | 88 with_invalid['PHONE_HOME_WHOLE_NUMBER'] = 'Invalid_Phone_Number' |
| 88 with_invalid['PHONE_FAX_WHOLE_NUMBER'] = 'Invalid_Fax_Number' | 89 with_invalid['PHONE_FAX_WHOLE_NUMBER'] = 'Invalid_Fax_Number' |
| 89 self.FillAutofillProfile(profiles=[with_invalid]) | 90 self.FillAutofillProfile(profiles=[with_invalid]) |
| 90 self.assertEqual([without_invalid], | 91 self.assertEqual([without_invalid], |
| 91 self.GetAutofillProfile()['profiles']) | 92 self.GetAutofillProfile()['profiles']) |
| 92 | 93 |
| 93 def testFilterIncompleteAddresses(self): | 94 def testAutofillPrefsStringSavedAsIs(self): |
| 94 """Test Autofill filters out profile with incomplete address info.""" | 95 """Test invalid credit card numbers typed in prefs should be saved as-is.""" |
| 96 credit_card = {'CREDIT_CARD_NUMBER': 'Not_0123-5Checked'} | |
| 97 self.FillAutofillProfile(credit_cards=[credit_card]) | |
| 98 self.assertEqual([credit_card], | |
| 99 self.GetAutofillProfile()['credit_cards'], | |
| 100 msg='Credit card number in prefs not saved as-is.') | |
| 101 | |
| 102 def _LuhnCreditCardNumberValidator(self, number): | |
| 103 """Validates whether a number is valid or invalid using the Luhn test. | |
| 104 | |
| 105 Validation example: | |
| 106 1. Example number: 49927398716 | |
| 107 2. Reverse the digits: 61789372994 | |
| 108 3. Sum the digits in the odd-numbered position for s1: | |
| 109 6 + 7 + 9 + 7 + 9 + 4 = 42 | |
| 110 4. Take the digits in the even-numbered position: 1, 8, 3, 2, 9 | |
| 111 4.1. Two times each digit in the even-numbered position: 2, 16, 6, 4, 18 | |
| 112 4.2. Sum the digits of each multiplication: 2, 7, 6, 4, 9 | |
| 113 (0 + 2 = 2, 1 + 6 = 7, 0 + 6 = 6, 0 + 4 = 4, 1 + 8 = 9) | |
|
dennis_jeffrey
2011/04/16 01:36:48
Aha - this clarifies so much. I think it would ha
| |
| 114 4.3. Sum together the digits for s2: 2 + 7 + 6 + 4 + 9 = 28 | |
| 115 5. Sum together s1 + s2 and if the sum ends in zero, the number passes the | |
| 116 Luhn test: 42 + 28 = 70 which is a valid credit card number. | |
| 117 | |
| 118 Args: | |
| 119 number: the credit card number being validated, as a string. | |
| 120 | |
| 121 Return: | |
| 122 boolean whether the credit card number is valid or not. | |
| 123 """ | |
| 124 # Filters out non-digit characters. | |
| 125 number = re.sub('[^0-9]', '', number) | |
| 126 reverse = [int(ch) for ch in str(number)][::-1] | |
| 127 # The divmod of the function splits a number into two digits, ready for | |
| 128 # summing. | |
| 129 return ((sum(reverse[0::2]) + sum(sum(divmod(d*2, 10)) | |
| 130 for d in reverse[1::2])) % 10 == 0) | |
| 131 | |
| 132 def testInvalidCreditCardNumberIsNotAggregated(self): | |
| 133 """Test credit card info with an invalid number is not aggregated. | |
| 134 | |
| 135 When filling out a form with an invalid credit card number (one that | |
| 136 does not pass the Luhn test) the credit card info should not be saved into | |
| 137 Autofill preferences. | |
| 138 """ | |
| 139 invalid_cc_info = {'CREDIT_CARD_NAME': 'Bob Smith', | |
| 140 'CREDIT_CARD_NUMBER': '4408 0412 3456 7890', | |
| 141 'CREDIT_CARD_EXP_MONTH': '12', | |
| 142 'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2014'} | |
| 143 | |
| 144 cc_number = invalid_cc_info['CREDIT_CARD_NUMBER'] | |
| 145 self.assertFalse(self._LuhnCreditCardNumberValidator(cc_number), | |
| 146 msg='This test requires an invalid credit card number.') | |
| 147 url = self.GetHttpURLForDataPath( | |
| 148 os.path.join('autofill', 'autofill_creditcard_form.html')) | |
| 149 self.NavigateToURL(url) | |
| 150 for key, value in invalid_cc_info.iteritems(): | |
| 151 script = ('document.getElementById("%s").value = "%s"; ' | |
| 152 'window.domAutomationController.send("done");') % (key, value) | |
| 153 self.ExecuteJavascript(script, 0, 0) | |
| 154 js_code = """ | |
| 155 document.getElementById("cc_submit").submit(); | |
| 156 window.addEventListener("unload", function() { | |
| 157 window.domAutomationController.send("done"); | |
| 158 }); | |
| 159 """ | |
| 160 self.ExecuteJavascript(js_code, 0, 0) | |
| 161 # Wait until the form is submitted and the page completes loading. | |
| 162 self.WaitUntil( | |
| 163 lambda: self.GetDOMValue('document.readyState'), | |
| 164 expect_retval='complete') | |
| 165 cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] | |
| 166 self.assertFalse( | |
| 167 cc_infobar, msg='Save credit card infobar offered to save CC info.') | |
| 168 | |
| 169 def testWhitespacesAndSeparatorCharsStrippedForValidCCNums(self): | |
| 170 """Test whitespaces and separator chars are stripped for valid CC numbers. | |
| 171 | |
| 172 The credit card numbers used in this test pass the Luhn test. | |
| 173 For reference: http://www.merriampark.com/anatomycc.htm | |
| 174 """ | |
| 175 credit_card_info = [{'CREDIT_CARD_NAME': 'Bob Smith', | |
| 176 'CREDIT_CARD_NUMBER': '4408 0412 3456 7893', | |
| 177 'CREDIT_CARD_EXP_MONTH': '12', | |
| 178 'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2014'}, | |
| 179 {'CREDIT_CARD_NAME': 'Jane Doe', | |
| 180 'CREDIT_CARD_NUMBER': '4417-1234-5678-9113', | |
| 181 'CREDIT_CARD_EXP_MONTH': '10', | |
| 182 'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2013'}] | |
| 183 | |
| 184 url = self.GetHttpURLForDataPath( | |
| 185 os.path.join('autofill', 'autofill_creditcard_form.html')) | |
| 186 for cc_info in credit_card_info: | |
| 187 cc_number = credit_card_info['CREDIT_CARD_NUMBER'] | |
| 188 self.assertTrue(self._LuhnCreditCardNumberValidator(cc_number), | |
| 189 msg='This test requires a valid credit card number.') | |
| 190 self.NavigateToURL(url) | |
| 191 for key, value in cc_info.iteritems(): | |
| 192 script = ('document.getElementById("%s").value = "%s"; ' | |
| 193 'window.domAutomationController.send("done");') % (key, value) | |
| 194 self.ExecuteJavascript(script, 0, 0) | |
| 195 js_code = """ | |
| 196 document.getElementById("cc_submit").submit(); | |
| 197 window.addEventListener("unload", function() { | |
| 198 window.domAutomationController.send("done"); | |
| 199 }); | |
| 200 """ | |
| 201 self.ExecuteJavascript(js_code, 0, 0) | |
| 202 # Wait until form is submitted and page completes loading. | |
| 203 self.WaitUntil( | |
| 204 lambda: self.GetDOMValue('document.readyState'), | |
| 205 expect_retval='complete') | |
| 206 self.PerformActionOnInfobar('accept', infobar_index=0) | |
| 207 | |
| 208 # Verify the filled-in credit card number against the aggregated number. | |
| 209 aggregated_cc_1 = ( | |
| 210 self.GetAutofillProfile()['credit_cards'][0]['CREDIT_CARD_NUMBER']) | |
| 211 aggregated_cc_2 = ( | |
| 212 self.GetAutofillProfile()['credit_cards'][1]['CREDIT_CARD_NUMBER']) | |
| 213 self.assertFalse((' ' in aggregated_cc_1 or ' ' in aggregated_cc_2 or | |
| 214 '-' in aggregated_cc_1 or '-' in aggregated_cc_2), | |
| 215 msg='Whitespaces or separator chars not stripped.') | |
| 216 | |
| 217 def testProfilesNotAggregatedWithNoAddress(self): | |
| 218 """Test Autofill does not aggregate profiles with no address info.""" | |
| 95 profile = {'NAME_FIRST': 'Bob', | 219 profile = {'NAME_FIRST': 'Bob', |
| 96 'NAME_LAST': 'Smith', | 220 'NAME_LAST': 'Smith', |
| 97 'EMAIL_ADDRESS': 'bsmith@example.com', | 221 'EMAIL_ADDRESS': 'bsmith@example.com', |
| 98 'COMPANY_NAME': 'Company X', | 222 'COMPANY_NAME': 'Company X', |
| 99 'PHONE_HOME_WHOLE_NUMBER': '650-123-4567',} | 223 'PHONE_HOME_WHOLE_NUMBER': '650-123-4567',} |
| 100 url = self.GetHttpURLForDataPath( | 224 url = self.GetHttpURLForDataPath( |
| 101 os.path.join('autofill', 'duplicate_profiles_test.html')) | 225 os.path.join('autofill', 'duplicate_profiles_test.html')) |
| 102 self.NavigateToURL(url) | 226 self.NavigateToURL(url) |
| 103 for key, value in profile.iteritems(): | 227 for key, value in profile.iteritems(): |
| 104 script = ('document.getElementById("%s").value = "%s"; ' | 228 script = ('document.getElementById("%s").value = "%s"; ' |
| 105 'window.domAutomationController.send("done");') % (key, value) | 229 'window.domAutomationController.send("done");') % (key, value) |
| 106 self.ExecuteJavascript(script, 0, 0) | 230 self.ExecuteJavascript(script, 0, 0) |
| 107 js_code = """ | 231 js_code = """ |
| 108 document.getElementById("merge_dup").submit(); | 232 document.getElementById("merge_dup").submit(); |
| 109 window.addEventListener("unload", function() { | 233 window.addEventListener("unload", function() { |
| 110 window.domAutomationController.send("done"); | 234 window.domAutomationController.send("done"); |
| 111 }); | 235 }); |
| 112 """ | 236 """ |
| 113 self.ExecuteJavascript(js_code, 0, 0) | 237 self.ExecuteJavascript(js_code, 0, 0) |
| 114 self.assertEqual([], self.GetAutofillProfile()['profiles']) | 238 self.assertFalse(self.GetAutofillProfile()['profiles'], |
| 239 msg='Profile with no address info was aggregated.') | |
| 115 | 240 |
| 116 def testFilterMalformedEmailAddresses(self): | 241 def testProfilesNotAggregatedWithInvalidEmail(self): |
| 117 """Test Autofill filters out malformed email address during form submit.""" | 242 """Test Autofill does not aggregate profiles with an invalid email.""" |
| 118 profile = {'NAME_FIRST': 'Bob', | 243 profile = {'NAME_FIRST': 'Bob', |
| 119 'NAME_LAST': 'Smith', | 244 'NAME_LAST': 'Smith', |
| 120 'EMAIL_ADDRESS': 'garbage', | 245 'EMAIL_ADDRESS': 'garbage', |
| 121 'ADDRESS_HOME_LINE1': '1234 H St.', | 246 'ADDRESS_HOME_LINE1': '1234 H St.', |
| 122 'ADDRESS_HOME_CITY': 'San Jose', | 247 'ADDRESS_HOME_CITY': 'San Jose', |
| 123 'ADDRESS_HOME_STATE': 'CA', | 248 'ADDRESS_HOME_STATE': 'CA', |
| 124 'ADDRESS_HOME_ZIP': '95110', | 249 'ADDRESS_HOME_ZIP': '95110', |
| 125 'COMPANY_NAME': 'Company X', | 250 'COMPANY_NAME': 'Company X', |
| 126 'PHONE_HOME_WHOLE_NUMBER': '408-123-4567',} | 251 'PHONE_HOME_WHOLE_NUMBER': '408-123-4567',} |
| 127 url = self.GetHttpURLForDataPath( | 252 url = self.GetHttpURLForDataPath( |
| 128 os.path.join('autofill', 'duplicate_profiles_test.html')) | 253 os.path.join('autofill', 'duplicate_profiles_test.html')) |
| 129 self.NavigateToURL(url) | 254 self.NavigateToURL(url) |
| 130 for key, value in profile.iteritems(): | 255 for key, value in profile.iteritems(): |
| 131 script = ('document.getElementById("%s").value = "%s"; ' | 256 script = ('document.getElementById("%s").value = "%s"; ' |
| 132 'window.domAutomationController.send("done");') % (key, value) | 257 'window.domAutomationController.send("done");') % (key, value) |
| 133 self.ExecuteJavascript(script, 0, 0) | 258 self.ExecuteJavascript(script, 0, 0) |
| 134 js_code = """ | 259 js_code = """ |
| 135 document.getElementById("merge_dup").submit(); | 260 document.getElementById("merge_dup").submit(); |
| 136 window.addEventListener("unload", function() { | 261 window.addEventListener("unload", function() { |
| 137 window.domAutomationController.send("done"); | 262 window.domAutomationController.send("done"); |
| 138 }); | 263 }); |
| 139 """ | 264 """ |
| 140 self.ExecuteJavascript(js_code, 0, 0) | 265 self.ExecuteJavascript(js_code, 0, 0) |
| 141 if 'EMAIL_ADDRESS' in self.GetAutofillProfile()['profiles'][0]: | 266 self.assertFalse(self.GetAutofillProfile()['profiles'], |
| 142 raise KeyError('TEST FAIL: Malformed email address is saved in profiles.') | 267 msg='Profile with invalid email was aggregated.') |
| 143 | 268 |
| 144 def _SendKeyEventsToPopulateForm(self, tab_index=0, windex=0): | 269 def _SendKeyEventsToPopulateForm(self, tab_index=0, windex=0): |
| 145 """Send key events to populate a web form with Autofill profile data. | 270 """Send key events to populate a web form with Autofill profile data. |
| 146 | 271 |
| 147 Args: | 272 Args: |
| 148 tab_index: The tab index, default is 0. | 273 tab_index: The tab index, default is 0. |
| 149 windex: The window index, default is 0. | 274 windex: The window index, default is 0. |
| 150 """ | 275 """ |
| 151 TAB_KEYPRESS = 0x09 # Tab keyboard key press. | 276 TAB_KEYPRESS = 0x09 # Tab keyboard key press. |
| 152 DOWN_KEYPRESS = 0x28 # Down arrow keyboard key press. | 277 DOWN_KEYPRESS = 0x28 # Down arrow keyboard key press. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 179 form_values = {} | 304 form_values = {} |
| 180 for key, value in profile_expected.iteritems(): | 305 for key, value in profile_expected.iteritems(): |
| 181 js_returning_field_value = ( | 306 js_returning_field_value = ( |
| 182 'var field_value = document.getElementById("%s").value;' | 307 'var field_value = document.getElementById("%s").value;' |
| 183 'window.domAutomationController.send(field_value);' | 308 'window.domAutomationController.send(field_value);' |
| 184 ) % key | 309 ) % key |
| 185 form_values[key] = self.ExecuteJavascript( | 310 form_values[key] = self.ExecuteJavascript( |
| 186 js_returning_field_value, 0, 0) | 311 js_returning_field_value, 0, 0) |
| 187 self.assertEqual( | 312 self.assertEqual( |
| 188 form_values[key], value, | 313 form_values[key], value, |
| 189 ('Original profile not equal to expected profile at key: "%s"\n' | 314 msg=('Original profile not equal to expected profile at key: "%s"\n' |
| 190 'Expected: "%s"\nReturned: "%s"' % (key, value, form_values[key]))) | 315 'Expected: "%s"\nReturned: "%s"' % ( |
| 316 key, value, form_values[key]))) | |
| 191 | 317 |
| 192 def testCCInfoNotStoredWhenAutocompleteOff(self): | 318 def testCCInfoNotStoredWhenAutocompleteOff(self): |
| 193 """Test CC info not offered to be saved when autocomplete=off for CC field. | 319 """Test CC info not offered to be saved when autocomplete=off for CC field. |
| 194 | 320 |
| 195 If the credit card number field has autocomplete turned off, then the credit | 321 If the credit card number field has autocomplete turned off, then the credit |
| 196 card infobar should not offer to save the credit card info. The credit card | 322 card infobar should not offer to save the credit card info. The credit card |
| 197 number must be a valid Luhn number. | 323 number must be a valid Luhn number. |
| 198 """ | 324 """ |
| 199 credit_card_info = {'CREDIT_CARD_NAME': 'Bob Smith', | 325 credit_card_info = {'CREDIT_CARD_NAME': 'Bob Smith', |
| 200 'CREDIT_CARD_NUMBER': '4408041234567893', | 326 'CREDIT_CARD_NUMBER': '4408041234567893', |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 213 window.addEventListener("unload", function() { | 339 window.addEventListener("unload", function() { |
| 214 window.domAutomationController.send("done"); | 340 window.domAutomationController.send("done"); |
| 215 }); | 341 }); |
| 216 """ | 342 """ |
| 217 self.ExecuteJavascript(js_code, 0, 0) | 343 self.ExecuteJavascript(js_code, 0, 0) |
| 218 # Wait until form is submitted and page completes loading. | 344 # Wait until form is submitted and page completes loading. |
| 219 self.WaitUntil( | 345 self.WaitUntil( |
| 220 lambda: self.GetDOMValue('document.readyState'), | 346 lambda: self.GetDOMValue('document.readyState'), |
| 221 expect_retval='complete') | 347 expect_retval='complete') |
| 222 cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] | 348 cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] |
| 223 self.assertEqual(0, len(cc_infobar), | 349 self.assertFalse(cc_infobar, |
| 224 'Save credit card infobar offered to save CC info.') | 350 msg='Save credit card infobar offered to save CC info.') |
| 225 | 351 |
| 226 def testNoAutofillForReadOnlyFields(self): | 352 def testNoAutofillForReadOnlyFields(self): |
| 227 """Test that Autofill does not fill in read-only fields.""" | 353 """Test that Autofill does not fill in read-only fields.""" |
| 228 profile = {'NAME_FIRST': 'Bob', | 354 profile = {'NAME_FIRST': 'Bob', |
| 229 'NAME_LAST': 'Smith', | 355 'NAME_LAST': 'Smith', |
| 230 'EMAIL_ADDRESS': 'bsmith@gmail.com', | 356 'EMAIL_ADDRESS': 'bsmith@gmail.com', |
| 231 'ADDRESS_HOME_LINE1': '1234 H St.', | 357 'ADDRESS_HOME_LINE1': '1234 H St.', |
| 232 'ADDRESS_HOME_CITY': 'San Jose', | 358 'ADDRESS_HOME_CITY': 'San Jose', |
| 233 'ADDRESS_HOME_STATE': 'CA', | 359 'ADDRESS_HOME_STATE': 'CA', |
| 234 'ADDRESS_HOME_ZIP': '95110', | 360 'ADDRESS_HOME_ZIP': '95110', |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 merged_profile = os.path.join(self.DataDir(), 'autofill', | 481 merged_profile = os.path.join(self.DataDir(), 'autofill', |
| 356 'merged-profiles.txt') | 482 'merged-profiles.txt') |
| 357 profile_dict = self.GetAutofillProfile()['profiles'] | 483 profile_dict = self.GetAutofillProfile()['profiles'] |
| 358 output = open(merged_profile, 'wb') | 484 output = open(merged_profile, 'wb') |
| 359 pickle.dump(profile_dict, output) | 485 pickle.dump(profile_dict, output) |
| 360 output.close() | 486 output.close() |
| 361 | 487 |
| 362 | 488 |
| 363 if __name__ == '__main__': | 489 if __name__ == '__main__': |
| 364 pyauto_functional.Main() | 490 pyauto_functional.Main() |
| OLD | NEW |