Skip to content

Commit

Permalink
Refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Nov 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 2685f0f commit 493ddcd
Showing 15 changed files with 339 additions and 45 deletions.
26 changes: 26 additions & 0 deletions internal/callgraph/language/java/callgraph_test.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,32 @@ func TestRunCallGraphWithSetupMock(t *testing.T) {
assert.Nil(t, err)
}

func TestRunCallGraphWithSetupSootWrapperError(t *testing.T) {

cmdMock := testdata.NewEchoCmdFactory()
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{}
swMock := testdata.MockSootHandler{GetSootWrapperError: fmt.Errorf("")}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)

err := cg.RunCallGraphWithSetup()

assert.Error(t, err)
}

func TestRunCallGraphWithSetupSootVersionError(t *testing.T) {

cmdMock := testdata.CmdFactoryMock{JavaVersionErr: fmt.Errorf("version error")}
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{}
swMock := testdata.MockSootHandler{}
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)

err := cg.RunCallGraphWithSetup()

assert.Error(t, err)
}

func TestRunCallGraphMock(t *testing.T) {
cmdMock := testdata.NewEchoCmdFactory()
fsMock := ioTestData.FileSystemMock{}
12 changes: 12 additions & 0 deletions internal/callgraph/language/java/cmd_factory_test.go
Original file line number Diff line number Diff line change
@@ -52,3 +52,15 @@ func TestMakeBuildMavenCmd(t *testing.T) {
assert.Contains(t, args, "-q")
assert.Contains(t, args, "-DskipTests")
}

func TestMakeJavaVersionCmd(t *testing.T) {
jarPath := "jarpath"
ctx, _ := ctxTestdata.NewContextMock()
cmd, err := CmdFactory{}.MakeJavaVersionCmd(jarPath, ctx)

assert.NoError(t, err)
assert.NotNil(t, cmd)
args := cmd.Args
assert.Contains(t, args, "java")
assert.Contains(t, args, "--version")
}
Binary file added internal/callgraph/language/java/soot-wrapper.jar
Binary file not shown.
17 changes: 8 additions & 9 deletions internal/callgraph/language/java/soot_handler.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"

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

return err
}
fmt.Println("downloading compressed content...")
err = downloadCompressedSootWrapper(fs, zipFile, version)
if err != nil {

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

}

