blob: b4f44f80fa20908839cc1d1ecc22aadfcd9cdcab [file] [log] [blame]
Asim Shankar45054a62014-05-15 10:32:54 -07001package caveat_test
2
3import (
Andres Erbsencdeacfe2014-06-11 14:55:16 -07004 "net"
Asim Shankar45054a62014-05-15 10:32:54 -07005 "testing"
6 "time"
7
8 "veyron/security/caveat"
Andres Erbsencdeacfe2014-06-11 14:55:16 -07009 "veyron2/naming"
Asim Shankar45054a62014-05-15 10:32:54 -070010 "veyron2/security"
11)
12
Andres Erbsencdeacfe2014-06-11 14:55:16 -070013// endpoint implements naming.Endpoint
14type endpoint struct {
15 naming.Endpoint
16 addr net.Addr
17}
18
19func (e endpoint) Addr() net.Addr { return e.addr }
20
Asim Shankar45054a62014-05-15 10:32:54 -070021type context struct {
Andres Erbsencdeacfe2014-06-11 14:55:16 -070022 local, remote security.PublicID
23 localEndpoint, remoteEndpoint endpoint
24 method string
Asim Shankar45054a62014-05-15 10:32:54 -070025}
26
27func (c *context) Method() string { return c.method }
28func (c *context) Name() string { return "some_name" }
29func (c *context) Suffix() string { return "some_suffix" }
30func (c *context) Label() security.Label { return security.AdminLabel }
31func (c *context) CaveatDischarges() security.CaveatDischargeMap { return nil }
32func (c *context) LocalID() security.PublicID { return c.local }
33func (c *context) RemoteID() security.PublicID { return c.remote }
Andres Erbsencdeacfe2014-06-11 14:55:16 -070034func (c *context) LocalEndpoint() naming.Endpoint { return &c.localEndpoint }
35func (c *context) RemoteEndpoint() naming.Endpoint { return &c.remoteEndpoint }
Asim Shankar45054a62014-05-15 10:32:54 -070036
37func TestCaveats(t *testing.T) {
38 var (
39 alice = security.FakePublicID("alice")
40 bob = security.FakePublicID("bob")
41 )
42 now := time.Now()
43 tests := []struct {
44 c security.Caveat
45 ok bool
46 }{
47 {&caveat.Expiry{IssueTime: now, ExpiryTime: now.Add(time.Hour)}, true},
48 {&caveat.Expiry{IssueTime: now.Add(-1 * time.Hour), ExpiryTime: now.Add(-1 * time.Minute)}, false},
49 {caveat.MethodRestriction(nil), false},
Andres Erbsencdeacfe2014-06-11 14:55:16 -070050 {caveat.NetworkType("udp"), false},
51 {caveat.NetworkType("tcp"), true},
Asim Shankar45054a62014-05-15 10:32:54 -070052 {caveat.MethodRestriction{"Pause", "Play"}, true},
53 {caveat.MethodRestriction{"List"}, false},
54 {caveat.PeerIdentity(nil), false},
55 {caveat.PeerIdentity{"fake/alice"}, true},
56 {caveat.PeerIdentity{"fake/carol"}, false},
57 {caveat.PeerIdentity{"fake/alice", "fake/carol"}, true},
58 }
Andres Erbsencdeacfe2014-06-11 14:55:16 -070059 ctx := &context{local: alice, remote: bob, method: "Play", remoteEndpoint: endpoint{addr: &net.TCPAddr{}}}
Asim Shankar45054a62014-05-15 10:32:54 -070060 for _, test := range tests {
61 if err := test.c.Validate(ctx); test.ok != (err == nil) {
62 t.Errorf("Caveat:%#v. Got error:%v, want error:%v", test.c, err, test.ok)
63 }
64 }
65}