@@ -3,6 +3,7 @@ package auth
33import (
44"testing"
55"gopkg.in/macaroon.v2"
6+ "time"
67)
78
89func TestCheckNonceFunction (t * testing.T ) {
@@ -16,7 +17,7 @@ func TestCheckNonceFunction(t *testing.T) {
1617// Check that check nonce function fail because macaroon not contains
1718// nonce and time constraint/caveat/field.
1819{
19- db := NewInMemoryDB (rootKey )
20+ db := NewInMemoryDB (rootKey , MacaroonLifetime )
2021
2122if err := CheckNonce (m , 1 , db , MacaroonLifetime ); err == nil {
2223t .Fatalf ("expected to fail because don't have time and nonce fields" +
@@ -33,7 +34,7 @@ func TestCheckNonceFunction(t *testing.T) {
3334// Check that check nonce function fail because macaroon not contains time
3435// constraint/caveat/field.
3536{
36- db := NewInMemoryDB (rootKey )
37+ db := NewInMemoryDB (rootKey , MacaroonLifetime )
3738
3839if err := CheckNonce (m , 1 , db , MacaroonLifetime ); err == nil {
3940t .Fatalf ("expected to fail because don't have time field: %v" , err )
@@ -48,7 +49,7 @@ func TestCheckNonceFunction(t *testing.T) {
4849// Check that check nonce function fail because macaroon has expired.
4950// working.
5051{
51- db := NewInMemoryDB (rootKey )
52+ db := NewInMemoryDB (rootKey , MacaroonLifetime )
5253
5354if err := CheckNonce (m , 1 , db , 0 ); err != ErrMacaroonExpired {
5455t .Fatalf ("expected to fail because macaron expired: %v" , err )
@@ -58,22 +59,57 @@ func TestCheckNonceFunction(t *testing.T) {
5859// Check that check nonce function fail because nonce has been used.
5960{
6061db := & InMemoryDB {
61- nonces : map [uint32 ] int64 { 1 : nonce },
62+ nonces : map [string ]time. Time { getKey ( 1 , nonce ): time . Now () },
6263rootKey : rootKey ,
6364}
6465
65- if err := CheckNonce (m , 1 , db , MacaroonLifetime ); err != ErrNonceRepeated {
66+ if err := CheckNonce (m , 1 , db , MacaroonLifetime ); err != ErrNonceUsed {
6667t .Fatalf ("expected to fail because macaron nonce has been used" +
6768": %v" , err )
6869}
6970}
7071
7172// Check that check nonce function fail because nonce has been used.
7273{
73- db := NewInMemoryDB (rootKey )
74+ db := NewInMemoryDB (rootKey , MacaroonLifetime )
7475
7576if err := CheckNonce (m , 1 , db , MacaroonLifetime ); err != nil {
76- t .Fatalf ("unable to check macaroon" )
77+ t .Fatalf ("unable to check macaroon: %v" , err )
7778}
7879}
7980}
81+
82+ func TestNonceFlush (t * testing.T ) {
83+ rootKey := []byte ("kek" )
84+ m , err := macaroon .New (rootKey , nil , "bitlum" , macaroon .LatestVersion )
85+ if err != nil {
86+ t .Fatalf ("unable to create macaron: %v" , err )
87+ }
88+
89+ flushPeriod := time .Millisecond * 50
90+ db := NewInMemoryDB (rootKey , flushPeriod )
91+
92+ // Pretend that nonce was already used
93+ nonce := int64 (100 )
94+ userID := uint32 (1 )
95+ db .nonces = map [string ]time.Time {getKey (userID , nonce ): time .Now ()}
96+
97+ m , err = AddNonce (m , nonce )
98+ if err != nil {
99+ t .Fatalf ("unable to add nonce: %v" , err )
100+ }
101+
102+ m , err = AddCurrentTime (m )
103+ if err != nil {
104+ t .Fatalf ("unable to add current time: %v" , err )
105+ }
106+
107+ // Start nonce flushing and wait more than flushing period.
108+ db .StartFlushing ()
109+ defer db .StopFlushing ()
110+ time .Sleep (2 * flushPeriod )
111+
112+ if err := CheckNonce (m , userID , db , MacaroonLifetime ); err != nil {
113+ t .Fatalf ("unable to check macaroon: %v" , err )
114+ }
115+ }
0 commit comments