summaryrefslogtreecommitdiffstats
path: root/dom/security/test/gtest/TestCSPParser.cpp
blob: 7964bf43a3b80c651ccaa2c4380850eeba07daa2 (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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include <string.h>
#include <stdlib.h>

#ifndef MOZILLA_INTERNAL_API
// some of the includes make use of internal string types
#define nsAString_h___
#define nsString_h___
#define nsStringFwd_h___
#define nsReadableUtils_h___
class nsACString;
class nsAString;
class nsAFlatString;
class nsAFlatCString;
class nsAdoptingString;
class nsAdoptingCString;
class nsXPIDLString;
template<class T> class nsReadingIterator;
#endif

#include "nsIContentSecurityPolicy.h"
#include "nsNetUtil.h"
#include "nsIScriptSecurityManager.h"
#include "mozilla/dom/nsCSPContext.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"

#ifndef MOZILLA_INTERNAL_API
#undef nsString_h___
#undef nsAString_h___
#undef nsReadableUtils_h___
#endif

/*
 * Testing the parser is non trivial, especially since we can not call
 * parser functionality directly in compiled code tests.
 * All the tests (except the fuzzy tests at the end) follow the same schemata:
 *   a) create an nsIContentSecurityPolicy object
 *   b) set the selfURI in SetRequestContext
 *   c) append one or more policies by calling AppendPolicy
 *   d) check if the policy count is correct by calling GetPolicyCount
 *   e) compare the result of the policy with the expected output
 *      using the struct PolicyTest;
 *
 * In general we test:
 * a) policies that the parser should accept
 * b) policies that the parser should reject
 * c) policies that are randomly generated (fuzzy tests)
 *
 * Please note that fuzzy tests are *DISABLED* by default and shold only
 * be run *OFFLINE* whenever code in nsCSPParser changes.
 * To run fuzzy tests, flip RUN_OFFLINE_TESTS to 1.
 *
 */

#define RUN_OFFLINE_TESTS 0

/*
 * Offline tests are separated in three different groups:
 *  * TestFuzzyPolicies - complete random ASCII input
 *  * TestFuzzyPoliciesIncDir - a directory name followed by random ASCII
 *  * TestFuzzyPoliciesIncDirLimASCII - a directory name followed by limited ASCII
 *    which represents more likely user input.
 *
 *  We run each of this categories |kFuzzyRuns| times.
 */

#if RUN_OFFLINE_TESTS
static const uint32_t kFuzzyRuns = 10000;
#endif

// For fuzzy testing we actually do not care about the output,
// we just want to make sure that the parser can handle random
// input, therefore we use kFuzzyExpectedPolicyCount to return early.
static const uint32_t kFuzzyExpectedPolicyCount = 111;

static const uint32_t kMaxPolicyLength = 96;

struct PolicyTest
{
  char policy[kMaxPolicyLength];
  char expectedResult[kMaxPolicyLength];
};

nsresult runTest(uint32_t aExpectedPolicyCount, // this should be 0 for policies which should fail to parse
                 const char* aPolicy,
                 const char* aExpectedResult) {

  nsresult rv;
  nsCOMPtr<nsIScriptSecurityManager> secman =
    do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  // we init the csp with http://www.selfuri.com
  nsCOMPtr<nsIURI> selfURI;
  rv = NS_NewURI(getter_AddRefs(selfURI), "http://www.selfuri.com");
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIPrincipal> selfURIPrincipal;
  // Can't use BasePrincipal::CreateCodebasePrincipal here
  // because the symbol is not visible here
  rv = secman->GetCodebasePrincipal(selfURI, getter_AddRefs(selfURIPrincipal));
  NS_ENSURE_SUCCESS(rv, rv);

  // create a CSP object
  nsCOMPtr<nsIContentSecurityPolicy> csp =
    do_CreateInstance(NS_CSPCONTEXT_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  // for testing the parser we only need to set a principal which is needed
  // to translate the keyword 'self' into an actual URI.
  rv = csp->SetRequestContext(nullptr, selfURIPrincipal);
  NS_ENSURE_SUCCESS(rv, rv);

  // append a policy
  nsString policyStr;
  policyStr.AssignASCII(aPolicy);
  rv = csp->AppendPolicy(policyStr, false, false);
  NS_ENSURE_SUCCESS(rv, rv);

  // when executing fuzzy tests we do not care about the actual output
  // of the parser, we just want to make sure that the parser is not crashing.
  if (aExpectedPolicyCount == kFuzzyExpectedPolicyCount) {
    return NS_OK;
  }

  // verify that the expected number of policies exists
  uint32_t actualPolicyCount;
  rv = csp->GetPolicyCount(&actualPolicyCount);
  NS_ENSURE_SUCCESS(rv, rv);
  if (actualPolicyCount != aExpectedPolicyCount) {
    EXPECT_TRUE(false) <<
      "Actual policy count not equal to expected policy count (" <<
      actualPolicyCount << " != " << aExpectedPolicyCount <<
      ") for policy: " << aPolicy;
    return NS_ERROR_UNEXPECTED;
  }

  // if the expected policy count is 0, we can return, because
  // we can not compare any output anyway. Used when parsing
  // errornous policies.
  if (aExpectedPolicyCount == 0) {
    return NS_OK;
  }

  // compare the parsed policy against the expected result
  nsString parsedPolicyStr;
  // checking policy at index 0, which is the one what we appended.
  rv = csp->GetPolicyString(0, parsedPolicyStr);
  NS_ENSURE_SUCCESS(rv, rv);

  if (!NS_ConvertUTF16toUTF8(parsedPolicyStr).EqualsASCII(aExpectedResult)) {
    EXPECT_TRUE(false) <<
      "Actual policy does not match expected policy (" <<
      NS_ConvertUTF16toUTF8(parsedPolicyStr).get() << " != " <<
      aExpectedResult << ")";
    return NS_ERROR_UNEXPECTED;
  }

  return NS_OK;
}

// ============================= run Tests ========================

nsresult runTestSuite(const PolicyTest* aPolicies,
                      uint32_t aPolicyCount,
                      uint32_t aExpectedPolicyCount) {
  nsresult rv;
  nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
  bool experimentalEnabledCache = false;
  bool strictDynamicEnabledCache = false;
  if (prefs)
  {
    prefs->GetBoolPref("security.csp.experimentalEnabled", &experimentalEnabledCache);
    prefs->SetBoolPref("security.csp.experimentalEnabled", true);

    prefs->GetBoolPref("security.csp.enableStrictDynamic", &strictDynamicEnabledCache);
    prefs->SetBoolPref("security.csp.enableStrictDynamic", true);
  }

  for (uint32_t i = 0; i < aPolicyCount; i++) {
    rv = runTest(aExpectedPolicyCount, aPolicies[i].policy, aPolicies[i].expectedResult);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  if (prefs) {
    prefs->SetBoolPref("security.csp.experimentalEnabled", experimentalEnabledCache);
    prefs->SetBoolPref("security.csp.enableStrictDynamic", strictDynamicEnabledCache);
  }

  return NS_OK;
}

// ============================= TestDirectives ========================

TEST(CSPParser, Directives)
{
  static const PolicyTest policies[] =
  {
    { "connect-src xn--mnchen-3ya.de",
      "connect-src http://xn--mnchen-3ya.de"},
    { "default-src http://www.example.com",
      "default-src http://www.example.com" },
    { "script-src http://www.example.com",
      "script-src http://www.example.com" },
    { "object-src http://www.example.com",
      "object-src http://www.example.com" },
    { "style-src http://www.example.com",
      "style-src http://www.example.com" },
    { "img-src http://www.example.com",
      "img-src http://www.example.com" },
    { "media-src http://www.example.com",
      "media-src http://www.example.com" },
    { "frame-src http://www.example.com",
      "frame-src http://www.example.com" },
    { "font-src http://www.example.com",
      "font-src http://www.example.com" },
    { "connect-src http://www.example.com",
      "connect-src http://www.example.com" },
    { "report-uri http://www.example.com",
      "report-uri http://www.example.com/" },
    { "script-src 'nonce-correctscriptnonce'",
      "script-src 'nonce-correctscriptnonce'" },
    { "script-src 'sha256-siVR8vAcqP06h2ppeNwqgjr0yZ6yned4X2VF84j4GmI='",
      "script-src 'sha256-siVR8vAcqP06h2ppeNwqgjr0yZ6yned4X2VF84j4GmI='" },
    { "referrer no-referrer",
      "referrer no-referrer" },
    { "require-sri-for script style",
      "require-sri-for script style"},
    { "script-src 'nonce-foo' 'unsafe-inline' ",
      "script-src 'nonce-foo' 'unsafe-inline'" },
    { "script-src 'nonce-foo' 'strict-dynamic' 'unsafe-inline' https:  ",
      "script-src 'nonce-foo' 'strict-dynamic' 'unsafe-inline' https:" },
    { "default-src 'sha256-siVR8' 'strict-dynamic' 'unsafe-inline' https:  ",
      "default-src 'sha256-siVR8' 'unsafe-inline' https:" },
    { "worker-src https://example.com",
      "worker-src https://example.com" },
    { "worker-src http://worker.com; frame-src http://frame.com; child-src http://child.com",
      "worker-src http://worker.com; frame-src http://frame.com; child-src http://child.com" },
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 1)));
}

// ============================= TestKeywords ========================

TEST(CSPParser, Keywords)
{
  static const PolicyTest policies[] =
  {
    { "script-src 'self'",
      "script-src http://www.selfuri.com" },
    { "script-src 'unsafe-inline'",
      "script-src 'unsafe-inline'" },
    { "script-src 'unsafe-eval'",
      "script-src 'unsafe-eval'" },
    { "script-src 'unsafe-inline' 'unsafe-eval'",
      "script-src 'unsafe-inline' 'unsafe-eval'" },
    { "script-src 'none'",
      "script-src 'none'" },
    { "img-src 'none'; script-src 'unsafe-eval' 'unsafe-inline'; default-src 'self'",
      "img-src 'none'; script-src 'unsafe-eval' 'unsafe-inline'; default-src http://www.selfuri.com" },
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 1)));
}

