github.com/saferwall/pe@v1.5.2/section_test.go (about) 1 // Copyright 2018 Saferwall. All rights reserved. 2 // Use of this source code is governed by Apache v2 license 3 // license that can be found in the LICENSE file. 4 5 package pe 6 7 import ( 8 "reflect" 9 "sort" 10 "testing" 11 ) 12 13 type TestSection struct { 14 sectionCount int 15 sectionIndex int 16 sectionName string 17 header ImageSectionHeader 18 sectionFlags []string 19 entropy float64 20 } 21 22 func TestParseSectionHeaders(t *testing.T) { 23 24 tests := []struct { 25 in string 26 out TestSection 27 }{ 28 {getAbsoluteFilePath("test/putty.exe"), 29 TestSection{ 30 sectionCount: 8, 31 sectionIndex: 3, 32 sectionName: ".pdata", 33 header: ImageSectionHeader{ 34 Name: [8]uint8{0x2e, 0x70, 0x64, 0x61, 0x74, 0x61, 0x0, 0x0}, 35 VirtualSize: 0x588c, 36 VirtualAddress: 0xd2000, 37 SizeOfRawData: 0x5a00, 38 PointerToRawData: 0xc9c00, 39 Characteristics: 0x40000040, 40 }, 41 sectionFlags: []string{"Initialized Data", "Readable"}, 42 entropy: 5.789589357441211, 43 }}, 44 } 45 46 for _, tt := range tests { 47 t.Run(tt.in, func(t *testing.T) { 48 file, err := New(tt.in, &Options{}) 49 if err != nil { 50 t.Fatalf("New(%s) failed, reason: %v", tt.in, err) 51 } 52 err = file.Parse() 53 if err != nil { 54 t.Fatalf("Parse(%s) failed, reason: %v", tt.in, err) 55 } 56 57 sections := file.Sections 58 if len(sections) != tt.out.sectionCount { 59 t.Errorf("sections count assertion failed, got %v, want %v", 60 len(sections), tt.out.sectionCount) 61 } 62 63 section := sections[tt.out.sectionIndex] 64 if !reflect.DeepEqual(section.Header, tt.out.header) { 65 t.Errorf("section header assertion failed, got %v, want %v", 66 section.Header, tt.out.header) 67 } 68 69 sectionName := sections[tt.out.sectionIndex].String() 70 if sectionName != tt.out.sectionName { 71 t.Errorf("section name assertion failed, got %v, want %v", 72 sectionName, tt.out.sectionName) 73 } 74 75 prettySectionFlags := section.PrettySectionFlags() 76 sort.Strings(prettySectionFlags) 77 if !reflect.DeepEqual(prettySectionFlags, tt.out.sectionFlags) { 78 t.Errorf("pretty section flags assertion failed, got %v, want %v", 79 prettySectionFlags, tt.out.sectionFlags) 80 } 81 82 entropy := sections[tt.out.sectionIndex].CalculateEntropy(file) 83 if entropy != tt.out.entropy { 84 t.Errorf("entropy calculation failed, got %v, want %v", 85 entropy, tt.out.entropy) 86 } 87 }) 88 } 89 }