github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/internal/file/zip_read_closer_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package file
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestFindArchiveStartOffset(t *testing.T) {
    14  	tests := []struct {
    15  		name        string
    16  		archivePrep func(tb testing.TB) string
    17  		expected    uint64
    18  	}{
    19  		{
    20  			name:        "standard, non-nested zip",
    21  			archivePrep: prepZipSourceFixture,
    22  			expected:    0,
    23  		},
    24  		{
    25  			name:        "zip with prepended bytes",
    26  			archivePrep: prependZipSourceFixtureWithString(t, "junk at the beginning of the file..."),
    27  			expected:    36,
    28  		},
    29  	}
    30  
    31  	for _, test := range tests {
    32  		t.Run(test.name, func(t *testing.T) {
    33  			archivePath := test.archivePrep(t)
    34  			f, err := os.Open(archivePath)
    35  			if err != nil {
    36  				t.Fatalf("could not open archive %q: %+v", archivePath, err)
    37  			}
    38  			fi, err := os.Stat(f.Name())
    39  			if err != nil {
    40  				t.Fatalf("unable to stat archive: %+v", err)
    41  			}
    42  
    43  			actual, err := findArchiveStartOffset(f, fi.Size())
    44  			if err != nil {
    45  				t.Fatalf("unable to find offset: %+v", err)
    46  			}
    47  			assert.Equal(t, test.expected, actual)
    48  		})
    49  	}
    50  }