The reconcile package is used for DOM reconcilation in Isomorphic Go web applications.

xsrf_test.go 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }