diff --git a/internal/callgraph/language/java/callgraph.go b/internal/callgraph/language/java/callgraph.go index 9f04bc6b..e6e0bcae 100644 --- a/internal/callgraph/language/java/callgraph.go +++ b/internal/callgraph/language/java/callgraph.go @@ -79,6 +79,7 @@ func (cg *Callgraph) javaVersion(path string) (string, error) { if len(match) > 1 { return match[1], nil } + return "", err } diff --git a/internal/callgraph/language/java/cmd_factory_test.go b/internal/callgraph/language/java/cmd_factory_test.go index ab9cc62e..89707d35 100644 --- a/internal/callgraph/language/java/cmd_factory_test.go +++ b/internal/callgraph/language/java/cmd_factory_test.go @@ -7,6 +7,8 @@ import ( "github.com/stretchr/testify/assert" ) +const jarPath = "jarpath" + func TestMakeMvnCopyDependenciesCmd(t *testing.T) { targetDir := "target" ctx, _ := ctxTestdata.NewContextMock() @@ -21,7 +23,6 @@ func TestMakeMvnCopyDependenciesCmd(t *testing.T) { } func TestMakeCallGraphGenerationCmd(t *testing.T) { - jarPath := "jarpath" targetClasses := []string{"targetclasses"} dependencyClasses := "dependencypath" outputName := ".outputName" @@ -40,7 +41,6 @@ func TestMakeCallGraphGenerationCmd(t *testing.T) { } func TestMakeBuildMavenCmd(t *testing.T) { - jarPath := "jarpath" ctx, _ := ctxTestdata.NewContextMock() cmd, err := CmdFactory{}.MakeBuildMavenCmd(jarPath, ctx) @@ -54,7 +54,6 @@ func TestMakeBuildMavenCmd(t *testing.T) { } func TestMakeJavaVersionCmd(t *testing.T) { - jarPath := "jarpath" ctx, _ := ctxTestdata.NewContextMock() cmd, err := CmdFactory{}.MakeJavaVersionCmd(jarPath, ctx) diff --git a/internal/callgraph/language/java/soot_handler.go b/internal/callgraph/language/java/soot_handler.go index b2aecc4a..942f24d0 100644 --- a/internal/callgraph/language/java/soot_handler.go +++ b/internal/callgraph/language/java/soot_handler.go @@ -52,13 +52,15 @@ func downloadSootWrapper(arc ioFs.IArchive, fs ioFs.IFileSystem, path string, ve return err } + zipPath := dir + "/soot_wrapper.zip" zipFile, err := fs.Create(zipPath) - defer zipFile.Close() if err != nil { return err } + defer zipFile.Close() + err = downloadCompressedSootWrapper(fs, zipFile, version) if err != nil { @@ -78,6 +80,7 @@ func downloadCompressedSootWrapper(fs ioFs.IFileSystem, zipFile *os.File, versio client := http.Client{ CheckRedirect: func(r *http.Request, via []*http.Request) error { r.URL.Opaque = r.URL.Path + return nil }, } @@ -89,8 +92,8 @@ func downloadCompressedSootWrapper(fs ioFs.IFileSystem, zipFile *os.File, versio defer resp.Body.Close() _, err = fs.Copy(zipFile, resp.Body) - return err + return err } func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc ioFs.IArchive) (string, error) { @@ -110,6 +113,7 @@ func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc io } path, err := filepath.Abs(path.Join(debrickedDir, "soot-wrapper.jar")) if err != nil { + return "", err } if _, err := fs.Stat(path); fs.IsNotExist(err) { @@ -121,6 +125,7 @@ func (sh SootHandler) GetSootWrapper(version string, fs ioFs.IFileSystem, arc io } else { version = "11" } // Handling correct jar to install + return path, downloadSootWrapper(arc, fs, path, version) } diff --git a/internal/callgraph/language/java/soot_handler_test.go b/internal/callgraph/language/java/soot_handler_test.go index 5af3801f..76f28c1c 100644 --- a/internal/callgraph/language/java/soot_handler_test.go +++ b/internal/callgraph/language/java/soot_handler_test.go @@ -21,7 +21,7 @@ func TestInitializeSootWrapper(t *testing.T) { func TestInitializeSootWrapperOpenEmbedError(t *testing.T) { errString := "fs open embed error" - fsMock := ioTestData.FileSystemMock{FsOpenEmbedError: fmt.Errorf(errString)} + fsMock := ioTestData.FileSystemMock{FsOpenEmbedError: fmt.Errorf(errString)} //nolint tempDir, err := fsMock.MkdirTemp(".tmp") assert.NoError(t, err) _, err = initializeSootWrapper(fsMock, tempDir) @@ -31,7 +31,7 @@ func TestInitializeSootWrapperOpenEmbedError(t *testing.T) { func TestInitializeSootWrapperFsReadAllError(t *testing.T) { errString := "fs read all error" - fsMock := ioTestData.FileSystemMock{FsReadAllError: fmt.Errorf(errString)} + fsMock := ioTestData.FileSystemMock{FsReadAllError: fmt.Errorf(errString)} //nolint tempDir, err := fsMock.MkdirTemp(".tmp") assert.NoError(t, err) _, err = initializeSootWrapper(fsMock, tempDir) @@ -41,7 +41,7 @@ func TestInitializeSootWrapperFsReadAllError(t *testing.T) { func TestInitializeSootWrapperFsWriteFileError(t *testing.T) { errString := "fs write file error" - fsMock := ioTestData.FileSystemMock{FsWriteFileError: fmt.Errorf(errString)} + fsMock := ioTestData.FileSystemMock{FsWriteFileError: fmt.Errorf(errString)} //nolint tempDir, err := fsMock.MkdirTemp(".tmp") assert.NoError(t, err) _, err = initializeSootWrapper(fsMock, tempDir) @@ -58,7 +58,7 @@ func TestDownloadSootWrapper(t *testing.T) { func TestDownloadSootWrapperMkdirTempError(t *testing.T) { errString := "mkdir temp error" - fsMock := ioTestData.FileSystemMock{MkdirTempError: fmt.Errorf(errString)} + fsMock := ioTestData.FileSystemMock{MkdirTempError: fmt.Errorf(errString)} //nolint arcMock := ioTestData.ArchiveMock{} err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11") assert.Error(t, err) @@ -67,7 +67,7 @@ func TestDownloadSootWrapperMkdirTempError(t *testing.T) { func TestDownloadSootWrapperCreateError(t *testing.T) { errString := "create error" - fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf(errString)} + fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf(errString)} //nolint arcMock := ioTestData.ArchiveMock{} err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11") assert.Error(t, err) @@ -77,7 +77,7 @@ func TestDownloadSootWrapperCreateError(t *testing.T) { func TestDownloadSootWrapperUnzipError(t *testing.T) { errString := "create error" fsMock := ioTestData.FileSystemMock{} - arcMock := ioTestData.ArchiveMock{UnzipFileError: fmt.Errorf(errString)} + arcMock := ioTestData.ArchiveMock{UnzipFileError: fmt.Errorf(errString)} //nolint err := downloadSootWrapper(arcMock, fsMock, "soot-wrapper.jar", "11") assert.Error(t, err) assert.Equal(t, err.Error(), errString) @@ -165,7 +165,7 @@ func TestGetSootWrapperInitialize(t *testing.T) { func TestGetSootWrapperMkdirError(t *testing.T) { errString := "mkdir error" - fsMock := ioTestData.FileSystemMock{MkdirError: fmt.Errorf(errString), StatError: fmt.Errorf(""), IsNotExistBool: true} + fsMock := ioTestData.FileSystemMock{MkdirError: fmt.Errorf(errString), StatError: fmt.Errorf(""), IsNotExistBool: true} //nolint arcMock := ioTestData.ArchiveMock{} sootHandler := SootHandler{} _, err := sootHandler.GetSootWrapper("11", fsMock, arcMock) diff --git a/internal/io/archive.go b/internal/io/archive.go index f2d5524f..fa6a902a 100644 --- a/internal/io/archive.go +++ b/internal/io/archive.go @@ -2,7 +2,6 @@ package io import ( "encoding/base64" - "fmt" "path" ) @@ -87,23 +86,23 @@ func (arc *Archive) UnzipFile(sourcePath string, targetPath string) error { return err } - defer arc.zip.CloseReader(r) + defer arc.zip.CloseReader(r) //nolint 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() + defer outFile.Close() //nolint rc, err := arc.zip.Open(f) if err != nil { return err } - defer arc.zip.Close(rc) + defer arc.zip.Close(rc) //nolint _, err = arc.fs.Copy(outFile, rc) + return err } diff --git a/internal/io/archive_test.go b/internal/io/archive_test.go index c614f2b9..9adff9d3 100644 --- a/internal/io/archive_test.go +++ b/internal/io/archive_test.go @@ -208,7 +208,7 @@ func TestUnzipFileCreateError(t *testing.T) { reader := zip.Reader{ File: []*zip.File{nil, nil}, } - readCloser := zip.ReadCloser{Reader: reader} + readCloser := zip.ReadCloser{Reader: reader} //nolint fsMock := ioTestData.FileSystemMock{CreateError: fmt.Errorf("%s", t.Name())} zipMock := ioTestData.ZipMock{ReaderCloser: &readCloser} a := Archive{ @@ -225,7 +225,7 @@ func TestUnzipFileOpenError(t *testing.T) { reader := zip.Reader{ File: []*zip.File{nil, nil}, } - readCloser := zip.ReadCloser{Reader: reader} + readCloser := zip.ReadCloser{Reader: reader} //nolint fsMock := ioTestData.FileSystemMock{} zipMock := ioTestData.ZipMock{ReaderCloser: &readCloser, OpenError: fmt.Errorf("%s", t.Name())} a := Archive{ @@ -242,7 +242,7 @@ 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) + defer zipStruct.CloseReader(r) //nolint fsMock := ioTestData.FileSystemMock{CopyError: fmt.Errorf("%s", t.Name())} zipMock := ioTestData.ZipMock{ReaderCloser: r} diff --git a/internal/io/zip_test.go b/internal/io/zip_test.go index 2c51305a..85af601d 100644 --- a/internal/io/zip_test.go +++ b/internal/io/zip_test.go @@ -12,7 +12,7 @@ const fileNameZip = "debricked-test.zip" func TestNewWriter(t *testing.T) { testFile, err := filesystem.Create(fileNameZip) - defer deleteFile(t, testFile) + defer deleteFile(t, testFile) //nolint assert.NoError(t, err) writer := zipStruct.NewWriter(testFile) @@ -26,7 +26,7 @@ func TestFileInfoHeader(t *testing.T) { assert.NoError(t, err) writer := zipStruct.NewWriter(testFile) - defer zipStruct.CloseWriter(writer) + defer zipStruct.CloseWriter(writer) //nolint info, _ := filesystem.StatFile(testFile) _, err = zipStruct.FileInfoHeader(info) @@ -36,11 +36,11 @@ func TestFileInfoHeader(t *testing.T) { func TestCreateHeader(t *testing.T) { testFile, err := filesystem.Create(fileNameZip) - defer deleteFile(t, testFile) + defer deleteFile(t, testFile) //nolint assert.NoError(t, err) writer := zipStruct.NewWriter(testFile) - defer zipStruct.CloseWriter(writer) + defer zipStruct.CloseWriter(writer) //nolint info, _ := filesystem.StatFile(testFile) header, _ := zipStruct.FileInfoHeader(info) @@ -57,7 +57,7 @@ func TestDeflate(t *testing.T) { func TestOpenZip(t *testing.T) { r, err := zipStruct.OpenReader("testdata/text.zip") assert.NoError(t, err) - defer zipStruct.CloseReader(r) + defer zipStruct.CloseReader(r) //nolint assert.NotNil(t, r, "reader not opened properly") _, err = zipStruct.Open(r.File[0]) @@ -67,7 +67,7 @@ func TestOpenZip(t *testing.T) { func TestCloseZip(t *testing.T) { r, err := zipStruct.OpenReader("testdata/text.zip") assert.NoError(t, err) - defer zipStruct.CloseReader(r) + defer zipStruct.CloseReader(r) //nolint assert.NotNil(t, r, "reader not opened properly") rc, err := zipStruct.Open(r.File[0])