blob: 53b99def762a7de68d6c0689c577035ab6d1741d [file] [log] [blame]
Ankur100eb272014-09-15 16:48:12 -07001package rt
2
3import (
4 "crypto/ecdsa"
5 "crypto/elliptic"
6 "crypto/rand"
7 "fmt"
8 "io/ioutil"
9 "os"
Ankur100eb272014-09-15 16:48:12 -070010 "testing"
11
Jiri Simsa519c5072014-09-17 21:37:57 -070012 "veyron.io/veyron/veyron2/security"
Ankur100eb272014-09-15 16:48:12 -070013)
14
15type s []string
16
Ankurb4f03a12014-09-24 10:19:03 -070017type rootsTester struct {
Ankur100eb272014-09-15 16:48:12 -070018 k1, k2, k3 security.PublicKey
19}
20
Ankurb4f03a12014-09-24 10:19:03 -070021func (t *rootsTester) testAdd(br security.BlessingRoots) error {
Ankur100eb272014-09-15 16:48:12 -070022 testdata := []struct {
23 root security.PublicKey
24 pattern security.BlessingPattern
25 }{
26 {t.k1, "veyron/..."},
27 {t.k2, "google/foo/..."},
28 {t.k1, "google"},
29 }
30 for _, d := range testdata {
31 if err := br.Add(d.root, d.pattern); err != nil {
32 return fmt.Errorf("%v.Add(%v, %q) failed: %s", br, d.root, d.pattern, err)
33 }
34 }
35 return nil
36}
37
Ankurb4f03a12014-09-24 10:19:03 -070038func (t *rootsTester) testRecognized(br security.BlessingRoots) error {
Ankur100eb272014-09-15 16:48:12 -070039 testdata := []struct {
40 root security.PublicKey
41 recognized []string
42 notRecognized []string
43 }{
44 {t.k1, s{"veyron", "veyron/foo", "veyron/foo/bar", "google"}, s{"google/foo", "foo", "foo/bar"}},
45 {t.k2, s{"google", "google/foo", "google/foo/bar"}, s{"google/bar", "veyron", "veyron/foo", "foo", "foo/bar"}},
46 {t.k3, s{}, s{"veyron", "veyron/foo", "veyron/bar", "google", "google/foo", "google/bar", "foo", "foo/bar"}},
47 }
48 for _, d := range testdata {
49 for _, b := range d.recognized {
50 if err := br.Recognized(d.root, b); err != nil {
51 return fmt.Errorf("%v.Recognized(%v, %q): got: %v, want nil", br, d.root, b, err)
52 }
53 }
54 for _, b := range d.notRecognized {
55 if err := matchesError(br.Recognized(d.root, b), "not a recognized root"); err != nil {
56 return fmt.Errorf("%v.Recognized(%v, %q): %v", br, d.root, b, err)
57 }
58 }
59 }
60 return nil
61}
62
63func mkKey() security.PublicKey {
64 s, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
65 if err != nil {
66 panic(err)
67 }
68 return security.NewECDSAPublicKey(&s.PublicKey)
69}
70
Ankur100eb272014-09-15 16:48:12 -070071func TestInMemoryBlessingRoots(t *testing.T) {
Ankur7c890592014-10-02 11:36:28 -070072 br := newInMemoryBlessingRoots()
Ankurb4f03a12014-09-24 10:19:03 -070073 rootsTester := rootsTester{mkKey(), mkKey(), mkKey()}
74 if err := rootsTester.testAdd(br); err != nil {
Ankur100eb272014-09-15 16:48:12 -070075 t.Error(err)
76 }
Ankurb4f03a12014-09-24 10:19:03 -070077 if err := rootsTester.testRecognized(br); err != nil {
Ankur100eb272014-09-15 16:48:12 -070078 t.Error(err)
79 }
80}
Ankurb4f03a12014-09-24 10:19:03 -070081
Ankur100eb272014-09-15 16:48:12 -070082func TestPersistingBlessingRoots(t *testing.T) {
83 newTempDir := func(name string) string {
84 dir, err := ioutil.TempDir("", name)
85 if err != nil {
86 t.Fatal(err)
87 }
88 return dir
89 }
90
Ankurb4f03a12014-09-24 10:19:03 -070091 rootsTester := rootsTester{mkKey(), mkKey(), mkKey()}
Ankur100eb272014-09-15 16:48:12 -070092
93 // Create a new persisting BlessingRoots and add key k1 as an authority over
94 // blessings matching "veyron/...".
95 dir := newTempDir("blessingstore")
96 defer os.RemoveAll(dir)
Ankurb4f03a12014-09-24 10:19:03 -070097 signer := newPrincipal(t)
Ankur7c890592014-10-02 11:36:28 -070098 br, err := newPersistingBlessingRoots(dir, signer)
Ankur100eb272014-09-15 16:48:12 -070099 if err != nil {
Ankur7c890592014-10-02 11:36:28 -0700100 t.Fatalf("newPersistingBlessingRoots failed: %s", err)
Ankur100eb272014-09-15 16:48:12 -0700101 }
102
Ankurb4f03a12014-09-24 10:19:03 -0700103 if err := rootsTester.testAdd(br); err != nil {
Ankur100eb272014-09-15 16:48:12 -0700104 t.Error(err)
105 }
Ankurb4f03a12014-09-24 10:19:03 -0700106 if err := rootsTester.testRecognized(br); err != nil {
Ankur100eb272014-09-15 16:48:12 -0700107 t.Error(err)
108 }
109
110 // Test that all mutations are appropriately reflected in a BlessingRoots
111 // constructed from same directory and signer.
Ankur7c890592014-10-02 11:36:28 -0700112 br, err = newPersistingBlessingRoots(dir, signer)
Ankur100eb272014-09-15 16:48:12 -0700113 if err != nil {
Ankur7c890592014-10-02 11:36:28 -0700114 t.Fatalf("newPersistingBlessingRoots failed: %s", err)
Ankur100eb272014-09-15 16:48:12 -0700115 }
Ankurb4f03a12014-09-24 10:19:03 -0700116 if err := rootsTester.testRecognized(br); err != nil {
Ankur100eb272014-09-15 16:48:12 -0700117 t.Error(err)
118 }
119}