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