Skip to content

Commit 28c1128

Browse files
ccojocarCosmin Cojocar
authored andcommitted
Add more tests to improve the coverage of resolve
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent d78f026 commit 28c1128

File tree

2 files changed

+210
-1
lines changed

2 files changed

+210
-1
lines changed

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ golang.org/x/crypto v0.0.0-20190907121410-71b5226ff739/go.mod h1:yigFU9vqHzYiE8U
4646
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83 h1:mgAKeshyNqWKdENOnQsg+8dRTwZFIwFaO3HNl52sweA=
4747
golang.org/x/crypto v0.0.0-20190909091759-094676da4a83/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
4848
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
49+
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392 h1:ACG4HJsFiNMf47Y4PeRoebLNy/2lXT9EtprMuTFWt1M=
4950
golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3H3cr1v9wB50oz8l4C4h62xy7jSTY=
5051
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
5152
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -58,6 +59,7 @@ golang.org/x/net v0.0.0-20190909003024-a7b16738d86b/go.mod h1:z5CRVTTTmAJ677TzLL
5859
golang.org/x/net v0.0.0-20190912160710-24e19bdeb0f2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
5960
golang.org/x/net v0.0.0-20190916140828-c8589233b77d/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
6061
golang.org/x/net v0.0.0-20190921015927-1a5e07d1ff72/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
62+
golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
6163
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
6264
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA=
6365
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -75,6 +77,7 @@ golang.org/x/sys v0.0.0-20190910064555-bbd175535a8b/go.mod h1:h1NjWce9XRLGQEsW7w
7577
golang.org/x/sys v0.0.0-20190911201528-7ad0cfa0b7b5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7678
golang.org/x/sys v0.0.0-20190913121621-c3b328c6e5a7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7779
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
80+
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69 h1:rOhMmluY6kLMhdnrivzec6lLgaVbMHMn2ISQXJeJ5EM=
7881
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7982
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
8083
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

resolve_test.go