// ============================= TestIgnoreUpperLowerCasePolicies ========================

TEST(CSPParser, IgnoreUpperLowerCasePolicies)
{
  static const PolicyTest policies[] =
  {
    { "script-src 'SELF'",
      "script-src http://www.selfuri.com" },
    { "sCriPt-src 'Unsafe-Inline'",
      "script-src 'unsafe-inline'" },
    { "SCRIPT-src 'unsafe-eval'",
      "script-src 'unsafe-eval'" },
    { "default-SRC 'unsafe-inline' 'unsafe-eval'",
      "default-src 'unsafe-inline' 'unsafe-eval'" },
    { "script-src 'NoNe'",
      "script-src 'none'" },
    { "img-sRc 'noNe'; scrIpt-src 'unsafe-EVAL' 'UNSAFE-inline'; deFAULT-src 'Self'",
      "img-src 'none'; script-src 'unsafe-eval' 'unsafe-inline'; default-src http://www.selfuri.com" },
    { "default-src HTTP://www.example.com",
      "default-src http://www.example.com" },
    { "default-src HTTP://WWW.EXAMPLE.COM",
      "default-src http://www.example.com" },
    { "default-src HTTPS://*.example.COM",
      "default-src https://*.example.com" },
    { "script-src 'none' test.com;",
      "script-src http://test.com" },
    { "script-src 'NoNCE-correctscriptnonce'",
      "script-src 'nonce-correctscriptnonce'" },
    { "script-src 'NoncE-NONCENEEDSTOBEUPPERCASE'",
      "script-src 'nonce-NONCENEEDSTOBEUPPERCASE'" },
    { "script-src 'SHA256-siVR8vAcqP06h2ppeNwqgjr0yZ6yned4X2VF84j4GmI='",
      "script-src 'sha256-siVR8vAcqP06h2ppeNwqgjr0yZ6yned4X2VF84j4GmI='" },
    { "refERRer No-refeRRer",
      "referrer no-referrer" },
    { "upgrade-INSECURE-requests",
      "upgrade-insecure-requests" },
    { "sanDBox alloW-foRMs",
      "sandbox allow-forms"},
    { "require-SRI-for sCript stYle",
      "require-sri-for script style"},
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 1)));
}

