File: nonce_store_test.go

package info (click to toggle)
golang-github-yohcop-openid-go 0.0~git20170901.0.cfc72ed-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 184 kB
  • sloc: makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,239 bytes parent folder | download
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
package openid

import (
	"testing"
	"time"
)

func TestDefaultNonceStore(t *testing.T) {
	*maxNonceAge = 60 * time.Second
	now := time.Now().UTC()
	// 30 seconds ago
	now30s := now.Add(-30 * time.Second)
	// 2 minutes ago
	now2m := now.Add(-2 * time.Minute)

	now30sStr := now30s.Format(time.RFC3339)
	now2mStr := now2m.Format(time.RFC3339)

	ns := NewSimpleNonceStore()
	reject(t, ns, "1", "foo")                        // invalid nonce
	reject(t, ns, "1", "fooBarBazLongerThan20Chars") // invalid nonce

	accept(t, ns, "1", now30sStr+"asd")
	reject(t, ns, "1", now30sStr+"asd") // same nonce
	accept(t, ns, "1", now30sStr+"xxx") // different nonce
	reject(t, ns, "1", now30sStr+"xxx") // different nonce again to verify storage of multiple nonces per endpoint
	accept(t, ns, "2", now30sStr+"asd") // different endpoint

	reject(t, ns, "1", now2mStr+"old") // too old
	reject(t, ns, "3", now2mStr+"old") // too old
}

func accept(t *testing.T, ns NonceStore, op, nonce string) {
	e := ns.Accept(op, nonce)
	if e != nil {
		t.Errorf("Should accept %s nonce %s", op, nonce)
	}
}

func reject(t *testing.T, ns NonceStore, op, nonce string) {
	e := ns.Accept(op, nonce)
	if e == nil {
		t.Errorf("Should reject %s nonce %s", op, nonce)
	}
}