@@ -5,11 +5,24 @@ import (
5
5
"testing"
6
6
7
7
"github.com/Checkmarx/kics/pkg/model"
8
+ "github.com/Checkmarx/kics/pkg/utils"
8
9
"github.com/Checkmarx/kics/test"
9
10
"github.com/rs/zerolog"
10
11
"github.com/stretchr/testify/require"
11
12
)
12
13
14
+ var OriginalData = `resource "aws_s3_bucket" "b" {
15
+ bucket = "my-tf-test-bucket"
16
+ acl = "authenticated-read"
17
+
18
+ tags = {
19
+ Name = "My bucket"
20
+ Environment = "Dev.123"
21
+ Environment = "test"
22
+ }
23
+ }
24
+ `
25
+
13
26
// Test_detectLine tests the functions [detectLine()] and all the methods called by them
14
27
func Test_detectLine (t * testing.T ) { //nolint
15
28
type args struct {
@@ -29,19 +42,11 @@ func Test_detectLine(t *testing.T) { //nolint
29
42
name : "detect_line" ,
30
43
args : args {
31
44
file : & model.FileMetadata {
32
- ScanID : "scanID" ,
33
- ID : "Test" ,
34
- Kind : model .KindTerraform ,
35
- OriginalData : `resource "aws_s3_bucket" "b" {
36
- bucket = "my-tf-test-bucket"
37
- acl = "authenticated-read"
38
-
39
- tags = {
40
- Name = "My bucket"
41
- Environment = "Dev"
42
- }
43
- }
44
- ` ,
45
+ ScanID : "scanID" ,
46
+ ID : "Test" ,
47
+ Kind : model .KindTerraform ,
48
+ OriginalData : OriginalData ,
49
+ LinesOriginalData : utils .SplitLines (OriginalData ),
45
50
},
46
51
searchKey : "aws_s3_bucket[b].acl" ,
47
52
},
@@ -50,14 +55,14 @@ func Test_detectLine(t *testing.T) { //nolint
50
55
},
51
56
want : model.VulnerabilityLines {
52
57
Line : 3 ,
53
- VulnLines : []model.CodeLine {
58
+ VulnLines : & []model.CodeLine {
54
59
{
55
60
Position : 2 ,
56
- Line : ` bucket = "my-tf-test-bucket"` ,
61
+ Line : ` bucket = "my-tf-test-bucket"` ,
57
62
},
58
63
{
59
64
Position : 3 ,
60
- Line : ` acl = "authenticated-read"` ,
65
+ Line : ` acl = "authenticated-read"` ,
61
66
},
62
67
{
63
68
Position : 4 ,
@@ -71,20 +76,11 @@ func Test_detectLine(t *testing.T) { //nolint
71
76
name : "detect_line_with_curly_brackets" ,
72
77
args : args {
73
78
file : & model.FileMetadata {
74
- ScanID : "scanID" ,
75
- ID : "Test" ,
76
- Kind : model .KindTerraform ,
77
- OriginalData : `resource "aws_s3_bucket" "b" {
78
- bucket = "my-tf-test-bucket"
79
- acl = "authenticated-read"
80
-
81
- tags = {
82
- Name = "My bucket"
83
- Environment = "Dev.123"
84
- Environment = "test"
85
- }
86
- }
87
- ` ,
79
+ ScanID : "scanID" ,
80
+ ID : "Test" ,
81
+ Kind : model .KindTerraform ,
82
+ OriginalData : OriginalData ,
83
+ LinesOriginalData : utils .SplitLines (OriginalData ),
88
84
},
89
85
searchKey : "aws_s3_bucket[b].Environment={{Dev.123}}" ,
90
86
},
@@ -93,18 +89,18 @@ func Test_detectLine(t *testing.T) { //nolint
93
89
},
94
90
want : model.VulnerabilityLines {
95
91
Line : 7 ,
96
- VulnLines : []model.CodeLine {
92
+ VulnLines : & []model.CodeLine {
97
93
{
98
94
Position : 6 ,
99
- Line : ` Name = "My bucket"` ,
95
+ Line : ` Name = "My bucket"` ,
100
96
},
101
97
{
102
98
Position : 7 ,
103
- Line : ` Environment = "Dev.123"` ,
99
+ Line : ` Environment = "Dev.123"` ,
104
100
},
105
101
{
106
102
Position : 8 ,
107
- Line : ` Environment = "test"` ,
103
+ Line : ` Environment = "test"` ,
108
104
},
109
105
},
110
106
LineWithVulnerabilty : "" ,
@@ -114,20 +110,11 @@ func Test_detectLine(t *testing.T) { //nolint
114
110
name : "detect_line_error" ,
115
111
args : args {
116
112
file : & model.FileMetadata {
117
- ScanID : "scanID" ,
118
- ID : "Test" ,
119
- Kind : model .KindTerraform ,
120
- OriginalData : `resource "aws_s3_bucket" "b" {
121
- bucket = "my-tf-test-bucket"
122
- acl = "authenticated-read"
123
-
124
- tags = {
125
- Name = "My bucket"
126
- Environment = "Dev.123"
127
- Environment = "test"
128
- }
129
- }
130
- ` ,
113
+ ScanID : "scanID" ,
114
+ ID : "Test" ,
115
+ Kind : model .KindTerraform ,
116
+ OriginalData : OriginalData ,
117
+ LinesOriginalData : utils .SplitLines (OriginalData ),
131
118
},
132
119
searchKey : "testing.error" ,
133
120
},
@@ -136,7 +123,7 @@ func Test_detectLine(t *testing.T) { //nolint
136
123
},
137
124
want : model.VulnerabilityLines {
138
125
Line : - 1 ,
139
- VulnLines : []model.CodeLine {},
126
+ VulnLines : & []model.CodeLine {},
140
127
},
141
128
},
142
129
}
@@ -155,6 +142,10 @@ func Test_detectLine(t *testing.T) { //nolint
155
142
}
156
143
}
157
144
145
+ var content = []byte (
146
+ `content1
147
+ content2` )
148
+
158
149
func Test_defaultDetectLine_prepareResolvedFiles (t * testing.T ) {
159
150
type args struct {
160
151
resFiles map [string ]model.ResolvedFile
@@ -169,10 +160,9 @@ func Test_defaultDetectLine_prepareResolvedFiles(t *testing.T) {
169
160
args : args {
170
161
resFiles : map [string ]model.ResolvedFile {
171
162
"file1" : {
172
- Content : []byte (
173
- `content1
174
- content2` ),
175
- Path : "testing/file1" ,
163
+ Content : content ,
164
+ Path : "testing/file1" ,
165
+ LinesContent : utils .SplitLines (string (content )),
176
166
},
177
167
},
178
168
},
0 commit comments