// ============================= TestPaths ========================

TEST(CSPParser, Paths)
{
  static const PolicyTest policies[] =
  {
    { "script-src http://www.example.com",
      "script-src http://www.example.com" },
    { "script-src http://www.example.com/",
      "script-src http://www.example.com/" },
    { "script-src http://www.example.com/path-1",
      "script-src http://www.example.com/path-1" },
    { "script-src http://www.example.com/path-1/",
      "script-src http://www.example.com/path-1/" },
    { "script-src http://www.example.com/path-1/path_2",
      "script-src http://www.example.com/path-1/path_2" },
    { "script-src http://www.example.com/path-1/path_2/",
      "script-src http://www.example.com/path-1/path_2/" },
    { "script-src http://www.example.com/path-1/path_2/file.js",
      "script-src http://www.example.com/path-1/path_2/file.js" },
    { "script-src http://www.example.com/path-1/path_2/file_1.js",
      "script-src http://www.example.com/path-1/path_2/file_1.js" },
    { "script-src http://www.example.com/path-1/path_2/file-2.js",
      "script-src http://www.example.com/path-1/path_2/file-2.js" },
    { "script-src http://www.example.com/path-1/path_2/f.js",
      "script-src http://www.example.com/path-1/path_2/f.js" },
    { "script-src http://www.example.com:88",
      "script-src http://www.example.com:88" },
    { "script-src http://www.example.com:88/",
      "script-src http://www.example.com:88/" },
    { "script-src http://www.example.com:88/path-1",
      "script-src http://www.example.com:88/path-1" },
    { "script-src http://www.example.com:88/path-1/",
      "script-src http://www.example.com:88/path-1/" },
    { "script-src http://www.example.com:88/path-1/path_2",
      "script-src http://www.example.com:88/path-1/path_2" },
    { "script-src http://www.example.com:88/path-1/path_2/",
      "script-src http://www.example.com:88/path-1/path_2/" },
    { "script-src http://www.example.com:88/path-1/path_2/file.js",
      "script-src http://www.example.com:88/path-1/path_2/file.js" },
    { "script-src http://www.example.com:*",
      "script-src http://www.example.com:*" },
    { "script-src http://www.example.com:*/",
      "script-src http://www.example.com:*/" },
    { "script-src http://www.example.com:*/path-1",
      "script-src http://www.example.com:*/path-1" },
    { "script-src http://www.example.com:*/path-1/",
      "script-src http://www.example.com:*/path-1/" },
    { "script-src http://www.example.com:*/path-1/path_2",
      "script-src http://www.example.com:*/path-1/path_2" },
    { "script-src http://www.example.com:*/path-1/path_2/",
      "script-src http://www.example.com:*/path-1/path_2/" },
    { "script-src http://www.example.com:*/path-1/path_2/file.js",
      "script-src http://www.example.com:*/path-1/path_2/file.js" },
    { "script-src http://www.example.com#foo",
      "script-src http://www.example.com" },
    { "script-src http://www.example.com?foo=bar",
      "script-src http://www.example.com" },
    { "script-src http://www.example.com:8888#foo",
      "script-src http://www.example.com:8888" },
    { "script-src http://www.example.com:8888?foo",
      "script-src http://www.example.com:8888" },
    { "script-src http://www.example.com/#foo",
      "script-src http://www.example.com/" },
    { "script-src http://www.example.com/?foo",
      "script-src http://www.example.com/" },
    { "script-src http://www.example.com/path-1/file.js#foo",
      "script-src http://www.example.com/path-1/file.js" },
    { "script-src http://www.example.com/path-1/file.js?foo",
      "script-src http://www.example.com/path-1/file.js" },
    { "script-src http://www.example.com/path-1/file.js?foo#bar",
      "script-src http://www.example.com/path-1/file.js" },
    { "report-uri http://www.example.com/",
      "report-uri http://www.example.com/" },
    { "report-uri http://www.example.com:8888/asdf",
      "report-uri http://www.example.com:8888/asdf" },
    { "report-uri http://www.example.com:8888/path_1/path_2",
      "report-uri http://www.example.com:8888/path_1/path_2" },
    { "report-uri http://www.example.com:8888/path_1/path_2/report.sjs&301",
      "report-uri http://www.example.com:8888/path_1/path_2/report.sjs&301" },
    { "report-uri /examplepath",
      "report-uri http://www.selfuri.com/examplepath" },
    { "connect-src http://www.example.com/foo%3Bsessionid=12%2C34",
      "connect-src http://www.example.com/foo;sessionid=12,34" },
    { "connect-src http://www.example.com/foo%3bsessionid=12%2c34",
      "connect-src http://www.example.com/foo;sessionid=12,34" },
    { "connect-src http://test.com/pathIncludingAz19-._~!$&'()*+=:@",
      "connect-src http://test.com/pathIncludingAz19-._~!$&'()*+=:@" },
    { "script-src http://www.example.com:88/.js",
      "script-src http://www.example.com:88/.js" },
    { "script-src https://foo.com/_abc/abc_/_/_a_b_c_",
      "script-src https://foo.com/_abc/abc_/_/_a_b_c_" }
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 1)));
}