Lines changed: 207 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var _ = Describe("Resolve ast node to concrete value", func() {
1313
Context("when attempting to resolve an ast node", func() {
1414
It("should successfully resolve basic literal", func() {
1515
var basicLiteral *ast.BasicLit
16-
1716
pkg := testutils.NewTestPackage()
1817
defer pkg.Close()
1918
pkg.AddFile("foo.go", `package main; const foo = "bar"; func main(){}`)
@@ -52,6 +51,47 @@ var _ = Describe("Resolve ast node to concrete value", func() {
5251
Expect(gosec.TryResolve(ident, ctx)).Should(BeTrue())
5352
})
5453

54+
It("should successfully resolve variable identifier", func() {
55+
var ident *ast.Ident
56+
pkg := testutils.NewTestPackage()
57+
defer pkg.Close()
58+
pkg.AddFile("foo.go", `package main; import "fmt"; func main(){ x := "test"; y := x; fmt.Println(y) }`)
59+
ctx := pkg.CreateContext("foo.go")
60+
v := testutils.NewMockVisitor()
61+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
62+
if node, ok := n.(*ast.Ident); ok && node.Name == "y" {
63+
ident = node
64+
return false
65+
}
66+
return true
67+
}
68+
v.Context = ctx
69+
ast.Walk(v, ctx.Root)
70+
Expect(ident).ShouldNot(BeNil())
71+
Expect(gosec.TryResolve(ident, ctx)).Should(BeTrue())
72+
})
73+
74+
It("should successfully not resolve variable identifier with no declaration", func() {
75+
var ident *ast.Ident
76+
pkg := testutils.NewTestPackage()
77+
defer pkg.Close()
78+
pkg.AddFile("foo.go", `package main; import "fmt"; func main(){ x := "test"; y := x; fmt.Println(y) }`)
79+
ctx := pkg.CreateContext("foo.go")
80+
v := testutils.NewMockVisitor()
81+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
82+
if node, ok := n.(*ast.Ident); ok && node.Name == "y" {
83+
ident = node
84+
return false
85+
}
86+
return true
87+
}
88+
v.Context = ctx
89+
ast.Walk(v, ctx.Root)
90+
Expect(ident).ShouldNot(BeNil())
91+
ident.Obj.Decl = nil
92+
Expect(gosec.TryResolve(ident, ctx)).Should(BeFalse())
93+
})
94+
5595
It("should successfully resolve assign statement", func() {
5696
var assign *ast.AssignStmt
5797
pkg := testutils.NewTestPackage()
@@ -73,6 +113,50 @@ var _ = Describe("Resolve ast node to concrete value", func() {
73113
Expect(gosec.TryResolve(assign, ctx)).Should(BeTrue())
74114
})
75115

116+
It("should successfully not resolve assign statement without rhs", func() {
117+
var assign *ast.AssignStmt
118+
pkg := testutils.NewTestPackage()
119+
defer pkg.Close()
120+
pkg.AddFile("foo.go", `package main; const x = "bar"; func main(){ y := x; println(y) }`)
121+
ctx := pkg.CreateContext("foo.go")
122+
v := testutils.NewMockVisitor()
123+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
124+
if node, ok := n.(*ast.AssignStmt); ok {
125+
if id, ok := node.Lhs[0].(*ast.Ident); ok && id.Name == "y" {
126+
assign = node
127+
}
128+
}
129+
return true
130+
}
131+
v.Context = ctx
132+
ast.Walk(v, ctx.Root)
133+
Expect(assign).ShouldNot(BeNil())
134+
assign.Rhs = []ast.Expr{}
135+
Expect(gosec.TryResolve(assign, ctx)).Should(BeFalse())
136+
})
137+
138+
It("should successfully not resolve assign statement with unsolvable rhs", func() {
139+
var assign *ast.AssignStmt
140+
pkg := testutils.NewTestPackage()
141+
defer pkg.Close()
142+
pkg.AddFile("foo.go", `package main; const x = "bar"; func main(){ y := x; println(y) }`)
143+
ctx := pkg.CreateContext("foo.go")
144+
v := testutils.NewMockVisitor()
145+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
146+
if node, ok := n.(*ast.AssignStmt); ok {
147+
if id, ok := node.Lhs[0].(*ast.Ident); ok && id.Name == "y" {
148+
assign = node
149+
}
150+
}
151+
return true
152+
}
153+
v.Context = ctx
154+
ast.Walk(v, ctx.Root)
155+
Expect(assign).ShouldNot(BeNil())
156+
assign.Rhs = []ast.Expr{&ast.CallExpr{}}
157+
Expect(gosec.TryResolve(assign, ctx)).Should(BeFalse())
158+
})
159+
76160
It("should successfully resolve a binary statement", func() {
77161
var target *ast.BinaryExpr
78162
pkg := testutils.NewTestPackage()
@@ -112,6 +196,49 @@ var _ = Describe("Resolve ast node to concrete value", func() {
112196
Expect(value).ShouldNot(BeNil())
113197
Expect(gosec.TryResolve(value, ctx)).Should(BeTrue())
114198
})
199+
It("should successfully not resolve value spec without values", func() {
200+
var value *ast.ValueSpec
201+
pkg := testutils.NewTestPackage()
202+
defer pkg.Close()
203+
pkg.AddFile("foo.go", `package main; const x = "bar"; func main(){ var y string = x; println(y) }`)
204+
ctx := pkg.CreateContext("foo.go")
205+
v := testutils.NewMockVisitor()
206+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
207+
if node, ok := n.(*ast.ValueSpec); ok {
208+
if len(node.Names) == 1 && node.Names[0].Name == "y" {
209+
value = node
210+
}
211+
}
212+
return true
213+
}
214+
v.Context = ctx
215+
ast.Walk(v, ctx.Root)
216+
Expect(value).ShouldNot(BeNil())
217+
value.Values = []ast.Expr{}
218+
Expect(gosec.TryResolve(value, ctx)).Should(BeFalse())
219+
})
220+
221+
It("should successfully not resolve value spec with unsolvable value", func() {
222+
var value *ast.ValueSpec
223+
pkg := testutils.NewTestPackage()
224+
defer pkg.Close()
225+
pkg.AddFile("foo.go", `package main; const x = "bar"; func main(){ var y string = x; println(y) }`)
226+
ctx := pkg.CreateContext("foo.go")
227+
v := testutils.NewMockVisitor()
228+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
229+
if node, ok := n.(*ast.ValueSpec); ok {
230+
if len(node.Names) == 1 && node.Names[0].Name == "y" {
231+
value = node
232+
}
233+
}
234+
return true
235+
}
236+
v.Context = ctx
237+
ast.Walk(v, ctx.Root)
238+
Expect(value).ShouldNot(BeNil())
239+
value.Values = []ast.Expr{&ast.CallExpr{}}
240+
Expect(gosec.TryResolve(value, ctx)).Should(BeFalse())
241+
})
115242

116243
It("should successfully resolve composite literal", func() {
117244
var value *ast.CompositeLit
@@ -131,5 +258,84 @@ var _ = Describe("Resolve ast node to concrete value", func() {
131258
Expect(value).ShouldNot(BeNil())
132259
Expect(gosec.TryResolve(value, ctx)).Should(BeTrue())
133260
})
261+
262+
It("should successfully not resolve composite literal without elst", func() {
263+
var value *ast.CompositeLit
264+
pkg := testutils.NewTestPackage()
265+
defer pkg.Close()
266+
pkg.AddFile("foo.go", `package main; func main(){ y := []string{"value1", "value2"}; println(y) }`)
267+
ctx := pkg.CreateContext("foo.go")
268+
v := testutils.NewMockVisitor()
269+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
270+
if node, ok := n.(*ast.CompositeLit); ok {
271+
value = node
272+
}
273+
return true
274+
}
275+
v.Context = ctx
276+
ast.Walk(v, ctx.Root)
277+
Expect(value).ShouldNot(BeNil())
278+
value.Elts = []ast.Expr{}
279+
Expect(gosec.TryResolve(value, ctx)).Should(BeFalse())
280+
})
281+
282+
It("should successfully not resolve composite literal with unsolvable elst", func() {
283+
var value *ast.CompositeLit
284+
pkg := testutils.NewTestPackage()
285+
defer pkg.Close()
286+
pkg.AddFile("foo.go", `package main; func main(){ y := []string{"value1", "value2"}; println(y) }`)
287+
ctx := pkg.CreateContext("foo.go")
288+
v := testutils.NewMockVisitor()
289+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
290+
if node, ok := n.(*ast.CompositeLit); ok {
291+
value = node
292+
}
293+
return true
294+
}
295+
v.Context = ctx
296+
ast.Walk(v, ctx.Root)
297+
Expect(value).ShouldNot(BeNil())
298+
value.Elts = []ast.Expr{&ast.CallExpr{}}
299+
Expect(gosec.TryResolve(value, ctx)).Should(BeFalse())
300+
})
301+
302+
It("should successfully not resolve call expressions", func() {
303+
var value *ast.CallExpr
304+
pkg := testutils.NewTestPackage()
305+
defer pkg.Close()
306+
pkg.AddFile("foo.go", `package main; func main(){ y := []string{"value1", "value2"}; println(y) }`)
307+
ctx := pkg.CreateContext("foo.go")
308+
v := testutils.NewMockVisitor()
309+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
310+
if node, ok := n.(*ast.CallExpr); ok {
311+
value = node
312+
}
313+
return true
314+
}
315+
v.Context = ctx
316+
ast.Walk(v, ctx.Root)
317+
Expect(value).ShouldNot(BeNil())
318+
Expect(gosec.TryResolve(value, ctx)).Should(BeFalse())
319+
})
320+
321+
It("should successfully not resolve call expressions", func() {
322+
var value *ast.ImportSpec
323+
pkg := testutils.NewTestPackage()
324+
defer pkg.Close()
325+
pkg.AddFile("foo.go", `package main; import "fmt"; func main(){ y := []string{"value1", "value2"}; fmt.Println(y) }`)
326+
ctx := pkg.CreateContext("foo.go")
327+
v := testutils.NewMockVisitor()
328+
v.Callback = func(n ast.Node, ctx *gosec.Context) bool {
329+
if node, ok := n.(*ast.ImportSpec); ok {
330+
value = node
331+
}
332+
return true
333+
}
334+
v.Context = ctx
335+
ast.Walk(v, ctx.Root)
336+
Expect(value).ShouldNot(BeNil())
337+
Expect(gosec.TryResolve(value, ctx)).Should(BeFalse())
338+
})
339+
134340
})
135341
})

0 commit comments

Comments
 (0)