github.com/saferwall/pe@v1.5.2/richheader_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  	"testing"
    10  )
    11  
    12  type TestRichHeader struct {
    13  	richHeader   RichHeader
    14  	compIDIndex  uint8
    15  	prettyProdID string
    16  	VSVersion    string
    17  	checksum     uint32
    18  }
    19  
    20  func TestParseRichHeader(t *testing.T) {
    21  
    22  	tests := []struct {
    23  		in  string
    24  		out TestRichHeader
    25  	}{
    26  		{getAbsoluteFilePath("test/kernel32.dll"),
    27  			TestRichHeader{
    28  				richHeader: RichHeader{
    29  					XORKey: 2796214951,
    30  					CompIDs: []CompID{
    31  						{
    32  							MinorCV:  27412,
    33  							ProdID:   257,
    34  							Count:    4,
    35  							Unmasked: 16870164,
    36  						},
    37  						{
    38  							MinorCV:  30729,
    39  							ProdID:   147,
    40  							Count:    193,
    41  							Unmasked: 9664521,
    42  						},
    43  						{
    44  							MinorCV:  0,
    45  							ProdID:   1,
    46  							Count:    1325,
    47  							Unmasked: 65536,
    48  						},
    49  						{
    50  							MinorCV:  27412,
    51  							ProdID:   260,
    52  							Count:    9,
    53  							Unmasked: 17066772,
    54  						},
    55  						{
    56  							MinorCV:  27412,
    57  							ProdID:   259,
    58  							Count:    3,
    59  							Unmasked: 17001236,
    60  						},
    61  						{
    62  							MinorCV:  27412,
    63  							ProdID:   256,
    64  							Count:    1,
    65  							Unmasked: 16804628,
    66  						},
    67  						{
    68  							MinorCV:  27412,
    69  							ProdID:   269,
    70  							Count:    209,
    71  							Unmasked: 17656596,
    72  						},
    73  						{
    74  							MinorCV:  27412,
    75  							ProdID:   255,
    76  							Count:    1,
    77  							Unmasked: 16739092,
    78  						},
    79  						{
    80  							MinorCV:  27412,
    81  							ProdID:   258,
    82  							Count:    1,
    83  							Unmasked: 16935700,
    84  						},
    85  					},
    86  					DansOffset: 128,
    87  					Raw: []byte{
    88  						0xe3, 0xbb, 0xc4, 0xf5, 0xa7, 0xda, 0xaa, 0xa6, 0xa7, 0xda, 0xaa, 0xa6, 0xa7, 0xda, 0xaa,
    89  						0xa6, 0xb3, 0xb1, 0xab, 0xa7, 0xa3, 0xda, 0xaa, 0xa6, 0xae, 0xa2, 0x39, 0xa6, 0x66, 0xda,
    90  						0xaa, 0xa6, 0xa7, 0xda, 0xab, 0xa6, 0x8a, 0xdf, 0xaa, 0xa6, 0xb3, 0xb1, 0xae, 0xa7, 0xae,
    91  						0xda, 0xaa, 0xa6, 0xb3, 0xb1, 0xa9, 0xa7, 0xa4, 0xda, 0xaa, 0xa6, 0xb3, 0xb1, 0xaa, 0xa7,
    92  						0xa6, 0xda, 0xaa, 0xa6, 0xb3, 0xb1, 0xa7, 0xa7, 0x76, 0xda, 0xaa, 0xa6, 0xb3, 0xb1, 0x55,
    93  						0xa6, 0xa6, 0xda, 0xaa, 0xa6, 0xb3, 0xb1, 0xa8, 0xa7, 0xa6, 0xda, 0xaa, 0xa6, 0x52, 0x69,
    94  						0x63, 0x68, 0xa7, 0xda, 0xaa, 0xa6},
    95  				},
    96  				compIDIndex:  3,
    97  				prettyProdID: "Utc1900_C",
    98  				VSVersion:    "Visual Studio 2015 14.00",
    99  				checksum:     0xa6aadaa7,
   100  			}},
   101  	}
   102  
   103  	for _, tt := range tests {
   104  		t.Run(tt.in, func(t *testing.T) {
   105  			ops := Options{Fast: true}
   106  			file, err := New(tt.in, &ops)
   107  			if err != nil {
   108  				t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
   109  			}
   110  
   111  			err = file.Parse()
   112  			if err != nil {
   113  				t.Fatalf("Parse(%s) failed, reason: %v", tt.in, err)
   114  			}
   115  
   116  			richHeader := file.RichHeader
   117  			if !reflect.DeepEqual(richHeader, tt.out.richHeader) {
   118  				t.Errorf("rich header test failed, got %v, want %v",
   119  				richHeader, tt.out)
   120  			}
   121  
   122  			prodID := richHeader.CompIDs[tt.out.compIDIndex].ProdID
   123  			prettyProdID := ProdIDtoStr(prodID)
   124  			if prettyProdID != tt.out.prettyProdID {
   125  				t.Errorf("rich header pretty prod ID failed, got %v, want %v",
   126  					prettyProdID, tt.out.prettyProdID)
   127  			}
   128  
   129  			VSVersion := ProdIDtoVSversion(prodID)
   130  			if VSVersion != tt.out.VSVersion {
   131  				t.Errorf("rich header VS verion of prod ID failed, got %v, want %v",
   132  					VSVersion, tt.out.VSVersion)
   133  			}
   134  
   135  			checksum := file.RichHeaderChecksum()
   136  			if checksum != tt.out.checksum {
   137  				t.Errorf("rich header checksum failed, got %v, want %v",
   138  					checksum, tt.out.checksum)
   139  			}
   140  
   141  		})
   142  	}
   143  }
   144  
   145  func TestRichHeaderHash(t *testing.T) {
   146  
   147  	tests := []struct {
   148  		in  string
   149  		out string
   150  	}{
   151  		{getAbsoluteFilePath("test/kernel32.dll"),
   152  			"4549320af6790d410f09ddc3bab86c86"},
   153  		{getAbsoluteFilePath("test/WdBoot.sys"),
   154  			"3cbccbf62a2a6a8066a5c9d294c90948"},
   155  	}
   156  
   157  	for _, tt := range tests {
   158  		t.Run(tt.in, func(t *testing.T) {
   159  			file, err := New(tt.in, &Options{})
   160  			if err != nil {
   161  				t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
   162  			}
   163  			err = file.Parse()
   164  			if err != nil {
   165  				t.Fatalf("Parse(%s) failed, reason: %v", tt.in, err)
   166  			}
   167  
   168  			got := file.RichHeaderHash()
   169  			if string(got) != tt.out {
   170  				t.Errorf("Authentihash(%s) got %v, want %v", tt.in, got, tt.out)
   171  			}
   172  
   173  		})
   174  	}
   175  }