// ============================= TestSimplePolicies ========================

TEST(CSPParser, SimplePolicies)
{
  static const PolicyTest policies[] =
  {
    { "default-src *",
      "default-src *" },
    { "default-src https:",
      "default-src https:" },
    { "default-src https://*",
      "default-src https://*" },
    { "default-src *:*",
      "default-src http://*:*" },
    { "default-src *:80",
      "default-src http://*:80" },
    { "default-src http://*:80",
      "default-src http://*:80" },
    { "default-src javascript:",
      "default-src javascript:" },
    { "default-src data:",
      "default-src data:" },
    { "script-src 'unsafe-eval' 'unsafe-inline' http://www.example.com",
      "script-src 'unsafe-eval' 'unsafe-inline' http://www.example.com" },
    { "object-src 'self'",
      "object-src http://www.selfuri.com" },
    { "style-src http://www.example.com 'self'",
      "style-src http://www.example.com http://www.selfuri.com" },
    { "media-src http://www.example.com http://www.test.com",
      "media-src http://www.example.com http://www.test.com" },
    { "connect-src http://www.test.com example.com *.other.com;",
      "connect-src http://www.test.com http://example.com http://*.other.com"},
    { "connect-src example.com *.other.com",
      "connect-src http://example.com http://*.other.com"},
    { "style-src *.other.com example.com",
      "style-src http://*.other.com http://example.com"},
    { "default-src 'self'; img-src *;",
      "default-src http://www.selfuri.com; img-src *" },
    { "object-src media1.example.com media2.example.com *.cdn.example.com;",
      "object-src http://media1.example.com http://media2.example.com http://*.cdn.example.com" },
    { "script-src trustedscripts.example.com",
      "script-src http://trustedscripts.example.com" },
    { "script-src 'self' ; default-src trustedscripts.example.com",
      "script-src http://www.selfuri.com; default-src http://trustedscripts.example.com" },
    { "default-src 'none'; report-uri http://localhost:49938/test",
      "default-src 'none'; report-uri http://localhost:49938/test" },
    { "default-src app://{app-host-is-uid}",
      "default-src app://{app-host-is-uid}" },
    { "   ;   default-src abc",
      "default-src http://abc" },
    { " ; ; ; ;     default-src            abc    ; ; ; ;",
      "default-src http://abc" },
    { "script-src 'none' 'none' 'none';",
      "script-src 'none'" },
    { "script-src http://www.example.com/path-1//",
      "script-src http://www.example.com/path-1//" },
    { "script-src http://www.example.com/path-1//path_2",
      "script-src http://www.example.com/path-1//path_2" },
    { "default-src 127.0.0.1",
      "default-src http://127.0.0.1" },
    { "default-src 127.0.0.1:*",
      "default-src http://127.0.0.1:*" },
    { "default-src -; ",
      "default-src http://-" },
    { "script-src 1",
      "script-src http://1" },
    { "upgrade-insecure-requests",
      "upgrade-insecure-requests" },
    { "upgrade-insecure-requests https:",
      "upgrade-insecure-requests" },
    { "sandbox allow-scripts allow-forms  ",
      "sandbox allow-scripts allow-forms" },
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 1)));
}

// ============================= TestPoliciesWithInvalidSrc ========================

TEST(CSPParser, PoliciesWithInvalidSrc)
{
  static const PolicyTest policies[] =
  {
    { "script-src 'self'; SCRIPT-SRC http://www.example.com",
      "script-src http://www.selfuri.com" },
    { "script-src 'none' test.com; script-src example.com",
      "script-src http://test.com" },
    { "default-src **",
      "default-src 'none'" },
    { "default-src 'self",
      "default-src 'none'" },
    { "default-src 'unsafe-inlin' ",
      "default-src 'none'" },
    { "default-src */",
      "default-src 'none'" },
    { "default-src",
      "default-src 'none'" },
    { "default-src 'unsafe-inlin' ",
      "default-src 'none'" },
    { "default-src :88",
      "default-src 'none'" },
    { "script-src abc::::::88",
      "script-src 'none'" },
    { "script-src *.*:*",
      "script-src 'none'" },
    { "img-src *::88",
      "img-src 'none'" },
    { "object-src http://localhost:",
      "object-src 'none'" },
    { "script-src test..com",
      "script-src 'none'" },
    { "script-src sub1.sub2.example+",
      "script-src 'none'" },
    { "script-src http://www.example.com//",
      "script-src 'none'" },
    { "script-src http://www.example.com:88path-1/",
      "script-src 'none'" },
    { "script-src http://www.example.com:88//",
      "script-src 'none'" },
    { "script-src http://www.example.com:88//path-1",
      "script-src 'none'" },
    { "script-src http://www.example.com:88//path-1",
      "script-src 'none'" },
    { "script-src http://www.example.com:88.js",
      "script-src 'none'" },
    { "script-src http://www.example.com:*.js",
      "script-src 'none'" },
    { "script-src http://www.example.com:*.",
      "script-src 'none'" },
    { "connect-src http://www.example.com/foo%zz;",
      "connect-src 'none'" },
    { "script-src https://foo.com/%$",
      "script-src 'none'" },
    { "require-SRI-for script elephants",
      "require-sri-for script"},
    { "sandbox    foo",
      "sandbox"},
  };

  // amount of tests - 1, because the latest should be ignored.
  uint32_t policyCount = (sizeof(policies) / sizeof(PolicyTest)) -1;
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 1)));
}

// ============================= TestBadPolicies ========================

TEST(CSPParser, BadPolicies)
{
  static const PolicyTest policies[] =
  {
    { "script-sr 'self", "" },
    { "", "" },
    { "; ; ; ; ; ; ;", "" },
    { "defaut-src asdf", "" },
    { "default-src: aaa", "" },
    { "asdf http://test.com", ""},
    { "referrer", ""},
    { "referrer foo", ""},
    { "require-sri-for", ""},
    { "require-sri-for foo", ""},
    { "report-uri", ""},
    { "report-uri http://:foo", ""},
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 0)));
}

