xsrf_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. separatorTests := []struct {
  34. name string
  35. token1 string
  36. token2 string
  37. }{
  38. {
  39. "Colon",
  40. generateTokenAtTime("foo:bar", "baz", "wah", now),
  41. generateTokenAtTime("foo", "bar:baz", "wah", now),
  42. },
  43. {
  44. "Colon and Underscore",
  45. generateTokenAtTime("key", ":foo:", "wah", now),
  46. generateTokenAtTime("key", "_foo_", "wah", now),
  47. },
  48. {
  49. "Colon and Double Colon",
  50. generateTokenAtTime("key", ":foo:", "wah", now),
  51. generateTokenAtTime("key", "::foo::", "wah", now),
  52. },
  53. }
  54. for _, st := range separatorTests {
  55. if st.token1 == st.token2 {
  56. t.Errorf("%v: Expected generated tokens to be different", st.name)
  57. }
  58. }
  59. }
  60. func TestInvalidToken(t *testing.T) {
  61. invalidTokenTests := []struct {
  62. name, key, userID, actionID string
  63. t time.Time
  64. }{
  65. {"Bad key", "foobar", userID, actionID, oneMinuteFromNow},
  66. {"Bad userID", key, "foobar", actionID, oneMinuteFromNow},
  67. {"Bad actionID", key, userID, "foobar", oneMinuteFromNow},
  68. {"Expired", key, userID, actionID, now.Add(Timeout + 1*time.Millisecond)},
  69. {"More than 1 minute from the future", key, userID, actionID, now.Add(-1*time.Nanosecond - 1*time.Minute)},
  70. }
  71. tok := generateTokenAtTime(key, userID, actionID, now)
  72. for _, itt := range invalidTokenTests {
  73. if validTokenAtTime(tok, itt.key, itt.userID, itt.actionID, itt.t) {
  74. t.Errorf("%v: Expected token to be invalid", itt.name)
  75. }
  76. }
  77. }
  78. // TestValidateBadData primarily tests that no unexpected panics are triggered
  79. // during parsing
  80. func TestValidateBadData(t *testing.T) {
  81. badDataTests := []struct {
  82. name, tok string
  83. }{
  84. {"Invalid Base64", "ASDab24(@)$*=="},
  85. {"No delimiter", base64.URLEncoding.EncodeToString([]byte("foobar12345678"))},
  86. {"Invalid time", base64.URLEncoding.EncodeToString([]byte("foobar:foobar"))},
  87. {"Wrong length", "1234" + generateTokenAtTime(key, userID, actionID, now)},
  88. }
  89. for _, bdt := range badDataTests {
  90. if validTokenAtTime(bdt.tok, key, userID, actionID, oneMinuteFromNow) {
  91. t.Errorf("%v: Expected token to be invalid", bdt.name)
  92. }
  93. }
  94. }