Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 5 | package mounttablelib_test |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 8 | "encoding/json" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 9 | "errors" |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 10 | "fmt" |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 11 | "io" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | "reflect" |
| 13 | "runtime/debug" |
| 14 | "sort" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 15 | "testing" |
| 16 | "time" |
| 17 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 18 | "v.io/v23" |
| 19 | "v.io/v23/context" |
David Why Use Two When One Will Do Presotto | 67b7071 | 2015-06-02 13:13:58 -0700 | [diff] [blame] | 20 | "v.io/v23/conventions" |
Robin Thellend | f15f295 | 2015-07-17 21:49:58 -0700 | [diff] [blame^] | 21 | "v.io/v23/glob" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 22 | "v.io/v23/naming" |
| 23 | "v.io/v23/options" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 24 | "v.io/v23/rpc" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 25 | "v.io/v23/security" |
Todd Wang | 387d8a4 | 2015-03-30 17:09:05 -0700 | [diff] [blame] | 26 | "v.io/v23/security/access" |
Asim Shankar | b425c55 | 2015-07-10 02:18:59 -0700 | [diff] [blame] | 27 | "v.io/v23/services/mounttable" |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 28 | "v.io/v23/services/stats" |
| 29 | "v.io/v23/vdl" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 30 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 31 | libstats "v.io/x/ref/lib/stats" |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 32 | "v.io/x/ref/lib/xrpc" |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 33 | "v.io/x/ref/services/debug/debuglib" |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 34 | "v.io/x/ref/services/mounttable/mounttablelib" |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 35 | "v.io/x/ref/test" |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 36 | "v.io/x/ref/test/testutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 37 | ) |
| 38 | |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 39 | func init() { |
| 40 | test.Init() |
| 41 | } |
| 42 | |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 43 | // Simulate different processes with different runtimes. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 44 | // rootCtx is the one running the mounttable service. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 45 | const ttlSecs = 60 * 60 |
| 46 | |
| 47 | func boom(t *testing.T, f string, v ...interface{}) { |
| 48 | t.Logf(f, v...) |
| 49 | t.Fatal(string(debug.Stack())) |
| 50 | } |
| 51 | |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 52 | func doMount(t *testing.T, ctx *context.T, ep, suffix, service string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 53 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 54 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 55 | if err := client.Call(ctx, name, "Mount", []interface{}{service, uint32(ttlSecs), 0}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 56 | if !shouldSucceed { |
| 57 | return |
| 58 | } |
| 59 | boom(t, "Failed to Mount %s onto %s: %s", service, name, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 63 | func doUnmount(t *testing.T, ctx *context.T, ep, suffix, service string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 64 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 65 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 66 | if err := client.Call(ctx, name, "Unmount", []interface{}{service}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 67 | if !shouldSucceed { |
| 68 | return |
| 69 | } |
Alex Fandrianto | a2e7c37 | 2015-04-20 10:46:10 -0700 | [diff] [blame] | 70 | boom(t, "Failed to Unmount %s off of %s: %s", service, name, err) |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 74 | func doGetPermissions(t *testing.T, ctx *context.T, ep, suffix string, shouldSucceed bool) (perms access.Permissions, version string) { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 75 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 76 | client := v23.GetClient(ctx) |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 77 | if err := client.Call(ctx, name, "GetPermissions", nil, []interface{}{&perms, &version}, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 78 | if !shouldSucceed { |
| 79 | return |
| 80 | } |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 81 | boom(t, "Failed to GetPermissions %s: %s", name, err) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 82 | } |
| 83 | return |
| 84 | } |
| 85 | |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 86 | func doSetPermissions(t *testing.T, ctx *context.T, ep, suffix string, perms access.Permissions, version string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 87 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 88 | client := v23.GetClient(ctx) |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 89 | if err := client.Call(ctx, name, "SetPermissions", []interface{}{perms, version}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 90 | if !shouldSucceed { |
| 91 | return |
| 92 | } |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 93 | boom(t, "Failed to SetPermissions %s: %s", name, err) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 94 | } |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 97 | func doDeleteNode(t *testing.T, ctx *context.T, ep, suffix string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 98 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 99 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 100 | if err := client.Call(ctx, name, "Delete", []interface{}{false}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 101 | if !shouldSucceed { |
| 102 | return |
| 103 | } |
| 104 | boom(t, "Failed to Delete node %s: %s", name, err) |
| 105 | } |
| 106 | } |
| 107 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 108 | func doDeleteSubtree(t *testing.T, ctx *context.T, ep, suffix string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 109 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 110 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 111 | if err := client.Call(ctx, name, "Delete", []interface{}{true}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 112 | if !shouldSucceed { |
| 113 | return |
| 114 | } |
| 115 | boom(t, "Failed to Delete subtree %s: %s", name, err) |
| 116 | } |
| 117 | } |
| 118 | |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 119 | func mountentry2names(e *naming.MountEntry) []string { |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 120 | names := make([]string, len(e.Servers)) |
| 121 | for idx, s := range e.Servers { |
| 122 | names[idx] = naming.JoinAddressName(s.Server, e.Name) |
| 123 | } |
| 124 | return names |
| 125 | } |
| 126 | |
| 127 | func strslice(strs ...string) []string { |
| 128 | return strs |
| 129 | } |
| 130 | |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 131 | func resolve(ctx *context.T, name string) (*naming.MountEntry, error) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 132 | // Resolve the name one level. |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 133 | var entry naming.MountEntry |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 134 | client := v23.GetClient(ctx) |
| 135 | if err := client.Call(ctx, name, "ResolveStep", nil, []interface{}{&entry}, options.NoResolve{}); err != nil { |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 136 | return nil, err |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 137 | } |
| 138 | if len(entry.Servers) < 1 { |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 139 | return nil, errors.New("resolve returned no servers") |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 140 | } |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 141 | return &entry, nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 144 | func export(t *testing.T, ctx *context.T, name, contents string) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 145 | // Resolve the name. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 146 | resolved, err := resolve(ctx, name) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 147 | if err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 148 | boom(t, "Failed to Export.Resolve %s: %s", name, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 149 | } |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 150 | // Export the value. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 151 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 152 | if err := client.Call(ctx, mountentry2names(resolved)[0], "Export", []interface{}{contents, true}, nil, options.NoResolve{}); err != nil { |
| 153 | boom(t, "Failed to Export.Call %s to %s: %s", name, contents, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 157 | func checkContents(t *testing.T, ctx *context.T, name, expected string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 158 | // Resolve the name. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 159 | resolved, err := resolve(ctx, name) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 160 | if err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 161 | if !shouldSucceed { |
| 162 | return |
| 163 | } |
| 164 | boom(t, "Failed to Resolve %s: %s", name, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 165 | } |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 166 | // Look up the value. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 167 | client := v23.GetClient(ctx) |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 168 | call, err := client.StartCall(ctx, mountentry2names(resolved)[0], "Lookup", nil, options.NoResolve{}) |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 169 | if err != nil { |
| 170 | if shouldSucceed { |
| 171 | boom(t, "Failed Lookup.StartCall %s: %s", name, err) |
| 172 | } |
| 173 | return |
| 174 | } |
| 175 | var contents []byte |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 176 | if err := call.Finish(&contents); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 177 | if shouldSucceed { |
| 178 | boom(t, "Failed to Lookup %s: %s", name, err) |
| 179 | } |
| 180 | return |
| 181 | } |
| 182 | if string(contents) != expected { |
| 183 | boom(t, "Lookup %s, expected %q, got %q", name, expected, contents) |
| 184 | } |
| 185 | if !shouldSucceed { |
| 186 | boom(t, "Lookup %s, expected failure, got %q", name, contents) |
| 187 | } |
| 188 | } |
| 189 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 190 | func newMT(t *testing.T, permsFile, persistDir, statsDir string, rootCtx *context.T) (func() error, string) { |
Cosmos Nicolaou | 64d573d | 2015-07-13 16:22:18 -0700 | [diff] [blame] | 191 | reservedDisp := debuglib.NewDispatcher(nil) |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 192 | ctx := v23.WithReservedNameDispatcher(rootCtx, reservedDisp) |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 193 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 194 | // Add mount table service. |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 195 | mt, err := mounttablelib.NewMountTableDispatcher(ctx, permsFile, persistDir, statsDir) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 196 | if err != nil { |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 197 | boom(t, "mounttablelib.NewMountTableDispatcher: %v", err) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 198 | } |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 199 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 200 | // Start serving on a loopback address. |
| 201 | server, err := xrpc.NewDispatchingServer(ctx, "", mt, options.ServesMountTable(true)) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 202 | if err != nil { |
| 203 | boom(t, "r.NewServer: %s", err) |
| 204 | } |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 205 | |
| 206 | estr := server.Status().Endpoints[0].String() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 207 | t.Logf("endpoint %s", estr) |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 208 | return server.Stop, estr |
| 209 | } |
| 210 | |
| 211 | func newCollection(t *testing.T, rootCtx *context.T) (func() error, string) { |
| 212 | // Start serving a collection service on a loopback address. This |
| 213 | // is just a service we can mount and test against. |
| 214 | server, err := xrpc.NewDispatchingServer(rootCtx, "collection", newCollectionServer()) |
| 215 | if err != nil { |
| 216 | boom(t, "r.NewServer: %s", err) |
| 217 | } |
| 218 | estr := server.Status().Endpoints[0].String() |
| 219 | t.Logf("endpoint %s", estr) |
| 220 | return server.Stop, estr |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | func TestMountTable(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 224 | rootCtx, aliceCtx, bobCtx, shutdown := initTest() |
| 225 | defer shutdown() |
| 226 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 227 | stop, mtAddr := newMT(t, "testdata/test.perms", "", "testMountTable", rootCtx) |
| 228 | defer stop() |
| 229 | stop, collectionAddr := newCollection(t, rootCtx) |
| 230 | defer stop() |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 231 | |
| 232 | collectionName := naming.JoinAddressName(collectionAddr, "collection") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 233 | |
| 234 | // Mount the collection server into the mount table. |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 235 | rootCtx.Infof("Mount the collection server into the mount table.") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 236 | doMount(t, rootCtx, mtAddr, "stuff", collectionName, true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 237 | |
| 238 | // Create a few objects and make sure we can read them. |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 239 | rootCtx.Infof("Create a few objects.") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 240 | export(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/the/rain"), "the rain") |
| 241 | export(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/in/spain"), "in spain") |
| 242 | export(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/falls"), "falls mainly on the plain") |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 243 | rootCtx.Infof("Make sure we can read them.") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 244 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/the/rain"), "the rain", true) |
| 245 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/in/spain"), "in spain", true) |
| 246 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/falls"), "falls mainly on the plain", true) |
| 247 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "/stuff/falls"), "falls mainly on the plain", true) |
| 248 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/nonexistant"), "falls mainly on the plain", false) |
| 249 | checkContents(t, bobCtx, naming.JoinAddressName(mtAddr, "stuff/the/rain"), "the rain", true) |
| 250 | checkContents(t, aliceCtx, naming.JoinAddressName(mtAddr, "stuff/the/rain"), "the rain", false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 251 | |
| 252 | // Test multiple mounts. |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 253 | rootCtx.Infof("Multiple mounts.") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 254 | doMount(t, rootCtx, mtAddr, "a/b", collectionName, true) |
| 255 | doMount(t, rootCtx, mtAddr, "x/y", collectionName, true) |
| 256 | doMount(t, rootCtx, mtAddr, "alpha//beta", collectionName, true) |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 257 | rootCtx.Infof("Make sure we can read them.") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 258 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/falls"), "falls mainly on the plain", true) |
| 259 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", true) |
| 260 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "x/y/falls"), "falls mainly on the plain", true) |
| 261 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "alpha/beta/falls"), "falls mainly on the plain", true) |
| 262 | checkContents(t, aliceCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", true) |
| 263 | checkContents(t, bobCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 264 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 265 | // Test getting/setting AccessLists. |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 266 | perms, version := doGetPermissions(t, rootCtx, mtAddr, "stuff", true) |
| 267 | doSetPermissions(t, rootCtx, mtAddr, "stuff", perms, "xyzzy", false) // bad version |
| 268 | doSetPermissions(t, rootCtx, mtAddr, "stuff", perms, version, true) // correct version |
Adam Sadovsky | b1f9e3c | 2015-04-08 11:03:49 -0700 | [diff] [blame] | 269 | _, nversion := doGetPermissions(t, rootCtx, mtAddr, "stuff", true) |
| 270 | if nversion == version { |
| 271 | boom(t, "version didn't change after SetPermissions: %s", nversion) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 272 | } |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 273 | doSetPermissions(t, rootCtx, mtAddr, "stuff", perms, "", true) // no version |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 274 | |
| 275 | // Bob should be able to create nodes under the mounttable root but not alice. |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 276 | doSetPermissions(t, aliceCtx, mtAddr, "onlybob", perms, "", false) |
| 277 | doSetPermissions(t, bobCtx, mtAddr, "onlybob", perms, "", true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 278 | |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 279 | // Test that setting Permissions to permissions that don't include the the setter's |
Suharsh Sivakumar | 7120abd | 2015-04-15 13:54:23 -0700 | [diff] [blame] | 280 | // blessings in Admin, automatically add their Blessings to Admin to prevent |
| 281 | // locking everyone out. |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 282 | perms, _ = doGetPermissions(t, bobCtx, mtAddr, "onlybob", true) |
| 283 | noRootPerms := perms.Copy() |
| 284 | noRootPerms.Clear("bob", "Admin") |
| 285 | doSetPermissions(t, bobCtx, mtAddr, "onlybob", noRootPerms, "", true) |
Suharsh Sivakumar | 7120abd | 2015-04-15 13:54:23 -0700 | [diff] [blame] | 286 | // This should succeed, because "bob" should automatically be added to "Admin" |
| 287 | // even though he cleared himself from "Admin". |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 288 | doSetPermissions(t, bobCtx, mtAddr, "onlybob", perms, "", true) |
| 289 | // Test that adding a non-standard perms is normalized when retrieved. |
| 290 | admin := perms["Admin"] |
Suharsh Sivakumar | 7120abd | 2015-04-15 13:54:23 -0700 | [diff] [blame] | 291 | admin.In = []security.BlessingPattern{"bob", "bob"} |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 292 | perms["Admin"] = admin |
| 293 | doSetPermissions(t, bobCtx, mtAddr, "onlybob", perms, "", true) |
| 294 | perms, _ = doGetPermissions(t, bobCtx, mtAddr, "onlybob", true) |
| 295 | if got, want := perms["Admin"].In, []security.BlessingPattern{"bob"}; !reflect.DeepEqual(got, want) { |
Suharsh Sivakumar | 7120abd | 2015-04-15 13:54:23 -0700 | [diff] [blame] | 296 | boom(t, "got %v, want %v", got, want) |
| 297 | } |
| 298 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 299 | // Test generic unmount. |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 300 | rootCtx.Info("Test generic unmount.") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 301 | doUnmount(t, rootCtx, mtAddr, "a/b", "", true) |
| 302 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 303 | |
| 304 | // Test specific unmount. |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 305 | rootCtx.Info("Test specific unmount.") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 306 | doMount(t, rootCtx, mtAddr, "a/b", collectionName, true) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 307 | doUnmount(t, rootCtx, mtAddr, "a/b", collectionName, true) |
| 308 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 309 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 310 | // Try timing out a mount. |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 311 | rootCtx.Info("Try timing out a mount.") |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 312 | ft := mounttablelib.NewFakeTimeClock() |
| 313 | mounttablelib.SetServerListClock(ft) |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 314 | doMount(t, rootCtx, mtAddr, "stuffWithTTL", collectionName, true) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 315 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuffWithTTL/the/rain"), "the rain", true) |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 316 | ft.Advance(time.Duration(ttlSecs+4) * time.Second) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 317 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuffWithTTL/the/rain"), "the rain", false) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 318 | |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 319 | // Test unauthorized mount. |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 320 | rootCtx.Info("Test unauthorized mount.") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 321 | doMount(t, bobCtx, mtAddr, "/a/b", collectionName, false) |
| 322 | doMount(t, aliceCtx, mtAddr, "/a/b", collectionName, false) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 323 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 324 | doUnmount(t, bobCtx, mtAddr, "x/y", collectionName, false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 327 | func doGlobX(t *testing.T, ctx *context.T, ep, suffix, pattern string, joinServer bool) []string { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 328 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 329 | client := v23.GetClient(ctx) |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 330 | call, err := client.StartCall(ctx, name, rpc.GlobMethod, []interface{}{pattern}, options.NoResolve{}) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 331 | if err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 332 | boom(t, "Glob.StartCall %s %s: %s", name, pattern, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 333 | } |
| 334 | var reply []string |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 335 | for { |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 336 | var gr naming.GlobReply |
David Why Use Two When One Will Do Presotto | e5e62d0 | 2015-02-19 14:35:20 -0800 | [diff] [blame] | 337 | err := call.Recv(&gr) |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 338 | if err == io.EOF { |
| 339 | break |
| 340 | } |
| 341 | if err != nil { |
| 342 | boom(t, "Glob.StartCall %s: %s", name, pattern, err) |
| 343 | } |
David Why Use Two When One Will Do Presotto | e5e62d0 | 2015-02-19 14:35:20 -0800 | [diff] [blame] | 344 | switch v := gr.(type) { |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 345 | case naming.GlobReplyEntry: |
David Why Use Two When One Will Do Presotto | e5e62d0 | 2015-02-19 14:35:20 -0800 | [diff] [blame] | 346 | if joinServer && len(v.Value.Servers) > 0 { |
| 347 | reply = append(reply, naming.JoinAddressName(v.Value.Servers[0].Server, v.Value.Name)) |
| 348 | } else { |
| 349 | reply = append(reply, v.Value.Name) |
| 350 | } |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 351 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 352 | } |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 353 | if err := call.Finish(); err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 354 | boom(t, "Glob.Finish %s: %s", name, pattern, err) |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 355 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 356 | return reply |
| 357 | } |
| 358 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 359 | func doGlob(t *testing.T, ctx *context.T, ep, suffix, pattern string) []string { |
| 360 | return doGlobX(t, ctx, ep, suffix, pattern, false) |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 363 | // checkMatch verified that the two slices contain the same string items, albeit |
| 364 | // not necessarily in the same order. Item repetitions are allowed, but their |
| 365 | // numbers need to match as well. |
| 366 | func checkMatch(t *testing.T, want []string, got []string) { |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 367 | if len(want) == 0 && len(got) == 0 { |
| 368 | return |
| 369 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 370 | w := sort.StringSlice(want) |
| 371 | w.Sort() |
| 372 | g := sort.StringSlice(got) |
| 373 | g.Sort() |
| 374 | if !reflect.DeepEqual(w, g) { |
| 375 | boom(t, "Glob expected %v got %v", want, got) |
| 376 | } |
| 377 | } |
| 378 | |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 379 | // checkExists makes sure a name exists (or not). |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 380 | func checkExists(t *testing.T, ctx *context.T, ep, suffix string, shouldSucceed bool) { |
| 381 | x := doGlobX(t, ctx, ep, "", suffix, false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 382 | if len(x) != 1 || x[0] != suffix { |
| 383 | if shouldSucceed { |
| 384 | boom(t, "Failed to find %s", suffix) |
| 385 | } |
| 386 | return |
| 387 | } |
| 388 | if !shouldSucceed { |
| 389 | boom(t, "%s exists but shouldn't", suffix) |
| 390 | } |
| 391 | } |
| 392 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 393 | func TestGlob(t *testing.T) { |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 394 | rootCtx, shutdown := test.V23Init() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 395 | defer shutdown() |
| 396 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 397 | stop, estr := newMT(t, "", "", "testGlob", rootCtx) |
| 398 | defer stop() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 399 | |
| 400 | // set up a mount space |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 401 | fakeServer := naming.JoinAddressName(estr, "quux") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 402 | doMount(t, rootCtx, estr, "one/bright/day", fakeServer, true) |
| 403 | doMount(t, rootCtx, estr, "in/the/middle", fakeServer, true) |
| 404 | doMount(t, rootCtx, estr, "of/the/night", fakeServer, true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 405 | |
| 406 | // Try various globs. |
| 407 | tests := []struct { |
| 408 | in string |
| 409 | expected []string |
| 410 | }{ |
| 411 | {"*", []string{"one", "in", "of"}}, |
Bogdan Caprita | b6b195a | 2014-06-11 17:55:03 -0700 | [diff] [blame] | 412 | {"...", []string{"", "one", "in", "of", "one/bright", "in/the", "of/the", "one/bright/day", "in/the/middle", "of/the/night"}}, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 413 | {"*/...", []string{"one", "in", "of", "one/bright", "in/the", "of/the", "one/bright/day", "in/the/middle", "of/the/night"}}, |
Bogdan Caprita | b6b195a | 2014-06-11 17:55:03 -0700 | [diff] [blame] | 414 | {"one/...", []string{"one", "one/bright", "one/bright/day"}}, |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 415 | {"of/the/night/two/dead/boys", []string{"of/the/night"}}, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 416 | {"*/the", []string{"in/the", "of/the"}}, |
| 417 | {"*/the/...", []string{"in/the", "of/the", "in/the/middle", "of/the/night"}}, |
| 418 | {"o*", []string{"one", "of"}}, |
| 419 | {"", []string{""}}, |
| 420 | } |
| 421 | for _, test := range tests { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 422 | out := doGlob(t, rootCtx, estr, "", test.in) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 423 | checkMatch(t, test.expected, out) |
| 424 | } |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 425 | |
| 426 | // Test Glob on a name that is under a mounted server. The result should the |
| 427 | // the address the mounted server with the extra suffix. |
| 428 | { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 429 | results := doGlobX(t, rootCtx, estr, "of/the/night/two/dead/boys/got/up/to/fight", "*", true) |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 430 | if len(results) != 1 { |
| 431 | boom(t, "Unexpected number of results. Got %v, want 1", len(results)) |
| 432 | } |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 433 | _, suffix := naming.SplitAddressName(results[0]) |
| 434 | if expected := "quux/two/dead/boys/got/up/to/fight"; suffix != expected { |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 435 | boom(t, "Unexpected suffix. Got %v, want %v", suffix, expected) |
| 436 | } |
| 437 | } |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Robin Thellend | f15f295 | 2015-07-17 21:49:58 -0700 | [diff] [blame^] | 440 | type fakeServerCall struct { |
| 441 | sendCount int |
| 442 | } |
Asim Shankar | b425c55 | 2015-07-10 02:18:59 -0700 | [diff] [blame] | 443 | |
| 444 | func (fakeServerCall) Security() security.Call { return security.NewCall(&security.CallParams{}) } |
| 445 | func (fakeServerCall) Suffix() string { return "" } |
| 446 | func (fakeServerCall) LocalEndpoint() naming.Endpoint { return nil } |
| 447 | func (fakeServerCall) RemoteEndpoint() naming.Endpoint { return nil } |
| 448 | func (fakeServerCall) GrantedBlessings() security.Blessings { return security.Blessings{} } |
| 449 | func (fakeServerCall) Server() rpc.Server { return nil } |
Robin Thellend | f15f295 | 2015-07-17 21:49:58 -0700 | [diff] [blame^] | 450 | func (c *fakeServerCall) SendStream() interface { |
| 451 | Send(naming.GlobReply) error |
| 452 | } { |
| 453 | return c |
| 454 | } |
| 455 | func (c *fakeServerCall) Send(reply naming.GlobReply) error { |
| 456 | c.sendCount++ |
| 457 | return nil |
| 458 | } |
Asim Shankar | b425c55 | 2015-07-10 02:18:59 -0700 | [diff] [blame] | 459 | |
| 460 | func TestGlobAborts(t *testing.T) { |
| 461 | ctx, shutdown := test.V23Init() |
| 462 | defer shutdown() |
| 463 | |
| 464 | mt, err := mounttablelib.NewMountTableDispatcher(ctx, "", "", "") |
| 465 | if err != nil { |
| 466 | t.Fatal(err) |
| 467 | } |
| 468 | |
| 469 | mount := func(name string) error { |
Asim Shankar | 56db085 | 2015-07-16 14:25:31 -0700 | [diff] [blame] | 470 | invoker, _, _ := mt.Lookup(ctx, name) |
Asim Shankar | b425c55 | 2015-07-10 02:18:59 -0700 | [diff] [blame] | 471 | server := naming.FormatEndpoint("tcp", name) |
| 472 | return invoker.(mounttable.MountTableServerStub).Mount(ctx, fakeServerCall{}, server, 0, 0) |
| 473 | } |
| 474 | // Mount 125 entries: 5 "directories" with 25 entries each. |
| 475 | for i := 0; i < 5; i++ { |
| 476 | for j := 0; j < 25; j++ { |
| 477 | if err := mount(fmt.Sprintf("%d/%d", i, j)); err != nil { |
| 478 | t.Fatalf("%v (%d, %d)", err, i, j) |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | glob := func(ctx *context.T) (int, error) { |
Asim Shankar | 56db085 | 2015-07-16 14:25:31 -0700 | [diff] [blame] | 484 | root, _, _ := mt.Lookup(ctx, "") |
Robin Thellend | f15f295 | 2015-07-17 21:49:58 -0700 | [diff] [blame^] | 485 | g, _ := glob.Parse("...") |
| 486 | fCall := &fakeServerCall{} |
| 487 | root.(rpc.Globber).Globber().AllGlobberX.Glob__(ctx, fCall, g) |
| 488 | return fCall.sendCount, nil |
Asim Shankar | b425c55 | 2015-07-10 02:18:59 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | got, err := glob(ctx) |
| 492 | if err != nil { |
| 493 | t.Fatal(err) |
| 494 | } |
| 495 | if want := 5 + 125 + 1; got != want { // 5 "directories", 125 entries, 1 root entry |
| 496 | t.Errorf("Got %d want %d", got, want) |
| 497 | } |
| 498 | canceled, cancel := context.WithCancel(ctx) |
| 499 | cancel() |
| 500 | if got, err = glob(canceled); err != nil { |
| 501 | t.Fatal(err) |
| 502 | } |
| 503 | if got != 0 { |
| 504 | t.Errorf("Glob returned entries even though the context was cancelled first (returned %d)", got) |
| 505 | } |
| 506 | } |
| 507 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 508 | func TestAccessListTemplate(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 509 | rootCtx, aliceCtx, bobCtx, shutdown := initTest() |
| 510 | defer shutdown() |
| 511 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 512 | stop, estr := newMT(t, "testdata/test.perms", "", "testAccessListTemplate", rootCtx) |
| 513 | defer stop() |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 514 | fakeServer := naming.JoinAddressName(estr, "quux") |
Tilak Sharma | da5165e | 2014-10-07 16:39:51 -0700 | [diff] [blame] | 515 | |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 516 | // Noone should be able to mount on someone else's names. |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 517 | doMount(t, aliceCtx, estr, "users/ted", fakeServer, false) |
| 518 | doMount(t, bobCtx, estr, "users/carol", fakeServer, false) |
| 519 | doMount(t, rootCtx, estr, "users/george", fakeServer, false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 520 | |
| 521 | // Anyone should be able to mount on their own names. |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 522 | doMount(t, aliceCtx, estr, "users/alice", fakeServer, true) |
| 523 | doMount(t, bobCtx, estr, "users/bob", fakeServer, true) |
| 524 | doMount(t, rootCtx, estr, "users/root", fakeServer, true) |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 525 | |
| 526 | // Make sure the counter works. |
| 527 | doUnmount(t, aliceCtx, estr, "users/alice", "", true) |
| 528 | doUnmount(t, bobCtx, estr, "users/bob", "", true) |
| 529 | doUnmount(t, rootCtx, estr, "users/root", "", true) |
| 530 | perms := access.Permissions{"Admin": access.AccessList{In: []security.BlessingPattern{security.AllPrincipals}}} |
| 531 | doSetPermissions(t, aliceCtx, estr, "users/alice/a/b/c/d", perms, "", true) |
| 532 | doSetPermissions(t, aliceCtx, estr, "users/alice/a/b/c/d", perms, "", true) |
| 533 | |
| 534 | // Do we obey limits? |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 535 | for i := 0; i < mounttablelib.DefaultMaxNodesPerUser()-5; i++ { |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 536 | node := fmt.Sprintf("users/alice/a/b/c/d/%d", i) |
| 537 | doSetPermissions(t, aliceCtx, estr, node, perms, "", true) |
| 538 | } |
| 539 | doSetPermissions(t, aliceCtx, estr, "users/alice/a/b/c/d/straw", perms, "", false) |
| 540 | |
| 541 | // See if the stats numbers are correct. |
| 542 | testcases := []struct { |
| 543 | key string |
| 544 | expected interface{} |
| 545 | }{ |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 546 | {"alice", int64(mounttablelib.DefaultMaxNodesPerUser())}, |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 547 | {"bob", int64(0)}, |
| 548 | {"root", int64(0)}, |
David Why Use Two When One Will Do Presotto | 67b7071 | 2015-06-02 13:13:58 -0700 | [diff] [blame] | 549 | {conventions.ServerUser, int64(3)}, |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 550 | } |
| 551 | for _, tc := range testcases { |
| 552 | name := "testAccessListTemplate/num-nodes-per-user/" + tc.key |
| 553 | got, err := libstats.Value(name) |
| 554 | if err != nil { |
| 555 | t.Errorf("unexpected error getting map entry for %s: %s", name, err) |
| 556 | } |
| 557 | if got != tc.expected { |
| 558 | t.Errorf("unexpected getting map entry for %s. Got %v, want %v", name, got, tc.expected) |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | func getUserNodeCounts(t *testing.T) (counts map[string]int32) { |
| 564 | s, err := libstats.Value("mounttable/num-nodes-per-user") |
| 565 | if err != nil { |
| 566 | boom(t, "Can't get mounttable statistics") |
| 567 | } |
| 568 | // This string is a json encoded map. Decode. |
| 569 | switch v := s.(type) { |
| 570 | default: |
| 571 | boom(t, "Wrong type for mounttable statistics") |
| 572 | case string: |
| 573 | err = json.Unmarshal([]byte(v), &counts) |
| 574 | if err != nil { |
| 575 | boom(t, "Can't unmarshal mounttable statistics") |
| 576 | } |
| 577 | } |
| 578 | return |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 581 | func TestGlobAccessLists(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 582 | rootCtx, aliceCtx, bobCtx, shutdown := initTest() |
| 583 | defer shutdown() |
| 584 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 585 | stop, estr := newMT(t, "testdata/test.perms", "", "testGlobAccessLists", rootCtx) |
| 586 | defer stop() |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 587 | |
| 588 | // set up a mount space |
| 589 | fakeServer := naming.JoinAddressName(estr, "quux") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 590 | doMount(t, aliceCtx, estr, "one/bright/day", fakeServer, false) // Fails because alice can't mount there. |
| 591 | doMount(t, bobCtx, estr, "one/bright/day", fakeServer, true) |
| 592 | doMount(t, rootCtx, estr, "a/b/c", fakeServer, true) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 593 | |
| 594 | // Try various globs. |
| 595 | tests := []struct { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 596 | ctx *context.T |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 597 | in string |
| 598 | expected []string |
| 599 | }{ |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 600 | {rootCtx, "*", []string{"one", "a", "stuff", "users"}}, |
| 601 | {aliceCtx, "*", []string{"one", "a", "users"}}, |
| 602 | {bobCtx, "*", []string{"one", "stuff", "users"}}, |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 603 | // bob, alice, and root have different visibility to the space. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 604 | {rootCtx, "*/...", []string{"one", "a", "one/bright", "a/b", "one/bright/day", "a/b/c", "stuff", "users"}}, |
| 605 | {aliceCtx, "*/...", []string{"one", "a", "one/bright", "a/b", "one/bright/day", "a/b/c", "users"}}, |
| 606 | {bobCtx, "*/...", []string{"one", "one/bright", "one/bright/day", "stuff", "users"}}, |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 607 | } |
| 608 | for _, test := range tests { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 609 | out := doGlob(t, test.ctx, estr, "", test.in) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 610 | checkMatch(t, test.expected, out) |
| 611 | } |
| 612 | } |
| 613 | |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 614 | func TestCleanup(t *testing.T) { |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 615 | rootCtx, shutdown := test.V23Init() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 616 | defer shutdown() |
| 617 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 618 | stop, estr := newMT(t, "", "", "testCleanup", rootCtx) |
| 619 | defer stop() |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 620 | |
| 621 | // Set up one mount. |
| 622 | fakeServer := naming.JoinAddressName(estr, "quux") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 623 | doMount(t, rootCtx, estr, "one/bright/day", fakeServer, true) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 624 | checkMatch(t, []string{"one", "one/bright", "one/bright/day"}, doGlob(t, rootCtx, estr, "", "*/...")) |
| 625 | |
| 626 | // After the unmount nothing should be left |
| 627 | doUnmount(t, rootCtx, estr, "one/bright/day", "", true) |
David Why Use Two When One Will Do Presotto | 4d26a99 | 2015-05-06 10:17:09 -0700 | [diff] [blame] | 628 | checkMatch(t, nil, doGlob(t, rootCtx, estr, "", "one")) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 629 | checkMatch(t, nil, doGlob(t, rootCtx, estr, "", "*/...")) |
| 630 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 631 | // Set up a mount, then set the AccessList. |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 632 | doMount(t, rootCtx, estr, "one/bright/day", fakeServer, true) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 633 | checkMatch(t, []string{"one", "one/bright", "one/bright/day"}, doGlob(t, rootCtx, estr, "", "*/...")) |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 634 | perms := access.Permissions{"Read": access.AccessList{In: []security.BlessingPattern{security.AllPrincipals}}} |
| 635 | doSetPermissions(t, rootCtx, estr, "one/bright", perms, "", true) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 636 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 637 | // After the unmount we should still have everything above the AccessList. |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 638 | doUnmount(t, rootCtx, estr, "one/bright/day", "", true) |
| 639 | checkMatch(t, []string{"one", "one/bright"}, doGlob(t, rootCtx, estr, "", "*/...")) |
| 640 | } |
| 641 | |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 642 | func TestDelete(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 643 | rootCtx, aliceCtx, bobCtx, shutdown := initTest() |
| 644 | defer shutdown() |
| 645 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 646 | stop, estr := newMT(t, "testdata/test.perms", "", "testDelete", rootCtx) |
| 647 | defer stop() |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 648 | |
| 649 | // set up a mount space |
| 650 | fakeServer := naming.JoinAddressName(estr, "quux") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 651 | doMount(t, bobCtx, estr, "one/bright/day", fakeServer, true) |
| 652 | doMount(t, rootCtx, estr, "a/b/c", fakeServer, true) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 653 | |
| 654 | // It shouldn't be possible to delete anything with children unless explicitly requested. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 655 | doDeleteNode(t, rootCtx, estr, "a/b", false) |
| 656 | checkExists(t, rootCtx, estr, "a/b", true) |
| 657 | doDeleteSubtree(t, rootCtx, estr, "a/b", true) |
| 658 | checkExists(t, rootCtx, estr, "a/b", false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 659 | |
| 660 | // Alice shouldn't be able to delete what bob created but bob and root should. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 661 | doDeleteNode(t, aliceCtx, estr, "one/bright/day", false) |
| 662 | checkExists(t, rootCtx, estr, "one/bright/day", true) |
| 663 | doDeleteNode(t, rootCtx, estr, "one/bright/day", true) |
| 664 | checkExists(t, rootCtx, estr, "one/bright/day", false) |
| 665 | doDeleteNode(t, bobCtx, estr, "one/bright", true) |
| 666 | checkExists(t, rootCtx, estr, "one/bright", false) |
David Why Use Two When One Will Do Presotto | e43213b | 2015-06-02 10:33:10 -0700 | [diff] [blame] | 667 | |
| 668 | // Make sure directory admin can delete directory children. |
| 669 | perms := access.Permissions{"Admin": access.AccessList{In: []security.BlessingPattern{"bob"}}} |
| 670 | doSetPermissions(t, bobCtx, estr, "hoohaa", perms, "", false) |
| 671 | doDeleteNode(t, rootCtx, estr, "hoohaa", true) |
| 672 | checkExists(t, rootCtx, estr, "hoohaa", false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 675 | func TestServerFormat(t *testing.T) { |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 676 | rootCtx, shutdown := test.V23Init() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 677 | defer shutdown() |
| 678 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 679 | stop, estr := newMT(t, "", "", "testerverFormat", rootCtx) |
| 680 | defer stop() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 681 | |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 682 | doMount(t, rootCtx, estr, "endpoint", naming.JoinAddressName(estr, "life/on/the/mississippi"), true) |
| 683 | doMount(t, rootCtx, estr, "hostport", "/atrampabroad:8000", true) |
| 684 | doMount(t, rootCtx, estr, "invalid/not/rooted", "atrampabroad:8000", false) |
| 685 | doMount(t, rootCtx, estr, "invalid/no/port", "/atrampabroad", false) |
| 686 | doMount(t, rootCtx, estr, "invalid/endpoint", "/@following the equator:8000@@@", false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | func TestExpiry(t *testing.T) { |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 690 | rootCtx, shutdown := test.V23Init() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 691 | defer shutdown() |
| 692 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 693 | stop, estr := newMT(t, "", "", "testExpiry", rootCtx) |
| 694 | defer stop() |
| 695 | stop, collectionAddr := newCollection(t, rootCtx) |
| 696 | defer stop() |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 697 | |
| 698 | collectionName := naming.JoinAddressName(collectionAddr, "collection") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 699 | |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 700 | ft := mounttablelib.NewFakeTimeClock() |
| 701 | mounttablelib.SetServerListClock(ft) |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 702 | doMount(t, rootCtx, estr, "a1/b1", collectionName, true) |
| 703 | doMount(t, rootCtx, estr, "a1/b2", collectionName, true) |
| 704 | doMount(t, rootCtx, estr, "a2/b1", collectionName, true) |
| 705 | doMount(t, rootCtx, estr, "a2/b2/c", collectionName, true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 706 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 707 | checkMatch(t, []string{"a1/b1", "a2/b1"}, doGlob(t, rootCtx, estr, "", "*/b1/...")) |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 708 | ft.Advance(time.Duration(ttlSecs/2) * time.Second) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 709 | checkMatch(t, []string{"a1/b1", "a2/b1"}, doGlob(t, rootCtx, estr, "", "*/b1/...")) |
| 710 | checkMatch(t, []string{"c"}, doGlob(t, rootCtx, estr, "a2/b2", "*")) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 711 | // Refresh only a1/b1. All the other mounts will expire upon the next |
| 712 | // ft advance. |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 713 | doMount(t, rootCtx, estr, "a1/b1", collectionName, true) |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 714 | ft.Advance(time.Duration(ttlSecs/2+4) * time.Second) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 715 | checkMatch(t, []string{"a1", "a1/b1"}, doGlob(t, rootCtx, estr, "", "*/...")) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 716 | checkMatch(t, []string{"a1/b1"}, doGlob(t, rootCtx, estr, "", "*/b1/...")) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 719 | func TestBadAccessLists(t *testing.T) { |
Cosmos Nicolaou | 27dc65b | 2015-07-10 16:23:19 -0700 | [diff] [blame] | 720 | ctx, shutdown := test.TestContext() |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 721 | defer shutdown() |
| 722 | _, err := mounttablelib.NewMountTableDispatcher(ctx, "testdata/invalid.perms", "", "mounttable") |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 723 | if err == nil { |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 724 | boom(t, "Expected json parse error in permissions file") |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 725 | } |
Cosmos Nicolaou | 19a50f6 | 2015-06-20 09:15:40 -0700 | [diff] [blame] | 726 | _, err = mounttablelib.NewMountTableDispatcher(ctx, "testdata/doesntexist.perms", "", "mounttable") |
Robin Thellend | c4b5169 | 2015-02-05 17:11:16 -0800 | [diff] [blame] | 727 | if err != nil { |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 728 | boom(t, "Missing permissions file should not cause an error") |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 729 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 730 | } |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 731 | |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 732 | func getCounter(t *testing.T, ctx *context.T, name string) int64 { |
| 733 | st := stats.StatsClient(name) |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 734 | v, err := st.Value(ctx) |
| 735 | if err != nil { |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 736 | t.Fatalf("Failed to get %q: %v", name, err) |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 737 | return -1 |
| 738 | } |
| 739 | var value int64 |
| 740 | if err := vdl.Convert(&value, v); err != nil { |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 741 | t.Fatalf("Unexpected value type for %q: %v", name, err) |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 742 | } |
| 743 | return value |
| 744 | } |
| 745 | |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 746 | func nodeCount(t *testing.T, ctx *context.T, addr string) int64 { |
| 747 | name := naming.JoinAddressName(addr, "__debug/stats/mounttable/num-nodes") |
| 748 | return getCounter(t, ctx, name) |
| 749 | } |
| 750 | |
| 751 | func serverCount(t *testing.T, ctx *context.T, addr string) int64 { |
| 752 | name := naming.JoinAddressName(addr, "__debug/stats/mounttable/num-mounted-servers") |
| 753 | return getCounter(t, ctx, name) |
| 754 | } |
| 755 | |
| 756 | func TestStatsCounters(t *testing.T) { |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 757 | rootCtx, shutdown := test.V23Init() |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 758 | defer shutdown() |
| 759 | |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 760 | ft := mounttablelib.NewFakeTimeClock() |
| 761 | mounttablelib.SetServerListClock(ft) |
Robin Thellend | 62909b2 | 2015-04-23 13:54:55 -0700 | [diff] [blame] | 762 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 763 | stop, estr := newMT(t, "", "", "mounttable", rootCtx) |
| 764 | defer stop() |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 765 | |
| 766 | // Test flat tree |
| 767 | for i := 1; i <= 10; i++ { |
| 768 | name := fmt.Sprintf("node%d", i) |
| 769 | addr := naming.JoinAddressName(estr, name) |
| 770 | doMount(t, rootCtx, estr, name, addr, true) |
| 771 | if expected, got := int64(i+1), nodeCount(t, rootCtx, estr); got != expected { |
| 772 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 773 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 774 | if expected, got := int64(i), serverCount(t, rootCtx, estr); got != expected { |
| 775 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 776 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 777 | } |
| 778 | for i := 1; i <= 10; i++ { |
| 779 | name := fmt.Sprintf("node%d", i) |
| 780 | if i%2 == 0 { |
| 781 | doUnmount(t, rootCtx, estr, name, "", true) |
| 782 | } else { |
| 783 | doDeleteSubtree(t, rootCtx, estr, name, true) |
| 784 | } |
| 785 | if expected, got := int64(11-i), nodeCount(t, rootCtx, estr); got != expected { |
| 786 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 787 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 788 | if expected, got := int64(10-i), serverCount(t, rootCtx, estr); got != expected { |
| 789 | t.Errorf("Unexpected number of server. Got %d, expected %d", got, expected) |
| 790 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | // Test deep tree |
| 794 | doMount(t, rootCtx, estr, "1/2/3/4/5/6/7/8/9a/10", naming.JoinAddressName(estr, ""), true) |
| 795 | doMount(t, rootCtx, estr, "1/2/3/4/5/6/7/8/9b/11", naming.JoinAddressName(estr, ""), true) |
| 796 | if expected, got := int64(13), nodeCount(t, rootCtx, estr); got != expected { |
| 797 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 798 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 799 | if expected, got := int64(2), serverCount(t, rootCtx, estr); got != expected { |
| 800 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 801 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 802 | doDeleteSubtree(t, rootCtx, estr, "1/2/3/4/5", true) |
| 803 | if expected, got := int64(5), nodeCount(t, rootCtx, estr); got != expected { |
| 804 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 805 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 806 | if expected, got := int64(0), serverCount(t, rootCtx, estr); got != expected { |
| 807 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 808 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 809 | doDeleteSubtree(t, rootCtx, estr, "1", true) |
| 810 | if expected, got := int64(1), nodeCount(t, rootCtx, estr); got != expected { |
| 811 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 812 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 813 | |
| 814 | // Test multiple servers per node |
| 815 | for i := 1; i <= 5; i++ { |
| 816 | server := naming.JoinAddressName(estr, fmt.Sprintf("addr%d", i)) |
| 817 | doMount(t, rootCtx, estr, "node1", server, true) |
| 818 | doMount(t, rootCtx, estr, "node2", server, true) |
| 819 | if expected, got := int64(3), nodeCount(t, rootCtx, estr); got != expected { |
| 820 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 821 | } |
| 822 | if expected, got := int64(2*i), serverCount(t, rootCtx, estr); got != expected { |
| 823 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 824 | } |
| 825 | } |
| 826 | doUnmount(t, rootCtx, estr, "node1", "", true) |
| 827 | if expected, got := int64(2), nodeCount(t, rootCtx, estr); got != expected { |
| 828 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 829 | } |
| 830 | if expected, got := int64(5), serverCount(t, rootCtx, estr); got != expected { |
| 831 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 832 | } |
| 833 | for i := 1; i <= 5; i++ { |
| 834 | server := naming.JoinAddressName(estr, fmt.Sprintf("addr%d", i)) |
| 835 | doUnmount(t, rootCtx, estr, "node2", server, true) |
| 836 | expectedNodes := int64(2) |
| 837 | if i == 5 { |
| 838 | expectedNodes = 1 |
| 839 | } |
| 840 | if expected, got := expectedNodes, nodeCount(t, rootCtx, estr); got != expected { |
| 841 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 842 | } |
| 843 | if expected, got := int64(5-i), serverCount(t, rootCtx, estr); got != expected { |
| 844 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 845 | } |
| 846 | } |
Robin Thellend | 93b1f6d | 2015-04-22 12:31:34 -0700 | [diff] [blame] | 847 | |
| 848 | // Mount on an existing intermediate node. |
| 849 | doMount(t, rootCtx, estr, "1/2/3/4/5/6/7/8/9a/10", naming.JoinAddressName(estr, ""), true) |
| 850 | doMount(t, rootCtx, estr, "1/2/3/4/5", naming.JoinAddressName(estr, ""), true) |
| 851 | if expected, got := int64(6), nodeCount(t, rootCtx, estr); got != expected { |
| 852 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 853 | } |
| 854 | if expected, got := int64(1), serverCount(t, rootCtx, estr); got != expected { |
| 855 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 856 | } |
Robin Thellend | 62909b2 | 2015-04-23 13:54:55 -0700 | [diff] [blame] | 857 | |
| 858 | // Test expired mounts |
| 859 | // "1/2/3/4/5" is still mounted from earlier. |
Matt Rosencrantz | 165e890 | 2015-06-12 15:27:27 -0700 | [diff] [blame] | 860 | ft.Advance(time.Duration(ttlSecs+4) * time.Second) |
Robin Thellend | 62909b2 | 2015-04-23 13:54:55 -0700 | [diff] [blame] | 861 | if _, err := resolve(rootCtx, naming.JoinAddressName(estr, "1/2/3/4/5")); err == nil { |
| 862 | t.Errorf("Expected failure. Got success") |
| 863 | } |
| 864 | if expected, got := int64(0), serverCount(t, rootCtx, estr); got != expected { |
| 865 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 866 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 867 | } |
| 868 | |
David Why Use Two When One Will Do Presotto | e3e932c | 2015-05-15 14:59:46 -0700 | [diff] [blame] | 869 | func TestIntermediateNodesCreatedFromConfig(t *testing.T) { |
| 870 | rootCtx, _, _, shutdown := initTest() |
| 871 | defer shutdown() |
| 872 | |
Matt Rosencrantz | bb6295d | 2015-06-19 15:13:58 -0700 | [diff] [blame] | 873 | stop, estr := newMT(t, "testdata/intermediate.perms", "", "TestIntermediateNodesCreatedFromConfig", rootCtx) |
| 874 | defer stop() |
David Why Use Two When One Will Do Presotto | e3e932c | 2015-05-15 14:59:46 -0700 | [diff] [blame] | 875 | |
| 876 | // x and x/y should have the same permissions at the root. |
| 877 | rootPerms, _ := doGetPermissions(t, rootCtx, estr, "", true) |
| 878 | if perms, _ := doGetPermissions(t, rootCtx, estr, "x", true); !reflect.DeepEqual(rootPerms, perms) { |
| 879 | boom(t, "for x got %v, want %v", perms, rootPerms) |
| 880 | } |
| 881 | if perms, _ := doGetPermissions(t, rootCtx, estr, "x/y", true); !reflect.DeepEqual(rootPerms, perms) { |
| 882 | boom(t, "for x/y got %v, want %v", perms, rootPerms) |
| 883 | } |
| 884 | if perms, _ := doGetPermissions(t, rootCtx, estr, "x/y/z", true); reflect.DeepEqual(rootPerms, perms) { |
| 885 | boom(t, "for x/y/z got %v, don't want %v", perms, rootPerms) |
| 886 | } |
| 887 | } |
| 888 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 889 | func initTest() (rootCtx *context.T, aliceCtx *context.T, bobCtx *context.T, shutdown v23.Shutdown) { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 890 | test.Init() |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 891 | ctx, shutdown := test.V23Init() |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 892 | var err error |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 893 | if rootCtx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal("root")); err != nil { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 894 | panic("failed to set root principal") |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 895 | } |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 896 | if aliceCtx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal("alice")); err != nil { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 897 | panic("failed to set alice principal") |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 898 | } |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 899 | if bobCtx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal("bob")); err != nil { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 900 | panic("failed to set bob principal") |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 901 | } |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 902 | for _, r := range []*context.T{rootCtx, aliceCtx, bobCtx} { |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 903 | // A hack to set the namespace roots to a value that won't work. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 904 | v23.GetNamespace(r).SetRoots() |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 905 | // And have all principals recognize each others blessings. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 906 | p1 := v23.GetPrincipal(r) |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 907 | for _, other := range []*context.T{rootCtx, aliceCtx, bobCtx} { |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 908 | // testutil.NewPrincipal has already setup each |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 909 | // principal to use the same blessing for both server |
| 910 | // and client activities. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 911 | if err := p1.AddToRoots(v23.GetPrincipal(other).BlessingStore().Default()); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 912 | panic(err) |
| 913 | } |
| 914 | } |
| 915 | } |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 916 | return rootCtx, aliceCtx, bobCtx, shutdown |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 917 | } |