Merge branch '8400-additional-gitignore' of https://github.com/wtsi-hgi/arvados close...
[arvados.git] / services / keepstore / perms_test.go
1 package main
2
3 import (
4         "strconv"
5         "testing"
6         "time"
7 )
8
9 const (
10         knownHash    = "acbd18db4cc2f85cedef654fccc4a4d8"
11         knownLocator = knownHash + "+3"
12         knownToken   = "hocfupkn2pjhrpgp2vxv8rsku7tvtx49arbc9s4bvu7p7wxqvk"
13         knownKey     = "13u9fkuccnboeewr0ne3mvapk28epf68a3bhj9q8sb4l6e4e5mkk" +
14                 "p6nhj2mmpscgu1zze5h5enydxfe3j215024u16ij4hjaiqs5u4pzsl3nczmaoxnc" +
15                 "ljkm4875xqn4xv058koz3vkptmzhyheiy6wzevzjmdvxhvcqsvr5abhl15c2d4o4" +
16                 "jhl0s91lojy1mtrzqqvprqcverls0xvy9vai9t1l1lvvazpuadafm71jl4mrwq2y" +
17                 "gokee3eamvjy8qq1fvy238838enjmy5wzy2md7yvsitp5vztft6j4q866efym7e6" +
18                 "vu5wm9fpnwjyxfldw3vbo01mgjs75rgo7qioh8z8ij7jpyp8508okhgbbex3ceei" +
19                 "786u5rw2a9gx743dj3fgq2irk"
20         knownSignature     = "257f3f5f5f0a4e4626a18fc74bd42ec34dcb228a"
21         knownTimestamp     = "7fffffff"
22         knownSigHint       = "+A" + knownSignature + "@" + knownTimestamp
23         knownSignedLocator = knownLocator + knownSigHint
24 )
25
26 func TestSignLocator(t *testing.T) {
27         defer func(b []byte) {
28                 PermissionSecret = b
29         }(PermissionSecret)
30
31         tsInt, err := strconv.ParseInt(knownTimestamp, 16, 0)
32         if err != nil {
33                 t.Fatal(err)
34         }
35         t0 := time.Unix(tsInt, 0)
36
37         PermissionSecret = []byte(knownKey)
38         if x := SignLocator(knownLocator, knownToken, t0); x != knownSignedLocator {
39                 t.Fatalf("Got %+q, expected %+q", x, knownSignedLocator)
40         }
41
42         PermissionSecret = []byte("arbitrarykey")
43         if x := SignLocator(knownLocator, knownToken, t0); x == knownSignedLocator {
44                 t.Fatalf("Got same signature %+q, even though PermissionSecret changed", x)
45         }
46 }
47
48 func TestVerifyLocator(t *testing.T) {
49         defer func(b []byte) {
50                 PermissionSecret = b
51         }(PermissionSecret)
52
53         PermissionSecret = []byte(knownKey)
54         if err := VerifySignature(knownSignedLocator, knownToken); err != nil {
55                 t.Fatal(err)
56         }
57
58         PermissionSecret = []byte("arbitrarykey")
59         if err := VerifySignature(knownSignedLocator, knownToken); err == nil {
60                 t.Fatal("Verified signature even with wrong PermissionSecret")
61         }
62 }