|
| 1 | +package application |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/flashbots/builder-hub/domain" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestCheckMeasurement_SingleExpectedValue(t *testing.T) { |
| 11 | + template := domain.Measurement{ |
| 12 | + Name: "test", |
| 13 | + AttestationType: "azure-tdx", |
| 14 | + Measurement: map[string]domain.SingleMeasurement{ |
| 15 | + "8": {Expected: []string{"0000"}}, |
| 16 | + "11": {Expected: []string{"aaaa"}}, |
| 17 | + }, |
| 18 | + } |
| 19 | + |
| 20 | + t.Run("exact match", func(t *testing.T) { |
| 21 | + measurement := map[string]string{ |
| 22 | + "8": "0000", |
| 23 | + "11": "aaaa", |
| 24 | + } |
| 25 | + require.True(t, checkMeasurement(measurement, template)) |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("wrong value", func(t *testing.T) { |
| 29 | + measurement := map[string]string{ |
| 30 | + "8": "0000", |
| 31 | + "11": "bbbb", |
| 32 | + } |
| 33 | + require.False(t, checkMeasurement(measurement, template)) |
| 34 | + }) |
| 35 | + |
| 36 | + t.Run("missing key", func(t *testing.T) { |
| 37 | + measurement := map[string]string{ |
| 38 | + "8": "0000", |
| 39 | + } |
| 40 | + require.False(t, checkMeasurement(measurement, template)) |
| 41 | + }) |
| 42 | + |
| 43 | + t.Run("extra key in measurement is allowed", func(t *testing.T) { |
| 44 | + measurement := map[string]string{ |
| 45 | + "8": "0000", |
| 46 | + "11": "aaaa", |
| 47 | + "99": "extra", |
| 48 | + } |
| 49 | + require.True(t, checkMeasurement(measurement, template)) |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +func TestCheckMeasurement_MultipleExpectedValues(t *testing.T) { |
| 54 | + template := domain.Measurement{ |
| 55 | + Name: "test", |
| 56 | + AttestationType: "azure-tdx", |
| 57 | + Measurement: map[string]domain.SingleMeasurement{ |
| 58 | + "8": {Expected: []string{"0000", "1111", "2222"}}, |
| 59 | + "11": {Expected: []string{"aaaa", "bbbb"}}, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + t.Run("first expected value matches", func(t *testing.T) { |
| 64 | + measurement := map[string]string{ |
| 65 | + "8": "0000", |
| 66 | + "11": "aaaa", |
| 67 | + } |
| 68 | + require.True(t, checkMeasurement(measurement, template)) |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("second expected value matches", func(t *testing.T) { |
| 72 | + measurement := map[string]string{ |
| 73 | + "8": "1111", |
| 74 | + "11": "bbbb", |
| 75 | + } |
| 76 | + require.True(t, checkMeasurement(measurement, template)) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("third expected value for key 8", func(t *testing.T) { |
| 80 | + measurement := map[string]string{ |
| 81 | + "8": "2222", |
| 82 | + "11": "aaaa", |
| 83 | + } |
| 84 | + require.True(t, checkMeasurement(measurement, template)) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("value not in expected list", func(t *testing.T) { |
| 88 | + measurement := map[string]string{ |
| 89 | + "8": "3333", |
| 90 | + "11": "aaaa", |
| 91 | + } |
| 92 | + require.False(t, checkMeasurement(measurement, template)) |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("one key matches, other does not", func(t *testing.T) { |
| 96 | + measurement := map[string]string{ |
| 97 | + "8": "0000", |
| 98 | + "11": "cccc", |
| 99 | + } |
| 100 | + require.False(t, checkMeasurement(measurement, template)) |
| 101 | + }) |
| 102 | +} |
| 103 | + |
| 104 | +func TestValidateMeasurement(t *testing.T) { |
| 105 | + templates := []domain.Measurement{ |
| 106 | + { |
| 107 | + Name: "template-1", |
| 108 | + AttestationType: "azure-tdx", |
| 109 | + Measurement: map[string]domain.SingleMeasurement{ |
| 110 | + "8": {Expected: []string{"0000"}}, |
| 111 | + "11": {Expected: []string{"aaaa", "bbbb"}}, |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + Name: "template-2", |
| 116 | + AttestationType: "azure-tdx", |
| 117 | + Measurement: map[string]domain.SingleMeasurement{ |
| 118 | + "8": {Expected: []string{"1111"}}, |
| 119 | + "11": {Expected: []string{"cccc"}}, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + t.Run("matches first template", func(t *testing.T) { |
| 125 | + measurement := map[string]string{ |
| 126 | + "8": "0000", |
| 127 | + "11": "aaaa", |
| 128 | + } |
| 129 | + name, err := validateMeasurement(measurement, templates) |
| 130 | + require.NoError(t, err) |
| 131 | + require.Equal(t, "template-1", name) |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("matches first template with second expected value", func(t *testing.T) { |
| 135 | + measurement := map[string]string{ |
| 136 | + "8": "0000", |
| 137 | + "11": "bbbb", |
| 138 | + } |
| 139 | + name, err := validateMeasurement(measurement, templates) |
| 140 | + require.NoError(t, err) |
| 141 | + require.Equal(t, "template-1", name) |
| 142 | + }) |
| 143 | + |
| 144 | + t.Run("matches second template", func(t *testing.T) { |
| 145 | + measurement := map[string]string{ |
| 146 | + "8": "1111", |
| 147 | + "11": "cccc", |
| 148 | + } |
| 149 | + name, err := validateMeasurement(measurement, templates) |
| 150 | + require.NoError(t, err) |
| 151 | + require.Equal(t, "template-2", name) |
| 152 | + }) |
| 153 | + |
| 154 | + t.Run("no match returns error", func(t *testing.T) { |
| 155 | + measurement := map[string]string{ |
| 156 | + "8": "9999", |
| 157 | + "11": "zzzz", |
| 158 | + } |
| 159 | + _, err := validateMeasurement(measurement, templates) |
| 160 | + require.ErrorIs(t, err, domain.ErrNotFound) |
| 161 | + }) |
| 162 | +} |
| 163 | + |
| 164 | +func TestMatchesAnyExpected(t *testing.T) { |
| 165 | + t.Run("empty list returns false", func(t *testing.T) { |
| 166 | + require.False(t, matchesAnyExpected("value", []string{})) |
| 167 | + }) |
| 168 | + |
| 169 | + t.Run("single value match", func(t *testing.T) { |
| 170 | + require.True(t, matchesAnyExpected("value", []string{"value"})) |
| 171 | + }) |
| 172 | + |
| 173 | + t.Run("single value no match", func(t *testing.T) { |
| 174 | + require.False(t, matchesAnyExpected("value", []string{"other"})) |
| 175 | + }) |
| 176 | + |
| 177 | + t.Run("multiple values first match", func(t *testing.T) { |
| 178 | + require.True(t, matchesAnyExpected("a", []string{"a", "b", "c"})) |
| 179 | + }) |
| 180 | + |
| 181 | + t.Run("multiple values middle match", func(t *testing.T) { |
| 182 | + require.True(t, matchesAnyExpected("b", []string{"a", "b", "c"})) |
| 183 | + }) |
| 184 | + |
| 185 | + t.Run("multiple values last match", func(t *testing.T) { |
| 186 | + require.True(t, matchesAnyExpected("c", []string{"a", "b", "c"})) |
| 187 | + }) |
| 188 | + |
| 189 | + t.Run("multiple values no match", func(t *testing.T) { |
| 190 | + require.False(t, matchesAnyExpected("d", []string{"a", "b", "c"})) |
| 191 | + }) |
| 192 | +} |
0 commit comments