Skip to content

Commit 21129ec

Browse files
authored
node: make jwt tests less time-dependent (#25120)
1 parent 01e5e9c commit 21129ec

File tree

1 file changed

+76
-31
lines changed

1 file changed

+76
-31
lines changed

node/rpcstack_test.go

Lines changed: 76 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -319,55 +319,100 @@ func TestJWT(t *testing.T) {
319319
wsUrl := fmt.Sprintf("ws://%v", srv.listenAddr())
320320
htUrl := fmt.Sprintf("http://%v", srv.listenAddr())
321321

322-
expOk := []string{
323-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()})),
324-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() + 4})),
325-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() - 4})),
326-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{
327-
"iat": time.Now().Unix(),
328-
"exp": time.Now().Unix() + 2,
329-
})),
330-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{
331-
"iat": time.Now().Unix(),
332-
"bar": "baz",
333-
})),
322+
expOk := []func() string{
323+
func() string {
324+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()}))
325+
},
326+
func() string {
327+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() + 4}))
328+
},
329+
func() string {
330+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() - 4}))
331+
},
332+
func() string {
333+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{
334+
"iat": time.Now().Unix(),
335+
"exp": time.Now().Unix() + 2,
336+
}))
337+
},
338+
func() string {
339+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{
340+
"iat": time.Now().Unix(),
341+
"bar": "baz",
342+
}))
343+
},
334344
}
335-
for i, token := range expOk {
345+
for i, tokenFn := range expOk {
346+
token := tokenFn()
336347
if err := wsRequest(t, wsUrl, "Authorization", token); err != nil {
337348
t.Errorf("test %d-ws, token '%v': expected ok, got %v", i, token, err)
338349
}
350+
token = tokenFn()
339351
if resp := rpcRequest(t, htUrl, "Authorization", token); resp.StatusCode != 200 {
340352
t.Errorf("test %d-http, token '%v': expected ok, got %v", i, token, resp.StatusCode)
341353
}
342354
}
343-
expFail := []string{
355+
356+
expFail := []func() string{
344357
// future
345-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() + 6})),
358+
func() string {
359+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() + 6}))
360+
},
346361
// stale
347-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() - 6})),
362+
func() string {
363+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() - 6}))
364+
},
348365
// wrong algo
349-
fmt.Sprintf("Bearer %v", issueToken(secret, jwt.SigningMethodHS512, testClaim{"iat": time.Now().Unix() + 4})),
366+
func() string {
367+
return fmt.Sprintf("Bearer %v", issueToken(secret, jwt.SigningMethodHS512, testClaim{"iat": time.Now().Unix() + 4}))
368+
},
350369
// expired
351-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix(), "exp": time.Now().Unix()})),
370+
func() string {
371+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix(), "exp": time.Now().Unix()}))
372+
},
352373
// missing mandatory iat
353-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{})),
354-
// wrong secret
355-
fmt.Sprintf("Bearer %v", issueToken([]byte("wrong"), nil, testClaim{"iat": time.Now().Unix()})),
356-
fmt.Sprintf("Bearer %v", issueToken([]byte{}, nil, testClaim{"iat": time.Now().Unix()})),
357-
fmt.Sprintf("Bearer %v", issueToken(nil, nil, testClaim{"iat": time.Now().Unix()})),
374+
func() string {
375+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{}))
376+
},
377+
// wrong secret
378+
func() string {
379+
return fmt.Sprintf("Bearer %v", issueToken([]byte("wrong"), nil, testClaim{"iat": time.Now().Unix()}))
380+
},
381+
func() string {
382+
return fmt.Sprintf("Bearer %v", issueToken([]byte{}, nil, testClaim{"iat": time.Now().Unix()}))
383+
},
384+
func() string {
385+
return fmt.Sprintf("Bearer %v", issueToken(nil, nil, testClaim{"iat": time.Now().Unix()}))
386+
},
358387
// Various malformed syntax
359-
fmt.Sprintf("%v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()})),
360-
fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()})),
361-
fmt.Sprintf("bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()})),
362-
fmt.Sprintf("Bearer: %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()})),
363-
fmt.Sprintf("Bearer:%v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()})),
364-
fmt.Sprintf("Bearer\t%v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()})),
365-
fmt.Sprintf("Bearer \t%v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()})),
388+
func() string {
389+
return fmt.Sprintf("%v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()}))
390+
},
391+
func() string {
392+
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()}))
393+
},
394+
func() string {
395+
return fmt.Sprintf("bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()}))
396+
},
397+
func() string {
398+
return fmt.Sprintf("Bearer: %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()}))
399+
},
400+
func() string {
401+
return fmt.Sprintf("Bearer:%v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()}))
402+
},
403+
func() string {
404+
return fmt.Sprintf("Bearer\t%v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()}))
405+
},
406+
func() string {
407+
return fmt.Sprintf("Bearer \t%v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix()}))
408+
},
366409
}
367-
for i, token := range expFail {
410+
for i, tokenFn := range expFail {
411+
token := tokenFn()
368412
if err := wsRequest(t, wsUrl, "Authorization", token); err == nil {
369413
t.Errorf("tc %d-ws, token '%v': expected not to allow, got ok", i, token)
370414
}
415+
token = tokenFn()
371416
if resp := rpcRequest(t, htUrl, "Authorization", token); resp.StatusCode != 403 {
372417
t.Errorf("tc %d-http, token '%v': expected not to allow, got %v", i, token, resp.StatusCode)
373418
}

0 commit comments

Comments
 (0)