Skip to content

Commit

Permalink
add debug information for testing
Browse files Browse the repository at this point in the history
Signed-off-by: Rumen Vasilev <[email protected]>
  • Loading branch information
rumenvasilev committed Nov 11, 2023
1 parent 1439fbd commit e9c402c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ all: pretty build
build: clean
@GOOS=$(target_os) GOARCH=$(target_arch) go build -mod vendor -o ./bin/$(pkg)-$(target_os)

linux:
@GOOS=linux GOARCH=$(target_arch) go build -mod vendor -o ./bin/$(pkg)-linux

windows:
@GOOS=windows GOARCH=$(target_arch) go build -mod vendor -o ./bin/$(pkg)-windows.exe

## Clean binaries
clean:
@rm -rf ./bin
Expand Down
3 changes: 2 additions & 1 deletion internal/core/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ func isIgnoredFile(cfgScanTests bool, cfgMaxFileSize int64, fullFilePath string,
}

// Check if it is a binary file
if util.IsBinaryFile(fullFilePath) {
yes, err = util.IsBinaryFile(fullFilePath)
if yes || err != nil {
return true, "is a binary file, ignoring"
}

Expand Down
17 changes: 12 additions & 5 deletions internal/util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,31 @@ func GetSignatureFiles(dir string) ([]string, error) {
return sigs, nil
}

func IsBinaryFile(path string) bool {
func IsBinaryFile(path string) (bool, error) {
buf := make([]byte, 512)

f, err := os.Open(path)
if err != nil {
// couldn't open file, exit with false
return false
return false, fmt.Errorf("couldn't open file %q", path)
}
defer f.Close()

_, err = f.Read(buf)
if err != nil {
// couldn't read file, exit with false
return false
return false, fmt.Errorf("couldn't read file %q", path)
}

// https://groups.google.com/g/golang-nuts/c/YeLL7L7SwWs/m/LGlsc9GIJlUJ
// if the encoding is invalid, it returns (RuneError, 1)
runerr, res := utf8.DecodeRune(buf)
return (res == 1 && runerr == utf8.RuneError)
runerr, p := utf8.DecodeRune(buf)
// DEBUG
fmt.Println(runerr, p)
if runerr == utf8.RuneError {
if p == 0 || p == 1 {
return true, nil
}
}
return false, nil
}
28 changes: 23 additions & 5 deletions internal/util/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,29 @@ func TestFileExists(t *testing.T) {
}

func Test_IsBinaryFile(t *testing.T) {
assert.False(t, IsBinaryFile("./strings.go"))
bin := findBinary(t)
require.NotEmpty(t, bin, "Make sure you've run `make build` before running this test. It relies on the binary being present in bin directory.")
assert.True(t, IsBinaryFile(bin), fmt.Sprintf("Evaluated binary file: %q", bin))
assert.False(t, IsBinaryFile("some/unexisting/file"))
tests := []struct {
name string
input string
want bool
wantErr string
}{
{"strings.go", "./strings.go", false, ""},
{"compiled binary", findBinary(t), true, ""},
{"non-existing file", "some/unexisting/file", false, "couldn't open file \"some/unexisting/file\""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.NotEmpty(t, tt.input, "Make sure you've run `make build` before running this test. It relies on the binary being present in bin directory.")
got, err := IsBinaryFile(tt.input)
if tt.wantErr != "" {
assert.Error(t, err)
assert.EqualError(t, err, tt.wantErr)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}

// finds the pre-built binary under the name <root>/bin/rvsecret*
Expand Down

0 comments on commit e9c402c

Please sign in to comment.