github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/file/cataloger/filemetadata/cataloger_test.go (about)

     1  package filemetadata
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	stereoscopeFile "github.com/anchore/stereoscope/pkg/file"
    11  	"github.com/anchore/stereoscope/pkg/imagetest"
    12  	"github.com/anchore/syft/syft/file"
    13  	"github.com/anchore/syft/syft/source"
    14  )
    15  
    16  func TestFileMetadataCataloger(t *testing.T) {
    17  	testImage := "image-file-type-mix"
    18  
    19  	img := imagetest.GetFixtureImage(t, "docker-archive", testImage)
    20  
    21  	c := NewCataloger()
    22  
    23  	src, err := source.NewFromStereoscopeImageObject(img, testImage, nil)
    24  	if err != nil {
    25  		t.Fatalf("could not create source: %+v", err)
    26  	}
    27  
    28  	resolver, err := src.FileResolver(source.SquashedScope)
    29  	if err != nil {
    30  		t.Fatalf("could not create resolver: %+v", err)
    31  	}
    32  
    33  	actual, err := c.Catalog(resolver)
    34  	if err != nil {
    35  		t.Fatalf("could not catalog: %+v", err)
    36  	}
    37  
    38  	tests := []struct {
    39  		path     string
    40  		exists   bool
    41  		expected file.Metadata
    42  		err      bool
    43  	}{
    44  		// note: it is difficult to add a hardlink-based test in a cross-platform way and is already covered well in stereoscope
    45  		{
    46  			path:   "/file-1.txt",
    47  			exists: true,
    48  			expected: file.Metadata{
    49  				FileInfo: stereoscopeFile.ManualInfo{
    50  					NameValue: "file-1.txt",
    51  					ModeValue: 0644,
    52  					SizeValue: 7,
    53  				},
    54  				Path:     "/file-1.txt",
    55  				Type:     stereoscopeFile.TypeRegular,
    56  				UserID:   1,
    57  				GroupID:  2,
    58  				MIMEType: "text/plain",
    59  			},
    60  		},
    61  		{
    62  			path:   "/symlink-1",
    63  			exists: true,
    64  			expected: file.Metadata{
    65  				Path: "/symlink-1",
    66  				FileInfo: stereoscopeFile.ManualInfo{
    67  					NameValue: "symlink-1",
    68  					ModeValue: 0777 | os.ModeSymlink,
    69  				},
    70  				Type:            stereoscopeFile.TypeSymLink,
    71  				LinkDestination: "file-1.txt",
    72  				UserID:          0,
    73  				GroupID:         0,
    74  				MIMEType:        "",
    75  			},
    76  		},
    77  		{
    78  			path:   "/char-device-1",
    79  			exists: true,
    80  			expected: file.Metadata{
    81  				Path: "/char-device-1",
    82  				FileInfo: stereoscopeFile.ManualInfo{
    83  					NameValue: "char-device-1",
    84  					ModeValue: 0644 | os.ModeDevice | os.ModeCharDevice,
    85  				},
    86  				Type:     stereoscopeFile.TypeCharacterDevice,
    87  				UserID:   0,
    88  				GroupID:  0,
    89  				MIMEType: "",
    90  			},
    91  		},
    92  		{
    93  			path:   "/block-device-1",
    94  			exists: true,
    95  			expected: file.Metadata{
    96  				Path: "/block-device-1",
    97  				FileInfo: stereoscopeFile.ManualInfo{
    98  					NameValue: "block-device-1",
    99  					ModeValue: 0644 | os.ModeDevice,
   100  				},
   101  				Type:     stereoscopeFile.TypeBlockDevice,
   102  				UserID:   0,
   103  				GroupID:  0,
   104  				MIMEType: "",
   105  			},
   106  		},
   107  		{
   108  			path:   "/fifo-1",
   109  			exists: true,
   110  			expected: file.Metadata{
   111  				Path: "/fifo-1",
   112  				FileInfo: stereoscopeFile.ManualInfo{
   113  					NameValue: "fifo-1",
   114  					ModeValue: 0644 | os.ModeNamedPipe,
   115  				},
   116  				Type:     stereoscopeFile.TypeFIFO,
   117  				UserID:   0,
   118  				GroupID:  0,
   119  				MIMEType: "",
   120  			},
   121  		},
   122  		{
   123  			path:   "/bin",
   124  			exists: true,
   125  			expected: file.Metadata{
   126  				Path: "/bin",
   127  				FileInfo: stereoscopeFile.ManualInfo{
   128  					NameValue: "bin",
   129  					ModeValue: 0755 | os.ModeDir,
   130  				},
   131  				Type:     stereoscopeFile.TypeDirectory,
   132  				UserID:   0,
   133  				GroupID:  0,
   134  				MIMEType: "",
   135  			},
   136  		},
   137  	}
   138  
   139  	for _, test := range tests {
   140  		t.Run(test.path, func(t *testing.T) {
   141  			_, ref, err := img.SquashedTree().File(stereoscopeFile.Path(test.path))
   142  			require.NoError(t, err)
   143  
   144  			l := file.NewLocationFromImage(test.path, *ref.Reference, img)
   145  
   146  			if _, ok := actual[l.Coordinates]; ok {
   147  				// we're not interested in keeping the test fixtures up to date with the latest file modification times
   148  				// thus ModTime is not under test
   149  				fi := test.expected.FileInfo.(stereoscopeFile.ManualInfo)
   150  				fi.ModTimeValue = actual[l.Coordinates].ModTime()
   151  				test.expected.FileInfo = fi
   152  			}
   153  
   154  			assert.True(t, test.expected.Equal(actual[l.Coordinates]))
   155  		})
   156  	}
   157  
   158  }