// ============================= TestGoodGeneratedPolicies ========================

TEST(CSPParser, GoodGeneratedPolicies)
{
  static const PolicyTest policies[] =
  {
    { "default-src 'self'; img-src *",
      "default-src http://www.selfuri.com; img-src *" },
    { "report-uri /policy",
      "report-uri http://www.selfuri.com/policy"},
    { "img-src *",
      "img-src *" },
    { "media-src foo.bar",
      "media-src http://foo.bar" },
    { "frame-src *.bar",
      "frame-src http://*.bar" },
    { "font-src com",
      "font-src http://com" },
    { "connect-src f00b4r.com",
      "connect-src http://f00b4r.com" },
    { "default-src {app-url-is-uid}",
      "default-src http://{app-url-is-uid}" },
    { "script-src *.a.b.c",
      "script-src http://*.a.b.c" },
    { "object-src *.b.c",
      "object-src http://*.b.c" },
    { "style-src a.b.c",
      "style-src http://a.b.c" },
    { "img-src a.com",
      "img-src http://a.com" },
    { "media-src http://abc.com",
      "media-src http://abc.com" },
    { "frame-src a2-c.com",
      "frame-src http://a2-c.com" },
    { "font-src https://a.com",
      "font-src https://a.com" },
    { "connect-src *.a.com",
      "connect-src http://*.a.com" },
    { "default-src a.com:23",
      "default-src http://a.com:23" },
    { "script-src https://a.com:200",
      "script-src https://a.com:200" },
    { "object-src data:",
      "object-src data:" },
    { "style-src javascript:",
      "style-src javascript:" },
    { "img-src {app-host-is-uid}",
      "img-src http://{app-host-is-uid}" },
    { "media-src app://{app-host-is-uid}",
      "media-src app://{app-host-is-uid}" },
    { "frame-src https://foobar.com:443",
      "frame-src https://foobar.com:443" },
    { "font-src https://a.com:443",
      "font-src https://a.com:443" },
    { "connect-src http://a.com:80",
      "connect-src http://a.com:80" },
    { "default-src http://foobar.com",
      "default-src http://foobar.com" },
    { "script-src https://foobar.com",
      "script-src https://foobar.com" },
    { "object-src https://{app-host-is-uid}",
      "object-src https://{app-host-is-uid}" },
    { "style-src 'none'",
      "style-src 'none'" },
    { "img-src foo.bar:21 https://ras.bar",
      "img-src http://foo.bar:21 https://ras.bar" },
    { "media-src http://foo.bar:21 https://ras.bar:443",
      "media-src http://foo.bar:21 https://ras.bar:443" },
    { "frame-src http://self.com:80",
      "frame-src http://self.com:80" },
    { "font-src http://self.com",
      "font-src http://self.com" },
    { "connect-src https://foo.com http://bar.com:88",
      "connect-src https://foo.com http://bar.com:88" },
    { "default-src * https://bar.com 'none'",
      "default-src * https://bar.com" },
    { "script-src *.foo.com",
      "script-src http://*.foo.com" },
    { "object-src http://b.com",
      "object-src http://b.com" },
    { "style-src http://bar.com:88",
      "style-src http://bar.com:88" },
    { "img-src https://bar.com:88",
      "img-src https://bar.com:88" },
    { "media-src http://bar.com:443",
      "media-src http://bar.com:443" },
    { "frame-src https://foo.com:88",
      "frame-src https://foo.com:88" },
    { "font-src http://foo.com",
      "font-src http://foo.com" },
    { "connect-src http://x.com:23",
      "connect-src http://x.com:23" },
    { "default-src http://barbaz.com",
      "default-src http://barbaz.com" },
    { "script-src http://somerandom.foo.com",
      "script-src http://somerandom.foo.com" },
    { "default-src *",
      "default-src *" },
    { "style-src http://bar.com:22",
      "style-src http://bar.com:22" },
    { "img-src https://foo.com:443",
      "img-src https://foo.com:443" },
    { "script-src https://foo.com; ",
      "script-src https://foo.com" },
    { "img-src bar.com:*",
      "img-src http://bar.com:*" },
    { "font-src https://foo.com:400",
      "font-src https://foo.com:400" },
    { "connect-src http://bar.com:400",
      "connect-src http://bar.com:400" },
    { "default-src http://evil.com",
      "default-src http://evil.com" },
    { "script-src https://evil.com:100",
      "script-src https://evil.com:100" },
    { "default-src bar.com; script-src https://foo.com",
      "default-src http://bar.com; script-src https://foo.com" },
    { "default-src 'self'; script-src 'self' https://*:*",
      "default-src http://www.selfuri.com; script-src http://www.selfuri.com https://*:*" },
    { "img-src http://self.com:34",
      "img-src http://self.com:34" },
    { "media-src http://subd.self.com:34",
      "media-src http://subd.self.com:34" },
    { "default-src 'none'",
      "default-src 'none'" },
    { "connect-src http://self",
      "connect-src http://self" },
    { "default-src http://foo",
      "default-src http://foo" },
    { "script-src http://foo:80",
      "script-src http://foo:80" },
    { "object-src http://bar",
      "object-src http://bar" },
    { "style-src http://three:80",
      "style-src http://three:80" },
    { "img-src https://foo:400",
      "img-src https://foo:400" },
    { "media-src https://self:34",
      "media-src https://self:34" },
    { "frame-src https://bar",
      "frame-src https://bar" },
    { "font-src http://three:81",
      "font-src http://three:81" },
    { "connect-src https://three:81",
      "connect-src https://three:81" },
    { "script-src http://self.com:80/foo",
      "script-src http://self.com:80/foo" },
    { "object-src http://self.com/foo",
      "object-src http://self.com/foo" },
    { "report-uri /report.py",
      "report-uri http://www.selfuri.com/report.py"},
    { "img-src http://foo.org:34/report.py",
      "img-src http://foo.org:34/report.py" },
    { "media-src foo/bar/report.py",
      "media-src http://foo/bar/report.py" },
    { "report-uri /",
      "report-uri http://www.selfuri.com/"},
    { "font-src https://self.com/report.py",
      "font-src https://self.com/report.py" },
    { "connect-src https://foo.com/report.py",
      "connect-src https://foo.com/report.py" },
    { "default-src *; report-uri  http://www.reporturi.com/",
      "default-src *; report-uri http://www.reporturi.com/" },
    { "default-src http://first.com",
      "default-src http://first.com" },
    { "script-src http://second.com",
      "script-src http://second.com" },
    { "object-src http://third.com",
      "object-src http://third.com" },
    { "style-src https://foobar.com:4443",
      "style-src https://foobar.com:4443" },
    { "img-src http://foobar.com:4443",
      "img-src http://foobar.com:4443" },
    { "media-src bar.com",
      "media-src http://bar.com" },
    { "frame-src http://bar.com",
      "frame-src http://bar.com" },
    { "font-src http://self.com/",
      "font-src http://self.com/" },
    { "script-src 'self'",
      "script-src http://www.selfuri.com" },
    { "default-src http://self.com/foo.png",
      "default-src http://self.com/foo.png" },
    { "script-src http://self.com/foo.js",
      "script-src http://self.com/foo.js" },
    { "object-src http://bar.com/foo.js",
      "object-src http://bar.com/foo.js" },
    { "style-src http://FOO.COM",
      "style-src http://foo.com" },
    { "img-src HTTP",
      "img-src http://http" },
    { "media-src http",
      "media-src http://http" },
    { "frame-src 'SELF'",
      "frame-src http://www.selfuri.com" },
    { "DEFAULT-src 'self';",
      "default-src http://www.selfuri.com" },
    { "default-src 'self' http://FOO.COM",
      "default-src http://www.selfuri.com http://foo.com" },
    { "default-src 'self' HTTP://foo.com",
      "default-src http://www.selfuri.com http://foo.com" },
    { "default-src 'NONE'",
      "default-src 'none'" },
    { "script-src policy-uri ",
      "script-src http://policy-uri" },
    { "img-src 'self'; ",
      "img-src http://www.selfuri.com" },
    { "frame-ancestors foo-bar.com",
      "frame-ancestors http://foo-bar.com" },
    { "frame-ancestors http://a.com",
      "frame-ancestors http://a.com" },
    { "frame-ancestors 'self'",
      "frame-ancestors http://www.selfuri.com" },
    { "frame-ancestors http://self.com:88",
      "frame-ancestors http://self.com:88" },
    { "frame-ancestors http://a.b.c.d.e.f.g.h.i.j.k.l.x.com",
      "frame-ancestors http://a.b.c.d.e.f.g.h.i.j.k.l.x.com" },
    { "frame-ancestors https://self.com:34",
      "frame-ancestors https://self.com:34" },
    { "default-src 'none'; frame-ancestors 'self'",
      "default-src 'none'; frame-ancestors http://www.selfuri.com" },
    { "frame-ancestors http://self:80",
      "frame-ancestors http://self:80" },
    { "frame-ancestors http://self.com/bar",
      "frame-ancestors http://self.com/bar" },
    { "default-src 'self'; frame-ancestors 'self'",
      "default-src http://www.selfuri.com; frame-ancestors http://www.selfuri.com" },
    { "frame-ancestors http://bar.com/foo.png",
      "frame-ancestors http://bar.com/foo.png" },
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 1)));
}

