8005: Add tar Ruby build dependency on CentOS 6.
[arvados.git] / sdk / go / keepclient / perms_test.go
1 package keepclient
2
3 import (
4         "testing"
5         "time"
6 )
7
8 const (
9         knownHash    = "acbd18db4cc2f85cedef654fccc4a4d8"
10         knownLocator = knownHash + "+3"
11         knownToken   = "hocfupkn2pjhrpgp2vxv8rsku7tvtx49arbc9s4bvu7p7wxqvk"
12         knownKey     = "13u9fkuccnboeewr0ne3mvapk28epf68a3bhj9q8sb4l6e4e5mkk" +
13                 "p6nhj2mmpscgu1zze5h5enydxfe3j215024u16ij4hjaiqs5u4pzsl3nczmaoxnc" +
14                 "ljkm4875xqn4xv058koz3vkptmzhyheiy6wzevzjmdvxhvcqsvr5abhl15c2d4o4" +
15                 "jhl0s91lojy1mtrzqqvprqcverls0xvy9vai9t1l1lvvazpuadafm71jl4mrwq2y" +
16                 "gokee3eamvjy8qq1fvy238838enjmy5wzy2md7yvsitp5vztft6j4q866efym7e6" +
17                 "vu5wm9fpnwjyxfldw3vbo01mgjs75rgo7qioh8z8ij7jpyp8508okhgbbex3ceei" +
18                 "786u5rw2a9gx743dj3fgq2irk"
19         knownSignature     = "257f3f5f5f0a4e4626a18fc74bd42ec34dcb228a"
20         knownTimestamp     = "7fffffff"
21         knownSigHint       = "+A" + knownSignature + "@" + knownTimestamp
22         knownSignedLocator = knownLocator + knownSigHint
23 )
24
25 func TestSignLocator(t *testing.T) {
26         if ts, err := parseHexTimestamp(knownTimestamp); err != nil {
27                 t.Errorf("bad knownTimestamp %s", knownTimestamp)
28         } else {
29                 if knownSignedLocator != SignLocator(knownLocator, knownToken, ts, []byte(knownKey)) {
30                         t.Fail()
31                 }
32         }
33 }
34
35 func TestVerifySignature(t *testing.T) {
36         if VerifySignature(knownSignedLocator, knownToken, []byte(knownKey)) != nil {
37                 t.Fail()
38         }
39 }
40
41 func TestVerifySignatureExtraHints(t *testing.T) {
42         if VerifySignature(knownLocator+"+K@xyzzy"+knownSigHint, knownToken, []byte(knownKey)) != nil {
43                 t.Fatal("Verify cannot handle hint before permission signature")
44         }
45
46         if VerifySignature(knownLocator+knownSigHint+"+Zfoo", knownToken, []byte(knownKey)) != nil {
47                 t.Fatal("Verify cannot handle hint after permission signature")
48         }
49
50         if VerifySignature(knownLocator+"+K@xyzzy"+knownSigHint+"+Zfoo", knownToken, []byte(knownKey)) != nil {
51                 t.Fatal("Verify cannot handle hints around permission signature")
52         }
53 }
54
55 // The size hint on the locator string should not affect signature validation.
56 func TestVerifySignatureWrongSize(t *testing.T) {
57         if VerifySignature(knownHash+"+999999"+knownSigHint, knownToken, []byte(knownKey)) != nil {
58                 t.Fatal("Verify cannot handle incorrect size hint")
59         }
60
61         if VerifySignature(knownHash+knownSigHint, knownToken, []byte(knownKey)) != nil {
62                 t.Fatal("Verify cannot handle missing size hint")
63         }
64 }
65
66 func TestVerifySignatureBadSig(t *testing.T) {
67         badLocator := knownLocator + "+Aaaaaaaaaaaaaaaa@" + knownTimestamp
68         if VerifySignature(badLocator, knownToken, []byte(knownKey)) != ErrSignatureMissing {
69                 t.Fail()
70         }
71 }
72
73 func TestVerifySignatureBadTimestamp(t *testing.T) {
74         badLocator := knownLocator + "+A" + knownSignature + "@OOOOOOOl"
75         if VerifySignature(badLocator, knownToken, []byte(knownKey)) != ErrSignatureMissing {
76                 t.Fail()
77         }
78 }
79
80 func TestVerifySignatureBadSecret(t *testing.T) {
81         if VerifySignature(knownSignedLocator, knownToken, []byte("00000000000000000000")) != ErrSignatureInvalid {
82                 t.Fail()
83         }
84 }
85
86 func TestVerifySignatureBadToken(t *testing.T) {
87         if VerifySignature(knownSignedLocator, "00000000", []byte(knownKey)) != ErrSignatureInvalid {
88                 t.Fail()
89         }
90 }
91
92 func TestVerifySignatureExpired(t *testing.T) {
93         yesterday := time.Now().AddDate(0, 0, -1)
94         expiredLocator := SignLocator(knownHash, knownToken, yesterday, []byte(knownKey))
95         if VerifySignature(expiredLocator, knownToken, []byte(knownKey)) != ErrSignatureExpired {
96                 t.Fail()
97         }
98 }