11package gosec_test
22
33import (
4+ "go/ast"
45 "io/ioutil"
56 "os"
67 "path/filepath"
@@ -9,6 +10,7 @@ import (
910 . "github.com/onsi/ginkgo"
1011 . "github.com/onsi/gomega"
1112 "github.com/securego/gosec"
13+ "github.com/securego/gosec/testutils"
1214)
1315
1416var _ = Describe ("Helpers" , func () {
@@ -91,4 +93,140 @@ var _ = Describe("Helpers", func() {
9193 Expect (len (r )).Should (Equal (0 ))
9294 })
9395 })
96+
97+ Context ("when getting call info" , func () {
98+ It ("should return the type and call name for selector expression" , func () {
99+ pkg := testutils .NewTestPackage ()
100+ defer pkg .Close ()
101+ pkg .AddFile ("main.go" , `
102+ package main
103+
104+ import(
105+ "bytes"
106+ )
107+
108+ func main() {
109+ b := new(bytes.Buffer)
110+ _, err := b.WriteString("test")
111+ if err != nil {
112+ panic(err)
113+ }
114+ }
115+ ` )
116+ ctx := pkg .CreateContext ("main.go" )
117+ result := map [string ]string {}
118+ visitor := testutils .NewMockVisitor ()
119+ visitor .Context = ctx
120+ visitor .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
121+ typeName , call , err := gosec .GetCallInfo (n , ctx )
122+ if err == nil {
123+ result [typeName ] = call
124+ }
125+ return true
126+ }
127+ ast .Walk (visitor , ctx .Root )
128+
129+ Expect (result ).Should (HaveKeyWithValue ("*bytes.Buffer" , "WriteString" ))
130+ })
131+
132+ It ("should return the type and call name for new selector expression" , func () {
133+ pkg := testutils .NewTestPackage ()
134+ defer pkg .Close ()
135+ pkg .AddFile ("main.go" , `
136+ package main
137+
138+ import(
139+ "bytes"
140+ )
141+
142+ func main() {
143+ _, err := new(bytes.Buffer).WriteString("test")
144+ if err != nil {
145+ panic(err)
146+ }
147+ }
148+ ` )
149+ ctx := pkg .CreateContext ("main.go" )
150+ result := map [string ]string {}
151+ visitor := testutils .NewMockVisitor ()
152+ visitor .Context = ctx
153+ visitor .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
154+ typeName , call , err := gosec .GetCallInfo (n , ctx )
155+ if err == nil {
156+ result [typeName ] = call
157+ }
158+ return true
159+ }
160+ ast .Walk (visitor , ctx .Root )
161+
162+ Expect (result ).Should (HaveKeyWithValue ("bytes.Buffer" , "WriteString" ))
163+ })
164+
165+ It ("should return the type and call name for function selector expression" , func () {
166+ pkg := testutils .NewTestPackage ()
167+ defer pkg .Close ()
168+ pkg .AddFile ("main.go" , `
169+ package main
170+
171+ import(
172+ "bytes"
173+ )
174+
175+ func createBuffer() *bytes.Buffer {
176+ return new(bytes.Buffer)
177+ }
178+
179+ func main() {
180+ _, err := createBuffer().WriteString("test")
181+ if err != nil {
182+ panic(err)
183+ }
184+ }
185+ ` )
186+ ctx := pkg .CreateContext ("main.go" )
187+ result := map [string ]string {}
188+ visitor := testutils .NewMockVisitor ()
189+ visitor .Context = ctx
190+ visitor .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
191+ typeName , call , err := gosec .GetCallInfo (n , ctx )
192+ if err == nil {
193+ result [typeName ] = call
194+ }
195+ return true
196+ }
197+ ast .Walk (visitor , ctx .Root )
198+
199+ Expect (result ).Should (HaveKeyWithValue ("*bytes.Buffer" , "WriteString" ))
200+ })
201+
202+ It ("should return the type and call name for package function" , func () {
203+ pkg := testutils .NewTestPackage ()
204+ defer pkg .Close ()
205+ pkg .AddFile ("main.go" , `
206+ package main
207+
208+ import(
209+ "fmt"
210+ )
211+
212+ func main() {
213+ fmt.Println("test")
214+ }
215+ ` )
216+ ctx := pkg .CreateContext ("main.go" )
217+ result := map [string ]string {}
218+ visitor := testutils .NewMockVisitor ()
219+ visitor .Context = ctx
220+ visitor .Callback = func (n ast.Node , ctx * gosec.Context ) bool {
221+ typeName , call , err := gosec .GetCallInfo (n , ctx )
222+ if err == nil {
223+ result [typeName ] = call
224+ }
225+ return true
226+ }
227+ ast .Walk (visitor , ctx .Root )
228+
229+ Expect (result ).Should (HaveKeyWithValue ("fmt" , "Println" ))
230+ })
231+ })
94232})
0 commit comments