1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
package sign
import (
"crypto/rsa"
"testing"
"time"
)
func TestNewCookieSigner(t *testing.T) {
privKey, err := rsa.GenerateKey(randReader, 1024)
if err != nil {
t.Fatalf("Unexpected priv key error, %#v", err)
}
signer := NewCookieSigner("keyID", privKey)
if e, a := "keyID", signer.keyID; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := privKey, signer.privKey; e != a {
t.Errorf("expect %v, got %v", e, a)
}
}
func TestSignCookie(t *testing.T) {
privKey, err := rsa.GenerateKey(randReader, 1024)
if err != nil {
t.Errorf("expect no error, got %v", err)
}
signer := NewCookieSigner("keyID", privKey)
cookies, err := signer.Sign("http*://*", time.Now().Add(1*time.Hour))
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := CookiePolicyName, cookies[0].Name; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := CookieSignatureName, cookies[1].Name; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := CookieKeyIDName, cookies[2].Name; e != a {
t.Errorf("expect %v, got %v", e, a)
}
}
func TestSignCookie_WithPolicy(t *testing.T) {
privKey, err := rsa.GenerateKey(randReader, 1024)
if err != nil {
t.Errorf("expect no error, got %v", err)
}
p := &Policy{
Statements: []Statement{
{
Resource: "*",
Condition: Condition{
DateLessThan: &AWSEpochTime{time.Now().Add(1 * time.Hour)},
},
},
},
}
signer := NewCookieSigner("keyID", privKey)
cookies, err := signer.SignWithPolicy(p)
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := CookiePolicyName, cookies[0].Name; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := CookieSignatureName, cookies[1].Name; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := CookieKeyIDName, cookies[2].Name; e != a {
t.Errorf("expect %v, got %v", e, a)
}
}
func TestSignCookie_WithCookieOptions(t *testing.T) {
privKey, err := rsa.GenerateKey(randReader, 1024)
if err != nil {
t.Errorf("expect no error, got %v", err)
}
expires := time.Now().Add(1 * time.Hour)
signer := NewCookieSigner("keyID", privKey)
cookies, err := signer.Sign("https://example.com/*", expires, func(o *CookieOptions) {
o.Path = "/"
o.Domain = ".example.com"
o.Secure = true
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if e, a := CookiePolicyName, cookies[0].Name; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := CookieSignatureName, cookies[1].Name; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := CookieKeyIDName, cookies[2].Name; e != a {
t.Errorf("expect %v, got %v", e, a)
}
for _, c := range cookies {
if e, a := "/", c.Path; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := ".example.com", c.Domain; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if !c.Secure {
t.Errorf("expect to be true")
}
}
}
|