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