Skip to content

Commit 778c944

Browse files
committed
Refactoring and tests
1 parent 74ec70d commit 778c944

File tree

15 files changed

+344
-45
lines changed

15 files changed

+344
-45
lines changed

internal/callgraph/language/java/callgraph_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,32 @@ func TestRunCallGraphWithSetupMock(t *testing.T) {
2222
assert.Nil(t, err)
2323
}
2424

25+
func TestRunCallGraphWithSetupSootWrapperError(t *testing.T) {
26+
27+
cmdMock := testdata.NewEchoCmdFactory()
28+
fsMock := ioTestData.FileSystemMock{}
29+
arcMock := ioTestData.ArchiveMock{}
30+
swMock := testdata.MockSootHandler{GetSootWrapperError: fmt.Errorf("")}
31+
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)
32+
33+
err := cg.RunCallGraphWithSetup()
34+
35+
assert.Error(t, err)
36+
}
37+
38+
func TestRunCallGraphWithSetupSootVersionError(t *testing.T) {
39+
40+
cmdMock := testdata.CmdFactoryMock{JavaVersionErr: fmt.Errorf("version error")}
41+
fsMock := ioTestData.FileSystemMock{}
42+
arcMock := ioTestData.ArchiveMock{}
43+
swMock := testdata.MockSootHandler{}
44+
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)
45+
46+
err := cg.RunCallGraphWithSetup()
47+
48+
assert.Error(t, err)
49+
}
50+
2551
func TestRunCallGraphMock(t *testing.T) {
2652
cmdMock := testdata.NewEchoCmdFactory()
2753
fsMock := ioTestData.FileSystemMock{}

internal/callgraph/language/java/cmd_factory_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,15 @@ func TestMakeBuildMavenCmd(t *testing.T) {
5252
assert.Contains(t, args, "-q")
5353
assert.Contains(t, args, "-DskipTests")
5454
}
55+
56+
func TestMakeJavaVersionCmd(t *testing.T) {
57+
jarPath := "jarpath"
58+
ctx, _ := ctxTestdata.NewContextMock()
59+
cmd, err := CmdFactory{}.MakeJavaVersionCmd(jarPath, ctx)
60+
61+
assert.NoError(t, err)
62+
assert.NotNil(t, cmd)
63+
args := cmd.Args
64+
assert.Contains(t, args, "java")
65+
assert.Contains(t, args, "--version")
66+
}
18.1 MB
Binary file not shown.

internal/callgraph/language/java/soot_handler.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path"
99
"path/filepath"
10+
"strconv"
1011
"strings"
1112

1213
ioFs "github.com/debricked/cli/internal/io"
@@ -53,13 +54,11 @@ func downloadSootWrapper(arc ioFs.IArchive, fs ioFs.IFileSystem, path string, ve
5354
}
5455
zipPath := dir + "/soot_wrapper.zip"
5556
zipFile, err := fs.Create(zipPath)
56-
fmt.Println("created zip file...")
5757
defer zipFile.Close()
5858
if err != nil {
5959

6060
return err
6161
}
62-
fmt.Println("downloading compressed content...")
6362
err = downloadCompressedSootWrapper(fs, zipFile, version)
6463
if err != nil {
6564

@@ -94,29 +93,34 @@ func downloadCompressedSootWrapper(fs ioFs.IFileSystem, zipFile *os.File, versio
9493

9594
}
9695

97-
func (_ SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) {
98-
if version != "11" && version != "17" && version != "21" {
96+
func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) {
97+
versionInt, err := strconv.Atoi(version)
98+
if err != nil {
99+
return "", fmt.Errorf("error when trying to convert java version string to int")
100+
}
101+
if versionInt < 11 {
99102
return "", fmt.Errorf("lowest supported version for running callgraph generation is 11")
100103
}
101-
fmt.Println("java version: ", version)
102104
debrickedDir := ".debricked"
103105
if _, err := fs.Stat(debrickedDir); fs.IsNotExist(err) {
104106
err := fs.Mkdir(debrickedDir, 0755)
105107
if err != nil {
106108
return "", err
107109
}
108110
}
109-
fmt.Println("created .debricked directory...")
110111
path, err := filepath.Abs(path.Join(debrickedDir, "soot-wrapper.jar"))
111112
if err != nil {
112113
return "", err
113114
}
114115
if _, err := fs.Stat(path); fs.IsNotExist(err) {
115-
fmt.Println("jar does not exist, downloading...")
116-
// Initialize or download if file does not already exists
117-
if version == "21" {
116+
if versionInt >= 21 {
118117
return initializeSootWrapper(fs, debrickedDir)
119118
}
119+
if versionInt >= 17 {
120+
version = "17"
121+
} else {
122+
version = "11"
123+
} // Handling correct jar to install
120124
return path, downloadSootWrapper(arc, fs, path, version)
121125
}
122126

internal/callgraph/language/java/soot_handler_test.go

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package java
22

33
import (
4+
"fmt"
45
"testing"
56

67
ioFs "github.com/debricked/cli/internal/io"
@@ -18,15 +19,70 @@ func TestInitializeSootWrapper(t *testing.T) {
1819
assert.NoError(t, err)
1920
}
2021

22+
func TestInitializeSootWrapperOpenEmbedError(t *testing.T) {
23+
errString := "fs open embed error"
24+
fsMock := ioTestData.FileSystemMock{FsOpenEmbedError: fmt.Errorf(errString)}
25+
tempDir, err := fsMock.MkdirTemp(".tmp")
26+
assert.NoError(t, err)
27+
_, err = initializeSootWrapper(fsMock, tempDir)
28+
assert.Error(t, err)
29+
assert.Equal(t, err.Error(), errString)
30+
}
31+
32+
func TestInitializeSootWrapperFsReadAllError(t *testing.T) {
33+
errString := "fs read all error"
34+
fsMock := ioTestData.FileSystemMock{FsReadAllError: fmt.Errorf(errString)}
35+
tempDir, err := fsMock.MkdirTemp(".tmp")
36+
assert.NoError(t, err)
37+
_, err = initializeSootWrapper(fsMock, tempDir)
38+
assert.Error(t, err)
39+
assert.Equal(t, err.Error(), errString)
40+
}
41+
42+
func TestInitializeSootWrapperFsWriteFileError(t *testing.T) {
43+
errString := "fs write file error"
44+
fsMock := ioTestData.FileSystemMock{FsWriteFileError: fmt.Errorf(errString)}
45+
tempDir, err := fsMock.MkdirTemp(".tmp")
46+
assert.NoError(t, err)
47+
_, err = initializeSootWrapper(fsMock, tempDir)
48+
assert.Error(t, err)
49+
assert.Equal(t, err.Error(), errString)
50+
}
51+
2152
func TestDownloadSootWrapper(t *testing.T) {
22-
fs := ioFs.FileSystem{}
23-
dir, _ := fs.MkdirTemp(".test_tmp")
24-
zip := ioFs.Zip{}
25-
arc := ioFs.NewArchiveWithStructs(dir, fs, zip)
26-
err := downloadSootWrapper(arc, fs, "soot-wrapper.jar", "11")
53+
fsMock := ioTestData.FileSystemMock{}
54+
arcMock := ioTestData.ArchiveMock{}
55+
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
2756
assert.NoError(t, err, "expected no error for downloading soot-wrapper jar")
2857
}
2958

59+
func TestDownloadSootWrapperMkdirTempError(t *testing.T) {
60+
errString := "mkdir temp error"
61+
fsMock := ioTestData.FileSystemMock{MkdirTempError: fmt.Errorf(errString)}
62+
arcMock := ioTestData.ArchiveMock{}
63+
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
64+
assert.Error(t, err)
65+
assert.Equal(t, err.Error(), errString)
66+
}
67+
68+
func TestDownloadSootWrapperCreateError(t *testing.T) {
69+
errString := "create error"
70+
fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf(errString)}
71+
arcMock := ioTestData.ArchiveMock{}
72+
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
73+
assert.Error(t, err)
74+
assert.Equal(t, err.Error(), errString)
75+
}
76+
77+
func TestDownloadSootWrapperUnzipError(t *testing.T) {
78+
errString := "create error"
79+
fsMock := ioTestData.FileSystemMock{}
80+
arcMock := ioTestData.ArchiveMock{UnzipFileError: fmt.Errorf(errString)}
81+
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
82+
assert.Error(t, err)
83+
assert.Equal(t, err.Error(), errString)
84+
}
85+
3086
func TestDownloadCompressedSootWrapper(t *testing.T) {
3187
fs := ioFs.FileSystem{}
3288
dir, err := fs.MkdirTemp(".test_tmp")
@@ -69,6 +125,11 @@ func TestGetSootWrapper(t *testing.T) {
69125
version: "21",
70126
expectError: false,
71127
},
128+
{
129+
name: "Version not int",
130+
version: "akjwdm",
131+
expectError: true,
132+
},
72133
}
73134

74135
for _, tt := range tests {
@@ -85,3 +146,29 @@ func TestGetSootWrapper(t *testing.T) {
85146
})
86147
}
87148
}
149+
150+
func TestGetSootWrapperDownload(t *testing.T) {
151+
fsMock := ioTestData.FileSystemMock{StatError: fmt.Errorf(""), IsNotExistBool: true}
152+
arcMock := ioTestData.ArchiveMock{}
153+
sootHandler := SootHandler{}
154+
_, err := sootHandler.GetSootWrapper("17", fsMock, arcMock)
155+
assert.NoError(t, err)
156+
}
157+
158+
func TestGetSootWrapperInitialize(t *testing.T) {
159+
fsMock := ioTestData.FileSystemMock{StatError: fmt.Errorf(""), IsNotExistBool: true}
160+
arcMock := ioTestData.ArchiveMock{}
161+
sootHandler := SootHandler{}
162+
_, err := sootHandler.GetSootWrapper("23", fsMock, arcMock)
163+
assert.NoError(t, err)
164+
}
165+
166+
func TestGetSootWrapperMkdirError(t *testing.T) {
167+
errString := "mkdir error"
168+
fsMock := ioTestData.FileSystemMock{MkdirError: fmt.Errorf(errString), StatError: fmt.Errorf(""), IsNotExistBool: true}
169+
arcMock := ioTestData.ArchiveMock{}
170+
sootHandler := SootHandler{}
171+
_, err := sootHandler.GetSootWrapper("11", fsMock, arcMock)
172+
assert.Error(t, err)
173+
assert.Equal(t, err.Error(), errString)
174+
}

internal/callgraph/language/java/testdata/cmd_factory_mock.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ func (f CmdFactoryMock) MakeJavaVersionCmd(workingDirectory string, ctx cgexec.I
4343
return exec.Command(f.JavaVersionName, "JavaVersion"), f.JavaVersionErr
4444
}
4545

46-
type MockSootHandler struct{}
46+
type MockSootHandler struct {
47+
GetSootWrapperError error
48+
}
4749

48-
func (_ MockSootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) {
49-
return "", nil
50+
func (msh MockSootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) {
51+
return "", msh.GetSootWrapperError
5052
}

internal/io/archive.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ func (arc *Archive) ZipFile(sourcePath string, targetPath string, zippedName str
4242

4343
return err
4444
}
45-
4645
zipFile, err := fs.Create(targetPath)
4746
if err != nil {
4847

4948
return err
5049
}
5150
defer fs.CloseFile(zipFile)
52-
5351
zipWriter := zip.NewWriter(zipFile)
54-
defer zip.Close(zipWriter)
52+
defer zip.CloseWriter(zipWriter)
5553

5654
info, err := fs.StatFile(zipFile)
5755
if err != nil {
@@ -84,32 +82,27 @@ func (arc *Archive) ZipFile(sourcePath string, targetPath string, zippedName str
8482
}
8583

8684
func (arc *Archive) UnzipFile(sourcePath string, targetPath string) error {
87-
// Unzip a file, or the first file if multiple in zip archive
8885
r, err := arc.zip.OpenReader(sourcePath)
8986
if err != nil {
9087

9188
return err
9289
}
9390
defer arc.zip.CloseReader(r)
9491

95-
for _, file := range r.File {
96-
fmt.Println(file.Name)
97-
}
98-
99-
f := r.File[1]
100-
92+
f := r.File[1] //TODO: Change to first file and error-check for multiple once sootwrapper builds only one
93+
fmt.Println("94")
10194
outFile, err := arc.fs.Create(targetPath)
10295
if err != nil {
10396
return err
10497
}
10598
defer outFile.Close()
106-
fmt.Println("file created: ", outFile.Name())
107-
rc, err := f.Open()
99+
100+
rc, err := arc.zip.Open(f)
108101
if err != nil {
109102
return err
110103
}
111-
defer rc.Close()
112-
fmt.Println("Copying over content...")
104+
defer arc.zip.Close(rc)
105+
113106
_, err = arc.fs.Copy(outFile, rc)
114107
return err
115108
}

internal/io/archive_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io
22

33
import (
4+
"archive/zip"
45
"fmt"
56
"testing"
67

@@ -189,3 +190,69 @@ func TestCleanup(t *testing.T) {
189190
err := a.Cleanup("testdir")
190191
assert.Nil(t, err)
191192
}
193+
194+
func TestUnzipFileReadFileError(t *testing.T) {
195+
fsMock := ioTestData.FileSystemMock{}
196+
zipMock := ioTestData.ZipMock{OpenReaderError: fmt.Errorf("%s", t.Name())}
197+
a := Archive{
198+
workingDirectory: ".",
199+
fs: fsMock,
200+
zip: zipMock,
201+
}
202+
err := a.UnzipFile("", "")
203+
assert.Error(t, err)
204+
assert.Equal(t, err.Error(), t.Name())
205+
}
206+
207+
func TestUnzipFileCreateError(t *testing.T) {
208+
reader := zip.Reader{
209+
File: []*zip.File{nil, nil},
210+
}
211+
readCloser := zip.ReadCloser{Reader: reader}
212+
fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf("%s", t.Name())}
213+
zipMock := ioTestData.ZipMock{ReaderCloser: &readCloser}
214+
a := Archive{
215+
workingDirectory: ".",
216+
fs: fsMock,
217+
zip: zipMock,
218+
}
219+
err := a.UnzipFile("", "")
220+
assert.Error(t, err)
221+
assert.Equal(t, err.Error(), t.Name())
222+
}
223+
224+
func TestUnzipFileOpenError(t *testing.T) {
225+
reader := zip.Reader{
226+
File: []*zip.File{nil, nil},
227+
}
228+
readCloser := zip.ReadCloser{Reader: reader}
229+
fsMock := ioTestData.FileSystemMock{}
230+
zipMock := ioTestData.ZipMock{ReaderCloser: &readCloser, OpenError: fmt.Errorf("%s", t.Name())}
231+
a := Archive{
232+
workingDirectory: ".",
233+
fs: fsMock,
234+
zip: zipMock,
235+
}
236+
err := a.UnzipFile("", "")
237+
assert.Error(t, err)
238+
assert.Equal(t, err.Error(), t.Name())
239+
}
240+
241+
func TestUnzipFileCopyError(t *testing.T) {
242+
r, err := zipStruct.OpenReader("testdata/text.zip")
243+
r.File = append(r.File, r.File[0]) // Hack solution (:
244+
assert.NoError(t, err)
245+
defer zipStruct.CloseReader(r)
246+
247+
fsMock := ioTestData.FileSystemMock{CopyError: fmt.Errorf("%s", t.Name())}
248+
zipMock := ioTestData.ZipMock{ReaderCloser: r}
249+
a := Archive{
250+
workingDirectory: ".",
251+
fs: fsMock,
252+
zip: zipMock,
253+
}
254+
fmt.Println(t.Name())
255+
err = a.UnzipFile("", "")
256+
assert.Error(t, err)
257+
assert.Equal(t, err.Error(), t.Name())
258+
}

0 commit comments

Comments
 (0)