github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/file/coordinate_set_test.go (about)

     1  package file
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/nextlinux/gosbom/gosbom/artifact"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestCoordinatesSet(t *testing.T) {
    12  
    13  	binA := Coordinates{
    14  		RealPath:     "/bin",
    15  		FileSystemID: "a",
    16  	}
    17  
    18  	binB := Coordinates{
    19  		RealPath:     "/bin",
    20  		FileSystemID: "b",
    21  	}
    22  
    23  	tests := []struct {
    24  		name     string
    25  		input    []Coordinates
    26  		expected []Coordinates
    27  	}{
    28  		{
    29  			name: "de-dup same location",
    30  			input: []Coordinates{
    31  				binA, binA, binA,
    32  			},
    33  			expected: []Coordinates{
    34  				binA,
    35  			},
    36  		},
    37  		{
    38  			name: "dont de-dup different filesystem",
    39  			input: []Coordinates{
    40  				binB, binA,
    41  			},
    42  			expected: []Coordinates{
    43  				binA, binB,
    44  			},
    45  		},
    46  	}
    47  
    48  	for _, test := range tests {
    49  		t.Run(test.name, func(t *testing.T) {
    50  			set := NewCoordinateSet(test.input...)
    51  			assert.Equal(t, test.expected, set.ToSlice())
    52  		})
    53  	}
    54  }
    55  
    56  func TestCoordinateSet_Hash(t *testing.T) {
    57  	etcA := Coordinates{
    58  		RealPath:     "/etc",
    59  		FileSystemID: "a",
    60  	}
    61  
    62  	etcB := Coordinates{
    63  		RealPath:     "/etc",
    64  		FileSystemID: "b",
    65  	}
    66  
    67  	binA := Coordinates{
    68  		RealPath:     "/bin",
    69  		FileSystemID: "a",
    70  	}
    71  
    72  	binB := Coordinates{
    73  		RealPath:     "/bin",
    74  		FileSystemID: "b",
    75  	}
    76  
    77  	tests := []struct {
    78  		name string
    79  		setA CoordinateSet
    80  		setB CoordinateSet
    81  		want assert.ComparisonAssertionFunc
    82  	}{
    83  		{
    84  			name: "empty sets have the same hash",
    85  			setA: NewCoordinateSet(),
    86  			setB: NewCoordinateSet(),
    87  			want: assert.Equal,
    88  		},
    89  		{
    90  			name: "sets with same elements have the same hash",
    91  			setA: NewCoordinateSet(binA, etcA),
    92  			setB: NewCoordinateSet(etcA, binA),
    93  			want: assert.Equal,
    94  		},
    95  		{
    96  			name: "sets with different elements have different hashes",
    97  			setA: NewCoordinateSet(binA, etcA),
    98  			setB: NewCoordinateSet(binA),
    99  			want: assert.NotEqual,
   100  		},
   101  		{
   102  			name: "sets with same paths but different FS IDs have different hashes",
   103  			setA: NewCoordinateSet(etcA, binA),
   104  			setB: NewCoordinateSet(etcB, binB),
   105  			want: assert.NotEqual,
   106  		},
   107  	}
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			gotA, err := artifact.IDByHash(tt.setA)
   111  			require.NoError(t, err)
   112  			gotB, err := artifact.IDByHash(tt.setB)
   113  			require.NoError(t, err)
   114  			tt.want(t, gotA, gotB)
   115  		})
   116  	}
   117  }