github.com/Psiphon-Labs/net@v0.0.0-20191204183604-f5d60dada742/xsrftoken/xsrf_test.go (about) 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 5 package xsrftoken 6 7 import ( 8 "encoding/base64" 9 "testing" 10 "time" 11 ) 12 13 const ( 14 key = "quay" 15 userID = "12345678" 16 actionID = "POST /form" 17 ) 18 19 var ( 20 now = time.Now() 21 oneMinuteFromNow = now.Add(1 * time.Minute) 22 ) 23 24 func TestValidToken(t *testing.T) { 25 tok := generateTokenAtTime(key, userID, actionID, now) 26 if !validTokenAtTime(tok, key, userID, actionID, oneMinuteFromNow) { 27 t.Error("One second later: Expected token to be valid") 28 } 29 if !validTokenAtTime(tok, key, userID, actionID, now.Add(Timeout-1*time.Nanosecond)) { 30 t.Error("Just before timeout: Expected token to be valid") 31 } 32 if !validTokenAtTime(tok, key, userID, actionID, now.Add(-1*time.Minute+1*time.Millisecond)) { 33 t.Error("One minute in the past: Expected token to be valid") 34 } 35 } 36 37 // TestSeparatorReplacement tests that separators are being correctly substituted 38 func TestSeparatorReplacement(t *testing.T) { 39 separatorTests := []struct { 40 name string 41 token1 string 42 token2 string 43 }{ 44 { 45 "Colon", 46 generateTokenAtTime("foo:bar", "baz", "wah", now), 47 generateTokenAtTime("foo", "bar:baz", "wah", now), 48 }, 49 { 50 "Colon and Underscore", 51 generateTokenAtTime("key", ":foo:", "wah", now), 52 generateTokenAtTime("key", "_foo_", "wah", now), 53 }, 54 { 55 "Colon and Double Colon", 56 generateTokenAtTime("key", ":foo:", "wah", now), 57 generateTokenAtTime("key", "::foo::", "wah", now), 58 }, 59 } 60 61 for _, st := range separatorTests { 62 if st.token1 == st.token2 { 63 t.Errorf("%v: Expected generated tokens to be different", st.name) 64 } 65 } 66 } 67 68 func TestInvalidToken(t *testing.T) { 69 invalidTokenTests := []struct { 70 name, key, userID, actionID string 71 t time.Time 72 }{ 73 {"Bad key", "foobar", userID, actionID, oneMinuteFromNow}, 74 {"Bad userID", key, "foobar", actionID, oneMinuteFromNow}, 75 {"Bad actionID", key, userID, "foobar", oneMinuteFromNow}, 76 {"Expired", key, userID, actionID, now.Add(Timeout + 1*time.Millisecond)}, 77 {"More than 1 minute from the future", key, userID, actionID, now.Add(-1*time.Nanosecond - 1*time.Minute)}, 78 } 79 80 tok := generateTokenAtTime(key, userID, actionID, now) 81 for _, itt := range invalidTokenTests { 82 if validTokenAtTime(tok, itt.key, itt.userID, itt.actionID, itt.t) { 83 t.Errorf("%v: Expected token to be invalid", itt.name) 84 } 85 } 86 } 87 88 // TestValidateBadData primarily tests that no unexpected panics are triggered 89 // during parsing 90 func TestValidateBadData(t *testing.T) { 91 badDataTests := []struct { 92 name, tok string 93 }{ 94 {"Invalid Base64", "ASDab24(@)$*=="}, 95 {"No delimiter", base64.URLEncoding.EncodeToString([]byte("foobar12345678"))}, 96 {"Invalid time", base64.URLEncoding.EncodeToString([]byte("foobar:foobar"))}, 97 {"Wrong length", "1234" + generateTokenAtTime(key, userID, actionID, now)}, 98 } 99 100 for _, bdt := range badDataTests { 101 if validTokenAtTime(bdt.tok, key, userID, actionID, oneMinuteFromNow) { 102 t.Errorf("%v: Expected token to be invalid", bdt.name) 103 } 104 } 105 }