// ============================= TestBadGeneratedPolicies ========================

TEST(CSPParser, BadGeneratedPolicies)
{
  static const PolicyTest policies[] =
  {
    { "foo.*.bar", ""},
    { "foo!bar.com", ""},
    { "x.*.a.com", ""},
    { "a#2-c.com", ""},
    { "http://foo.com:bar.com:23", ""},
    { "f!oo.bar", ""},
    { "ht!ps://f-oo.bar", ""},
    { "https://f-oo.bar:3f", ""},
    { "**", ""},
    { "*a", ""},
    { "http://username:password@self.com/foo", ""},
    { "http://other:pass1@self.com/foo", ""},
    { "http://user1:pass1@self.com/foo", ""},
    { "http://username:password@self.com/bar", ""},
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 0)));
}

// ============ TestGoodGeneratedPoliciesForPathHandling ============

TEST(CSPParser, GoodGeneratedPoliciesForPathHandling)
{
  // Once bug 808292 (Implement path-level host-source matching to CSP)
  // lands we have to update the expected output to include the parsed path

  static const PolicyTest policies[] =
  {
    { "img-src http://test1.example.com",
      "img-src http://test1.example.com" },
    { "img-src http://test1.example.com/",
      "img-src http://test1.example.com/" },
    { "img-src http://test1.example.com/path-1",
      "img-src http://test1.example.com/path-1" },
    { "img-src http://test1.example.com/path-1/",
      "img-src http://test1.example.com/path-1/" },
    { "img-src http://test1.example.com/path-1/path_2/",
      "img-src http://test1.example.com/path-1/path_2/" },
    { "img-src http://test1.example.com/path-1/path_2/file.js",
      "img-src http://test1.example.com/path-1/path_2/file.js" },
    { "img-src http://test1.example.com/path-1/path_2/file_1.js",
      "img-src http://test1.example.com/path-1/path_2/file_1.js" },
    { "img-src http://test1.example.com/path-1/path_2/file-2.js",
      "img-src http://test1.example.com/path-1/path_2/file-2.js" },
    { "img-src http://test1.example.com/path-1/path_2/f.js",
      "img-src http://test1.example.com/path-1/path_2/f.js" },
    { "img-src http://test1.example.com/path-1/path_2/f.oo.js",
      "img-src http://test1.example.com/path-1/path_2/f.oo.js" },
    { "img-src test1.example.com",
      "img-src http://test1.example.com" },
    { "img-src test1.example.com/",
      "img-src http://test1.example.com/" },
    { "img-src test1.example.com/path-1",
      "img-src http://test1.example.com/path-1" },
    { "img-src test1.example.com/path-1/",
      "img-src http://test1.example.com/path-1/" },
    { "img-src test1.example.com/path-1/path_2/",
      "img-src http://test1.example.com/path-1/path_2/" },
    { "img-src test1.example.com/path-1/path_2/file.js",
      "img-src http://test1.example.com/path-1/path_2/file.js" },
    { "img-src test1.example.com/path-1/path_2/file_1.js",
      "img-src http://test1.example.com/path-1/path_2/file_1.js" },
    { "img-src test1.example.com/path-1/path_2/file-2.js",
      "img-src http://test1.example.com/path-1/path_2/file-2.js" },
    { "img-src test1.example.com/path-1/path_2/f.js",
      "img-src http://test1.example.com/path-1/path_2/f.js" },
    { "img-src test1.example.com/path-1/path_2/f.oo.js",
      "img-src http://test1.example.com/path-1/path_2/f.oo.js" },
    { "img-src *.example.com",
      "img-src http://*.example.com" },
    { "img-src *.example.com/",
      "img-src http://*.example.com/" },
    { "img-src *.example.com/path-1",
      "img-src http://*.example.com/path-1" },
    { "img-src *.example.com/path-1/",
      "img-src http://*.example.com/path-1/" },
    { "img-src *.example.com/path-1/path_2/",
      "img-src http://*.example.com/path-1/path_2/" },
    { "img-src *.example.com/path-1/path_2/file.js",
      "img-src http://*.example.com/path-1/path_2/file.js" },
    { "img-src *.example.com/path-1/path_2/file_1.js",
      "img-src http://*.example.com/path-1/path_2/file_1.js" },
    { "img-src *.example.com/path-1/path_2/file-2.js",
      "img-src http://*.example.com/path-1/path_2/file-2.js" },
    { "img-src *.example.com/path-1/path_2/f.js",
      "img-src http://*.example.com/path-1/path_2/f.js" },
    { "img-src *.example.com/path-1/path_2/f.oo.js",
      "img-src http://*.example.com/path-1/path_2/f.oo.js" },
    { "img-src test1.example.com:80",
      "img-src http://test1.example.com:80" },
    { "img-src test1.example.com:80/",
      "img-src http://test1.example.com:80/" },
    { "img-src test1.example.com:80/path-1",
      "img-src http://test1.example.com:80/path-1" },
    { "img-src test1.example.com:80/path-1/",
      "img-src http://test1.example.com:80/path-1/" },
    { "img-src test1.example.com:80/path-1/path_2",
      "img-src http://test1.example.com:80/path-1/path_2" },
    { "img-src test1.example.com:80/path-1/path_2/",
      "img-src http://test1.example.com:80/path-1/path_2/" },
    { "img-src test1.example.com:80/path-1/path_2/file.js",
      "img-src http://test1.example.com:80/path-1/path_2/file.js" },
    { "img-src test1.example.com:80/path-1/path_2/f.ile.js",
      "img-src http://test1.example.com:80/path-1/path_2/f.ile.js" },
    { "img-src test1.example.com:*",
      "img-src http://test1.example.com:*" },
    { "img-src test1.example.com:*/",
      "img-src http://test1.example.com:*/" },
    { "img-src test1.example.com:*/path-1",
      "img-src http://test1.example.com:*/path-1" },
    { "img-src test1.example.com:*/path-1/",
      "img-src http://test1.example.com:*/path-1/" },
    { "img-src test1.example.com:*/path-1/path_2",
      "img-src http://test1.example.com:*/path-1/path_2" },
    { "img-src test1.example.com:*/path-1/path_2/",
      "img-src http://test1.example.com:*/path-1/path_2/" },
    { "img-src test1.example.com:*/path-1/path_2/file.js",
      "img-src http://test1.example.com:*/path-1/path_2/file.js" },
    { "img-src test1.example.com:*/path-1/path_2/f.ile.js",
      "img-src http://test1.example.com:*/path-1/path_2/f.ile.js" },
    { "img-src http://test1.example.com/abc//",
      "img-src http://test1.example.com/abc//" },
    { "img-src https://test1.example.com/abc/def//",
      "img-src https://test1.example.com/abc/def//" },
    { "img-src https://test1.example.com/abc/def/ghi//",
      "img-src https://test1.example.com/abc/def/ghi//" },
    { "img-src http://test1.example.com:80/abc//",
      "img-src http://test1.example.com:80/abc//" },
    { "img-src https://test1.example.com:80/abc/def//",
      "img-src https://test1.example.com:80/abc/def//" },
    { "img-src https://test1.example.com:80/abc/def/ghi//",
      "img-src https://test1.example.com:80/abc/def/ghi//" },
    { "img-src https://test1.example.com/abc////////////def/",
      "img-src https://test1.example.com/abc////////////def/" },
    { "img-src https://test1.example.com/abc////////////",
      "img-src https://test1.example.com/abc////////////" },
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 1)));
}

