github.com/saferwall/pe@v1.5.2/file_test.go (about)

     1  // Copyright 2021 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  	"io/ioutil"
     9  	"testing"
    10  )
    11  
    12  var peTests = []struct {
    13  	in  string
    14  	out error
    15  }{
    16  	{getAbsoluteFilePath("test/putty.exe"), nil},
    17  }
    18  
    19  func TestParse(t *testing.T) {
    20  	for _, tt := range peTests {
    21  		t.Run(tt.in, func(t *testing.T) {
    22  			file, err := New(tt.in, &Options{})
    23  			if err != nil {
    24  				t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
    25  			}
    26  
    27  			got := file.Parse()
    28  			if got != nil {
    29  				t.Errorf("Parse(%s) got %v, want %v", tt.in, got, tt.out)
    30  			}
    31  		})
    32  	}
    33  }
    34  
    35  func TestParseOmitDirectories(t *testing.T) {
    36  	for _, tt := range peTests {
    37  		t.Run(tt.in, func(t *testing.T) {
    38  			file, err := New(tt.in, &Options{OmitSecurityDirectory: true})
    39  			if err != nil {
    40  				t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
    41  			}
    42  
    43  			got := file.Parse()
    44  			if got != nil {
    45  				t.Errorf("Parse(%s) got %v, want %v", tt.in, got, tt.out)
    46  			}
    47  			// Should expect an empty certificate
    48  			if file.Certificates.Raw != nil {
    49  				t.Errorf("Parse(%s) expected empty certificate", tt.in)
    50  			}
    51  		})
    52  	}
    53  }
    54  
    55  func TestNewBytes(t *testing.T) {
    56  	for _, tt := range peTests {
    57  		t.Run(tt.in, func(t *testing.T) {
    58  			data, _ := ioutil.ReadFile(tt.in)
    59  			file, err := NewBytes(data, &Options{})
    60  			if err != nil {
    61  				t.Fatalf("NewBytes(%s) failed, reason: %v", tt.in, err)
    62  			}
    63  
    64  			got := file.Parse()
    65  			if got != nil {
    66  				t.Errorf("Parse(%s) got %v, want %v", tt.in, got, tt.out)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func TestChecksum(t *testing.T) {
    73  
    74  	tests := []struct {
    75  		in  string
    76  		out uint32
    77  	}{
    78  		// file is DWORD aligned.
    79  		{getAbsoluteFilePath("test/putty.exe"),
    80  			0x00122C22},
    81  		// file is not DWORD aligned and needs paddings.
    82  		{getAbsoluteFilePath("test/010001e68577ef704792448ff474d22c6545167231982447c568e55041169ef0"),
    83  			0x0006D558},
    84  	}
    85  
    86  	for _, tt := range tests {
    87  		t.Run(tt.in, func(t *testing.T) {
    88  			file, err := New(tt.in, &Options{})
    89  			if err != nil {
    90  				t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
    91  			}
    92  			err = file.Parse()
    93  			if err != nil {
    94  				t.Fatalf("Parse(%s) failed, reason: %v", tt.in, err)
    95  			}
    96  
    97  			got := file.Checksum()
    98  			if got != tt.out {
    99  				t.Errorf("Checksum(%s) got %v, want %v", tt.in, got, tt.out)
   100  			}
   101  
   102  		})
   103  	}
   104  }