summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/test/test_form_submission.html
blob: ecccabcafdfc5cc13434ebd82bf27b08e003530f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<!DOCTYPE HTML>
<html>
<head>
  <title>Satchel Test for Form Submisstion</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>  
  <script type="text/javascript" src="satchel_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<iframe id="iframe" src="https://example.com/tests/toolkit/components/satchel/test/subtst_form_submission_1.html"></iframe>
<div id="content" style="display: none">
  
  <!-- ===== Things that should not be saved. ===== -->

  <!-- autocomplete=off for input -->
  <form id="form1" onsubmit="return checkSubmit(1)">
    <input type="text" name="test1" autocomplete="off">
    <button type="submit">Submit</button>
  </form>

  <!-- autocomplete=off for form -->
  <form id="form2" onsubmit="return checkSubmit(2)" autocomplete="off">
    <input type="text" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- don't save type=hidden -->
  <form id="form3" onsubmit="return checkSubmit(3)">
    <input type="hidden" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- don't save type=checkbox -->
  <form id="form4" onsubmit="return checkSubmit(4)">
    <input type="checkbox" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- Don't save empty values. -->
  <form id="form5" onsubmit="return checkSubmit(5)">
    <input type="text" name="test1" value="originalValue">
    <button type="submit">Submit</button>
  </form>

  <!-- Don't save unchanged values. -->
  <form id="form6" onsubmit="return checkSubmit(6)">
    <input type="text" name="test1" value="dontSaveThis">
    <button type="submit">Submit</button>
  </form>

  <!-- Don't save unchanged values. (.value not touched) -->
  <form id="form7" onsubmit="return checkSubmit(7)">
    <input type="text" name="test1" value="dontSaveThis">
    <button type="submit">Submit</button>
  </form>

  <!-- No field name or ID. -->
  <form id="form8" onsubmit="return checkSubmit(8)">
    <input type="text">
    <button type="submit">Submit</button>
  </form>

  <!-- Nothing to save! -->
  <form id="form9" onsubmit="return checkSubmit(9)">
    <button type="submit">Submit</button>
  </form>

  <!-- input with name too long (300 chars.) -->
  <form id="form10" onsubmit="return checkSubmit(10)">
    <input type="text" name="12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890">
    <button type="submit">Submit</button>
  </form>

  <!-- input with value too long (300 chars.) -->
  <form id="form11" onsubmit="return checkSubmit(11)">
    <input type="text" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- input with value of one space (which should be trimmed) -->
  <form id="form12" onsubmit="return checkSubmit(12)">
    <input type="text" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- password field -->
  <form id="form13" onsubmit="return checkSubmit(13)">
    <input type="password" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- password field (type changed after pageload) -->
  <form id="form14" onsubmit="return checkSubmit(14)">
    <input type="text" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- input with sensitive data (16 digit credit card number) -->
  <form id="form15" onsubmit="return checkSubmit(15)">
    <script type="text/javascript">
      var form = document.getElementById('form15');
      for (let i = 0; i != 10; i++)
      {
        let input = document.createElement('input');
        input.type = 'text';
        input.name = 'test' + (i + 1);
        form.appendChild(input);
      }
    </script>
    <button type="submit">Submit</button>
  </form>

  <!-- input with sensitive data (15 digit credit card number) -->
  <form id="form16" onsubmit="return checkSubmit(16)">
    <script type="text/javascript">
      form = document.getElementById('form16');
      for (let i = 0; i != 10; i++)
      {
        let input = document.createElement('input');
        input.type = 'text';
        input.name = 'test' + (i + 1);
        form.appendChild(input);
      }
    </script>
    <button type="submit">Submit</button>
  </form>

  <!-- input with sensitive data (9 digit credit card number) -->
  <form id="form17" onsubmit="return checkSubmit(17)">
    <input type="text" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- input with sensitive data (16 digit hyphenated credit card number) -->
  <form id="form18" onsubmit="return checkSubmit(18)">
    <input type="text" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- input with sensitive data (15 digit whitespace-separated credit card number) -->
  <form id="form19" onsubmit="return checkSubmit(19)">
    <input type="text" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- form data submitted through HTTPS, when browser.formfill.saveHttpsForms is false -->
  <form id="form20" action="https://www.example.com/" onsubmit="return checkSubmit(20)">
    <input type="text" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- Form 21 is submitted into an iframe, not declared here. -->

  <!-- Don't save values if the form is invalid. -->
  <form id="form22" onsubmit="return checkSubmit(22);">
    <input type='email' name='test1' oninvalid="return checkSubmit(22);">
    <button type='submit'>Submit</button>
  </form>

  <!-- Don't save values if the form is invalid. -->
  <form id="form23" onsubmit="return checkSubmit(23);">
    <input type='email' value='foo' oninvalid="return checkSubmit(23);">
    <input type='text' name='test1'>
    <button type='submit'>Submit</button>
  </form>

  <!-- Don't save values if the input name is 'searchbar-history' -->
  <form id="form24" onsubmit="return checkSubmit(24);">
    <input type='text' name='searchbar-history'>
    <button type='submit'>Submit</button>
  </form>

  <!-- ===== Things that should be saved ===== -->

  <!-- Form 100 is submitted into an iframe, not declared here. -->

  <!-- input with no default value -->
  <form id="form101" onsubmit="return checkSubmit(101)">
    <input type="text" name="test1">
    <button type="submit">Submit</button>
  </form>

  <!-- input with a default value -->
  <form id="form102" onsubmit="return checkSubmit(102)">
    <input type="text" name="test2" value="originalValue">
    <button type="submit">Submit</button>
  </form>

  <!-- input uses id but not name -->
  <form id="form103" onsubmit="return checkSubmit(103)">
    <input type="text" id="test3">
    <button type="submit">Submit</button>
  </form>

  <!-- input with leading and trailing space -->
  <form id="form104" onsubmit="return checkSubmit(104)">
    <input type="text" name="test4">
    <button type="submit">Submit</button>
  </form>

  <!-- input with leading and trailing whitespace -->
  <form id="form105" onsubmit="return checkSubmit(105)">
    <input type="text" name="test5">
    <button type="submit">Submit</button>
  </form>

  <!-- input that looks like sensitive data but doesn't
       satisfy the requirements (incorrect length) -->
  <form id="form106" onsubmit="return checkSubmit(106)">
    <input type="text" name="test6">
    <button type="submit">Submit</button>
  </form>

  <!-- input that looks like sensitive data but doesn't
       satisfy the requirements (Luhn check fails for 16 chars) -->
  <form id="form107" onsubmit="return checkSubmit(107)">
    <script type="text/javascript">
      form = document.getElementById('form107');
      for (let i = 0; i != 10; i++)
      {
        let input = document.createElement('input');
        input.type = 'text';
        input.name = 'test7_' + (i + 1);
        form.appendChild(input);
      }
    </script>
    <button type="submit">Submit</button>
  </form>

  <!-- input that looks like sensitive data but doesn't
       satisfy the requirements (Luhn check fails for 15 chars) -->
  <form id="form108" onsubmit="return checkSubmit(108)">
    <script type="text/javascript">
      form = document.getElementById('form108');
      for (let i = 0; i != 10; i++)
      {
        let input = document.createElement('input');
        input.type = 'text';
        input.name = 'test8_' + (i + 1);
        form.appendChild(input);
      }
    </script>
    <button type="submit">Submit</button>
  </form>

  <!-- form data submitted through HTTPS, when browser.formfill.saveHttpsForms is true -->
  <form id="form109" action="https://www.example.com/" onsubmit="return checkSubmit(109)">
    <input type="text" name="test9">
    <button type="submit">Submit</button>
  </form>

  <!-- regular form data, when browser.formfill.saveHttpsForms is false -->
  <form id="form110" onsubmit="return checkSubmit(110)">
    <input type="text" name="test10">
    <button type="submit">Submit</button>
  </form>

