github.com/hongwozai/go-src-1.4.3@v0.0.0-20191127132709-dc3fce3dbccb/src/debug/pe/file_test.go (about)

     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  
     5  package pe
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  )
    11  
    12  type fileTest struct {
    13  	file     string
    14  	hdr      FileHeader
    15  	opthdr   interface{}
    16  	sections []*SectionHeader
    17  	symbols  []*Symbol
    18  }
    19  
    20  var fileTests = []fileTest{
    21  	{
    22  		"testdata/gcc-386-mingw-obj",
    23  		FileHeader{0x014c, 0x000c, 0x0, 0x64a, 0x1e, 0x0, 0x104},
    24  		nil,
    25  		[]*SectionHeader{
    26  			{".text", 0, 0, 36, 500, 1440, 0, 3, 0, 0x60300020},
    27  			{".data", 0, 0, 0, 0, 0, 0, 0, 0, 3224371264},
    28  			{".bss", 0, 0, 0, 0, 0, 0, 0, 0, 3224371328},
    29  			{".debug_abbrev", 0, 0, 137, 536, 0, 0, 0, 0, 0x42100000},
    30  			{".debug_info", 0, 0, 418, 673, 1470, 0, 7, 0, 1108344832},
    31  			{".debug_line", 0, 0, 128, 1091, 1540, 0, 1, 0, 1108344832},
    32  			{".rdata", 0, 0, 16, 1219, 0, 0, 0, 0, 1076887616},
    33  			{".debug_frame", 0, 0, 52, 1235, 1550, 0, 2, 0, 1110441984},
    34  			{".debug_loc", 0, 0, 56, 1287, 0, 0, 0, 0, 1108344832},
    35  			{".debug_pubnames", 0, 0, 27, 1343, 1570, 0, 1, 0, 1108344832},
    36  			{".debug_pubtypes", 0, 0, 38, 1370, 1580, 0, 1, 0, 1108344832},
    37  			{".debug_aranges", 0, 0, 32, 1408, 1590, 0, 2, 0, 1108344832},
    38  		},
    39  		[]*Symbol{
    40  			{".file", 0x0, -2, 0x0, 0x67},
    41  			{"_main", 0x0, 1, 0x20, 0x2},
    42  			{".text", 0x0, 1, 0x0, 0x3},
    43  			{".data", 0x0, 2, 0x0, 0x3},
    44  			{".bss", 0x0, 3, 0x0, 0x3},
    45  			{".debug_abbrev", 0x0, 4, 0x0, 0x3},
    46  			{".debug_info", 0x0, 5, 0x0, 0x3},
    47  			{".debug_line", 0x0, 6, 0x0, 0x3},
    48  			{".rdata", 0x0, 7, 0x0, 0x3},
    49  			{".debug_frame", 0x0, 8, 0x0, 0x3},
    50  			{".debug_loc", 0x0, 9, 0x0, 0x3},
    51  			{".debug_pubnames", 0x0, 10, 0x0, 0x3},
    52  			{".debug_pubtypes", 0x0, 11, 0x0, 0x3},
    53  			{".debug_aranges", 0x0, 12, 0x0, 0x3},
    54  			{"___main", 0x0, 0, 0x20, 0x2},
    55  			{"_puts", 0x0, 0, 0x20, 0x2},
    56  		},
    57  	},
    58  	{
    59  		"testdata/gcc-386-mingw-exec",
    60  		FileHeader{0x014c, 0x000f, 0x4c6a1b60, 0x3c00, 0x282, 0xe0, 0x107},
    61  		&OptionalHeader32{
    62  			0x10b, 0x2, 0x38, 0xe00, 0x1a00, 0x200, 0x1160, 0x1000, 0x2000, 0x400000, 0x1000, 0x200, 0x4, 0x0, 0x1, 0x0, 0x4, 0x0, 0x0, 0x10000, 0x400, 0x14abb, 0x3, 0x0, 0x200000, 0x1000, 0x100000, 0x1000, 0x0, 0x10,
    63  			[16]DataDirectory{
    64  				{0x0, 0x0},
    65  				{0x5000, 0x3c8},
    66  				{0x0, 0x0},
    67  				{0x0, 0x0},
    68  				{0x0, 0x0},
    69  				{0x0, 0x0},
    70  				{0x0, 0x0},
    71  				{0x0, 0x0},
    72  				{0x0, 0x0},
    73  				{0x7000, 0x18},
    74  				{0x0, 0x0},
    75  				{0x0, 0x0},
    76  				{0x0, 0x0},
    77  				{0x0, 0x0},
    78  				{0x0, 0x0},
    79  				{0x0, 0x0},
    80  			},
    81  		},
    82  		[]*SectionHeader{
    83  			{".text", 0xcd8, 0x1000, 0xe00, 0x400, 0x0, 0x0, 0x0, 0x0, 0x60500060},
    84  			{".data", 0x10, 0x2000, 0x200, 0x1200, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
    85  			{".rdata", 0x120, 0x3000, 0x200, 0x1400, 0x0, 0x0, 0x0, 0x0, 0x40300040},
    86  			{".bss", 0xdc, 0x4000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0400080},
    87  			{".idata", 0x3c8, 0x5000, 0x400, 0x1600, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
    88  			{".CRT", 0x18, 0x6000, 0x200, 0x1a00, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
    89  			{".tls", 0x20, 0x7000, 0x200, 0x1c00, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
    90  			{".debug_aranges", 0x20, 0x8000, 0x200, 0x1e00, 0x0, 0x0, 0x0, 0x0, 0x42100000},
    91  			{".debug_pubnames", 0x51, 0x9000, 0x200, 0x2000, 0x0, 0x0, 0x0, 0x0, 0x42100000},
    92  			{".debug_pubtypes", 0x91, 0xa000, 0x200, 0x2200, 0x0, 0x0, 0x0, 0x0, 0x42100000},
    93  			{".debug_info", 0xe22, 0xb000, 0x1000, 0x2400, 0x0, 0x0, 0x0, 0x0, 0x42100000},
    94  			{".debug_abbrev", 0x157, 0xc000, 0x200, 0x3400, 0x0, 0x0, 0x0, 0x0, 0x42100000},
    95  			{".debug_line", 0x144, 0xd000, 0x200, 0x3600, 0x0, 0x0, 0x0, 0x0, 0x42100000},
    96  			{".debug_frame", 0x34, 0xe000, 0x200, 0x3800, 0x0, 0x0, 0x0, 0x0, 0x42300000},
    97  			{".debug_loc", 0x38, 0xf000, 0x200, 0x3a00, 0x0, 0x0, 0x0, 0x0, 0x42100000},
    98  		},
    99  		[]*Symbol{},
   100  	},
   101  	{
   102  		"testdata/gcc-amd64-mingw-obj",
   103  		FileHeader{0x8664, 0x6, 0x0, 0x198, 0x12, 0x0, 0x4},
   104  		nil,
   105  		[]*SectionHeader{
   106  			{".text", 0x0, 0x0, 0x30, 0x104, 0x15c, 0x0, 0x3, 0x0, 0x60500020},
   107  			{".data", 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0500040},
   108  			{".bss", 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0500080},
   109  			{".rdata", 0x0, 0x0, 0x10, 0x134, 0x0, 0x0, 0x0, 0x0, 0x40500040},
   110  			{".xdata", 0x0, 0x0, 0xc, 0x144, 0x0, 0x0, 0x0, 0x0, 0x40300040},
   111  			{".pdata", 0x0, 0x0, 0xc, 0x150, 0x17a, 0x0, 0x3, 0x0, 0x40300040},
   112  		},
   113  		[]*Symbol{
   114  			{".file", 0x0, -2, 0x0, 0x67},
   115  			{"main", 0x0, 1, 0x20, 0x2},
   116  			{".text", 0x0, 1, 0x0, 0x3},
   117  			{".data", 0x0, 2, 0x0, 0x3},
   118  			{".bss", 0x0, 3, 0x0, 0x3},
   119  			{".rdata", 0x0, 4, 0x0, 0x3},
   120  			{".xdata", 0x0, 5, 0x0, 0x3},
   121  			{".pdata", 0x0, 6, 0x0, 0x3},
   122  			{"__main", 0x0, 0, 0x20, 0x2},
   123  			{"puts", 0x0, 0, 0x20, 0x2},
   124  		},
   125  	},
   126  	{
   127  		"testdata/gcc-amd64-mingw-exec",
   128  		FileHeader{0x8664, 0x11, 0x53e4364f, 0x39600, 0x6fc, 0xf0, 0x27},
   129  		&OptionalHeader64{
   130  			0x20b, 0x2, 0x16, 0x6a00, 0x2400, 0x1600, 0x14e0, 0x1000, 0x400000, 0x1000, 0x200, 0x4, 0x0, 0x0, 0x0, 0x5, 0x2, 0x0, 0x45000, 0x600, 0x46f19, 0x3, 0x0, 0x200000, 0x1000, 0x100000, 0x1000, 0x0, 0x10,
   131  			[16]DataDirectory{
   132  				{0x0, 0x0},
   133  				{0xe000, 0x990},
   134  				{0x0, 0x0},
   135  				{0xa000, 0x498},
   136  				{0x0, 0x0},
   137  				{0x0, 0x0},
   138  				{0x0, 0x0},
   139  				{0x0, 0x0},
   140  				{0x0, 0x0},
   141  				{0x10000, 0x28},
   142  				{0x0, 0x0},
   143  				{0x0, 0x0},
   144  				{0xe254, 0x218},
   145  				{0x0, 0x0},
   146  				{0x0, 0x0},
   147  				{0x0, 0x0},
   148  			}},
   149  		[]*SectionHeader{
   150  			{".text", 0x6860, 0x1000, 0x6a00, 0x600, 0x0, 0x0, 0x0, 0x0, 0x60500020},
   151  			{".data", 0xe0, 0x8000, 0x200, 0x7000, 0x0, 0x0, 0x0, 0x0, 0xc0500040},
   152  			{".rdata", 0x6b0, 0x9000, 0x800, 0x7200, 0x0, 0x0, 0x0, 0x0, 0x40600040},
   153  			{".pdata", 0x498, 0xa000, 0x600, 0x7a00, 0x0, 0x0, 0x0, 0x0, 0x40300040},
   154  			{".xdata", 0x488, 0xb000, 0x600, 0x8000, 0x0, 0x0, 0x0, 0x0, 0x40300040},
   155  			{".bss", 0x1410, 0xc000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0600080},
   156  			{".idata", 0x990, 0xe000, 0xa00, 0x8600, 0x0, 0x0, 0x0, 0x0, 0xc0300040},
   157  			{".CRT", 0x68, 0xf000, 0x200, 0x9000, 0x0, 0x0, 0x0, 0x0, 0xc0400040},
   158  			{".tls", 0x48, 0x10000, 0x200, 0x9200, 0x0, 0x0, 0x0, 0x0, 0xc0600040},
   159  			{".debug_aranges", 0x600, 0x11000, 0x600, 0x9400, 0x0, 0x0, 0x0, 0x0, 0x42500040},
   160  			{".debug_info", 0x1316e, 0x12000, 0x13200, 0x9a00, 0x0, 0x0, 0x0, 0x0, 0x42100040},
   161  			{".debug_abbrev", 0x2ccb, 0x26000, 0x2e00, 0x1cc00, 0x0, 0x0, 0x0, 0x0, 0x42100040},
   162  			{".debug_line", 0x3c4d, 0x29000, 0x3e00, 0x1fa00, 0x0, 0x0, 0x0, 0x0, 0x42100040},
   163  			{".debug_frame", 0x18b8, 0x2d000, 0x1a00, 0x23800, 0x0, 0x0, 0x0, 0x0, 0x42400040},
   164  			{".debug_str", 0x396, 0x2f000, 0x400, 0x25200, 0x0, 0x0, 0x0, 0x0, 0x42100040},
   165  			{".debug_loc", 0x13240, 0x30000, 0x13400, 0x25600, 0x0, 0x0, 0x0, 0x0, 0x42100040},
   166  			{".debug_ranges", 0xa70, 0x44000, 0xc00, 0x38a00, 0x0, 0x0, 0x0, 0x0, 0x42100040},
   167  		},
   168  		[]*Symbol{},
   169  	},
   170  }
   171  
   172  func isOptHdrEq(a, b interface{}) bool {
   173  	switch va := a.(type) {
   174  	case *OptionalHeader32:
   175  		vb, ok := b.(*OptionalHeader32)
   176  		if !ok {
   177  			return false
   178  		}
   179  		return *vb == *va
   180  	case *OptionalHeader64:
   181  		vb, ok := b.(*OptionalHeader64)
   182  		if !ok {
   183  			return false
   184  		}
   185  		return *vb == *va
   186  	case nil:
   187  		return b == nil
   188  	}
   189  	return false
   190  }
   191  
   192  func TestOpen(t *testing.T) {
   193  	for i := range fileTests {
   194  		tt := &fileTests[i]
   195  
   196  		f, err := Open(tt.file)
   197  		if err != nil {
   198  			t.Error(err)
   199  			continue
   200  		}
   201  		if !reflect.DeepEqual(f.FileHeader, tt.hdr) {
   202  			t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.FileHeader, tt.hdr)
   203  			continue
   204  		}
   205  		if !isOptHdrEq(tt.opthdr, f.OptionalHeader) {
   206  			t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.OptionalHeader, tt.opthdr)
   207  			continue
   208  		}
   209  
   210  		for i, sh := range f.Sections {
   211  			if i >= len(tt.sections) {
   212  				break
   213  			}
   214  			have := &sh.SectionHeader
   215  			want := tt.sections[i]
   216  			if !reflect.DeepEqual(have, want) {
   217  				t.Errorf("open %s, section %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want)
   218  			}
   219  		}
   220  		tn := len(tt.sections)
   221  		fn := len(f.Sections)
   222  		if tn != fn {
   223  			t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn)
   224  		}
   225  		for i, have := range f.Symbols {
   226  			if i >= len(tt.symbols) {
   227  				break
   228  			}
   229  			want := tt.symbols[i]
   230  			if !reflect.DeepEqual(have, want) {
   231  				t.Errorf("open %s, symbol %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want)
   232  			}
   233  		}
   234  	}
   235  }
   236  
   237  func TestOpenFailure(t *testing.T) {
   238  	filename := "file.go"    // not a PE file
   239  	_, err := Open(filename) // don't crash
   240  	if err == nil {
   241  		t.Errorf("open %s: succeeded unexpectedly", filename)
   242  	}
   243  }