-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
reloc_test.go
176 lines (155 loc) · 4.61 KB
/
reloc_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2018 Saferwall. All rights reserved.
// Use of this source code is governed by Apache v2 license
// license that can be found in the LICENSE file.
package pe
import (
"testing"
)
func TestParseRelocDirectoryData(t *testing.T) {
type TestRelocData struct {
imgBaseRelocation ImageBaseRelocation
relocEntriesCount int
relocDataIndex int
}
tests := []struct {
in string
out TestRelocData
}{
{
getAbsoluteFilePath("test/putty.exe"),
TestRelocData{
imgBaseRelocation: ImageBaseRelocation{
VirtualAddress: 0xd8000, SizeOfBlock: 0xc},
relocEntriesCount: 18,
relocDataIndex: 17,
},
},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
ops := Options{Fast: true}
file, err := New(tt.in, &ops)
if err != nil {
t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
}
err = file.Parse()
if err != nil {
t.Fatalf("Parse(%s) failed, reason: %v", tt.in, err)
}
var va, size uint32
switch file.Is64 {
case true:
oh64 := file.NtHeader.OptionalHeader.(ImageOptionalHeader64)
dirEntry := oh64.DataDirectory[ImageDirectoryEntryBaseReloc]
va = dirEntry.VirtualAddress
size = dirEntry.Size
case false:
oh32 := file.NtHeader.OptionalHeader.(ImageOptionalHeader32)
dirEntry := oh32.DataDirectory[ImageDirectoryEntryBaseReloc]
va = dirEntry.VirtualAddress
size = dirEntry.Size
}
err = file.parseRelocDirectory(va, size)
if err != nil {
t.Fatalf("parseRelocDirectory(%s) failed, reason: %v", tt.in, err)
}
relocs := file.Relocations
if len(relocs) != tt.out.relocEntriesCount {
t.Errorf("relocations entries count assertion failed, got %v, want %v",
len(relocs), tt.out.relocEntriesCount)
}
imgBaseRelocation := relocs[tt.out.relocDataIndex].Data
if imgBaseRelocation != tt.out.imgBaseRelocation {
t.Errorf("reloc data assertion failed, got %v, want %v",
imgBaseRelocation, tt.out.imgBaseRelocation)
}
})
}
}
func TestParseRelocDirectoryEntry(t *testing.T) {
type TestRelocEntry struct {
imgBaseRelocationEntry ImageBaseRelocationEntry
relocEntriesCount int
relocDataIndex int
relocEntryIndex int
relocTypeMeaning string
}
tests := []struct {
in string
out TestRelocEntry
}{
{
getAbsoluteFilePath("test/putty.exe"),
TestRelocEntry{
imgBaseRelocationEntry: ImageBaseRelocationEntry{
Data: 0xab00,
Offset: 0xb00,
Type: 0xa,
},
relocDataIndex: 0x1,
relocEntriesCount: 154,
relocEntryIndex: 17,
relocTypeMeaning: "DIR64",
},
},
{
getAbsoluteFilePath("test/arp.dll"),
TestRelocEntry{
imgBaseRelocationEntry: ImageBaseRelocationEntry{
Data: 0x8004,
Offset: 0x4,
Type: 0x8,
},
relocDataIndex: 3,
relocEntriesCount: 204,
relocEntryIndex: 1,
relocTypeMeaning: "RISC-V Low12s",
},
},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
ops := Options{Fast: true}
file, err := New(tt.in, &ops)
if err != nil {
t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
}
err = file.Parse()
if err != nil {
t.Fatalf("Parse(%s) failed, reason: %v", tt.in, err)
}
var va, size uint32
switch file.Is64 {
case true:
oh64 := file.NtHeader.OptionalHeader.(ImageOptionalHeader64)
dirEntry := oh64.DataDirectory[ImageDirectoryEntryBaseReloc]
va = dirEntry.VirtualAddress
size = dirEntry.Size
case false:
oh32 := file.NtHeader.OptionalHeader.(ImageOptionalHeader32)
dirEntry := oh32.DataDirectory[ImageDirectoryEntryBaseReloc]
va = dirEntry.VirtualAddress
size = dirEntry.Size
}
err = file.parseRelocDirectory(va, size)
if err != nil {
t.Fatalf("parseRelocDirectory(%s) failed, reason: %v", tt.in, err)
}
reloc := file.Relocations[tt.out.relocDataIndex]
if len(reloc.Entries) != tt.out.relocEntriesCount {
t.Errorf("relocations entries count assertion failed, got %v, want %v",
len(reloc.Entries), tt.out.relocEntriesCount)
}
relocEntry := reloc.Entries[tt.out.relocEntryIndex]
if relocEntry != tt.out.imgBaseRelocationEntry {
t.Errorf("reloc image base relocation entry assertion failed, got %v, want %v",
relocEntry, tt.out.imgBaseRelocationEntry)
}
relocType := relocEntry.Type.String(file)
if relocType != tt.out.relocTypeMeaning {
t.Errorf("pretty reloc type assertion failed, got %v, want %v", relocType,
tt.out.relocTypeMeaning)
}
})
}
}