github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/formats/syftjson/to_format_model_test.go (about)

     1  package syftjson
     2  
     3  import (
     4  	"encoding/json"
     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/syft/syft/file"
    12  	"github.com/anchore/syft/syft/formats/syftjson/model"
    13  	"github.com/anchore/syft/syft/internal/sourcemetadata"
    14  	"github.com/anchore/syft/syft/source"
    15  )
    16  
    17  func Test_toSourceModel_IgnoreBase(t *testing.T) {
    18  	tests := []struct {
    19  		name string
    20  		src  source.Description
    21  	}{
    22  		{
    23  			name: "directory",
    24  			src: source.Description{
    25  				ID: "test-id",
    26  				Metadata: source.DirectorySourceMetadata{
    27  					Path: "some/path",
    28  					Base: "some/base",
    29  				},
    30  			},
    31  		},
    32  	}
    33  	for _, test := range tests {
    34  		t.Run(test.name, func(t *testing.T) {
    35  			// assert the model transformation is correct
    36  			actual := toSourceModel(test.src)
    37  
    38  			by, err := json.Marshal(actual)
    39  			require.NoError(t, err)
    40  			assert.NotContains(t, string(by), "some/base")
    41  		})
    42  	}
    43  }
    44  
    45  func Test_toSourceModel(t *testing.T) {
    46  	tracker := sourcemetadata.NewCompletionTester(t)
    47  
    48  	tests := []struct {
    49  		name     string
    50  		src      source.Description
    51  		expected model.Source
    52  	}{
    53  		{
    54  			name: "directory",
    55  			src: source.Description{
    56  				ID:      "test-id",
    57  				Name:    "some-name",
    58  				Version: "some-version",
    59  				Metadata: source.DirectorySourceMetadata{
    60  					Path: "some/path",
    61  					Base: "some/base",
    62  				},
    63  			},
    64  			expected: model.Source{
    65  				ID:      "test-id",
    66  				Name:    "some-name",
    67  				Version: "some-version",
    68  				Type:    "directory",
    69  				Metadata: source.DirectorySourceMetadata{
    70  					Path: "some/path",
    71  					Base: "some/base",
    72  				},
    73  			},
    74  		},
    75  		{
    76  			name: "file",
    77  			src: source.Description{
    78  				ID:      "test-id",
    79  				Name:    "some-name",
    80  				Version: "some-version",
    81  				Metadata: source.FileSourceMetadata{
    82  					Path:     "some/path",
    83  					Digests:  []file.Digest{{Algorithm: "sha256", Value: "some-digest"}},
    84  					MIMEType: "text/plain",
    85  				},
    86  			},
    87  			expected: model.Source{
    88  				ID:      "test-id",
    89  				Name:    "some-name",
    90  				Version: "some-version",
    91  				Type:    "file",
    92  				Metadata: source.FileSourceMetadata{
    93  					Path:     "some/path",
    94  					Digests:  []file.Digest{{Algorithm: "sha256", Value: "some-digest"}},
    95  					MIMEType: "text/plain",
    96  				},
    97  			},
    98  		},
    99  		{
   100  			name: "image",
   101  			src: source.Description{
   102  				ID:      "test-id",
   103  				Name:    "some-name",
   104  				Version: "some-version",
   105  				Metadata: source.StereoscopeImageSourceMetadata{
   106  					UserInput:      "user-input",
   107  					ID:             "id...",
   108  					ManifestDigest: "digest...",
   109  					MediaType:      "type...",
   110  				},
   111  			},
   112  			expected: model.Source{
   113  				ID:      "test-id",
   114  				Name:    "some-name",
   115  				Version: "some-version",
   116  				Type:    "image",
   117  				Metadata: source.StereoscopeImageSourceMetadata{
   118  					UserInput:      "user-input",
   119  					ID:             "id...",
   120  					ManifestDigest: "digest...",
   121  					MediaType:      "type...",
   122  					RepoDigests:    []string{},
   123  					Tags:           []string{},
   124  				},
   125  			},
   126  		},
   127  		// below are regression tests for when the name/version are not provided
   128  		// historically we've hoisted up the name/version from the metadata, now it is a simple pass-through
   129  		{
   130  			name: "directory - no name/version",
   131  			src: source.Description{
   132  				ID: "test-id",
   133  				Metadata: source.DirectorySourceMetadata{
   134  					Path: "some/path",
   135  					Base: "some/base",
   136  				},
   137  			},
   138  			expected: model.Source{
   139  				ID:   "test-id",
   140  				Type: "directory",
   141  				Metadata: source.DirectorySourceMetadata{
   142  					Path: "some/path",
   143  					Base: "some/base",
   144  				},
   145  			},
   146  		},
   147  		{
   148  			name: "file - no name/version",
   149  			src: source.Description{
   150  				ID: "test-id",
   151  				Metadata: source.FileSourceMetadata{
   152  					Path:     "some/path",
   153  					Digests:  []file.Digest{{Algorithm: "sha256", Value: "some-digest"}},
   154  					MIMEType: "text/plain",
   155  				},
   156  			},
   157  			expected: model.Source{
   158  				ID:   "test-id",
   159  				Type: "file",
   160  				Metadata: source.FileSourceMetadata{
   161  					Path:     "some/path",
   162  					Digests:  []file.Digest{{Algorithm: "sha256", Value: "some-digest"}},
   163  					MIMEType: "text/plain",
   164  				},
   165  			},
   166  		},
   167  		{
   168  			name: "image - no name/version",
   169  			src: source.Description{
   170  				ID: "test-id",
   171  				Metadata: source.StereoscopeImageSourceMetadata{
   172  					UserInput:      "user-input",
   173  					ID:             "id...",
   174  					ManifestDigest: "digest...",
   175  					MediaType:      "type...",
   176  				},
   177  			},
   178  			expected: model.Source{
   179  				ID:   "test-id",
   180  				Type: "image",
   181  				Metadata: source.StereoscopeImageSourceMetadata{
   182  					UserInput:      "user-input",
   183  					ID:             "id...",
   184  					ManifestDigest: "digest...",
   185  					MediaType:      "type...",
   186  					RepoDigests:    []string{},
   187  					Tags:           []string{},
   188  				},
   189  			},
   190  		},
   191  	}
   192  	for _, test := range tests {
   193  		t.Run(test.name, func(t *testing.T) {
   194  			// assert the model transformation is correct
   195  			actual := toSourceModel(test.src)
   196  			assert.Equal(t, test.expected, actual)
   197  
   198  			// track each scheme tested (passed or not)
   199  			tracker.Tested(t, test.expected.Metadata)
   200  		})
   201  	}
   202  }
   203  
   204  func Test_toFileType(t *testing.T) {
   205  
   206  	badType := stereoscopeFile.Type(0x1337)
   207  	var allTypesTested []stereoscopeFile.Type
   208  	tests := []struct {
   209  		ty   stereoscopeFile.Type
   210  		name string
   211  	}{
   212  		{
   213  			ty:   stereoscopeFile.TypeRegular,
   214  			name: "RegularFile",
   215  		},
   216  		{
   217  			ty:   stereoscopeFile.TypeDirectory,
   218  			name: "Directory",
   219  		},
   220  		{
   221  			ty:   stereoscopeFile.TypeSymLink,
   222  			name: "SymbolicLink",
   223  		},
   224  		{
   225  			ty:   stereoscopeFile.TypeHardLink,
   226  			name: "HardLink",
   227  		},
   228  		{
   229  			ty:   stereoscopeFile.TypeSocket,
   230  			name: "Socket",
   231  		},
   232  		{
   233  			ty:   stereoscopeFile.TypeCharacterDevice,
   234  			name: "CharacterDevice",
   235  		},
   236  		{
   237  			ty:   stereoscopeFile.TypeBlockDevice,
   238  			name: "BlockDevice",
   239  		},
   240  		{
   241  			ty:   stereoscopeFile.TypeFIFO,
   242  			name: "FIFONode",
   243  		},
   244  		{
   245  			ty:   stereoscopeFile.TypeIrregular,
   246  			name: "IrregularFile",
   247  		},
   248  		{
   249  			ty:   badType,
   250  			name: "Unknown",
   251  		},
   252  	}
   253  	for _, tt := range tests {
   254  		t.Run(tt.name, func(t *testing.T) {
   255  			assert.Equalf(t, tt.name, toFileType(tt.ty), "toFileType(%v)", tt.ty)
   256  			if tt.ty != badType {
   257  				allTypesTested = append(allTypesTested, tt.ty)
   258  			}
   259  		})
   260  	}
   261  
   262  	assert.ElementsMatch(t, allTypesTested, stereoscopeFile.AllTypes(), "not all file.Types are under test")
   263  }
   264  
   265  func Test_toFileMetadataEntry(t *testing.T) {
   266  	coords := file.Coordinates{
   267  		RealPath:     "/path",
   268  		FileSystemID: "x",
   269  	}
   270  	tests := []struct {
   271  		name     string
   272  		metadata *file.Metadata
   273  		want     *model.FileMetadataEntry
   274  	}{
   275  		{
   276  			name: "no metadata",
   277  		},
   278  		{
   279  			name: "no file info",
   280  			metadata: &file.Metadata{
   281  				FileInfo: nil,
   282  			},
   283  			want: &model.FileMetadataEntry{
   284  				Type: stereoscopeFile.TypeRegular.String(),
   285  			},
   286  		},
   287  		{
   288  			name: "with file info",
   289  			metadata: &file.Metadata{
   290  				FileInfo: &stereoscopeFile.ManualInfo{
   291  					ModeValue: 1,
   292  				},
   293  			},
   294  			want: &model.FileMetadataEntry{
   295  				Mode: 1,
   296  				Type: stereoscopeFile.TypeRegular.String(),
   297  			},
   298  		},
   299  	}
   300  	for _, tt := range tests {
   301  		t.Run(tt.name, func(t *testing.T) {
   302  			assert.Equal(t, tt.want, toFileMetadataEntry(coords, tt.metadata))
   303  		})
   304  	}
   305  }