</div>
<pre id="test">
<script class="testbody" type="text/javascript">

var numSubmittedForms = 0;

var ccNumbers = {
  valid15: [
    "930771457288760", "474915027480942",
    "924894781317325", "714816113937185",
    "790466087343106", "474320195408363",
    "219211148122351", "633038472250799",
    "354236732906484", "095347810189325",
  ],
  valid16: [
    "3091269135815020", "5471839082338112",
    "0580828863575793", "5015290610002932",
    "9465714503078607", "4302068493801686",
    "2721398408985465", "6160334316984331",
    "8643619970075142", "0218246069710785"
  ],
  invalid15: [
    "526931005800649", "724952425140686",
    "379761391174135", "030551436468583",
    "947377014076746", "254848023655752",
    "226871580283345", "708025346034339",
    "917585839076788", "918632588027666"
  ],
  invalid16: [
    "9946177098017064", "4081194386488872",
    "3095975979578034", "3662215692222536",
    "6723210018630429", "4411962856225025",
    "8276996369036686", "4449796938248871",
    "3350852696538147", "5011802870046957"
  ],
};

function checkInitialState() {
  countEntries(null, null,
    function (num) {
      ok(!num, "checking for initially empty storage");
      startTest();
    });
}

function startTest() {
  // Fill in values for the various fields. We could just set the <input>'s
  // value attribute, but we don't save default form values (and we want to
  // ensure unsaved values are because of autocomplete=off or whatever).
  $_(1, "test1").value = "dontSaveThis";
  $_(2, "test1").value = "dontSaveThis";
  $_(3, "test1").value = "dontSaveThis";
  $_(4, "test1").value = "dontSaveThis";
  $_(5, "test1").value = "";
  $_(6, "test1").value = "dontSaveThis";
  // Form 7 deliberately left untouched.
  // Form 8 has an input with no name or input attribute.
  let input = document.getElementById("form8").elements[0];
  is(input.type, "text", "checking we got unidentified input");
  input.value = "dontSaveThis";
  // Form 9 has nothing to modify.
  $_(10, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890").value = "dontSaveThis";
  $_(11, "test1").value = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
  $_(12, "test1").value = " ";
  $_(13, "test1").value = "dontSaveThis";
  $_(14, "test1").type  = "password";
  $_(14, "test1").value = "dontSaveThis";

  var testData = ccNumbers.valid16;
  for (let i = 0; i != testData.length; i++) {
    $_(15, "test" + (i + 1)).value = testData[i];
  }

  testData = ccNumbers.valid15;
  for (let i = 0; i != testData.length; i++) {
    $_(16, "test" + (i + 1)).value = testData[i];
  }
  $_(17, "test1").value = "001064088";
  $_(18, "test1").value = "0000-0000-0080-4609";
  $_(19, "test1").value = "0000 0000 0222 331";
  $_(20, "test1").value = "dontSaveThis";
  $_(22, "test1").value = "dontSaveThis";
  $_(23, "test1").value = "dontSaveThis";
  $_(24, "searchbar-history").value = "dontSaveThis";

  $_(101, "test1").value = "savedValue";
  $_(102, "test2").value = "savedValue";
  $_(103, "test3").value = "savedValue";
  $_(104, "test4").value = " trimTrailingAndLeadingSpace ";
  $_(105, "test5").value = "\t trimTrailingAndLeadingWhitespace\t ";
  $_(106, "test6").value = "00000000109181";

  testData = ccNumbers.invalid16;
  for (let i = 0; i != testData.length; i++) {
    $_(107, "test7_" + (i + 1)).value = testData[i];
  }

  testData = ccNumbers.invalid15;
  for (let i = 0; i != testData.length; i++) {
    $_(108, "test8_" + (i + 1)).value = testData[i];
  }

  $_(109, "test9").value = "savedValue";
  $_(110, "test10").value = "savedValue";

  // submit the first form.
  var button = getFormSubmitButton(1);
  button.click();
}


// Called by each form's onsubmit handler.
function checkSubmit(formNum) {
  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

  ok(true, "form " + formNum + " submitted");
  numSubmittedForms++;

  // Check for expected storage state.
  switch (formNum) {
    // Test 1-24 should not save anything.
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
      countEntries(null, null,
        function (num) {
          ok(!num, "checking for empty storage");
          submitForm(formNum);
        });
        return false;
    case 100:
        checkForSave("subtest2", "subtestValue", "checking saved subtest value");
        break;
    case 101:
        checkForSave("test1", "savedValue", "checking saved value");
        break;
    case 102:
        checkForSave("test2", "savedValue", "checking saved value");
        break;
    case 103:
        checkForSave("test3", "savedValue", "checking saved value");
        break;
    case 104:
        checkForSave("test4", "trimTrailingAndLeadingSpace", "checking saved value is trimmed on both sides");
        break;
    case 105:
        checkForSave("test5", "trimTrailingAndLeadingWhitespace", "checking saved value is trimmed on both sides");
        break;
    case 106:
        checkForSave("test6", "00000000109181", "checking saved value");
        break;
    case 107:
        for (let i = 0; i != ccNumbers.invalid16.length; i++) {
          checkForSave("test7_" + (i + 1), ccNumbers.invalid16[i], "checking saved value");
        }
        break;
    case 108:
        for (let i = 0; i != ccNumbers.invalid15.length; i++) {
          checkForSave("test8_" + (i + 1), ccNumbers.invalid15[i], "checking saved value");
        }
        break;
    case 109:
        checkForSave("test9", "savedValue", "checking saved value");
        break;
    case 110:
        checkForSave("test10", "savedValue", "checking saved value");
        break;
    default:
        ok(false, "Unexpected form submission");
        break;
  }

  return submitForm(formNum);
}

function submitForm(formNum)
{
  // Forms 13 and 14 would trigger a save-password notification. Temporarily
  // disable pwmgr, then reenable it.
  if (formNum == 12)
      SpecialPowers.setBoolPref("signon.rememberSignons", false);
  if (formNum == 14)
      SpecialPowers.clearUserPref("signon.rememberSignons");

  // Forms 20 and 21 requires browser.formfill.saveHttpsForms to be false
  if (formNum == 19)
    SpecialPowers.setBoolPref("browser.formfill.saveHttpsForms", false);
  // Reset preference now that 20 and 21 are over
  if (formNum == 21)
    SpecialPowers.clearUserPref("browser.formfill.saveHttpsForms");

  // End the test now on SeaMonkey.
  if (formNum == 21 && navigator.userAgent.match(/ SeaMonkey\//)) {
    checkObserver.uninit();
    is(numSubmittedForms, 21, "Ensuring all forms were submitted.");

    todo(false, "Skipping remaining checks on SeaMonkey ftb. (Bug 589471)");
    // finish(), yet let the test actually end first, to be safe.
    SimpleTest.executeSoon(SimpleTest.finish);

    return false; // return false to cancel current form submission
  }

  // Form 109 requires browser.formfill.save_https_forms to be true;
  // Form 110 requires it to be false.
  if (formNum == 108)
    SpecialPowers.setBoolPref("browser.formfill.saveHttpsForms", true);
  if (formNum == 109)
    SpecialPowers.setBoolPref("browser.formfill.saveHttpsForms", false);
  if (formNum == 110)
    SpecialPowers.clearUserPref("browser.formfill.saveHttpsForms");

  // End the test at the last form.
  if (formNum == 110) {
    is(numSubmittedForms, 35, "Ensuring all forms were submitted.");
    checkObserver.uninit();
    SimpleTest.finish();
    return false; // return false to cancel current form submission
  }

  // This timeout is here so that button.click() is never called before this
  // function returns. If button.click() is called before returning, a long
  // chain of submits will happen recursively since the submit is dispatched
  // immediately.
  //
  // This in itself is fine, but if there are errors in the code, mochitests
  // will in some cases give you "server too busy", which is hard to debug!
  //
  setTimeout(function() {
    checkObserver.waitForChecks(function() {
      var nextFormNum = formNum == 24 ? 100 : (formNum + 1);

      // Submit the next form. Special cases are Forms 21 and 100, which happen
      // from an HTTPS domain in an iframe.
      if (nextFormNum == 21 || nextFormNum == 100) {
        ok(true, "submitting iframe test " + nextFormNum);
        document.getElementById("iframe").contentWindow.clickButton(nextFormNum);
      }
      else {
        var button = getFormSubmitButton(nextFormNum);
        button.click();
      }
    });
  }, 0);

  return false; // cancel current form submission
}

checkObserver.init();

window.onload = checkInitialState;

SimpleTest.waitForExplicitFinish();

</script>
</pre>
</body>
</html>