// ============ TestBadGeneratedPoliciesForPathHandling ============

TEST(CSPParser, BadGeneratedPoliciesForPathHandling)
{
  static const PolicyTest policies[] =
  {
    { "img-src test1.example.com:88path-1/",
      "img-src 'none'" },
    { "img-src test1.example.com:80.js",
      "img-src 'none'" },
    { "img-src test1.example.com:*.js",
      "img-src 'none'" },
    { "img-src test1.example.com:*.",
      "img-src 'none'" },
    { "img-src http://test1.example.com//",
      "img-src 'none'" },
    { "img-src http://test1.example.com:80//",
      "img-src 'none'" },
    { "img-src http://test1.example.com:80abc",
      "img-src 'none'" },
  };

  uint32_t policyCount = sizeof(policies) / sizeof(PolicyTest);
  ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(policies, policyCount, 1)));
}

// ============================= TestFuzzyPolicies ========================

// Use a policy, eliminate one character at a time,
// and feed it as input to the parser.

TEST(CSPParser, ShorteningPolicies)
{
  char pol[] = "default-src http://www.sub1.sub2.example.com:88/path1/path2/ 'unsafe-inline' 'none'";
  uint32_t len = static_cast<uint32_t>(sizeof(pol));

  PolicyTest testPol[1];
  memset(&testPol[0].policy, '\0', kMaxPolicyLength * sizeof(char));

  while (--len) {
    memset(&testPol[0].policy, '\0', kMaxPolicyLength * sizeof(char));
    memcpy(&testPol[0].policy, &pol, len * sizeof(char));
    ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(testPol, 1,
                                          kFuzzyExpectedPolicyCount)));
  }
}

