You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB

  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xsrftoken
  5. import (
  6. "encoding/base64"
  7. "testing"
  8. "time"
  9. )
  10. const (
  11. key = "quay"
  12. userID = "12345678"
  13. actionID = "POST /form"
  14. )
  15. var (
  16. now = time.Now()
  17. oneMinuteFromNow = now.Add(1 * time.Minute)
  18. )
  19. func TestValidToken(t *testing.T) {
  20. tok := generateTokenAtTime(key, userID, actionID, now)
  21. if !validTokenAtTime(tok, key, userID, actionID, oneMinuteFromNow) {
  22. t.Error("One second later: Expected token to be valid")
  23. }
  24. if !validTokenAtTime(tok, key, userID, actionID, now.Add(Timeout-1*time.Nanosecond)) {
  25. t.Error("Just before timeout: Expected token to be valid")
  26. }
  27. if !validTokenAtTime(tok, key, userID, actionID, now.Add(-1*time.Minute+1*time.Millisecond)) {
  28. t.Error("One minute in the past: Expected token to be valid")
  29. }
  30. }
  31. // TestSeparatorReplacement tests that separators are being correctly substituted
  32. func TestSeparatorReplacement(t *testing.T) {
  33. tok := generateTokenAtTime("foo:bar", "baz", "wah", now)
  34. tok2 := generateTokenAtTime("foo", "bar:baz", "wah", now)
  35. if tok == tok2 {
  36. t.Errorf("Expected generated tokens to be different")
  37. }
  38. }
  39. func TestInvalidToken(t *testing.T) {
  40. invalidTokenTests := []struct {
  41. name, key, userID, actionID string
  42. t time.Time
  43. }{
  44. {"Bad key", "foobar", userID, actionID, oneMinuteFromNow},
  45. {"Bad userID", key, "foobar", actionID, oneMinuteFromNow},
  46. {"Bad actionID", key, userID, "foobar", oneMinuteFromNow},
  47. {"Expired", key, userID, actionID, now.Add(Timeout + 1*time.Millisecond)},
  48. {"More than 1 minute from the future", key, userID, actionID, now.Add(-1*time.Nanosecond - 1*time.Minute)},
  49. }
  50. tok := generateTokenAtTime(key, userID, actionID, now)
  51. for _, itt := range invalidTokenTests {
  52. if validTokenAtTime(tok, itt.key, itt.userID, itt.actionID, itt.t) {
  53. t.Errorf("%v: Expected token to be invalid", itt.name)
  54. }
  55. }
  56. }
  57. // TestValidateBadData primarily tests that no unexpected panics are triggered
  58. // during parsing
  59. func TestValidateBadData(t *testing.T) {
  60. badDataTests := []struct {
  61. name, tok string
  62. }{
  63. {"Invalid Base64", "ASDab24(@)$*=="},
  64. {"No delimiter", base64.URLEncoding.EncodeToString([]byte("foobar12345678"))},
  65. {"Invalid time", base64.URLEncoding.EncodeToString([]byte("foobar:foobar"))},
  66. {"Wrong length", "1234" + generateTokenAtTime(key, userID, actionID, now)},
  67. }
  68. for _, bdt := range badDataTests {
  69. if validTokenAtTime(bdt.tok, key, userID, actionID, oneMinuteFromNow) {
  70. t.Errorf("%v: Expected token to be invalid", bdt.name)
  71. }
  72. }
  73. }