github.com/anchore/syft@v1.38.2/internal/file/zip_file_manifest_test.go (about)

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