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

     1  package pe
     2  
     3  import (
     4  	"crypto/md5"
     5  	"encoding/hex"
     6  	"testing"
     7  )
     8  
     9  type TestOverlay struct {
    10  	overlayOffset int64
    11  	overlayLength int64
    12  	md5str        string
    13  }
    14  
    15  var overlayTests = []struct {
    16  	in  string
    17  	out TestOverlay
    18  }{
    19  	{getAbsoluteFilePath("test/putty.exe"),
    20  		TestOverlay{
    21  			overlayOffset: 1163264,
    22  			overlayLength: 15760,
    23  			md5str:        "1f46295a513e744895a6acf1029e136f",
    24  		}},
    25  }
    26  
    27  func TestFile_NewOverlayReader(t *testing.T) {
    28  	for _, tt := range overlayTests {
    29  		t.Run(tt.in, func(t *testing.T) {
    30  			file, err := New(tt.in, &Options{})
    31  			if err != nil {
    32  				t.Fatalf("New(%s) failed, reason: %v", tt.in, err)
    33  			}
    34  
    35  			if err := file.Parse(); err != nil {
    36  				t.Fatalf("Parse(%s) failed, reason: %v", tt.in, err)
    37  			}
    38  			if file.OverlayOffset != tt.out.overlayOffset {
    39  				t.Errorf("overlayLength failed, got %d, want %d", file.OverlayOffset, tt.out.overlayOffset)
    40  			}
    41  
    42  			overlayLength := file.OverlayLength()
    43  			if overlayLength != tt.out.overlayLength {
    44  				t.Errorf("overlayOffset failed, got %d, want %d", overlayLength, tt.out.overlayLength)
    45  			}
    46  
    47  			overlay, _ := file.Overlay()
    48  			h := md5.New()
    49  			h.Write(overlay)
    50  			md5str := hex.EncodeToString(h.Sum(nil))
    51  			if md5str != tt.out.md5str {
    52  				t.Errorf("overlayOffset failed, got %s, want %s", md5str, tt.out.md5str)
    53  			}
    54  		})
    55  	}
    56  }