Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package rt_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 5 | "io" |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 6 | "io/ioutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 7 | "os" |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 8 | "reflect" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 9 | "regexp" |
| 10 | "testing" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 11 | "time" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 13 | "veyron.io/veyron/veyron2" |
| 14 | "veyron.io/veyron/veyron2/naming" |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 15 | "veyron.io/veyron/veyron2/options" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 16 | "veyron.io/veyron/veyron2/rt" |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 17 | "veyron.io/veyron/veyron2/security" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 18 | "veyron.io/veyron/veyron2/vlog" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 19 | |
| 20 | "veyron.io/veyron/veyron/lib/expect" |
| 21 | "veyron.io/veyron/veyron/lib/modules" |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 22 | "veyron.io/veyron/veyron/lib/testutil" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 23 | irt "veyron.io/veyron/veyron/runtimes/google/rt" |
Ankur | 0e4aead | 2014-10-14 18:50:37 -0700 | [diff] [blame] | 24 | vsecurity "veyron.io/veyron/veyron/security" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 27 | type context struct { |
| 28 | local security.Principal |
| 29 | } |
| 30 | |
| 31 | func (*context) Method() string { return "" } |
| 32 | func (*context) Name() string { return "" } |
| 33 | func (*context) Suffix() string { return "" } |
| 34 | func (*context) Label() (l security.Label) { return } |
| 35 | func (*context) Discharges() map[string]security.Discharge { return nil } |
| 36 | func (*context) LocalID() security.PublicID { return nil } |
| 37 | func (*context) RemoteID() security.PublicID { return nil } |
| 38 | func (c *context) LocalPrincipal() security.Principal { return c.local } |
| 39 | func (*context) LocalBlessings() security.Blessings { return nil } |
| 40 | func (*context) RemoteBlessings() security.Blessings { return nil } |
| 41 | func (*context) LocalEndpoint() naming.Endpoint { return nil } |
| 42 | func (*context) RemoteEndpoint() naming.Endpoint { return nil } |
| 43 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 44 | func init() { |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 45 | testutil.Init() |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 46 | modules.RegisterChild("child", "", child) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func TestHelperProcess(t *testing.T) { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 50 | modules.DispatchInTest() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | func TestInit(t *testing.T) { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 54 | r, err := rt.New(profileOpt) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 55 | if err != nil { |
| 56 | t.Fatalf("error: %s", err) |
| 57 | } |
| 58 | l := r.Logger() |
| 59 | args := fmt.Sprintf("%s", l) |
| 60 | expected := regexp.MustCompile("name=veyron logdirs=\\[/tmp\\] logtostderr=true|false alsologtostderr=false|true max_stack_buf_size=4292608 v=[0-9] stderrthreshold=2 vmodule= log_backtrace_at=:0") |
| 61 | if !expected.MatchString(args) { |
| 62 | t.Errorf("unexpected default args: %q", args) |
| 63 | } |
| 64 | if id := r.Identity().PublicID(); id == nil || len(id.Names()) == 0 || len(id.Names()[0]) == 0 { |
| 65 | t.Errorf("New should have created an identity. Created %v", id) |
| 66 | } |
| 67 | } |
| 68 | |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 69 | func child(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 70 | r := rt.Init() |
| 71 | vlog.Infof("%s\n", r.Logger()) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 72 | fmt.Fprintf(stdout, "%s\n", r.Logger()) |
| 73 | modules.WaitForEOF(stdin) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 74 | fmt.Printf("done\n") |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 75 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func TestInitArgs(t *testing.T) { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 79 | sh := modules.NewShell("child") |
| 80 | defer sh.Cleanup(os.Stderr, os.Stderr) |
Cosmos Nicolaou | 612ad38 | 2014-10-29 19:41:35 -0700 | [diff] [blame^] | 81 | h, err := sh.Start("child", nil, "--logtostderr=true", "--vv=3", "--", "foobar") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 82 | if err != nil { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 83 | t.Fatalf("unexpected error: %s", err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 84 | } |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 85 | s := expect.NewSession(t, h.Stdout(), time.Minute) |
| 86 | s.Expect(fmt.Sprintf("name=veyron "+ |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 87 | "logdirs=[%s] "+ |
| 88 | "logtostderr=true "+ |
| 89 | "alsologtostderr=true "+ |
| 90 | "max_stack_buf_size=4292608 "+ |
| 91 | "v=3 "+ |
| 92 | "stderrthreshold=2 "+ |
| 93 | "vmodule= "+ |
| 94 | "log_backtrace_at=:0", |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 95 | os.TempDir())) |
| 96 | h.CloseStdin() |
| 97 | s.Expect("done") |
| 98 | s.ExpectEOF() |
| 99 | h.Shutdown(os.Stderr, os.Stderr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 100 | } |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 101 | |
| 102 | func TestInitPrincipal(t *testing.T) { |
| 103 | newRT := func() veyron2.Runtime { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 104 | r, err := rt.New(profileOpt) |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 105 | if err != nil { |
| 106 | t.Fatalf("rt.New failed: %v", err) |
| 107 | } |
| 108 | return r |
| 109 | } |
| 110 | testPrincipal := func(r veyron2.Runtime) security.Principal { |
| 111 | p := r.Principal() |
| 112 | if p == nil { |
| 113 | t.Fatalf("rt.Principal() returned nil") |
| 114 | } |
| 115 | blessings := p.BlessingStore().Default() |
| 116 | if blessings == nil { |
| 117 | t.Fatalf("rt.Principal().BlessingStore().Default() returned nil") |
| 118 | |
| 119 | } |
| 120 | if n := len(blessings.ForContext(&context{local: p})); n != 1 { |
| 121 | t.Fatalf("rt.Principal().BlessingStore().Default() returned Blessing %v with %d recognized blessings, want exactly one recognized blessing", blessings, n) |
| 122 | } |
| 123 | return p |
| 124 | } |
| 125 | origCredentialsDir := os.Getenv(irt.VeyronCredentialsEnvVar) |
| 126 | defer os.Setenv(irt.VeyronCredentialsEnvVar, origCredentialsDir) |
| 127 | |
| 128 | // Test that even with VEYRON_CREDENTIALS unset the runtime's Principal |
| 129 | // is correctly initialized. |
| 130 | if err := os.Setenv(irt.VeyronCredentialsEnvVar, ""); err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | testPrincipal(newRT()) |
| 134 | |
| 135 | // Test that with VEYRON_CREDENTIALS set the runtime's Principal is correctly |
| 136 | // initialized. |
| 137 | credentials, err := ioutil.TempDir("", "credentials") |
| 138 | if err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | defer os.RemoveAll(credentials) |
| 142 | if err := os.Setenv(irt.VeyronCredentialsEnvVar, credentials); err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | p := testPrincipal(newRT()) |
| 146 | |
| 147 | // Mutate the roots and store of this principal. |
| 148 | blessing, err := p.BlessSelf("irrelevant") |
| 149 | if err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
Asim Shankar | 48bf0e6 | 2014-10-03 16:27:05 -0700 | [diff] [blame] | 152 | if _, err := p.BlessingStore().Set(blessing, security.AllPrincipals); err != nil { |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 153 | t.Fatal(err) |
| 154 | } |
| 155 | if err := p.AddToRoots(blessing); err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | |
| 159 | // Test that the same principal gets initialized on creating a new runtime |
| 160 | // from the same credentials directory. |
| 161 | if got := newRT().Principal(); !reflect.DeepEqual(got, p) { |
Asim Shankar | ae8d4c5 | 2014-10-08 13:03:31 -0700 | [diff] [blame] | 162 | t.Fatalf("Initialized Principal: %v, expected: %v", got.PublicKey(), p.PublicKey()) |
Ankur | 7c89059 | 2014-10-02 11:36:28 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
Ankur | 0e4aead | 2014-10-14 18:50:37 -0700 | [diff] [blame] | 165 | |
| 166 | func TestInitPrincipalFromOption(t *testing.T) { |
| 167 | p, err := vsecurity.NewPrincipal() |
| 168 | if err != nil { |
| 169 | t.Fatalf("NewPrincipal() failed: %v", err) |
| 170 | } |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 171 | r, err := rt.New(profileOpt, options.RuntimePrincipal{p}) |
Ankur | 0e4aead | 2014-10-14 18:50:37 -0700 | [diff] [blame] | 172 | if err != nil { |
| 173 | t.Fatalf("rt.New failed: %v", err) |
| 174 | } |
| 175 | |
| 176 | if got := r.Principal(); !reflect.DeepEqual(got, p) { |
| 177 | t.Fatalf("r.Principal(): got %v, want %v", got, p) |
| 178 | } |
| 179 | } |