Skip to content

Commit b504783

Browse files
MVrachevccojocar
authored andcommitted
Change unit tests to check for one thing (#381)
The unit tests should check for a single thing at a time. This was not true for some the tests. Signed-off-by: Martin Vrachev <[email protected]>
1 parent 7dbc65b commit b504783

File tree

1 file changed

+109
-22
lines changed

1 file changed

+109
-22
lines changed

testutils/source.go

Lines changed: 109 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -788,36 +788,123 @@ func main() {
788788
// SampleCodeG301 - mkdir permission check
789789
SampleCodeG301 = []CodeSample{{[]string{`
790790
package main
791-
import "os"
791+
792+
import (
793+
"fmt"
794+
"os"
795+
)
796+
797+
func main() {
798+
err := os.Mkdir("/tmp/mydir", 0777)
799+
if err != nil {
800+
fmt.Println("Error when creating a directory!")
801+
return
802+
}
803+
}`}, 1, gosec.NewConfig()}, {[]string{`
804+
package main
805+
806+
import (
807+
"fmt"
808+
"os"
809+
)
810+
792811
func main() {
793-
os.Mkdir("/tmp/mydir", 0777)
794-
os.Mkdir("/tmp/mydir", 0600)
795-
os.MkdirAll("/tmp/mydir/mysubidr", 0775)
796-
}`}, 2, gosec.NewConfig()}}
812+
err := os.MkdirAll("/tmp/mydir", 0777)
813+
if err != nil {
814+
fmt.Println("Error when creating a directory!")
815+
return
816+
}
817+
}`}, 1, gosec.NewConfig()}, {[]string{`
818+
package main
819+
820+
import (
821+
"fmt"
822+
"os"
823+
)
824+
825+
func main() {
826+
err := os.Mkdir("/tmp/mydir", 0600)
827+
if err != nil {
828+
fmt.Println("Error when creating a directory!")
829+
return
830+
}
831+
}`}, 0, gosec.NewConfig()}}
797832

798833
// SampleCodeG302 - file create / chmod permissions check
799834
SampleCodeG302 = []CodeSample{{[]string{`
800835
package main
801-
import "os"
836+
837+
import (
838+
"fmt"
839+
"os"
840+
)
841+
802842
func main() {
803-
os.Chmod("/tmp/somefile", 0777)
804-
os.Chmod("/tmp/someotherfile", 0600)
805-
os.OpenFile("/tmp/thing", os.O_CREATE|os.O_WRONLY, 0666)
806-
os.OpenFile("/tmp/thing", os.O_CREATE|os.O_WRONLY, 0600)
807-
}`}, 2, gosec.NewConfig()}}
843+
err := os.Chmod("/tmp/somefile", 0777)
844+
if err != nil {
845+
fmt.Println("Error when changing file permissions!")
846+
return
847+
}
848+
}`}, 1, gosec.NewConfig()}, {[]string{`
849+
package main
850+
851+
import (
852+
"fmt"
853+
"os"
854+
)
855+
856+
func main() {
857+
_, err := os.OpenFile("/tmp/thing", os.O_CREATE|os.O_WRONLY, 0666)
858+
if err != nil {
859+
fmt.Println("Error opening a file!")
860+
return
861+
}
862+
}`}, 1, gosec.NewConfig()}, {[]string{`
863+
package main
864+
865+
import (
866+
"fmt"
867+
"os"
868+
)
869+
870+
func main() {
871+
err := os.Chmod("/tmp/mydir", 0400)
872+
if err != nil {
873+
fmt.Println("Error")
874+
return
875+
}
876+
}`}, 0, gosec.NewConfig()}, {[]string{`
877+
package main
878+
879+
import (
880+
"fmt"
881+
"os"
882+
)
883+
884+
func main() {
885+
_, err := os.OpenFile("/tmp/thing", os.O_CREATE|os.O_WRONLY, 0600)
886+
if err != nil {
887+
fmt.Println("Error opening a file!")
888+
return
889+
}
890+
}
891+
`}, 0, gosec.NewConfig()}}
808892

809893
// SampleCodeG303 - bad tempfile permissions & hardcoded shared path
810894
SampleCodeG303 = []CodeSample{{[]string{`
811895
package samples
896+
812897
import (
898+
"fmt"
813899
"io/ioutil"
814-
"os"
815900
)
901+
816902
func main() {
817-
file1, _ := os.Create("/tmp/demo1")
818-
defer file1.Close()
819-
ioutil.WriteFile("/tmp/demo2", []byte("This is some data"), 0644)
820-
}`}, 2, gosec.NewConfig()}}
903+
err := ioutil.WriteFile("/tmp/demo2", []byte("This is some data"), 0644)
904+
if err != nil {
905+
fmt.Println("Error while writing!")
906+
}
907+
}`}, 1, gosec.NewConfig()}}
821908

822909
// SampleCodeG304 - potential file inclusion vulnerability
823910
SampleCodeG304 = []CodeSample{{[]string{`
@@ -828,12 +915,12 @@ import (
828915
"log"
829916
)
830917
func main() {
831-
f := os.Getenv("tainted_file")
832-
body, err := ioutil.ReadFile(f)
833-
if err != nil {
834-
log.Printf("Error: %v\n", err)
835-
}
836-
log.Print(body)
918+
f := os.Getenv("tainted_file")
919+
body, err := ioutil.ReadFile(f)
920+
if err != nil {
921+
log.Printf("Error: %v\n", err)
922+
}
923+
log.Print(body)
837924
838925
}`}, 1, gosec.NewConfig()}, {[]string{`
839926
package main

0 commit comments

Comments
 (0)