forked from
willdot.net/cocoon
A fork of the Cocoon PDS but being made more distributed.
1package dpop
2
3import (
4 "testing"
5 "time"
6
7 "github.com/stretchr/testify/assert"
8)
9
10func TestRoundtime(t *testing.T) {
11 tt := map[string]struct {
12 input time.Time
13 truncateTo time.Duration
14 expected time.Time
15 }{
16 "between 15:00:00 and 15:15:00 - rounds to 15:00:00": {
17 input: time.Date(2026, time.May, 01, 15, 14, 59, 0, time.UTC),
18 truncateTo: time.Minute * 15,
19 expected: time.Date(2026, time.May, 01, 15, 00, 0, 0, time.UTC),
20 },
21 "between 15:15:01 - rounds to 15:15:00": {
22 input: time.Date(2026, time.May, 01, 15, 15, 01, 0, time.UTC),
23 truncateTo: time.Minute * 15,
24 expected: time.Date(2026, time.May, 01, 15, 15, 0, 0, time.UTC),
25 },
26 "between 15:15:00 - rounds to 15:15:00": {
27 input: time.Date(2026, time.May, 01, 15, 15, 0, 0, time.UTC),
28 truncateTo: time.Minute * 15,
29 expected: time.Date(2026, time.May, 01, 15, 15, 0, 0, time.UTC),
30 },
31 }
32
33 for name, tc := range tt {
34 t.Run(name, func(t *testing.T) {
35 res := tc.input.Truncate(tc.truncateTo)
36 assert.Equal(t, tc.expected, res)
37 })
38 }
39}
40
41func TestTotpNonce(t *testing.T) {
42 startTime := time.Date(2026, time.May, 23, 15, 0, 0, 0, time.UTC)
43 args := TotpNonceArgs{
44 timeRoundDuration: time.Minute * 2,
45 Secret: []byte("secret"),
46 }
47 nonce := NewTotpNonce(args)
48 nonce.currentTimePeriodStart = startTime
49
50 assert.Equal(t, "ntNYQtG1F3h1U5OKz8Rs4yMJf08GUAtrGU9qg58Rt1o", nonce.curr)
51 assert.Equal(t, "aIHUJrjFeadCwoZQmd4aJ_g-Pm4ehkwckeXmqz3_42g", nonce.prev)
52 assert.Equal(t, "ON5oeN1v2NnseUWoUgK1CIU__qQn4mL9xbxJSu-ifUY", nonce.next)
53
54 // try and rotate after a simulated 16 minutes to make it go into the next time period
55 // and so will rotate the nonces
56 nonce.rotate(nonce.currentTimePeriodStart.Add(time.Minute * 16))
57
58 assert.Equal(t, "ON5oeN1v2NnseUWoUgK1CIU__qQn4mL9xbxJSu-ifUY", nonce.curr)
59 assert.Equal(t, "ntNYQtG1F3h1U5OKz8Rs4yMJf08GUAtrGU9qg58Rt1o", nonce.prev)
60 assert.Equal(t, "HTvHm6VQP7cBbTZKWT_zhJ09bxD6B6JfhX-kArn1Hvo", nonce.next)
61
62 // try and rotate after a simulated 5 minutes to which won't make it go into
63 // the next time period and so won't rotate
64 nonce.rotate(nonce.currentTimePeriodStart.Add(time.Minute * 5))
65
66 assert.Equal(t, "ON5oeN1v2NnseUWoUgK1CIU__qQn4mL9xbxJSu-ifUY", nonce.curr)
67 assert.Equal(t, "ntNYQtG1F3h1U5OKz8Rs4yMJf08GUAtrGU9qg58Rt1o", nonce.prev)
68 assert.Equal(t, "HTvHm6VQP7cBbTZKWT_zhJ09bxD6B6JfhX-kArn1Hvo", nonce.next)
69}