-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
resource_test.go
263 lines (231 loc) · 5.72 KB
/
resource_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// 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 (
"reflect"
"testing"
)
type TestRsrcDir struct {
Level1ImgRsrcDir ImageResourceDirectory
Level2Index int
Level2ImgRsrcDir ImageResourceDirectory
Level3Index int
Level3ImgRsrcDir ImageResourceDirectory
Level3RsrcDirEntry ResourceDirectoryEntry
Level4Index int
Level4RsrcDirEntry ResourceDirectoryEntry
}
func TestParseResourceDirectory(t *testing.T) {
tests := []struct {
in string
out TestRsrcDir
}{
{
getAbsoluteFilePath("test/putty.exe"),
TestRsrcDir{
Level1ImgRsrcDir: ImageResourceDirectory{
Characteristics: 0x0,
TimeDateStamp: 0x0,
MajorVersion: 0x0,
MinorVersion: 0x0,
NumberOfNamedEntries: 0x0,
NumberOfIDEntries: 0x6,
},
Level2Index: 0x3,
Level2ImgRsrcDir: ImageResourceDirectory{
Characteristics: 0x0,
TimeDateStamp: 0x0,
MajorVersion: 0x0,
MinorVersion: 0x0,
NumberOfNamedEntries: 0x0,
NumberOfIDEntries: 0x1,
},
Level3Index: 0x0,
Level3ImgRsrcDir: ImageResourceDirectory{
Characteristics: 0x0,
TimeDateStamp: 0x0,
MajorVersion: 0x0,
MinorVersion: 0x0,
NumberOfNamedEntries: 0x0,
NumberOfIDEntries: 0x1,
},
Level4Index: 0x0,
Level4RsrcDirEntry: ResourceDirectoryEntry{
Struct: ImageResourceDirectoryEntry{
Name: 0x409,
OffsetToData: 0x460,
},
Name: "",
ID: 0x409,
IsResourceDir: false,
Data: ResourceDataEntry{
Lang: 0x9,
SubLang: 0x1,
Struct: ImageResourceDataEntry{
OffsetToData: 0x124838,
Size: 0x324,
CodePage: 0x0,
Reserved: 0x0,
},
},
},
},
},
}
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
if file.Is64 {
oh64 := file.NtHeader.OptionalHeader.(ImageOptionalHeader64)
dirEntry := oh64.DataDirectory[ImageDirectoryEntryResource]
va = dirEntry.VirtualAddress
size = dirEntry.Size
} else {
oh32 := file.NtHeader.OptionalHeader.(ImageOptionalHeader32)
dirEntry := oh32.DataDirectory[ImageDirectoryEntryResource]
va = dirEntry.VirtualAddress
size = dirEntry.Size
}
err = file.parseResourceDirectory(va, size)
if err != nil {
t.Fatalf("parseResourceDirectory(%s) failed, reason: %v", tt.in, err)
}
rsrc := file.Resources
if rsrc.Struct != tt.out.Level1ImgRsrcDir {
t.Fatalf("level 1 resource directory assertion failed, got %v, want %v",
rsrc.Struct, tt.out.Level1ImgRsrcDir)
}
rsrcDirLevel2 := rsrc.Entries[tt.out.Level2Index].Directory
if rsrcDirLevel2.Struct != tt.out.Level2ImgRsrcDir {
t.Fatalf("level 2 resource directory assertion failed, got %v, want %v",
rsrc.Struct, tt.out.Level2ImgRsrcDir)
}
rsrcDirLevel3 := rsrcDirLevel2.Entries[tt.out.Level3Index].Directory
if rsrcDirLevel3.Struct != tt.out.Level3ImgRsrcDir {
t.Fatalf("level 3 resource directory assertion failed, got %v, want %v",
rsrc.Struct, tt.out.Level3ImgRsrcDir)
}
rsrcDirEntry := rsrcDirLevel3.Entries[tt.out.Level4Index]
if !reflect.DeepEqual(rsrcDirEntry, tt.out.Level4RsrcDirEntry) {
t.Fatalf("level 3 resource directory entry assertion failed, got %v, want %v",
rsrc.Struct, tt.out.Level3ImgRsrcDir)
}
})
}
}
func TestResourceTypeString(t *testing.T) {
tests := []struct {
in ResourceType
out string
}{
{
RTCursor,
"Cursor",
},
{
ResourceType(0xff),
"?",
},
}
for _, tt := range tests {
t.Run(tt.out, func(t *testing.T) {
rsrcTypeString := tt.in.String()
if rsrcTypeString != tt.out {
t.Fatalf("resource type string conversion failed, got %v, want %v",
rsrcTypeString, tt.out)
}
})
}
}
func TestResourceLangString(t *testing.T) {
tests := []struct {
in ResourceLang
out string
}{
{
LangArabic,
"Arabic (ar)",
},
{
ResourceLang(0xffff),
"?",
},
}
for _, tt := range tests {
t.Run(tt.out, func(t *testing.T) {
rsrcLangString := tt.in.String()
if rsrcLangString != tt.out {
t.Fatalf("resource language string conversion failed, got %v, want %v",
rsrcLangString, tt.out)
}
})
}
}
func TestResourceSubLangString(t *testing.T) {
tests := []struct {
in ResourceSubLang
out string
}{
{
SubLangArabicMorocco,
"Arabic Morocco (ar-MA)",
},
{
ResourceSubLang(0xffff),
"?",
},
}
for _, tt := range tests {
t.Run(tt.out, func(t *testing.T) {
rsrcSubLangString := tt.in.String()
if rsrcSubLangString != tt.out {
t.Fatalf("resource sub-language string conversion failed, got %v, want %v",
rsrcSubLangString, tt.out)
}
})
}
}
func TestPrettyResourceLang(t *testing.T) {
type resourceLang struct {
lang ResourceLang
subLang int
}
tests := []struct {
in resourceLang
out string
}{
{
resourceLang{
lang: LangEnglish,
subLang: 0x1,
},
"English United States (en-US)",
},
{
resourceLang{
lang: ResourceLang(0xff),
subLang: 0x1,
},
"?",
},
}
for _, tt := range tests {
t.Run(tt.out, func(t *testing.T) {
prettyRsrcLang := PrettyResourceLang(tt.in.lang, tt.in.subLang)
if prettyRsrcLang != tt.out {
t.Fatalf("pretty resource language failed, got %v, want %v",
prettyRsrcLang, tt.out)
}
})
}
}