github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/third_party/code.google.com/p/xsrftoken/xsrf_test.go (about)

     1  // Copyright 2012 Google Inc. All Rights Reserved.
     2  // 
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // 
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  // 
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package xsrftoken
    16  
    17  import (
    18  	"encoding/base64"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  const (
    24  	key      = "quay"
    25  	userID   = "12345678"
    26  	actionID = "POST /form"
    27  )
    28  
    29  var (
    30  	now              = time.Now()
    31  	oneMinuteFromNow = now.Add(1 * time.Minute)
    32  )
    33  
    34  func TestValidToken(t *testing.T) {
    35  	tok := generateAtTime(key, userID, actionID, now)
    36  	if !validAtTime(tok, key, userID, actionID, oneMinuteFromNow) {
    37  		t.Error("One second later: Expected token to be valid")
    38  	}
    39  	if !validAtTime(tok, key, userID, actionID, now.Add(Timeout-1*time.Nanosecond)) {
    40  		t.Error("Just before timeout: Expected token to be valid")
    41  	}
    42  	if !validAtTime(tok, key, userID, actionID, now.Add(-1*time.Minute)) {
    43  		t.Error("One minute in the past: Expected token to be valid")
    44  	}
    45  }
    46  
    47  // TestSeparatorReplacement tests that separators are being correctly substituted
    48  func TestSeparatorReplacement(t *testing.T) {
    49  	tok := generateAtTime("foo:bar", "baz", "wah", now)
    50  	tok2 := generateAtTime("foo", "bar:baz", "wah", now)
    51  	if tok == tok2 {
    52  		t.Errorf("Expected generated tokens to be different")
    53  	}
    54  }
    55  
    56  func TestInvalidToken(t *testing.T) {
    57  	invalidTokenTests := []struct {
    58  		name, key, userID, actionID string
    59  		t                           time.Time
    60  	}{
    61  		{"Bad key", "foobar", userID, actionID, oneMinuteFromNow},
    62  		{"Bad userID", key, "foobar", actionID, oneMinuteFromNow},
    63  		{"Bad actionID", key, userID, "foobar", oneMinuteFromNow},
    64  		{"Expired", key, userID, actionID, now.Add(Timeout)},
    65  		{"More than 1 minute from the future", key, userID, actionID, now.Add(-1*time.Nanosecond - 1*time.Minute)},
    66  	}
    67  
    68  	tok := generateAtTime(key, userID, actionID, now)
    69  	for _, itt := range invalidTokenTests {
    70  		if validAtTime(tok, itt.key, itt.userID, itt.actionID, itt.t) {
    71  			t.Errorf("%v: Expected token to be invalid", itt.name)
    72  		}
    73  	}
    74  }
    75  
    76  // TestValidateBadData primarily tests that no unexpected panics are triggered
    77  // during parsing
    78  func TestValidateBadData(t *testing.T) {
    79  	badDataTests := []struct {
    80  		name, tok string
    81  	}{
    82  		{"Invalid Base64", "ASDab24(@)$*=="},
    83  		{"No delimiter", base64.URLEncoding.EncodeToString([]byte("foobar12345678"))},
    84  		{"Invalid time", base64.URLEncoding.EncodeToString([]byte("foobar:foobar"))},
    85  	}
    86  
    87  	for _, bdt := range badDataTests {
    88  		if validAtTime(bdt.tok, key, userID, actionID, oneMinuteFromNow) {
    89  			t.Errorf("%v: Expected token to be invalid", bdt.name)
    90  		}
    91  	}
    92  }