Skip to content

Commit

Permalink
tests: change sample dir to git submodule (#40)
Browse files Browse the repository at this point in the history
* tests: change sample dir to git submodule

This commit remove the text/samples directory and add the git submodule
to horusec-examples repository. This commit also update the tests to
read the files from text/examples/samples instead just text/samples.

Co-authored-by: Matheus Alcantara [email protected]
Signed-off-by: Matheus Alcantara <[email protected]>

* Chore: Upgrade examples to latest version and fix unit tests

Signed-off-by: wiliansilvazup <[email protected]>
Co-authored-by: Matheus Alcantara <[email protected]>

Co-authored-by: wilian <[email protected]>
  • Loading branch information
matheusalcantarazup and wiliansilvazup authored Sep 22, 2021
1 parent 482b18f commit fae395d
Show file tree
Hide file tree
Showing 23 changed files with 32 additions and 51,143 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: true
- uses: actions/setup-go@v2
- name: coverage
run: make coverage
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: true
- uses: actions/setup-go@v2
- name: test
run: make test
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "text/examples"]
path = text/examples
url = https://github.com/ZupIT/horusec-examples-vulnerabilities
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ exclude-rules:
run:
skip-dirs:
- vendor/
- examples/
- text/examples/
- tmp
- e2e/
skip-files:
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ GO_IMPORTS ?= goimports
GO_IMPORTS_LOCAL ?= github.com/ZupIT/horusec-engine
HORUSEC ?= horusec
ADDLICENSE ?= addlicense
GO_LIST_TO_TEST ?= $$(go list ./... | grep -v /text/examples/)

fmt:
$(GOFMT) -w $(GO_FILES)
Expand All @@ -22,7 +23,8 @@ coverage:
curl -fsSL https://raw.githubusercontent.com/ZupIT/horusec-devkit/main/scripts/coverage.sh | bash -s 66.3 .

test:
$(GO) clean -testcache && $(GO) test -v ./... -timeout=2m -parallel=1 -failfast -short
$(GO) clean -testcache
$(GO) test -v $(GO_LIST_TO_TEST) -race -timeout=5m -parallel=1 -failfast -short

fix-imports:
ifeq (, $(shell which $(GO_IMPORTS)))
Expand Down
1 change: 1 addition & 0 deletions text/examples
Submodule examples added at 1b969f
19 changes: 8 additions & 11 deletions text/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func TestNameFormattingAndDisplaying(t *testing.T) {
}

