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

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package file
     5  
     6  import (
     7  	"encoding/json"
     8  	"os"
     9  	"path"
    10  	"testing"
    11  )
    12  
    13  func TestNewZipFileManifest(t *testing.T) {
    14  	cwd, err := os.Getwd()
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  
    19  	sourceDirPath := path.Join(cwd, "test-fixtures", "zip-source")
    20  	err = ensureNestedZipExists(t, sourceDirPath)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	archiveFilePath := setupZipFileTest(t, sourceDirPath, false)
    26  
    27  	actual, err := NewZipFileManifest(archiveFilePath)
    28  	if err != nil {
    29  		t.Fatalf("unable to extract from unzip archive: %+v", err)
    30  	}
    31  
    32  	if len(expectedZipArchiveEntries) != len(actual) {
    33  		t.Fatalf("mismatched manifest: %d != %d", len(actual), len(expectedZipArchiveEntries))
    34  	}
    35  
    36  	for _, e := range expectedZipArchiveEntries {
    37  		_, ok := actual[e]
    38  		if !ok {
    39  			t.Errorf("missing path: %s", e)
    40  		}
    41  	}
    42  
    43  	if t.Failed() {
    44  		b, err := json.MarshalIndent(actual, "", "  ")
    45  		if err != nil {
    46  			t.Fatalf("can't show results: %+v", err)
    47  		}
    48  
    49  		t.Errorf("full result: %s", string(b))
    50  	}
    51  }
    52  
    53  func TestNewZip64FileManifest(t *testing.T) {
    54  	cwd, err := os.Getwd()
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	sourceDirPath := path.Join(cwd, "test-fixtures", "zip-source")
    60  	archiveFilePath := setupZipFileTest(t, sourceDirPath, true)
    61  
    62  	actual, err := NewZipFileManifest(archiveFilePath)
    63  	if err != nil {
    64  		t.Fatalf("unable to extract from unzip archive: %+v", err)
    65  	}
    66  
    67  	if len(expectedZipArchiveEntries) != len(actual) {
    68  		t.Fatalf("mismatched manifest: %d != %d", len(actual), len(expectedZipArchiveEntries))
    69  	}
    70  
    71  	for _, e := range expectedZipArchiveEntries {
    72  		_, ok := actual[e]
    73  		if !ok {
    74  			t.Errorf("missing path: %s", e)
    75  		}
    76  	}
    77  
    78  	if t.Failed() {
    79  		b, err := json.MarshalIndent(actual, "", "  ")
    80  		if err != nil {
    81  			t.Fatalf("can't show results: %+v", err)
    82  		}
    83  
    84  		t.Errorf("full result: %s", string(b))
    85  	}
    86  }
    87  
    88  func TestZipFileManifest_GlobMatch(t *testing.T) {
    89  	cwd, err := os.Getwd()
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  
    94  	sourceDirPath := path.Join(cwd, "test-fixtures", "zip-source")
    95  	err = ensureNestedZipExists(t, sourceDirPath)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	archiveFilePath := setupZipFileTest(t, sourceDirPath, false)
   101  
   102  	z, err := NewZipFileManifest(archiveFilePath)
   103  	if err != nil {
   104  		t.Fatalf("unable to extract from unzip archive: %+v", err)
   105  	}
   106  
   107  	cases := []struct {
   108  		glob     string
   109  		expected string
   110  	}{
   111  		{
   112  			"/b*",
   113  			"b-file.txt",
   114  		},
   115  		{
   116  			"*/a-file.txt",
   117  			"some-dir/a-file.txt",
   118  		},
   119  		{
   120  			"**/*.zip",
   121  			"nested.zip",
   122  		},
   123  	}
   124  
   125  	for _, tc := range cases {
   126  		t.Run(tc.glob, func(t *testing.T) {
   127  			glob := tc.glob
   128  
   129  			results := z.GlobMatch(glob)
   130  
   131  			if len(results) == 1 && results[0] == tc.expected {
   132  				return
   133  			}
   134  
   135  			t.Errorf("unexpected results for glob '%s': %+v", glob, results)
   136  		})
   137  	}
   138  }