// ============================= TestFuzzyPolicies ========================

// We generate kFuzzyRuns inputs by (pseudo) randomly picking from the 128
// ASCII characters; feed them to the parser and verfy that the parser
// handles the input gracefully.
//
// Please note, that by using srand(0) we get deterministic results!

#if RUN_OFFLINE_TESTS

TEST(CSPParser, FuzzyPolicies)
{
  // init srand with 0 so we get same results
  srand(0);

  PolicyTest testPol[1];
  memset(&testPol[0].policy, '\0', kMaxPolicyLength);

  for (uint32_t index = 0; index < kFuzzyRuns; index++) {
    // randomly select the length of the next policy
    uint32_t polLength = rand() % kMaxPolicyLength;
    // reset memory of the policy string
    memset(&testPol[0].policy, '\0', kMaxPolicyLength * sizeof(char));

    for (uint32_t i = 0; i < polLength; i++) {
      // fill the policy array with random ASCII chars
      testPol[0].policy[i] = static_cast<char>(rand() % 128);
    }
    ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(testPol, 1,
                                          kFuzzyExpectedPolicyCount)));
  }
}

#endif

// ============================= TestFuzzyPoliciesIncDir ========================

// In a similar fashion as in TestFuzzyPolicies, we again (pseudo) randomly
// generate input for the parser, but this time also include a valid directive
// followed by the random input.

#if RUN_OFFLINE_TESTS

TEST(CSPParser, FuzzyPoliciesIncDir)
{
  // init srand with 0 so we get same results
  srand(0);

  PolicyTest testPol[1];
  memset(&testPol[0].policy, '\0', kMaxPolicyLength);

  char defaultSrc[] = "default-src ";
  int defaultSrcLen = sizeof(defaultSrc) - 1;
  // copy default-src into the policy array
  memcpy(&testPol[0].policy, &defaultSrc, (defaultSrcLen * sizeof(char)));

  for (uint32_t index = 0; index < kFuzzyRuns; index++) {
    // randomly select the length of the next policy
    uint32_t polLength = rand() % (kMaxPolicyLength - defaultSrcLen);
    // reset memory of the policy string, but leave default-src.
    memset((&(testPol[0].policy) + (defaultSrcLen * sizeof(char))),
           '\0', (kMaxPolicyLength - defaultSrcLen) * sizeof(char));

    // do not start at index 0 so we do not overwrite 'default-src'
    for (uint32_t i = defaultSrcLen; i < polLength; i++) {
      // fill the policy array with random ASCII chars
      testPol[0].policy[i] = static_cast<char>(rand() % 128);
    }
    ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(testPol, 1,
                                          kFuzzyExpectedPolicyCount)));
  }
}

#endif

// ============================= TestFuzzyPoliciesIncDirLimASCII ==============

// Same as TestFuzzyPoliciesIncDir() but using limited ASCII,
// which represents more likely input.

#if RUN_OFFLINE_TESTS

TEST(CSPParser, FuzzyPoliciesIncDirLimASCII)
{
  char input[] = "1234567890" \
                 "abcdefghijklmnopqrstuvwxyz" \
                 "ABCDEFGHIJKLMNOPQRSTUVWZYZ" \
                 "!@#^&*()-+_=";

  // init srand with 0 so we get same results
  srand(0);

  PolicyTest testPol[1];
  memset(&testPol[0].policy, '\0', kMaxPolicyLength);

  char defaultSrc[] = "default-src ";
  int defaultSrcLen = sizeof(defaultSrc) - 1;
  // copy default-src into the policy array
  memcpy(&testPol[0].policy, &defaultSrc, (defaultSrcLen * sizeof(char)));

  for (uint32_t index = 0; index < kFuzzyRuns; index++) {
    // randomly select the length of the next policy
    uint32_t polLength = rand() % (kMaxPolicyLength - defaultSrcLen);
    // reset memory of the policy string, but leave default-src.
    memset((&(testPol[0].policy) + (defaultSrcLen * sizeof(char))),
           '\0', (kMaxPolicyLength - defaultSrcLen) * sizeof(char));

    // do not start at index 0 so we do not overwrite 'default-src'
    for (uint32_t i = defaultSrcLen; i < polLength; i++) {
      // fill the policy array with chars from the pre-defined input
      uint32_t inputIndex = rand() % sizeof(input);
      testPol[0].policy[i] = input[inputIndex];
    }
    ASSERT_TRUE(NS_SUCCEEDED(runTestSuite(testPol, 1,
                                          kFuzzyExpectedPolicyCount)));
  }
}
#endif