-
Notifications
You must be signed in to change notification settings - Fork 7
/
digests_test.go
151 lines (125 loc) · 3.89 KB
/
digests_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/veraison/swid"
)
func TestDigests_AddDigest_OK(t *testing.T) {
d := NewDigests()
require.NotNil(t, d)
tvs := []struct {
alg uint64
val []byte
}{
{
alg: swid.Sha256,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75"),
},
{
alg: swid.Sha256_128,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f36"),
},
{
alg: swid.Sha256_120,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f"),
},
{
alg: swid.Sha256_96,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3a"),
},
{
alg: swid.Sha256_64,
val: MustHexDecode(t, "e45b72f5c0c0b572"),
},
{
alg: swid.Sha256_32,
val: MustHexDecode(t, "e45b72ab"),
},
{
alg: swid.Sha384,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75e45b72f5c0c0b572db4d8d3ab7e97f36"),
},
{
alg: swid.Sha512,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75"),
},
{
alg: swid.Sha3_224,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f36e45b72f5c0c0b572db4d8d3a"),
},
{
alg: swid.Sha3_256,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75"),
},
{
alg: swid.Sha3_384,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75e45b72f5c0c0b572db4d8d3ab7e97f36"),
},
{
alg: swid.Sha3_512,
val: MustHexDecode(t, "e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75e45b72f5c0c0b572db4d8d3ab7e97f368ff74e62347a824decb67a84e5224d75"),
},
}
for _, tv := range tvs {
assert.NotNil(t, d.AddDigest(tv.alg, tv.val))
assert.Nil(t, d.Valid())
}
}
func TestDigests_Valid_empty(t *testing.T) {
d := NewDigests()
require.NotNil(t, d)
// simulate evil CBOR
*d = append(*d, swid.HashEntry{
HashAlgID: 666,
HashValue: []byte{0x66, 0x66, 0x06},
})
assert.EqualError(t, d.Valid(), "digest at index 0: unknown hash algorithm 666")
}
func TestDigests_AddDigest_unknown_algo(t *testing.T) {
d := NewDigests()
require.NotNil(t, d)
assert.Nil(t, d.AddDigest(0, []byte("0 is a reserved value")))
}
func TestDigests_AddDigest_inconsistent_length_for_algo(t *testing.T) {
d := NewDigests()
require.NotNil(t, d)
assert.Nil(t, d.AddDigest(swid.Sha3_512, MustHexDecode(t, "deadbeef")))
}
func TestDigests_MarshalJSON(t *testing.T) {
d := NewDigests().
AddDigest(swid.Sha256_32, MustHexDecode(t, "e45b72ab")).
AddDigest(swid.Sha256_64, MustHexDecode(t, "e45b72f5c0c0b572"))
require.NotNil(t, d)
expected := `[ "sha-256-32;5Ftyqw==", "sha-256-64;5Fty9cDAtXI=" ]`
actual, err := json.Marshal(d)
assert.Nil(t, err)
assert.JSONEq(t, expected, string(actual))
}
func TestDigests_MarshalCBOR(t *testing.T) {
d := NewDigests().
AddDigest(swid.Sha256_32, MustHexDecode(t, "e45b72ab")).
AddDigest(swid.Sha256_64, MustHexDecode(t, "e45b72f5c0c0b572"))
require.NotNil(t, d)
// [[6, h'E45B72AB'], [5, h'E45B72F5C0C0B572']]
expected := MustHexDecode(t, "82820644e45b72ab820548e45b72f5c0c0b572")
actual, err := em.Marshal(d)
fmt.Printf("CBOR: %x\n", actual)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
}
func TestDigests_UnmarshalCBOR(t *testing.T) {
// [[6, h'E45B72AB'], [5, h'E45B72F5C0C0B572']]
tv := MustHexDecode(t, "82820644e45b72ab820548e45b72f5c0c0b572")
var actual Digests
err := dm.Unmarshal(tv, &actual)
assert.Nil(t, err)
assert.Equal(t, swid.Sha256_32, actual[0].HashAlgID)
assert.Equal(t, MustHexDecode(t, "e45b72ab"), actual[0].HashValue)
assert.Equal(t, swid.Sha256_64, actual[1].HashAlgID)
assert.Equal(t, MustHexDecode(t, "e45b72f5c0c0b572"), actual[1].HashValue)
}