-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser_test.go
More file actions
1678 lines (1491 loc) · 37.6 KB
/
Copy pathparser_test.go
File metadata and controls
1678 lines (1491 loc) · 37.6 KB
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
package spec
import (
"errors"
"os"
"strings"
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"github.com/zenizh/go-capturer"
)
var (
tmTestValid = `spec_version = "` + Version + `"
threatmodel "test" {
author = "@xntrik"
}
`
tmTestValidJson = `{"spec_version": "` + Version + `",
"threatmodel": {
"test": {
"author": "@xntrik"
}
}
}`
)
func TestNewTMParser(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
tmParser.validateSpec("blep")
out := capturer.CaptureStdout(func() {
tmParser.validateSpec("blop")
})
if !strings.Contains(out, "No provided version.") {
t.Error("Missing stdout from a blank spec version")
t.Log(out)
}
tmw := &ThreatmodelWrapped{
SpecVersion: "NOPE",
}
tmParser.wrapped = tmw
_ = capturer.CaptureStdout(func() {
tmParser.validateSpec("blop")
})
// @TODO: When we tidy up spec versioning, redo these tests
// if !strings.Contains(out, "Provided version ('NOPE') doesn't match") {
// t.Error("Missing stdout from a blank spec version")
// t.Log(out)
// }
if tmParser.GetWrapped() == nil {
t.Error("GetWrapped shouldn't return nil")
t.Logf("%+v\n", tmParser.GetWrapped())
}
}
func TestParserHclString(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseFile("./testdata/including/corp-app.hcl", false)
if err != nil {
t.Errorf("Error parsing legit TM file: %s", err)
}
hclOut := tmParser.HclString()
// t.Errorf("hclString:\n%s\n", tmParser.HclString())
if !strings.Contains(hclOut, "threatmodel \"Tower of London\"") {
t.Errorf("Did not find Tower of London HCL")
}
if !strings.Contains(hclOut, "A historic castle") {
t.Errorf("Did not find 'A historic castle'")
}
}
func TestParseInvalidFileExt(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseFile("./testdata/tm1.csv", false)
if err == nil {
t.Errorf("Error parsing illegitimate TM extension: %s", err)
}
}
func TestParseHCLFile(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/tm1.hcl", false)
if err != nil {
t.Errorf("Error parsing legit TM file: %s", err)
}
err = tmParser.ParseFile("./testdata/tm1.hcl", false)
if err != nil {
t.Errorf("Error parsing legit TM file: %s", err)
}
err = tmParser.ParseHCLFile("./testdata/tm-invalid.hcl", false)
if err == nil {
t.Errorf("Error parsing broken TM file: %s", err)
}
}
func TestParseRepository(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/tm1.hcl", false)
if err != nil {
t.Fatalf("Error parsing legit TM file: %s", err)
}
var found *Threatmodel
for i, tm := range tmParser.GetWrapped().Threatmodels {
if tm.Name == "tm tm1 two" {
found = &tmParser.GetWrapped().Threatmodels[i]
}
}
if found == nil {
t.Fatal("Couldn't find the 'tm tm1 two' threat model")
}
exp := []string{
"https://github.com/threatcl/spec",
"https://gitlab.com/threatcl/example",
}
if len(found.Repository) != len(exp) {
t.Fatalf("Expected %d repository entries, got %d", len(exp), len(found.Repository))
}
for i, want := range exp {
if found.Repository[i] != want {
t.Errorf("Expected repository[%d] to be %q, got %q", i, want, found.Repository[i])
}
}
}
func TestParseJsonFile(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseJSONFile("./testdata/tm1.json", false)
if err != nil {
t.Errorf("Error parsing legit TM file: %s", err)
}
err = tmParser.ParseFile("./testdata/tm1.json", false)
if err != nil {
t.Errorf("Error parsing legit TM file: %s", err)
}
err = tmParser.ParseJSONFile("./testdata/tm-invalid.json", false)
if err == nil {
t.Errorf("Error parsing broken TM file: %s", err)
}
}
func TestParseHCLFileWithVar(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/tm-withvar.hcl", false)
if err != nil {
t.Errorf("Error parsing legit TM file: %s", err)
}
foundVarVal := false
for _, tm := range tmParser.GetWrapped().Threatmodels {
for _, threat := range tm.Threats {
t.Logf("%s - %s", threat.Description, threat.Control)
if strings.Contains(threat.Description, "test_var_val") {
foundVarVal = true
}
}
}
if !foundVarVal {
t.Errorf("We didn't find the variable")
}
}
func TestParseHCLFileWithImport(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/tm-withimport.hcl", false)
if err != nil {
t.Errorf("Error parsing legit TM file: %s", err)
}
foundImport := false
foundImportSubfolder := false
for _, tm := range tmParser.GetWrapped().Threatmodels {
for _, threat := range tm.Threats {
t.Logf("%s - %s", threat.Description, threat.Control)
if strings.Contains(threat.Description, "ANd it should have spaces") {
if threat.Control == "Valid controls only" {
foundImport = true
}
}
if threat.Description == "words" {
if threat.Control == "Still valid controls only" {
foundImportSubfolder = true
}
}
}
}
if !foundImport {
t.Errorf("We didn't find the imported control")
}
if !foundImportSubfolder {
t.Errorf("We didn't find the imported control from the subfolder")
}
}
func TestParseHCLFileWithExpandedControlImports(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/tm-with-expanded-controls.hcl", false)
if err != nil {
t.Errorf("Error parsing TM file with expanded control imports: %s", err)
}
foundLegacyControl := false
foundExpandedAuthControl := false
foundExpandedEncryptionControl := false
foundExpandedAccessControl := false
for _, tm := range tmParser.GetWrapped().Threatmodels {
if tm.Name == "test_expanded_controls" {
for _, threat := range tm.Threats {
// Check legacy control
if threat.Control == "Valid controls only" {
foundLegacyControl = true
}
// Check expanded controls
for _, control := range threat.Controls {
if control.Name == "auth_control" && control.Description == "Multi-factor authentication required" && control.Implemented == true {
foundExpandedAuthControl = true
}
if control.Name == "encryption_control" && control.Description == "Data encrypted at rest and in transit" && control.Implemented == false {
foundExpandedEncryptionControl = true
}
if control.Name == "access_control" && control.Description == "Role-based access control implemented" && control.Implemented == true {
foundExpandedAccessControl = true
}
}
}
}
}
if !foundLegacyControl {
t.Errorf("We didn't find the legacy imported control")
}
if !foundExpandedAuthControl {
t.Errorf("We didn't find the expanded authentication control")
}
if !foundExpandedEncryptionControl {
t.Errorf("We didn't find the expanded encryption control")
}
if !foundExpandedAccessControl {
t.Errorf("We didn't find the expanded access control")
}
}
func TestParseHCLFileWithControlImports(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/tm-with-control-import.hcl", false)
if err != nil {
t.Errorf("Error parsing TM file with control import: %s", err)
}
foundSingleImport := false
foundMultipleImports := false
foundMixedApproach := false
for _, tm := range tmParser.GetWrapped().Threatmodels {
if tm.Name == "test_control_import" {
for _, threat := range tm.Threats {
if threat.Description == "Test control import" {
// Should have 1 control from control_import
if len(threat.Controls) == 1 {
control := threat.Controls[0]
if control.Name == "authentication_control" &&
control.Description == "Multi-factor authentication required" &&
control.Implemented == true {
foundSingleImport = true
}
}
}
if threat.Description == "Test multiple control imports" {
// Should have 2 controls from control_import array
if len(threat.Controls) == 2 {
authFound := false
encFound := false
for _, control := range threat.Controls {
if control.Name == "authentication_control" {
authFound = true
}
if control.Name == "encryption_control" {
encFound = true
}
}
if authFound && encFound {
foundMultipleImports = true
}
}
}
if threat.Description == "Test mixed approach" {
// Should have 2 controls: 1 from control_import + 1 from expanded_control block
if len(threat.Controls) == 2 {
importFound := false
customFound := false
for _, control := range threat.Controls {
if control.Name == "access_control" {
importFound = true
}
if control.Name == "custom_control" {
customFound = true
}
}
if importFound && customFound {
foundMixedApproach = true
}
}
}
}
}
}
if !foundSingleImport {
t.Errorf("We didn't find the single control import")
}
if !foundMultipleImports {
t.Errorf("We didn't find the multiple control imports")
}
if !foundMixedApproach {
t.Errorf("We didn't find the mixed approach")
}
}
func TestParseHCLFileWithMissingImport(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/tm-withimport-missingfile.hcl", false)
if err != nil && !strings.Contains(err.Error(), "othercontrols.hcl: no such file or directory") {
t.Errorf("Different error parsing legit TM file: %s", err)
}
}
func TestParseHCLFileWithBadRefImport(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/tm-withimport-badref.hcl", false)
if err != nil && !strings.Contains(err.Error(), "This object does not have an attribute named \"aer_control_name\"") {
t.Errorf("Error parsing legit TM file: %s", err)
}
}
func TestAddTMAndWrite(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
tm := Threatmodel{
Name: "test",
Author: "x",
}
out := capturer.CaptureStdout(func() {
_ = tmParser.AddTMAndWrite(tm, os.Stdout, false)
})
if !strings.Contains(out, "threatmodel \"test\"") {
t.Error("The tm wasn't added correctly")
}
out = capturer.CaptureStdout(func() {
_ = tmParser.AddTMAndWrite(tm, os.Stdout, true)
})
if !strings.Contains(out, "Name: (string) (len=4) \"test\"") {
t.Error("The tm wasn't added correctly")
}
}
// parsercovFailWriter always errors on Write, to exercise the write error
// branch in AddTMAndWrite
type parsercovFailWriter struct{}
func (w *parsercovFailWriter) Write(p []byte) (int, error) {
return 0, errors.New("parsercov write error")
}
func TestAddTMAndWriteFailingWriter(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
// The description needs to be larger than bufio's default buffer so
// that the underlying writer's error surfaces from Write
tm := Threatmodel{
Name: "test",
Author: "x",
Description: strings.Repeat("A", 8192),
}
err := tmParser.AddTMAndWrite(tm, &parsercovFailWriter{}, false)
if err == nil {
t.Error("Expected an error from the failing writer")
} else if !strings.Contains(err.Error(), "parsercov write error") {
t.Errorf("Unexpected error from failing writer: %s", err)
}
}
func TestParseFileInvalidContents(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseFile("./testdata/tm-invalid.hcl", false)
if err == nil {
t.Error("Expected an error parsing a broken HCL TM file via ParseFile")
}
tmParser = NewThreatmodelParser(defaultCfg)
err = tmParser.ParseFile("./testdata/tm-invalid.json", false)
if err == nil {
t.Error("Expected an error parsing a broken JSON TM file via ParseFile")
}
}
func TestParseHCLFileControlImportFallback(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/parsercov-tm-fallback.hcl", false)
if err != nil {
t.Fatalf("Error parsing TM file with expanded_control fallback: %s", err)
}
foundFallbackControl := false
for _, tm := range tmParser.GetWrapped().Threatmodels {
if tm.Name == "parsercov_fallback" {
for _, threat := range tm.Threats {
for _, control := range threat.Controls {
if control.Name == "parsercov_fallback_control" &&
control.Description == "Control living only in the control namespace" &&
control.Implemented == true &&
control.RiskReduction == 40 {
foundFallbackControl = true
}
}
}
}
}
if !foundFallbackControl {
t.Errorf("We didn't find the control resolved via the expanded_control fallback")
}
}
func TestParseHCLFileBareExpandedControlImport(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile("./testdata/parsercov-tm-bare-expanded.hcl", false)
if err != nil {
t.Fatalf("Error parsing TM file with bare expanded control import: %s", err)
}
foundBareExpanded := false
foundMiscComponent := false
for _, tm := range tmParser.GetWrapped().Threatmodels {
if tm.Name == "parsercov_bare_expanded" {
for _, threat := range tm.Threats {
for _, control := range threat.Controls {
if control.Name == "bare_expanded" &&
control.Description == "Expanded control with no notes or attributes" &&
control.ImplementationNotes == "" &&
len(control.Attributes) == 0 {
foundBareExpanded = true
}
if control.Name == "misc_component" &&
control.Description == "A component of another type" {
foundMiscComponent = true
}
}
}
}
}
if !foundBareExpanded {
t.Errorf("We didn't find the bare expanded control")
}
if !foundMiscComponent {
t.Errorf("We didn't find the misc component imported as a control")
}
}
func TestParseHCLFileControlImportEmptyLibrary(t *testing.T) {
cases := []struct {
name string
file string
exp string
}{
{
"empty_expanded_control_import",
"./testdata/parsercov-tm-empty-expanded.hcl",
"no expanded_control or control imports available",
},
{
"empty_control_import",
"./testdata/parsercov-tm-empty-control.hcl",
"no control imports available",
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLFile(tc.file, false)
if err == nil {
t.Errorf("%s: An error was expected but none was thrown", tc.name)
} else if !strings.Contains(err.Error(), tc.exp) {
t.Errorf("%s: Expected error '%s', got: %s", tc.name, tc.exp, err)
}
})
}
}
func TestParseHCLRawControlImportErrors(t *testing.T) {
cases := []struct {
name string
in string
exp string
}{
{
"invalid_import_format",
`threatmodel "test" {
author = "j"
threat "test_threat" {
description = "threat"
control_imports = ["bogus"]
}
}`,
"invalid control import format: bogus",
},
{
"invalid_import_prefix",
`threatmodel "test" {
author = "j"
threat "test_threat" {
description = "threat"
control_imports = ["notimport.control.foo"]
}
}`,
"invalid control import format: notimport.control.foo",
},
{
"unsupported_control_type",
`threatmodel "test" {
author = "j"
threat "test_threat" {
description = "threat"
control_imports = ["import.widget.foo"]
}
}`,
"unsupported control type: widget",
},
{
"no_imports_available",
`threatmodel "test" {
author = "j"
threat "test_threat" {
description = "threat"
control_imports = ["import.control.foo"]
}
}`,
"no imports available",
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLRaw([]byte(tc.in))
if err == nil {
t.Errorf("%s: An error was expected but none was thrown", tc.name)
} else if !strings.Contains(err.Error(), tc.exp) {
t.Errorf("%s: Expected error '%s', got: %s", tc.name, tc.exp, err)
}
})
}
}
func TestParseHCLRawShallowExtractErrors(t *testing.T) {
cases := []struct {
name string
in string
exp string
errorthrown bool
}{
{
"variable_missing_label",
`variable {
value = "test_var_val"
}
threatmodel "test" {
author = "j"
}`,
"Missing name for variable",
true,
},
{
"variable_value_not_string",
`variable "test_var" {
value = ["not", "a", "string"]
}
threatmodel "test" {
author = "j"
}`,
"Unsuitable value type",
true,
},
{
"threatmodel_missing_label",
`threatmodel {
author = "j"
}`,
"Missing name for threatmodel",
true,
},
{
"imports_not_a_list",
`threatmodel "test" {
imports = "controls.hcl"
author = "j"
}`,
"Unsuitable value type",
true,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
err := tmParser.ParseHCLRaw([]byte(tc.in))
if err != nil {
t.Logf("Err: '%s'. Expected: '%s'.", err.Error(), tc.exp)
if !strings.Contains(err.Error(), tc.exp) {
t.Errorf("%s: Error parsing hcl tm: %s", tc.name, err)
}
} else {
t.Logf("Expected: '%s'.", tc.exp)
if tc.errorthrown {
t.Errorf("%s: An error was expected but none was thrown", tc.name)
}
}
})
}
}
func TestResolveControlImportNullEntry(t *testing.T) {
defaultCfg := &ThreatmodelSpecConfig{}
defaultCfg.setDefaults()
tmParser := NewThreatmodelParser(defaultCfg)
ctx := &hcl.EvalContext{}
ctx.Variables = map[string]cty.Value{
"import": cty.ObjectVal(map[string]cty.Value{
"control": cty.ObjectVal(map[string]cty.Value{
"ghost_control": cty.NullVal(cty.EmptyObject),
}),
"expanded_control": cty.ObjectVal(map[string]cty.Value{}),
}),
}
_, err := tmParser.resolveControlImport("import.control.ghost_control", ctx)
if err == nil {
t.Error("Expected an error resolving a null control entry")
} else if !strings.Contains(err.Error(), "control 'ghost_control' not found in imports") {
t.Errorf("Unexpected error resolving a null control entry: %s", err)
}
}
func TestParseHCLRaw(t *testing.T) {
cases := []struct {
name string
in string
exp string
errorthrown bool
}{
{
"valid_hcltm",
tmTestValid,
"",
false,
},
{
"invalid_block",
"spec_version \"` + Version + `\"",
"Invalid block definition",
true,
},
{
"invalid_number_literal",
"spec_version = 0.1.0\"",
"Invalid number literal",
true,
},
{
"invalid_spec_version",
"spec_veon = \"` + Version + `\"",
"Unsupported argument; An argument named \"spec_veon\"",
true,
},
{
"invalid_dupe_tm",
tmTestValid + `
threatmodel "test" {
author = "j"
}
`,
"TM 'test': duplicate found",
true,
},
{
"invalid_dupe_infoasset",
`threatmodel "test" {
author = "j"
information_asset "asset" {information_classification = "Public"}
information_asset "asset" {information_classification = "Public"}
}
`,
"TM 'test': duplicate information_asset 'asset'",
true,
},
{
"invalid_tminfoassetref",
`threatmodel "test" {
author = "j"
threat "test_threat" {
description = "threat"
information_asset_refs = ["nope"]
}
}
`,
"trying to refer to non-existent information_asset 'nope'",
true,
},
{
"invalid_tminfoassetref2",
`threatmodel "test" {
author = "j"
information_asset "asset" {information_classification = "Public"}
threat "test_threat" {
description = "threat"
information_asset_refs = ["nope"]
}
}
`,
"trying to refer to non-existent information_asset 'nope'",
true,
},
{
"tminfoassetref",
`threatmodel "test" {
author = "j"
information_asset "asset" {information_classification = "Public"}
threat {
description = "threat"
information_asset_refs = ["asset"]
}
}
`,
"",
false,
},
{
"tmimportfailurestdin",
`threatmodel "test" {
imports = ["errorhere.hcl"]
author = "j"
threat {
description = "threat"
information_asset_refs = ["asset"]
}
}
`,
"errorhere.hcl: no such file or directory",
true,
},
{
"tmvar_working",
`variable "test_var" {
value = "test_var_val"
}
threatmodel "test" {
author = "j"
threat {
description = var.test_var
}
}`,
"",
false,
},
{
"tmvar_arg_block_req_err",
`variable 1 {
value = "test_var_val"
}
threatmodel "test" {
author = "j"
threat "test_threat" {
description = var.test_var
}
}`,
"Argument or block definition required",
true,
},
{
"tmvar_wrong_arg_err",
`variable "1" {
value = "test_var_val"
nope = 2
}
threatmodel "test" {
author = "j"
threat "test_threat" {
description = "var.test_var"
}
}`,
"An argument named \"nope\" is not expected here",
true,
},
// {
// "tmvar_wrong_arg_err2",
// `variable "test_var" {
// value = 1
// }
// threatmodel "test" {
// author = "j"
// threat {
// description = var.test_var
// }
// }`,
// "An argument named \"nope\" is not expected here",
// true,
// },
{
"var_in_tm_err",
`threatmodel "test" {
variable "test_var" {
value = "test_var_val"
}
author = "j"
threat "test_threat" {
description = var.test_var
}
}`,
"Blocks of type \"variable\" are not expected here",
true,
},
{
"dfd_missing_ia_from_datastore",
`threatmodel "dfdtest" {
author = "j"
information_asset "valid_asset" {information_classification = "Public"}
data_flow_diagram {
data_store "1" {
information_asset = "nope"
}
}
}`,
"TM 'dfdtest' DFD Data Store '1' trying to refer to non-existent information_asset 'nope'",
true,
},
{
"dfd_dupe_process",
`threatmodel "dfdtest" {
author = "j"
data_flow_diagram {
process "1" {}
process "1" {}
}
}`,
"duplicate process found in dfd '1'",
true,
},
{
"dfd_dupe_flow",
`threatmodel "dfdtest" {
author = "j"
data_flow_diagram {
process "1" {}
process "2" {}
flow "http" {