Skip to content

Commit

Permalink
test: add more test for go part
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Jul 20, 2024
1 parent a5f314d commit 12532b0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"debug/gosym"
"debug/pe"
"fmt"
"maps"
"reflect"
Expand Down Expand Up @@ -173,3 +174,9 @@ func TestCollect(t *testing.T) {
assert.Contains(t, result, k)
}
}

func TestGetImageBase(t *testing.T) {
mockFile := new(pe.File)
mockFile.OptionalHeader = 42
assert.Panics(t, func() { GetImageBase(mockFile) })
}
38 changes: 38 additions & 0 deletions internal/wrapper/elf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package wrapper

import (
"debug/elf"
"encoding/binary"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGoArchReturnsExpectedArchitectures(t *testing.T) {
tests := []struct {
machine elf.Machine
expectedArch string
byteOrder binary.ByteOrder
}{
{elf.EM_386, "386", binary.LittleEndian},
{elf.EM_X86_64, "amd64", binary.LittleEndian},
{elf.EM_ARM, "arm", binary.LittleEndian},
{elf.EM_AARCH64, "arm64", binary.LittleEndian},
{elf.EM_PPC64, "ppc64", binary.BigEndian}, // Adjusted for big endian
{elf.EM_PPC64, "ppc64le", binary.LittleEndian}, // Explicitly little endian
{elf.EM_S390, "s390x", binary.BigEndian},
{0, "", binary.LittleEndian}, // Test for an unsupported machine type
}

for _, test := range tests {
mockFile := new(elf.File)
mockFile.FileHeader = elf.FileHeader{
Machine: test.machine,
ByteOrder: test.byteOrder,
}
wrapper := ElfWrapper{file: mockFile}

arch := wrapper.GoArch()
assert.Equal(t, test.expectedArch, arch)
}
}
28 changes: 28 additions & 0 deletions internal/wrapper/macho_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package wrapper

import (
"debug/macho"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGoArchReturnsCorrectArchitectureString(t *testing.T) {
tests := []struct {
cpu macho.Cpu
expected string
}{
{macho.Cpu386, "386"},
{macho.CpuAmd64, "amd64"},
{macho.CpuArm, "arm"},
{macho.CpuArm64, "arm64"},
{macho.CpuPpc64, "ppc64"},
{macho.Cpu(0), ""}, // Unsupported CPU type
}

for _, test := range tests {
m := MachoWrapper{file: &macho.File{FileHeader: macho.FileHeader{Cpu: test.cpu}}}
result := m.GoArch()
assert.Equal(t, test.expected, result)
}
}
32 changes: 32 additions & 0 deletions internal/wrapper/pe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package wrapper

import (
"debug/pe"
"testing"
)

func TestGoArchReturnsExpectedArchitectureString(t *testing.T) {
tests := []struct {
name string
machine uint16
expected string
}{
{"Returns386ForI386Machine", pe.IMAGE_FILE_MACHINE_I386, "386"},
{"ReturnsAmd64ForAmd64Machine", pe.IMAGE_FILE_MACHINE_AMD64, "amd64"},
{"ReturnsArmForArmMachine", pe.IMAGE_FILE_MACHINE_ARMNT, "arm"},
{"ReturnsArm64ForArm64Machine", pe.IMAGE_FILE_MACHINE_ARM64, "arm64"},
{"ReturnsEmptyStringForUnknownMachine", 0xFFFF, ""},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
mockFile := &pe.File{FileHeader: pe.FileHeader{Machine: test.machine}}
wrapper := PeWrapper{file: mockFile}

result := wrapper.GoArch()
if result != test.expected {
t.Errorf("Expected %s, got %s for machine type %v", test.expected, result, test.machine)
}
})
}
}
12 changes: 12 additions & 0 deletions internal/wrapper/wrapper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package wrapper

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewWrapper(t *testing.T) {
ret := NewWrapper(nil)
assert.Equal(t, nil, ret)

Check failure on line 11 in internal/wrapper/wrapper_test.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

nil-compare: use assert.Nil (testifylint)
}

0 comments on commit 12532b0

Please sign in to comment.