func TestReadAndCreateTextFileWithELFFile(t *testing.T) {
textFile, err := ReadAndCreateTextFile(filepath.Join("samples", "test.elf"))
textFile, err := ReadAndCreateTextFile(filepath.Join("examples", "elf", "example1", "test.elf"))

if err != nil {
t.Fatal(err)
Expand All @@ -323,7 +323,7 @@ func TestReadAndCreateTextFileWithELFFile(t *testing.T) {
}

func TestReadAndCreateTextFileWithPEFile(t *testing.T) {
textFile, err := ReadAndCreateTextFile(filepath.Join("samples", "test.pe"))
textFile, err := ReadAndCreateTextFile(filepath.Join("examples", "pe", "example1", "test.pe"))

if err != nil {
t.Fatal(err)
Expand All @@ -340,7 +340,7 @@ func TestReadAndCreateTextFileWithMachOFile(t *testing.T) {
t.Log("TODO: Skip Mach-O files")
t.Skip()

textFile, err := ReadAndCreateTextFile(filepath.Join("samples", "test.macho"))
textFile, err := ReadAndCreateTextFile(filepath.Join("examples", "pe", "example1", "test.macho"))

if err != nil {
t.Fatal(err)
Expand All @@ -355,27 +355,24 @@ func TestReadAndCreateTextFileWithMachOFile(t *testing.T) {

func TestTextFiles_GetAllFilesUnits(t *testing.T) {
t.Run("Should return unit with nine files when get any files", func(t *testing.T) {
path := "./samples"
path, err := filepath.Abs(path)
path, err := filepath.Abs("examples")
assert.NoError(t, err)
textUnit, err := LoadDirIntoSingleUnit(path, []string{"**"})
assert.NoError(t, err)
assert.Equal(t, 13, len(textUnit.Files))
assert.Equal(t, 237, len(textUnit.Files))
})
t.Run("Should return multi unit with 4 textFiles and max of 3 files per textFile when get any files", func(t *testing.T) {
path := "./samples"
path, err := filepath.Abs(path)
path, err := filepath.Abs("examples")
assert.NoError(t, err)
textUnit, err := LoadDirIntoMultiUnit(path, 3, []string{"**"})
assert.NoError(t, err)
assert.Equal(t, 5, len(textUnit))
assert.Equal(t, 80, len(textUnit))
for _, item := range textUnit {
assert.LessOrEqual(t, len(item.Files), 3)
}
})
t.Run("Should return unit with tree files when get go files", func(t *testing.T) {
path := "./samples"
path, err := filepath.Abs(path)
path, err := filepath.Abs("examples")
assert.NoError(t, err)
textUnit, err := LoadDirIntoSingleUnit(path, []string{".perf"})
assert.NoError(t, err)
Expand Down
16 changes: 8 additions & 8 deletions text/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import (
)

func TestReadTextFileControlWithUTF8(t *testing.T) {
expectedBytes, err := ioutil.ReadFile(filepath.Join("samples", "PetsController.utf8.cs"))
expectedBytes, err := ioutil.ReadFile(filepath.Join("examples", "csharp", "example2", "PetsController.utf8.cs"))
if err != nil {
t.Fatal(err)
}

bytesReadFromUTF8File, err := ReadTextFileUnix(filepath.Join("samples", "PetsController.utf8.cs"))
bytesReadFromUTF8File, err := ReadTextFileUnix(filepath.Join("examples", "csharp", "example2", "PetsController.utf8.cs"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -39,12 +39,12 @@ func TestReadTextFileControlWithUTF8(t *testing.T) {
}

func TestReadTextFileControlWithUTF8WithBOM(t *testing.T) {
expectedBytes, err := ioutil.ReadFile(filepath.Join("samples", "PetsController.utf8.cs"))
expectedBytes, err := ioutil.ReadFile(filepath.Join("examples", "csharp", "example2", "PetsController.utf8.cs"))
if err != nil {
t.Fatal(err)
}

bytesReadFromUTF8File, err := ReadTextFileUnix(filepath.Join("samples", "PetsController.utf8bom.cs"))
bytesReadFromUTF8File, err := ReadTextFileUnix(filepath.Join("examples", "csharp", "example2", "PetsController.utf8bom.cs"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -55,12 +55,12 @@ func TestReadTextFileControlWithUTF8WithBOM(t *testing.T) {
}

func TestReadTextFileWithUTF16LEWithBOM(t *testing.T) {
expectedBytes, err := ioutil.ReadFile(filepath.Join("samples", "PetsController.utf8.cs"))
expectedBytes, err := ioutil.ReadFile(filepath.Join("examples", "csharp", "example2", "PetsController.utf8.cs"))
if err != nil {
t.Fatal(err)
}

bytesReadFromUTF16File, err := ReadTextFileUnix(filepath.Join("samples", "PetsController.utf16lebom.cs"))
bytesReadFromUTF16File, err := ReadTextFileUnix(filepath.Join("examples", "csharp", "example2", "PetsController.utf16lebom.cs"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -71,12 +71,12 @@ func TestReadTextFileWithUTF16LEWithBOM(t *testing.T) {
}

func TestReadTextFileWithUTF16BEWithBOM(t *testing.T) {
expectedBytes, err := ioutil.ReadFile(filepath.Join("samples", "PetsController.utf8.cs"))
expectedBytes, err := ioutil.ReadFile(filepath.Join("examples", "csharp", "example2", "PetsController.utf8.cs"))
if err != nil {
t.Fatal(err)
}

bytesReadFromUTF16File, err := ReadTextFileUnix(filepath.Join("samples", "PetsController.utf16bebom.cs"))
bytesReadFromUTF16File, err := ReadTextFileUnix(filepath.Join("examples", "csharp", "example2", "PetsController.utf16bebom.cs"))
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions text/reader_win_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import (
)

func TestWinReadTextFileShouldFailWithUTF16LEWithoutBOM(t *testing.T) {
_, err := ReadTextFileWin(filepath.Join("samples", "PetsController.utf16le.cs"))
_, err := ReadTextFileWin(filepath.Join("examples", "csharp", "example2", "PetsController.utf16le.cs"))

if err == nil {
t.Fatalf("Should have returned error for files encoded with UTF16 LE without BOM")
}
}

func TestWinReadTextFileShouldFailWithUTF16BEWithoutBOM(t *testing.T) {
_, err := ReadTextFileWin(filepath.Join("samples", "PetsController.utf16be.cs"))
_, err := ReadTextFileWin(filepath.Join("examples", "csharp", "example2", "PetsController.utf16be.cs"))

if err == nil {
t.Fatalf("Should have returned error for files encoded with UTF16 BE without BOM")
Expand Down
Binary file removed text/samples/PetsController.utf16be.cs
Binary file not shown.
Binary file removed text/samples/PetsController.utf16bebom.cs
Binary file not shown.
Binary file removed text/samples/PetsController.utf16le.cs
Binary file not shown.
Binary file removed text/samples/PetsController.utf16lebom.cs
Binary file not shown.
66 changes: 0 additions & 66 deletions text/samples/PetsController.utf8.cs

This file was deleted.

66 changes: 0 additions & 66 deletions text/samples/PetsController.utf8bom.cs

This file was deleted.

Loading

0 comments on commit fae395d

Please sign in to comment.