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 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 5 | package rt_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 9 | "io/ioutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 10 | "os" |
| 11 | "regexp" |
| 12 | "testing" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 13 | "time" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 14 | |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 15 | "v.io/x/lib/vlog" |
| 16 | |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 17 | "v.io/v23" |
Matt Rosencrantz | 250558f | 2015-03-17 11:37:31 -0700 | [diff] [blame] | 18 | "v.io/v23/context" |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 19 | "v.io/v23/security" |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 20 | |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 21 | "v.io/x/ref" |
Todd Wang | b351149 | 2015-04-07 23:32:34 -0700 | [diff] [blame] | 22 | vsecurity "v.io/x/ref/lib/security" |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 23 | "v.io/x/ref/test" |
| 24 | "v.io/x/ref/test/expect" |
| 25 | "v.io/x/ref/test/modules" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Suharsh Sivakumar | d19c95d | 2015-02-19 14:44:50 -0800 | [diff] [blame] | 28 | //go:generate v23 test generate |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 29 | |
| 30 | func TestInit(t *testing.T) { |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 31 | ref.EnvClearCredentials() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 32 | ctx, shutdown := v23.Init() |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 33 | defer shutdown() |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 34 | |
Matt Rosencrantz | 97d67a9 | 2015-01-27 21:03:12 -0800 | [diff] [blame] | 35 | l := vlog.Log |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 36 | fmt.Println(l) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 37 | args := fmt.Sprintf("%s", l) |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 38 | expected := regexp.MustCompile("name=vlog logdirs=\\[/tmp\\] logtostderr=true|false alsologtostderr=false|true max_stack_buf_size=4292608 v=[0-9] stderrthreshold=2 vmodule= log_backtrace_at=:0") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 39 | if !expected.MatchString(args) { |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 40 | t.Errorf("unexpected default args: %s, want %s", args, expected) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 41 | } |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 42 | p := v23.GetPrincipal(ctx) |
Asim Shankar | 220a015 | 2014-10-30 21:21:09 -0700 | [diff] [blame] | 43 | if p == nil { |
| 44 | t.Fatalf("A new principal should have been created") |
| 45 | } |
| 46 | if p.BlessingStore() == nil { |
| 47 | t.Fatalf("The principal must have a BlessingStore") |
| 48 | } |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 49 | if p.BlessingStore().Default().IsZero() { |
| 50 | t.Errorf("Principal().BlessingStore().Default() should not be the zero value") |
Asim Shankar | 220a015 | 2014-10-30 21:21:09 -0700 | [diff] [blame] | 51 | } |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 52 | if p.BlessingStore().ForPeer().IsZero() { |
| 53 | t.Errorf("Principal().BlessingStore().ForPeer() should not be the zero value") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 57 | var child = modules.Register(func(env *modules.Env, args ...string) error { |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 58 | _, shutdown := test.V23Init() |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 59 | defer shutdown() |
Matt Rosencrantz | 0610a23 | 2014-12-04 10:26:39 -0800 | [diff] [blame] | 60 | |
Matt Rosencrantz | 97d67a9 | 2015-01-27 21:03:12 -0800 | [diff] [blame] | 61 | logger := vlog.Log |
Matt Rosencrantz | 97eb540 | 2015-01-08 14:51:52 -0800 | [diff] [blame] | 62 | vlog.Infof("%s\n", logger) |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 63 | fmt.Fprintf(env.Stdout, "%s\n", logger) |
| 64 | modules.WaitForEOF(env.Stdin) |
| 65 | fmt.Fprintf(env.Stdout, "done\n") |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 66 | return nil |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 67 | }, "child") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 68 | |
| 69 | func TestInitArgs(t *testing.T) { |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 70 | sh, err := modules.NewShell(nil, nil, testing.Verbose(), t) |
Cosmos Nicolaou | 344cc4a | 2014-11-26 15:38:43 -0800 | [diff] [blame] | 71 | if err != nil { |
| 72 | t.Fatalf("unexpected error: %s", err) |
| 73 | } |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 74 | defer sh.Cleanup(os.Stderr, os.Stderr) |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 75 | h, err := sh.Start(nil, child, "--logtostderr=true", "--vmodule=*=3", "--", "foobar") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 76 | if err != nil { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 77 | t.Fatalf("unexpected error: %s", err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 78 | } |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 79 | h.Expect(fmt.Sprintf("name=vlog "+ |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 80 | "logdirs=[%s] "+ |
| 81 | "logtostderr=true "+ |
| 82 | "alsologtostderr=true "+ |
| 83 | "max_stack_buf_size=4292608 "+ |
Cosmos Nicolaou | d83b90b | 2014-11-30 15:01:31 -0800 | [diff] [blame] | 84 | "v=0 "+ |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 85 | "stderrthreshold=2 "+ |
Cosmos Nicolaou | d83b90b | 2014-11-30 15:01:31 -0800 | [diff] [blame] | 86 | "vmodule=*=3 "+ |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 87 | "log_backtrace_at=:0", |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 88 | os.TempDir())) |
| 89 | h.CloseStdin() |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 90 | h.Expect("done") |
| 91 | h.ExpectEOF() |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 92 | h.Shutdown(os.Stderr, os.Stderr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 93 | } |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 94 | |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 95 | func validatePrincipal(p security.Principal) error { |
| 96 | if p == nil { |
| 97 | return fmt.Errorf("nil principal") |
Matt Rosencrantz | 0610a23 | 2014-12-04 10:26:39 -0800 | [diff] [blame] | 98 | } |
Ankur | d864681 | 2015-03-12 10:48:41 -0700 | [diff] [blame] | 99 | call := security.NewCall(&security.CallParams{LocalPrincipal: p, RemoteBlessings: p.BlessingStore().Default()}) |
Matt Rosencrantz | 250558f | 2015-03-17 11:37:31 -0700 | [diff] [blame] | 100 | ctx, cancel := context.RootContext() |
| 101 | defer cancel() |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 102 | blessings, rejected := security.RemoteBlessingNames(ctx, call) |
Asim Shankar | 2bf7b1e | 2015-02-27 00:45:12 -0800 | [diff] [blame] | 103 | if n := len(blessings); n != 1 { |
| 104 | return fmt.Errorf("rt.Principal().BlessingStore().Default() return blessings:%v (rejected:%v), want exactly one recognized blessing", blessings, rejected) |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 105 | } |
| 106 | return nil |
| 107 | } |
| 108 | |
| 109 | func defaultBlessing(p security.Principal) string { |
Ankur | d864681 | 2015-03-12 10:48:41 -0700 | [diff] [blame] | 110 | call := security.NewCall(&security.CallParams{LocalPrincipal: p, RemoteBlessings: p.BlessingStore().Default()}) |
Matt Rosencrantz | 250558f | 2015-03-17 11:37:31 -0700 | [diff] [blame] | 111 | ctx, cancel := context.RootContext() |
| 112 | defer cancel() |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 113 | b, _ := security.RemoteBlessingNames(ctx, call) |
Ryan Brown | 41093a9 | 2015-02-10 10:59:14 -0800 | [diff] [blame] | 114 | return b[0] |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | func tmpDir(t *testing.T) string { |
| 118 | dir, err := ioutil.TempDir("", "rt_test_dir") |
| 119 | if err != nil { |
| 120 | t.Fatalf("unexpected error: %s", err) |
| 121 | } |
| 122 | return dir |
| 123 | } |
| 124 | |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 125 | var principal = modules.Register(func(env *modules.Env, args ...string) error { |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 126 | ctx, shutdown := test.V23Init() |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 127 | defer shutdown() |
Matt Rosencrantz | 0610a23 | 2014-12-04 10:26:39 -0800 | [diff] [blame] | 128 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 129 | p := v23.GetPrincipal(ctx) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 130 | if err := validatePrincipal(p); err != nil { |
Ankur | 9f95794 | 2014-11-24 16:34:18 -0800 | [diff] [blame] | 131 | return err |
| 132 | } |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 133 | fmt.Fprintf(env.Stdout, "DEFAULT_BLESSING=%s\n", defaultBlessing(p)) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 134 | return nil |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 135 | }, "principal") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 136 | |
| 137 | // Runner runs a principal as a subprocess and reports back with its |
| 138 | // own security info and it's childs. |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 139 | var runner = modules.Register(func(env *modules.Env, args ...string) error { |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 140 | ctx, shutdown := test.V23Init() |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 141 | defer shutdown() |
| 142 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 143 | p := v23.GetPrincipal(ctx) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 144 | if err := validatePrincipal(p); err != nil { |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 145 | return err |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 146 | } |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 147 | fmt.Fprintf(env.Stdout, "RUNNER_DEFAULT_BLESSING=%v\n", defaultBlessing(p)) |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 148 | sh, err := modules.NewShell(ctx, p, false, nil) |
Cosmos Nicolaou | 344cc4a | 2014-11-26 15:38:43 -0800 | [diff] [blame] | 149 | if err != nil { |
| 150 | return err |
| 151 | } |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 152 | if _, err := sh.Start(nil, principal, args...); err != nil { |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 153 | return err |
| 154 | } |
Suharsh Sivakumar | d02ac22 | 2014-11-18 14:02:15 -0800 | [diff] [blame] | 155 | // Cleanup copies the output of sh to these Writers. |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 156 | sh.Cleanup(env.Stdout, env.Stderr) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 157 | return nil |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 158 | }, "runner") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 159 | |
Suharsh Sivakumar | 4d36f81 | 2015-01-15 13:58:00 -0800 | [diff] [blame] | 160 | func createCredentialsInDir(t *testing.T, dir string, blessing string) { |
| 161 | principal, err := vsecurity.CreatePersistentPrincipal(dir, nil) |
| 162 | if err != nil { |
| 163 | t.Fatalf("unexpected error: %s", err) |
| 164 | } |
| 165 | if err := vsecurity.InitDefaultBlessings(principal, blessing); err != nil { |
| 166 | t.Fatalf("unexpected error: %s", err) |
| 167 | } |
| 168 | } |
| 169 | |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 170 | func TestPrincipalInheritance(t *testing.T) { |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 171 | sh, err := modules.NewShell(nil, nil, testing.Verbose(), t) |
Cosmos Nicolaou | 344cc4a | 2014-11-26 15:38:43 -0800 | [diff] [blame] | 172 | if err != nil { |
| 173 | t.Fatalf("unexpected error: %s", err) |
| 174 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 175 | defer func() { |
| 176 | sh.Cleanup(os.Stdout, os.Stderr) |
| 177 | }() |
| 178 | |
Ankur | 9f95794 | 2014-11-24 16:34:18 -0800 | [diff] [blame] | 179 | // Test that the child inherits from the parent's credentials correctly. |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 180 | // The running test process may or may not have a credentials directory set |
| 181 | // up so we have to use a 'runner' process to ensure the correct setup. |
| 182 | cdir := tmpDir(t) |
| 183 | defer os.RemoveAll(cdir) |
| 184 | |
Ankur | 9f95794 | 2014-11-24 16:34:18 -0800 | [diff] [blame] | 185 | createCredentialsInDir(t, cdir, "test") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 186 | |
| 187 | // directory supplied by the environment. |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 188 | credEnv := []string{ref.EnvCredentials + "=" + cdir} |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 189 | |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 190 | h, err := sh.Start(credEnv, runner) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 191 | if err != nil { |
| 192 | t.Fatalf("unexpected error: %s", err) |
| 193 | } |
Ankur | 9f95794 | 2014-11-24 16:34:18 -0800 | [diff] [blame] | 194 | |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 195 | runnerBlessing := h.ExpectVar("RUNNER_DEFAULT_BLESSING") |
| 196 | principalBlessing := h.ExpectVar("DEFAULT_BLESSING") |
| 197 | if err := h.Error(); err != nil { |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 198 | t.Fatalf("failed to read input from children: %s", err) |
| 199 | } |
| 200 | h.Shutdown(os.Stdout, os.Stderr) |
Ankur | 9f95794 | 2014-11-24 16:34:18 -0800 | [diff] [blame] | 201 | |
| 202 | wantRunnerBlessing := "test" |
| 203 | wantPrincipalBlessing := "test/child" |
| 204 | if runnerBlessing != wantRunnerBlessing || principalBlessing != wantPrincipalBlessing { |
| 205 | t.Fatalf("unexpected default blessing: got runner %s, principal %s, want runner %s, principal %s", runnerBlessing, principalBlessing, wantRunnerBlessing, wantPrincipalBlessing) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } |
| 209 | |
| 210 | func TestPrincipalInit(t *testing.T) { |
Ankur | 9f95794 | 2014-11-24 16:34:18 -0800 | [diff] [blame] | 211 | // Collect the process' public key and error status |
| 212 | collect := func(sh *modules.Shell, env []string, args ...string) string { |
Todd Wang | 9587390 | 2015-05-22 14:21:30 -0700 | [diff] [blame] | 213 | h, err := sh.Start(env, principal, args...) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 214 | if err != nil { |
| 215 | t.Fatalf("unexpected error: %s", err) |
| 216 | } |
| 217 | s := expect.NewSession(t, h.Stdout(), time.Minute) |
| 218 | s.SetVerbosity(testing.Verbose()) |
Ankur | 9f95794 | 2014-11-24 16:34:18 -0800 | [diff] [blame] | 219 | return s.ExpectVar("DEFAULT_BLESSING") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // A credentials directory may, or may, not have been already specified. |
| 223 | // Either way, we want to use our own, so we set it aside and use our own. |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 224 | origCredentialsDir := os.Getenv(ref.EnvCredentials) |
| 225 | defer os.Setenv(ref.EnvCredentials, origCredentialsDir) |
| 226 | if err := os.Setenv(ref.EnvCredentials, ""); err != nil { |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 227 | t.Fatal(err) |
| 228 | } |
| 229 | |
Ankur | a4b7c4a | 2015-02-02 11:41:24 -0800 | [diff] [blame] | 230 | // We create two shells -- one initializing the principal for a child process |
| 231 | // via a credentials directory and the other via an agent. |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 232 | sh, err := modules.NewShell(nil, nil, testing.Verbose(), t) |
Cosmos Nicolaou | 344cc4a | 2014-11-26 15:38:43 -0800 | [diff] [blame] | 233 | if err != nil { |
| 234 | t.Fatalf("unexpected error: %s", err) |
| 235 | } |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 236 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 237 | |
Todd Wang | 60052d8 | 2015-05-22 15:00:10 -0700 | [diff] [blame] | 238 | ctx, shutdown := test.V23Init() |
Ankur | a4b7c4a | 2015-02-02 11:41:24 -0800 | [diff] [blame] | 239 | defer shutdown() |
| 240 | |
Cosmos Nicolaou | 9e90984 | 2015-03-17 11:58:59 -0700 | [diff] [blame] | 241 | agentSh, err := modules.NewShell(ctx, v23.GetPrincipal(ctx), testing.Verbose(), t) |
Ankur | a4b7c4a | 2015-02-02 11:41:24 -0800 | [diff] [blame] | 242 | if err != nil { |
| 243 | t.Fatalf("unexpected error: %s", err) |
| 244 | } |
| 245 | defer agentSh.Cleanup(os.Stderr, os.Stderr) |
| 246 | |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 247 | // Test that with ref.EnvCredentials unset the runtime's Principal |
Ankur | a4b7c4a | 2015-02-02 11:41:24 -0800 | [diff] [blame] | 248 | // is correctly initialized for both shells. |
| 249 | if len(collect(sh, nil)) == 0 { |
| 250 | t.Fatalf("Without agent: child returned an empty default blessings set") |
| 251 | } |
Cosmos Nicolaou | a18a1eb | 2015-03-12 13:15:01 -0700 | [diff] [blame] | 252 | if got, want := collect(agentSh, nil), test.TestBlessing+security.ChainSeparator+"child"; got != want { |
Ankur | a4b7c4a | 2015-02-02 11:41:24 -0800 | [diff] [blame] | 253 | t.Fatalf("With agent: got %q, want %q", got, want) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 256 | // Test that credentials specified via the ref.EnvCredentials |
Asim Shankar | 59b8b69 | 2015-03-30 01:23:36 -0700 | [diff] [blame] | 257 | // environment variable take precedence over an agent. |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 258 | cdir1 := tmpDir(t) |
| 259 | defer os.RemoveAll(cdir1) |
Ankur | 9f95794 | 2014-11-24 16:34:18 -0800 | [diff] [blame] | 260 | createCredentialsInDir(t, cdir1, "test_env") |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 261 | credEnv := []string{ref.EnvCredentials + "=" + cdir1} |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 262 | |
Ankur | a4b7c4a | 2015-02-02 11:41:24 -0800 | [diff] [blame] | 263 | if got, want := collect(sh, credEnv), "test_env"; got != want { |
| 264 | t.Errorf("Without agent: got default blessings: %q, want %q", got, want) |
| 265 | } |
| 266 | if got, want := collect(agentSh, credEnv), "test_env"; got != want { |
| 267 | t.Errorf("With agent: got default blessings: %q, want %q", got, want) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Ankur | a4b7c4a | 2015-02-02 11:41:24 -0800 | [diff] [blame] | 270 | // Test that credentials specified via the command line take precedence over the |
Todd Wang | 8123b5e | 2015-05-14 18:44:43 -0700 | [diff] [blame] | 271 | // ref.EnvCredentials environment variable and also the agent. |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 272 | cdir2 := tmpDir(t) |
| 273 | defer os.RemoveAll(cdir2) |
Ankur | 9f95794 | 2014-11-24 16:34:18 -0800 | [diff] [blame] | 274 | createCredentialsInDir(t, cdir2, "test_cmd") |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 275 | |
Asim Shankar | f32d24d | 2015-04-01 16:34:26 -0700 | [diff] [blame] | 276 | if got, want := collect(sh, credEnv, "--v23.credentials="+cdir2), "test_cmd"; got != want { |
Ankur | a4b7c4a | 2015-02-02 11:41:24 -0800 | [diff] [blame] | 277 | t.Errorf("Without agent: got %q, want %q", got, want) |
| 278 | } |
Asim Shankar | f32d24d | 2015-04-01 16:34:26 -0700 | [diff] [blame] | 279 | if got, want := collect(agentSh, credEnv, "--v23.credentials="+cdir2), "test_cmd"; got != want { |
Ankur | a4b7c4a | 2015-02-02 11:41:24 -0800 | [diff] [blame] | 280 | t.Errorf("With agent: got %q, want %q", got, want) |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 281 | } |
| 282 | } |