-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathnitrite_test.go
More file actions
108 lines (89 loc) · 2.28 KB
/
nitrite_test.go
File metadata and controls
108 lines (89 loc) · 2.28 KB
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
package nitrite_test
import (
"errors"
"testing"
"time"
"github.com/fxamacker/cbor/v2"
"github.com/hf/nitrite"
)
func requireNoError(t *testing.T, got error) {
if got != nil {
t.Fatalf("unexpected error: %v", got)
}
}
func requireEqual(t *testing.T, got, want interface{}) {
if got != want {
t.Fatalf("not equal: got %v, want %v", got, want)
}
}
func requireErrorIs(t *testing.T, got, want error) {
if !errors.Is(got, want) {
t.Fatalf("unexpected error type: got %T, want %T", got, want)
}
}
type testingCOSEPayload struct {
_ struct{} `cbor:",toarray"`
Protected []byte
Unprotected cbor.RawMessage
Payload []byte
Signature []byte
}
func TestAttestationCreatedAt(t *testing.T) {
timeToMillis := func(t time.Time) uint64 {
return uint64(t.UnixNano() / 1e6)
}
t.Run("happy path", func(t *testing.T) {
// given
wantTime := time.Now()
doc := nitrite.Document{
Timestamp: timeToMillis(wantTime),
}
docBytes, err := cbor.Marshal(doc)
requireNoError(t, err)
cosePayload := testingCOSEPayload{
Payload: docBytes,
}
cosePayloadBytes, err := cbor.Marshal(cosePayload)
requireNoError(t, err)
// when
gotTime, err := nitrite.Timestamp(cosePayloadBytes)
// then
requireNoError(t, err)
requireEqual(t, timeToMillis(gotTime), timeToMillis(wantTime))
})
t.Run("cannot unmarshal COSE payload", func(t *testing.T) {
// when
_, err := nitrite.Timestamp([]byte("invalid"))
// then
requireErrorIs(t, err, nitrite.ErrBadCOSESign1Structure)
})
t.Run("cannot unmarshal Document", func(t *testing.T) {
// given
cosePayload := testingCOSEPayload{
Payload: []byte("invalid"),
}
cosePayloadBytes, err := cbor.Marshal(cosePayload)
requireNoError(t, err)
// when
_, err = nitrite.Timestamp(cosePayloadBytes)
// then
requireErrorIs(t, err, nitrite.ErrBadAttestationDocument)
})
t.Run("attestation document has no timestamp", func(t *testing.T) {
// given
doc := nitrite.Document{
Timestamp: 0,
}
docBytes, err := cbor.Marshal(doc)
requireNoError(t, err)
cosePayload := testingCOSEPayload{
Payload: docBytes,
}
cosePayloadBytes, err := cbor.Marshal(cosePayload)
requireNoError(t, err)
// when
_, err = nitrite.Timestamp(cosePayloadBytes)
// then
requireErrorIs(t, err, nitrite.ErrMandatoryFieldsMissing)
})
}