func (_ SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) {
if version != "11" && version != "17" && version != "21" {
func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) {
versionInt, err := strconv.Atoi(version)
if err != nil {
return "", fmt.Errorf("error when trying to convert java version string to int")
}
if versionInt < 11 {
return "", fmt.Errorf("lowest supported version for running callgraph generation is 11")
}
fmt.Println("java version: ", version)
debrickedDir := ".debricked"
if _, err := fs.Stat(debrickedDir); fs.IsNotExist(err) {
err := fs.Mkdir(debrickedDir, 0755)
if err != nil {
return "", err
}
}
fmt.Println("created .debricked directory...")
path, err := filepath.Abs(path.Join(debrickedDir, "soot-wrapper.jar"))
if err != nil {
return "", err
}
if _, err := fs.Stat(path); fs.IsNotExist(err) {
fmt.Println("jar does not exist, downloading...")
// Initialize or download if file does not already exists
if version == "21" {
if versionInt >= 21 {
return initializeSootWrapper(fs, debrickedDir)
}
return path, downloadSootWrapper(arc, fs, path, version)
97 changes: 92 additions & 5 deletions internal/callgraph/language/java/soot_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package java

import (
"fmt"
"testing"

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

func TestInitializeSootWrapperOpenEmbedError(t *testing.T) {
errString := "fs open embed error"
fsMock := ioTestData.FileSystemMock{FsOpenEmbedError: fmt.Errorf(errString)}
tempDir, err := fsMock.MkdirTemp(".tmp")
assert.NoError(t, err)
_, err = initializeSootWrapper(fsMock, tempDir)
assert.Error(t, err)
assert.Equal(t, err.Error(), errString)
}

func TestInitializeSootWrapperFsReadAllError(t *testing.T) {
errString := "fs read all error"
fsMock := ioTestData.FileSystemMock{FsReadAllError: fmt.Errorf(errString)}
tempDir, err := fsMock.MkdirTemp(".tmp")
assert.NoError(t, err)
_, err = initializeSootWrapper(fsMock, tempDir)
assert.Error(t, err)
assert.Equal(t, err.Error(), errString)
}

func TestInitializeSootWrapperFsWriteFileError(t *testing.T) {
errString := "fs write file error"
fsMock := ioTestData.FileSystemMock{FsWriteFileError: fmt.Errorf(errString)}
tempDir, err := fsMock.MkdirTemp(".tmp")
assert.NoError(t, err)
_, err = initializeSootWrapper(fsMock, tempDir)
assert.Error(t, err)
assert.Equal(t, err.Error(), errString)
}

func TestDownloadSootWrapper(t *testing.T) {
fs := ioFs.FileSystem{}
dir, _ := fs.MkdirTemp(".test_tmp")
zip := ioFs.Zip{}
arc := ioFs.NewArchiveWithStructs(dir, fs, zip)
err := downloadSootWrapper(arc, fs, "soot-wrapper.jar", "11")
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{}
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
assert.NoError(t, err, "expected no error for downloading soot-wrapper jar")
}

func TestDownloadSootWrapperMkdirTempError(t *testing.T) {
errString := "mkdir temp error"
fsMock := ioTestData.FileSystemMock{MkdirTempError: fmt.Errorf(errString)}
arcMock := ioTestData.ArchiveMock{}
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
assert.Error(t, err)
assert.Equal(t, err.Error(), errString)
}

func TestDownloadSootWrapperCreateError(t *testing.T) {
errString := "create error"
fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf(errString)}
arcMock := ioTestData.ArchiveMock{}
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
assert.Error(t, err)
assert.Equal(t, err.Error(), errString)
}

func TestDownloadSootWrapperUnzipError(t *testing.T) {
errString := "create error"
fsMock := ioTestData.FileSystemMock{}
arcMock := ioTestData.ArchiveMock{UnzipFileError: fmt.Errorf(errString)}
err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11")
assert.Error(t, err)
assert.Equal(t, err.Error(), errString)
}

func TestDownloadCompressedSootWrapper(t *testing.T) {
fs := ioFs.FileSystem{}
dir, err := fs.MkdirTemp(".test_tmp")
@@ -69,6 +125,11 @@ func TestGetSootWrapper(t *testing.T) {
version: "21",
expectError: false,
},
{
name: "Version not int",
version: "akjwdm",
expectError: true,
},
}

for _, tt := range tests {
@@ -85,3 +146,29 @@ func TestGetSootWrapper(t *testing.T) {
})
}
}

func TestGetSootWrapperDownload(t *testing.T) {
fsMock := ioTestData.FileSystemMock{StatError: fmt.Errorf(""), IsNotExistBool: true}
arcMock := ioTestData.ArchiveMock{}
sootHandler := SootHandler{}
_, err := sootHandler.GetSootWrapper("17", fsMock, arcMock)
assert.NoError(t, err)
}

func TestGetSootWrapperInitialize(t *testing.T) {
fsMock := ioTestData.FileSystemMock{StatError: fmt.Errorf(""), IsNotExistBool: true}
arcMock := ioTestData.ArchiveMock{}
sootHandler := SootHandler{}
_, err := sootHandler.GetSootWrapper("23", fsMock, arcMock)
assert.NoError(t, err)
}

func TestGetSootWrapperMkdirError(t *testing.T) {
errString := "mkdir error"
fsMock := ioTestData.FileSystemMock{MkdirError: fmt.Errorf(errString), StatError: fmt.Errorf(""), IsNotExistBool: true}
arcMock := ioTestData.ArchiveMock{}
sootHandler := SootHandler{}
_, err := sootHandler.GetSootWrapper("11", fsMock, arcMock)
assert.Error(t, err)
assert.Equal(t, err.Error(), errString)
}
8 changes: 5 additions & 3 deletions internal/callgraph/language/java/testdata/cmd_factory_mock.go
Original file line number Diff line number Diff line change
@@ -43,8 +43,10 @@ func (f CmdFactoryMock) MakeJavaVersionCmd(workingDirectory string, ctx cgexec.I
return exec.Command(f.JavaVersionName, "JavaVersion"), f.JavaVersionErr
}

type MockSootHandler struct{}
type MockSootHandler struct {
GetSootWrapperError error
}

func (_ MockSootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) {
return "", nil
func (msh MockSootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) {
return "", msh.GetSootWrapperError
}
21 changes: 7 additions & 14 deletions internal/io/archive.go
Original file line number Diff line number Diff line change
@@ -42,16 +42,14 @@ func (arc *Archive) ZipFile(sourcePath string, targetPath string, zippedName str

return err
}

zipFile, err := fs.Create(targetPath)
if err != nil {

return err
}
defer fs.CloseFile(zipFile)

zipWriter := zip.NewWriter(zipFile)
defer zip.Close(zipWriter)
defer zip.CloseWriter(zipWriter)

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

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

return err
}
defer arc.zip.CloseReader(r)

for _, file := range r.File {
fmt.Println(file.Name)
}

f := r.File[1]

f := r.File[1] //TODO: Change to first file and error-check for multiple once sootwrapper builds only one
fmt.Println("94")
outFile, err := arc.fs.Create(targetPath)
if err != nil {
return err
}
defer outFile.Close()
fmt.Println("file created: ", outFile.Name())
rc, err := f.Open()

rc, err := arc.zip.Open(f)
if err != nil {
return err
}
defer rc.Close()
fmt.Println("Copying over content...")
defer arc.zip.Close(rc)

_, err = arc.fs.Copy(outFile, rc)
return err
}
67 changes: 67 additions & 0 deletions internal/io/archive_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io

import (
"archive/zip"
"fmt"
"testing"

@@ -189,3 +190,69 @@ func TestCleanup(t *testing.T) {
err := a.Cleanup("testdir")
assert.Nil(t, err)
}

func TestUnzipFileReadFileError(t *testing.T) {
fsMock := ioTestData.FileSystemMock{}
zipMock := ioTestData.ZipMock{OpenReaderError: fmt.Errorf("%s", t.Name())}
a := Archive{
workingDirectory: ".",
fs: fsMock,
zip: zipMock,
}
err := a.UnzipFile("", "")
assert.Error(t, err)
assert.Equal(t, err.Error(), t.Name())
}

func TestUnzipFileCreateError(t *testing.T) {
reader := zip.Reader{
File: []*zip.File{nil, nil},
}
readCloser := zip.ReadCloser{Reader: reader}
fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf("%s", t.Name())}
zipMock := ioTestData.ZipMock{ReaderCloser: &readCloser}
a := Archive{
workingDirectory: ".",
fs: fsMock,
zip: zipMock,
}
err := a.UnzipFile("", "")
assert.Error(t, err)
assert.Equal(t, err.Error(), t.Name())
}

func TestUnzipFileOpenError(t *testing.T) {
reader := zip.Reader{
File: []*zip.File{nil, nil},
}
readCloser := zip.ReadCloser{Reader: reader}
fsMock := ioTestData.FileSystemMock{}
zipMock := ioTestData.ZipMock{ReaderCloser: &readCloser, OpenError: fmt.Errorf("%s", t.Name())}
a := Archive{
workingDirectory: ".",
fs: fsMock,
zip: zipMock,
}
err := a.UnzipFile("", "")
assert.Error(t, err)
assert.Equal(t, err.Error(), t.Name())
}

func TestUnzipFileCopyError(t *testing.T) {
r, err := zipStruct.OpenReader("testdata/text.zip")
r.File = append(r.File, r.File[0]) // Hack solution (:
assert.NoError(t, err)
defer zipStruct.CloseReader(r)

fsMock := ioTestData.FileSystemMock{CopyError: fmt.Errorf("%s", t.Name())}
zipMock := ioTestData.ZipMock{ReaderCloser: r}
a := Archive{
workingDirectory: ".",
fs: fsMock,
zip: zipMock,
}
fmt.Println(t.Name())
err = a.UnzipFile("", "")
assert.Error(t, err)
assert.Equal(t, err.Error(), t.Name())
}
Loading

0 comments on commit 493ddcd

Please sign in to comment.