1// Copyright 2009 The Go Authors.  All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package pe
6
7import (
8	"reflect"
9	"testing"
10)
11
12type fileTest struct {
13	file     string
14	hdr      FileHeader
15	sections []*SectionHeader
16	symbols  []*Symbol
17}
18
19var fileTests = []fileTest{
20	{
21		"testdata/gcc-386-mingw-obj",
22		FileHeader{0x014c, 0x000c, 0x0, 0x64a, 0x1e, 0x0, 0x104},
23		[]*SectionHeader{
24			{".text", 0, 0, 36, 500, 1440, 0, 3, 0, 0x60300020},
25			{".data", 0, 0, 0, 0, 0, 0, 0, 0, 3224371264},
26			{".bss", 0, 0, 0, 0, 0, 0, 0, 0, 3224371328},
27			{".debug_abbrev", 0, 0, 137, 536, 0, 0, 0, 0, 0x42100000},
28			{".debug_info", 0, 0, 418, 673, 1470, 0, 7, 0, 1108344832},
29			{".debug_line", 0, 0, 128, 1091, 1540, 0, 1, 0, 1108344832},
30			{".rdata", 0, 0, 16, 1219, 0, 0, 0, 0, 1076887616},
31			{".debug_frame", 0, 0, 52, 1235, 1550, 0, 2, 0, 1110441984},
32			{".debug_loc", 0, 0, 56, 1287, 0, 0, 0, 0, 1108344832},
33			{".debug_pubnames", 0, 0, 27, 1343, 1570, 0, 1, 0, 1108344832},
34			{".debug_pubtypes", 0, 0, 38, 1370, 1580, 0, 1, 0, 1108344832},
35			{".debug_aranges", 0, 0, 32, 1408, 1590, 0, 2, 0, 1108344832},
36		},
37		[]*Symbol{
38			{".file", 0x0, -2, 0x0, 0x67},
39			{"_main", 0x0, 1, 0x20, 0x2},
40			{".text", 0x0, 1, 0x0, 0x3},
41			{".data", 0x0, 2, 0x0, 0x3},
42			{".bss", 0x0, 3, 0x0, 0x3},
43			{".debug_abbrev", 0x0, 4, 0x0, 0x3},
44			{".debug_info", 0x0, 5, 0x0, 0x3},
45			{".debug_line", 0x0, 6, 0x0, 0x3},
46			{".rdata", 0x0, 7, 0x0, 0x3},
47			{".debug_frame", 0x0, 8, 0x0, 0x3},
48			{".debug_loc", 0x0, 9, 0x0, 0x3},
49			{".debug_pubnames", 0x0, 10, 0x0, 0x3},
50			{".debug_pubtypes", 0x0, 11, 0x0, 0x3},
51			{".debug_aranges", 0x0, 12, 0x0, 0x3},
52			{"___main", 0x0, 0, 0x20, 0x2},
53			{"_puts", 0x0, 0, 0x20, 0x2},
54		},
55	},
56	{
57		"testdata/gcc-386-mingw-exec",
58		FileHeader{0x014c, 0x000f, 0x4c6a1b60, 0x3c00, 0x282, 0xe0, 0x107},
59		[]*SectionHeader{
60			{Name: ".text", VirtualSize: 0xcd8, VirtualAddress: 0x1000, Size: 0xe00, Offset: 0x400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x60500060},
61			{Name: ".data", VirtualSize: 0x10, VirtualAddress: 0x2000, Size: 0x200, Offset: 0x1200, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0300040},
62			{Name: ".rdata", VirtualSize: 0x120, VirtualAddress: 0x3000, Size: 0x200, Offset: 0x1400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x40300040},
63			{Name: ".bss", VirtualSize: 0xdc, VirtualAddress: 0x4000, Size: 0x0, Offset: 0x0, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0400080},
64			{Name: ".idata", VirtualSize: 0x3c8, VirtualAddress: 0x5000, Size: 0x400, Offset: 0x1600, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0300040},
65			{Name: ".CRT", VirtualSize: 0x18, VirtualAddress: 0x6000, Size: 0x200, Offset: 0x1a00, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0300040},
66			{Name: ".tls", VirtualSize: 0x20, VirtualAddress: 0x7000, Size: 0x200, Offset: 0x1c00, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0xc0300040},
67			{Name: ".debug_aranges", VirtualSize: 0x20, VirtualAddress: 0x8000, Size: 0x200, Offset: 0x1e00, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
68			{Name: ".debug_pubnames", VirtualSize: 0x51, VirtualAddress: 0x9000, Size: 0x200, Offset: 0x2000, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
69			{Name: ".debug_pubtypes", VirtualSize: 0x91, VirtualAddress: 0xa000, Size: 0x200, Offset: 0x2200, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
70			{Name: ".debug_info", VirtualSize: 0xe22, VirtualAddress: 0xb000, Size: 0x1000, Offset: 0x2400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
71			{Name: ".debug_abbrev", VirtualSize: 0x157, VirtualAddress: 0xc000, Size: 0x200, Offset: 0x3400, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
72			{Name: ".debug_line", VirtualSize: 0x144, VirtualAddress: 0xd000, Size: 0x200, Offset: 0x3600, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
73			{Name: ".debug_frame", VirtualSize: 0x34, VirtualAddress: 0xe000, Size: 0x200, Offset: 0x3800, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42300000},
74			{Name: ".debug_loc", VirtualSize: 0x38, VirtualAddress: 0xf000, Size: 0x200, Offset: 0x3a00, PointerToRelocations: 0x0, PointerToLineNumbers: 0x0, NumberOfRelocations: 0x0, NumberOfLineNumbers: 0x0, Characteristics: 0x42100000},
75		},
76		[]*Symbol{},
77	},
78}
79
80func TestOpen(t *testing.T) {
81	for i := range fileTests {
82		tt := &fileTests[i]
83
84		f, err := Open(tt.file)
85		if err != nil {
86			t.Error(err)
87			continue
88		}
89		if !reflect.DeepEqual(f.FileHeader, tt.hdr) {
90			t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.FileHeader, tt.hdr)
91			continue
92		}
93
94		for i, sh := range f.Sections {
95			if i >= len(tt.sections) {
96				break
97			}
98			have := &sh.SectionHeader
99			want := tt.sections[i]
100			if !reflect.DeepEqual(have, want) {
101				t.Errorf("open %s, section %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want)
102			}
103		}
104		tn := len(tt.sections)
105		fn := len(f.Sections)
106		if tn != fn {
107			t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn)
108		}
109		for i, have := range f.Symbols {
110			if i >= len(tt.symbols) {
111				break
112			}
113			want := tt.symbols[i]
114			if !reflect.DeepEqual(have, want) {
115				t.Errorf("open %s, symbol %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want)
116			}
117		}
118	}
119}
120
121func TestOpenFailure(t *testing.T) {
122	filename := "file.go"    // not a PE file
123	_, err := Open(filename) // don't crash
124	if err == nil {
125		t.Errorf("open %s: succeeded unexpectedly", filename)
126	}
127}
128