|
| 1 | +// Copyright 2026 Contributors to the Veraison project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package compositeevidenceparser |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/hex" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_Parse_CBORCollection_OK(t *testing.T) { |
| 16 | + expected := []ComponentEvidence{ |
| 17 | + { |
| 18 | + label: "bretwaldadom", |
| 19 | + data: []byte{0xa1, 0x0a}, |
| 20 | + mediaType: "application/eat-ucs+cbor", |
| 21 | + parentLabel: "", |
| 22 | + depth: 0, |
| 23 | + }, |
| 24 | + { |
| 25 | + label: "polyscopic", |
| 26 | + data: []byte(`{"eat_nonce": ...}`), |
| 27 | + mediaType: "application/eat-ucs+json", |
| 28 | + parentLabel: "murmurless", |
| 29 | + depth: 1, |
| 30 | + }, |
| 31 | + { |
| 32 | + label: "photoelectrograph", |
| 33 | + data: []byte{0x82, 0x78, 0x18}, |
| 34 | + mediaType: "application/eat-ucs+cbor", |
| 35 | + parentLabel: "", |
| 36 | + depth: 1, |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + b := mustReadFile(t, "testdata/collection-1.cbor") |
| 41 | + |
| 42 | + var cmwParser cmwParser |
| 43 | + ev, err := cmwParser.Parse(b) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + assert.ElementsMatch(t, expected, ev) |
| 47 | +} |
| 48 | + |
| 49 | +func Test_ParseJSONCollection_OK(t *testing.T) { |
| 50 | + expected := []ComponentEvidence{ |
| 51 | + { |
| 52 | + label: "bretwaldadom", |
| 53 | + data: []byte{0xa1, 0x0a}, |
| 54 | + mediaType: "application/eat-ucs+cbor", |
| 55 | + parentLabel: "", |
| 56 | + depth: 0, |
| 57 | + }, |
| 58 | + { |
| 59 | + label: "polyscopic", |
| 60 | + data: []byte(`{"eat_nonce": ...}`), |
| 61 | + mediaType: "application/eat-ucs+json", |
| 62 | + parentLabel: "murmurless", |
| 63 | + depth: 1, |
| 64 | + }, |
| 65 | + { |
| 66 | + label: "photoelectrograph", |
| 67 | + data: []byte{0x82, 0x78, 0x18}, |
| 68 | + mediaType: "application/eat-ucs+cbor", |
| 69 | + parentLabel: "", |
| 70 | + depth: 1, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + b := mustReadFile(t, "testdata/collection-1.json") |
| 75 | + |
| 76 | + var cmwParser cmwParser |
| 77 | + ev, err := cmwParser.Parse(b) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + assert.ElementsMatch(t, expected, ev) |
| 81 | +} |
| 82 | + |
| 83 | +func Test_Parse_MixedKey_Collection_OK(t *testing.T) { |
| 84 | + expected := []ComponentEvidence{ |
| 85 | + { |
| 86 | + label: "1024", |
| 87 | + data: []byte{0xaa}, |
| 88 | + mediaType: "text/plain; charset=utf-8", |
| 89 | + parentLabel: "", |
| 90 | + depth: 0, |
| 91 | + }, |
| 92 | + { |
| 93 | + label: "string", |
| 94 | + data: []byte{0xff}, |
| 95 | + mediaType: "text/plain; charset=utf-8", |
| 96 | + parentLabel: "", |
| 97 | + depth: 0, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + tv := mustReadFile(t, "testdata/collection-cbor-mixed-keys.cbor") |
| 102 | + |
| 103 | + var cmwParser cmwParser |
| 104 | + ev, err := cmwParser.Parse(tv) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + assert.ElementsMatch(t, expected, ev) |
| 108 | +} |
| 109 | + |
| 110 | +func Test_Parse_Collection_NOK(t *testing.T) { |
| 111 | + tests := []struct { |
| 112 | + name string |
| 113 | + data string |
| 114 | + expectedErr string |
| 115 | + }{ |
| 116 | + { |
| 117 | + "CMW monad instead of collection", |
| 118 | + `83781d6170706c69636174696f6e2f7369676e65642d636f72696d2b63626f724dd901f6d28440a044d901f5a04003`, |
| 119 | + `evidence is not a CMW collection`, |
| 120 | + }, |
| 121 | + { |
| 122 | + "Invalid CMW CBOR header", |
| 123 | + `63781d6170706c69636174696f6e2f7369676e65642d636f72696d2b63626f724dd901f6d28440a044d901f5a04003`, |
| 124 | + "unable to unmarshal CMW collection: unknown start symbol for CMW", |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + for _, tt := range tests { |
| 129 | + var cmwParser cmwParser |
| 130 | + cmw, err := hexDecode(tt.data) |
| 131 | + require.Nil(t, err) |
| 132 | + |
| 133 | + _, err = cmwParser.Parse(cmw) |
| 134 | + assert.ErrorContains(t, err, tt.expectedErr) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func Test_SupportedMediaTypes(t *testing.T) { |
| 139 | + expected := []string{"application/cmw+cbor", "application/cmw+json"} |
| 140 | + |
| 141 | + var cmwParser cmwParser |
| 142 | + mtList := cmwParser.SupportedMediaTypes() |
| 143 | + |
| 144 | + assert.ElementsMatch(t, expected, mtList) |
| 145 | +} |
| 146 | + |
| 147 | +func hexDecode(s string) ([]byte, error) { |
| 148 | + // allow a long hex string to be split over multiple lines (with soft or |
| 149 | + // hard tab indentation) |
| 150 | + m := regexp.MustCompile("[ \t\n]") |
| 151 | + s = m.ReplaceAllString(s, "") |
| 152 | + |
| 153 | + data, err := hex.DecodeString(s) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + |
| 158 | + return data, nil |
| 159 | +} |
| 160 | + |
| 161 | +func mustReadFile(t *testing.T, fname string) []byte { |
| 162 | + b, err := os.ReadFile(fname) |
| 163 | + require.NoError(t, err) |
| 164 | + return b |
| 165 | +} |
0 commit comments