This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_test.go
120 lines (111 loc) · 2.61 KB
/
file_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package pecoff
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"sync"
"testing"
"github.com/RIscRIpt/pecoff/binutil"
"github.com/RIscRIpt/pecoff/windef"
)
const (
testDir string = "test_files/"
shortTestMaxSize int64 = 16 * 1024
)
const (
fnDelim = "_"
fnNrDetailType = 0
fnNrDetailBits = 1
fnNrDetailCompiler = 2
fnNrDetailComment = 3
)
func parseFile(t *testing.T, filename string) *File {
rawFile, err := os.Open(testDir + filename)
if err != nil {
t.Errorf("Failed to open test file `%s`", filename)
return nil
}
defer rawFile.Close()
file := Explore(binutil.WrapReaderAt(rawFile))
err = file.ReadAll()
if err != nil {
if fe, ok := err.(*FileError); ok {
err = fe.ToMultiError()
}
t.Errorf("Error occured while parsing file `%s`: %v", filename, err)
return nil
}
file.Seal()
return file
}
func getAllTestFiles(t *testing.T) []os.FileInfo {
files, err := ioutil.ReadDir(testDir)
if err != nil {
t.Fatalf("ioutil.ReadDir failed: %s", err.Error())
}
// filter-out contents of test directory, leave only appropriate files
filtered := files[:0]
for _, file := range files {
if file.IsDir() {
continue
}
if testing.Short() && file.Size() >= shortTestMaxSize {
continue
}
filtered = append(filtered, file)
}
return filtered
}
func TestParseAllParallel(t *testing.T) {
var wg sync.WaitGroup
for _, file := range getAllTestFiles(t) {
wg.Add(1)
go func(filename string) {
defer wg.Done()
parseFile(t, filename)
}(file.Name())
}
wg.Wait()
}
func TestBitness(t *testing.T) {
var wg sync.WaitGroup
for _, file := range getAllTestFiles(t) {
wg.Add(1)
go func(file os.FileInfo) {
defer wg.Done()
filename := file.Name()
fnDetails := strings.Split(filename, fnDelim)
bits, err := strconv.Atoi(fnDetails[fnNrDetailBits])
if err != nil {
t.Errorf("Failed to read file bitness from its name (%s): %s", fnDetails[fnNrDetailBits], err.Error())
return
}
parsedFile := parseFile(t, filename)
if parsedFile.is64Bit() {
if bits != 64 {
t.Errorf("File bitness doesn't match. Expected %d, got %d (%s)", bits, 64, filename)
return
}
} else { //must be 32bit
if bits != 32 {
t.Errorf("File bitness doesn't match. Expected %d, got %d (%s)", bits, 32, filename)
return
}
}
}(file)
}
wg.Wait()
}
func Example_MachineType() {
file, _ := os.Open(testDir + "exe_32_fasm+1-71-39_aslr")
defer file.Close()
pe := Explore(binutil.WrapReaderAt(file))
pe.ReadDosHeader()
pe.ReadFileHeader()
pe.Seal()
fmt.Println(windef.MAP_IMAGE_FILE_MACHINE[pe.FileHeader.Machine])
// Output:
// I386
}