Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 1 | // TODO(caprita): This file is becoming unmanageable; split into several test |
| 2 | // files. |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 3 | // TODO(rjkroege): Add a more extensive unit test case to exercise ACL logic. |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 4 | |
Jiri Simsa | 70c3205 | 2014-06-18 11:38:21 -0700 | [diff] [blame] | 5 | package impl_test |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 8 | "bytes" |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 9 | "crypto/md5" |
| 10 | "encoding/base64" |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 11 | "encoding/hex" |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 12 | "encoding/json" |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 13 | "flag" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 14 | "fmt" |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 15 | "io" |
Jiri Simsa | 70c3205 | 2014-06-18 11:38:21 -0700 | [diff] [blame] | 16 | "io/ioutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 17 | "os" |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 18 | goexec "os/exec" |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 19 | "os/user" |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 20 | "path" |
Jiri Simsa | 70c3205 | 2014-06-18 11:38:21 -0700 | [diff] [blame] | 21 | "path/filepath" |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 22 | "reflect" |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 23 | "regexp" |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 24 | "sort" |
Jiri Simsa | 70c3205 | 2014-06-18 11:38:21 -0700 | [diff] [blame] | 25 | "strings" |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 26 | "syscall" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 27 | "testing" |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 28 | "time" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 29 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 30 | "v.io/core/veyron2" |
| 31 | "v.io/core/veyron2/context" |
| 32 | "v.io/core/veyron2/ipc" |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 33 | "v.io/core/veyron2/mgmt" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 34 | "v.io/core/veyron2/naming" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 35 | "v.io/core/veyron2/security" |
| 36 | "v.io/core/veyron2/services/mgmt/application" |
| 37 | "v.io/core/veyron2/services/mgmt/device" |
| 38 | "v.io/core/veyron2/services/mgmt/logreader" |
| 39 | "v.io/core/veyron2/services/mgmt/pprof" |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 40 | "v.io/core/veyron2/services/mgmt/repository" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 41 | "v.io/core/veyron2/services/mgmt/stats" |
| 42 | "v.io/core/veyron2/services/security/access" |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 43 | "v.io/core/veyron2/verror" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 44 | "v.io/core/veyron2/vlog" |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 45 | |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 46 | "v.io/core/veyron/lib/expect" |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 47 | "v.io/core/veyron/lib/flags/consts" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 48 | "v.io/core/veyron/lib/modules" |
| 49 | "v.io/core/veyron/lib/signals" |
| 50 | "v.io/core/veyron/lib/testutil" |
| 51 | tsecurity "v.io/core/veyron/lib/testutil/security" |
| 52 | binaryimpl "v.io/core/veyron/services/mgmt/binary/impl" |
| 53 | "v.io/core/veyron/services/mgmt/device/config" |
| 54 | "v.io/core/veyron/services/mgmt/device/impl" |
Asim Shankar | ab75bc3 | 2015-02-12 21:52:04 -0800 | [diff] [blame] | 55 | "v.io/core/veyron/services/mgmt/device/starter" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 56 | libbinary "v.io/core/veyron/services/mgmt/lib/binary" |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 57 | mgmttest "v.io/core/veyron/services/mgmt/lib/testutil" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 58 | suidhelper "v.io/core/veyron/services/mgmt/suidhelper/impl" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 59 | ) |
| 60 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 61 | const ( |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 62 | // Modules command names. |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 63 | execScriptCmd = "execScriptCmd" |
| 64 | deviceManagerCmd = "deviceManager" |
| 65 | appCmd = "app" |
| 66 | installerCmd = "installer" |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 67 | uninstallerCmd = "uninstaller" |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 68 | |
| 69 | testFlagName = "random_test_flag" |
| 70 | // VEYRON prefix is necessary to pass the env filtering. |
| 71 | testEnvVarName = "VEYRON_RANDOM_ENV_VALUE" |
Robin Thellend | f598628 | 2015-02-13 11:01:03 -0800 | [diff] [blame] | 72 | |
| 73 | redirectEnv = "DEVICE_MANAGER_DONT_REDIRECT_STDOUT_STDERR" |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 74 | |
| 75 | noPairingToken = "" |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 76 | ) |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 77 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 78 | var flagValue = flag.String(testFlagName, "default", "") |
| 79 | |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 80 | func init() { |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 81 | // The installer sets this flag on the installed device manager, so we |
| 82 | // need to ensure it's defined. |
| 83 | flag.String("name", "", "") |
| 84 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 85 | modules.RegisterChild(execScriptCmd, "", execScript) |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 86 | modules.RegisterChild(deviceManagerCmd, "", deviceManager) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 87 | modules.RegisterChild(appCmd, "", app) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 88 | |
| 89 | if modules.IsModulesProcess() { |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 90 | return |
| 91 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Suharsh Sivakumar | 9d17e4a | 2015-02-02 22:42:16 -0800 | [diff] [blame] | 94 | func TestMain(m *testing.M) { |
| 95 | testutil.Init() |
| 96 | os.Exit(m.Run()) |
| 97 | } |
| 98 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 99 | // TestHelperProcess is the entrypoint for the modules commands in a |
| 100 | // a test subprocess. |
| 101 | func TestHelperProcess(t *testing.T) { |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 102 | modules.DispatchInTest() |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // TestSuidHelper is testing boilerplate for suidhelper that does not |
Matt Rosencrantz | 5180d16 | 2014-12-03 13:48:40 -0800 | [diff] [blame] | 106 | // create a runtime because the suidhelper is not a Veyron application. |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 107 | func TestSuidHelper(t *testing.T) { |
| 108 | if os.Getenv("VEYRON_SUIDHELPER_TEST") != "1" { |
| 109 | return |
| 110 | } |
| 111 | vlog.VI(1).Infof("TestSuidHelper starting") |
| 112 | |
| 113 | if err := suidhelper.Run(os.Environ()); err != nil { |
| 114 | vlog.Fatalf("Failed to Run() setuidhelper: %v", err) |
| 115 | } |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // execScript launches the script passed as argument. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 119 | func execScript(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 120 | if want, got := 1, len(args); want != got { |
| 121 | vlog.Fatalf("execScript expected %d arguments, got %d instead", want, got) |
| 122 | } |
| 123 | script := args[0] |
Robin Thellend | f598628 | 2015-02-13 11:01:03 -0800 | [diff] [blame] | 124 | osenv := []string{redirectEnv + "=1"} |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 125 | if env["PAUSE_BEFORE_STOP"] == "1" { |
| 126 | osenv = append(osenv, "PAUSE_BEFORE_STOP=1") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 127 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 128 | |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 129 | cmd := goexec.Cmd{ |
| 130 | Path: script, |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 131 | Env: osenv, |
| 132 | Stdin: stdin, |
| 133 | Stderr: stderr, |
| 134 | Stdout: stdout, |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 135 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 136 | |
| 137 | return cmd.Run() |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 140 | // deviceManager sets up a device manager server. It accepts the name to |
| 141 | // publish the server under as an argument. Additional arguments can optionally |
| 142 | // specify device manager config settings. |
| 143 | func deviceManager(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 144 | ctx, shutdown := testutil.InitForTest() |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 145 | if len(args) == 0 { |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 146 | vlog.Fatalf("deviceManager expected at least an argument") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 147 | } |
| 148 | publishName := args[0] |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 149 | args = args[1:] |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 150 | defer fmt.Fprintf(stdout, "%v terminated\n", publishName) |
| 151 | defer vlog.VI(1).Infof("%v terminated", publishName) |
| 152 | defer shutdown() |
| 153 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 154 | |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 155 | // Satisfy the contract described in doc.go by passing the config state |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 156 | // through to the device manager dispatcher constructor. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 157 | configState, err := config.Load() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 158 | if err != nil { |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 159 | vlog.Fatalf("Failed to decode config state: %v", err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 160 | } |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 161 | |
| 162 | // This exemplifies how to override or set specific config fields, if, |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 163 | // for example, the device manager is invoked 'by hand' instead of via a |
| 164 | // script prepared by a previous version of the device manager. |
gauthamt | 27f38ba | 2015-02-13 13:45:01 -0800 | [diff] [blame] | 165 | var pairingToken string |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 166 | if len(args) > 0 { |
gauthamt | 27f38ba | 2015-02-13 13:45:01 -0800 | [diff] [blame] | 167 | if want, got := 4, len(args); want > got { |
| 168 | vlog.Fatalf("expected atleast %d additional arguments, got %d instead: %q", want, got, args) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 169 | } |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 170 | configState.Root, configState.Helper, configState.Origin, configState.CurrentLink = args[0], args[1], args[2], args[3] |
gauthamt | 27f38ba | 2015-02-13 13:45:01 -0800 | [diff] [blame] | 171 | if len(args) > 4 { |
| 172 | pairingToken = args[4] |
| 173 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 174 | } |
Robin Thellend | acaa432 | 2015-02-05 11:00:28 -0800 | [diff] [blame] | 175 | |
Asim Shankar | ab75bc3 | 2015-02-12 21:52:04 -0800 | [diff] [blame] | 176 | stop, err := starter.Start(ctx, starter.Args{ |
| 177 | Namespace: starter.NamespaceArgs{ |
| 178 | ListenSpec: ipc.ListenSpec{Addrs: ipc.ListenAddrs{{"tcp", "127.0.0.1:0"}}}, |
| 179 | }, |
| 180 | Device: starter.DeviceArgs{ |
| 181 | Name: publishName, |
| 182 | ListenSpec: ipc.ListenSpec{Addrs: ipc.ListenAddrs{{"tcp", "127.0.0.1:0"}}}, |
| 183 | ConfigState: configState, |
| 184 | TestMode: strings.HasSuffix(fmt.Sprint(veyron2.GetPrincipal(ctx).BlessingStore().Default()), "/testdm"), |
| 185 | RestartCallback: func() { fmt.Println("restart handler") }, |
gauthamt | 27f38ba | 2015-02-13 13:45:01 -0800 | [diff] [blame] | 186 | PairingToken: pairingToken, |
Asim Shankar | ab75bc3 | 2015-02-12 21:52:04 -0800 | [diff] [blame] | 187 | }, |
| 188 | // TODO(rthellend): Wire up the local mounttable like the real device |
| 189 | // manager, i.e. mount the device manager and the apps on it, and mount |
| 190 | // the local mounttable in the global namespace. |
| 191 | // MountGlobalNamespaceInLocalNamespace: true, |
| 192 | }) |
Robin Thellend | acaa432 | 2015-02-05 11:00:28 -0800 | [diff] [blame] | 193 | if err != nil { |
Asim Shankar | ab75bc3 | 2015-02-12 21:52:04 -0800 | [diff] [blame] | 194 | vlog.Errorf("starter.Start failed: %v", err) |
Robin Thellend | acaa432 | 2015-02-05 11:00:28 -0800 | [diff] [blame] | 195 | return err |
| 196 | } |
| 197 | defer stop() |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 198 | fmt.Fprintf(stdout, "ready:%d\n", os.Getpid()) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 199 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 200 | <-signals.ShutdownOnSignals(ctx) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 201 | if val, present := env["PAUSE_BEFORE_STOP"]; present && val == "1" { |
| 202 | modules.WaitForEOF(stdin) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 203 | } |
Asim Shankar | ab75bc3 | 2015-02-12 21:52:04 -0800 | [diff] [blame] | 204 | // TODO(ashankar): Figure out a way to incorporate this check in the test. |
| 205 | // if impl.DispatcherLeaking(dispatcher) { |
| 206 | // vlog.Fatalf("device manager leaking resources") |
| 207 | // } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 208 | return nil |
| 209 | } |
| 210 | |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 211 | // appService defines a test service that the test app should be running. |
| 212 | // TODO(caprita): Use this to make calls to the app and verify how Suspend/Stop |
| 213 | // interact with an active service. |
| 214 | type appService struct{} |
| 215 | |
Todd Wang | 1fe7cdd | 2014-11-12 12:51:49 -0800 | [diff] [blame] | 216 | func (appService) Echo(_ ipc.ServerContext, message string) (string, error) { |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 217 | return message, nil |
| 218 | } |
| 219 | |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 220 | func (appService) Cat(_ ipc.ServerContext, file string) (string, error) { |
| 221 | if file == "" || file[0] == filepath.Separator || file[0] == '.' { |
| 222 | return "", fmt.Errorf("illegal file name: %q", file) |
| 223 | } |
| 224 | bytes, err := ioutil.ReadFile(file) |
| 225 | if err != nil { |
| 226 | return "", err |
| 227 | } |
| 228 | return string(bytes), nil |
| 229 | } |
| 230 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 231 | type pingArgs struct { |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 232 | Username, FlagValue, EnvValue string |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 235 | func ping(ctx *context.T) { |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 236 | helperEnv := os.Getenv(suidhelper.SavedArgs) |
| 237 | d := json.NewDecoder(strings.NewReader(helperEnv)) |
| 238 | var savedArgs suidhelper.ArgsSavedForTest |
| 239 | if err := d.Decode(&savedArgs); err != nil { |
| 240 | vlog.Fatalf("Failed to decode preserved argument %v: %v", helperEnv, err) |
| 241 | } |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 242 | args := &pingArgs{ |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 243 | // TODO(rjkroege): Consider validating additional parameters |
| 244 | // from helper. |
| 245 | Username: savedArgs.Uname, |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 246 | FlagValue: *flagValue, |
| 247 | EnvValue: os.Getenv(testEnvVarName), |
| 248 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 249 | client := veyron2.GetClient(ctx) |
| 250 | if call, err := client.StartCall(ctx, "pingserver", "Ping", []interface{}{args}); err != nil { |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 251 | vlog.Fatalf("StartCall failed: %v", err) |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 252 | } else if err := call.Finish(); err != nil { |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 253 | vlog.Fatalf("Finish failed: %v", err) |
| 254 | } |
| 255 | } |
| 256 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 257 | func cat(ctx *context.T, name, file string) (string, error) { |
| 258 | ctx, cancel := context.WithTimeout(ctx, time.Minute) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 259 | defer cancel() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 260 | client := veyron2.GetClient(ctx) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 261 | call, err := client.StartCall(ctx, name, "Cat", []interface{}{file}) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 262 | if err != nil { |
| 263 | return "", err |
| 264 | } |
| 265 | var content string |
| 266 | if ferr := call.Finish(&content, &err); ferr != nil { |
| 267 | err = ferr |
| 268 | } |
| 269 | return content, err |
| 270 | } |
| 271 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 272 | func app(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 273 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 274 | defer shutdown() |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 275 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 276 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 277 | |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 278 | if expected, got := 1, len(args); expected != got { |
| 279 | vlog.Fatalf("Unexpected number of arguments: expected %d, got %d", expected, got) |
| 280 | } |
| 281 | publishName := args[0] |
| 282 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 283 | server, _ := mgmttest.NewServer(ctx) |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 284 | defer server.Stop() |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 285 | if err := server.Serve(publishName, new(appService), nil); err != nil { |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 286 | vlog.Fatalf("Serve(%v) failed: %v", publishName, err) |
| 287 | } |
Cosmos Nicolaou | e339153 | 2014-11-25 19:08:32 -0800 | [diff] [blame] | 288 | // Some of our tests look for log files, so make sure they are flushed |
| 289 | // to ensure that at least the files exist. |
| 290 | vlog.FlushLog() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 291 | ping(ctx) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 292 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 293 | <-signals.ShutdownOnSignals(ctx) |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 294 | if err := ioutil.WriteFile("testfile", []byte("goodbye world"), 0600); err != nil { |
| 295 | vlog.Fatalf("Failed to write testfile: %v", err) |
| 296 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 297 | return nil |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 300 | // TODO(rjkroege): generateDeviceManagerScript and generateSuidHelperScript have |
| 301 | // code similarity that might benefit from refactoring. |
| 302 | // generateDeviceManagerScript is very similar in behavior to generateScript in |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 303 | // device_invoker.go. However, we chose to re-implement it here for two |
| 304 | // reasons: (1) avoid making generateScript public; and (2) how the test choses |
| 305 | // to invoke the device manager subprocess the first time should be independent |
| 306 | // of how device manager implementation sets up its updated versions. |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 307 | func generateDeviceManagerScript(t *testing.T, root string, args, env []string) string { |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 308 | env = impl.VeyronEnvironment(env) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 309 | output := "#!/bin/bash\n" |
Bogdan Caprita | 069341a | 2014-12-09 22:59:17 -0800 | [diff] [blame] | 310 | output += strings.Join(config.QuoteEnv(env), " ") + " exec " |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 311 | output += strings.Join(args, " ") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 312 | if err := os.MkdirAll(filepath.Join(root, "factory"), 0755); err != nil { |
| 313 | t.Fatalf("MkdirAll failed: %v", err) |
| 314 | } |
| 315 | // Why pigeons? To show that the name we choose for the initial script |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 316 | // doesn't matter and in particular is independent of how device manager |
| 317 | // names its updated version scripts (deviced.sh). |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 318 | path := filepath.Join(root, "factory", "pigeons.sh") |
| 319 | if err := ioutil.WriteFile(path, []byte(output), 0755); err != nil { |
| 320 | t.Fatalf("WriteFile(%v) failed: %v", path, err) |
| 321 | } |
| 322 | return path |
| 323 | } |
| 324 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 325 | // TestDeviceManagerUpdateAndRevert makes the device manager go through the |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 326 | // motions of updating itself to newer versions (twice), and reverting itself |
| 327 | // back (twice). It also checks that update and revert fail when they're |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 328 | // supposed to. The initial device manager is started 'by hand' via a module |
| 329 | // command. Further versions are started through the soft link that the device |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 330 | // manager itself updates. |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 331 | func TestDeviceManagerUpdateAndRevert(t *testing.T) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 332 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 333 | defer shutdown() |
| 334 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 335 | |
| 336 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, veyron2.GetPrincipal(ctx)) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 337 | defer deferFn() |
| 338 | |
| 339 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 340 | envelope, cleanup := startMockRepos(t, ctx) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 341 | defer cleanup() |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 342 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 343 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Bogdan Caprita | 080a730 | 2014-08-20 15:35:37 -0700 | [diff] [blame] | 344 | defer cleanup() |
Jiri Simsa | e0fc61a | 2014-07-30 14:41:55 -0700 | [diff] [blame] | 345 | |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 346 | // Current link does not have to live in the root dir, but it's |
| 347 | // convenient to put it there so we have everything in one place. |
| 348 | currLink := filepath.Join(root, "current_link") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 349 | |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 350 | // Since the device manager will be restarted, use the |
| 351 | // VeyronCredentials environment variable to maintain the same set of |
| 352 | // credentials across runs. |
| 353 | // Without this, authentication/authorizatin state - such as the blessings |
| 354 | // of the device manager and the signatures used for ACL integrity checks |
| 355 | // - will not carry over between updates to the binary, which would not |
| 356 | // be reflective of intended use. |
| 357 | dmCreds, err := ioutil.TempDir("", "TestDeviceManagerUpdateAndRevert") |
| 358 | if err != nil { |
| 359 | t.Fatal(err) |
| 360 | } |
| 361 | defer os.RemoveAll(dmCreds) |
| 362 | dmEnv := []string{fmt.Sprintf("%v=%v", consts.VeyronCredentials, dmCreds)} |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 363 | dmArgs := []string{"factoryDM", root, "unused_helper", mockApplicationRepoName, currLink} |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 364 | args, env := sh.CommandEnvelope(deviceManagerCmd, dmEnv, dmArgs...) |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 365 | scriptPathFactory := generateDeviceManagerScript(t, root, args, env) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 366 | |
| 367 | if err := os.Symlink(scriptPathFactory, currLink); err != nil { |
| 368 | t.Fatalf("Symlink(%q, %q) failed: %v", scriptPathFactory, currLink, err) |
| 369 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 370 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 371 | // We instruct the initial device manager that we run to pause before |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 372 | // stopping its service, so that we get a chance to verify that |
| 373 | // attempting an update while another one is ongoing will fail. |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 374 | dmPauseBeforeStopEnv := append(dmEnv, "PAUSE_BEFORE_STOP=1") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 375 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 376 | // Start the initial version of the device manager, the so-called |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 377 | // "factory" version. We use the modules-generated command to start it. |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 378 | // We could have also used the scriptPathFactory to start it, but this |
| 379 | // demonstrates that the initial device manager could be started by hand |
| 380 | // as long as the right initial configuration is passed into the device |
| 381 | // manager implementation. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 382 | dmh, dms := mgmttest.RunShellCommand(t, sh, dmPauseBeforeStopEnv, deviceManagerCmd, dmArgs...) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 383 | defer func() { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 384 | syscall.Kill(dmh.Pid(), syscall.SIGINT) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 385 | }() |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 386 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 387 | mgmttest.ReadPID(t, dms) |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 388 | // Brand new device manager must be claimed first. |
| 389 | claimDevice(t, ctx, "factoryDM", "mydevice", noPairingToken) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 390 | |
| 391 | // Simulate an invalid envelope in the application repository. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 392 | *envelope = envelopeFromShell(sh, dmPauseBeforeStopEnv, deviceManagerCmd, "bogus", dmArgs...) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 393 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 394 | updateDeviceExpectError(t, ctx, "factoryDM", impl.ErrAppTitleMismatch.ID) |
| 395 | revertDeviceExpectError(t, ctx, "factoryDM", impl.ErrUpdateNoOp.ID) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 396 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 397 | // Set up a second version of the device manager. The information in the |
| 398 | // envelope will be used by the device manager to stage the next |
| 399 | // version. |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 400 | *envelope = envelopeFromShell(sh, dmEnv, deviceManagerCmd, application.DeviceManagerTitle, "v2DM") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 401 | updateDevice(t, ctx, "factoryDM") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 402 | |
| 403 | // Current link should have been updated to point to v2. |
| 404 | evalLink := func() string { |
| 405 | path, err := filepath.EvalSymlinks(currLink) |
| 406 | if err != nil { |
| 407 | t.Fatalf("EvalSymlinks(%v) failed: %v", currLink, err) |
| 408 | } |
| 409 | return path |
| 410 | } |
| 411 | scriptPathV2 := evalLink() |
| 412 | if scriptPathFactory == scriptPathV2 { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 413 | t.Fatalf("current link didn't change") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 416 | updateDeviceExpectError(t, ctx, "factoryDM", impl.ErrOperationInProgress.ID) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 417 | |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 418 | dmh.CloseStdin() |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 419 | |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 420 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 421 | dms.Expect("factoryDM terminated") |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 422 | dmh.Shutdown(os.Stderr, os.Stderr) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 423 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 424 | // A successful update means the device manager has stopped itself. We |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 425 | // relaunch it from the current link. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 426 | resolveExpectNotFound(t, ctx, "v2DM") // Ensure a clean slate. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 427 | |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 428 | dmh, dms = mgmttest.RunShellCommand(t, sh, dmEnv, execScriptCmd, currLink) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 429 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 430 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 431 | resolve(t, ctx, "v2DM", 1) // Current link should have been launching v2. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 432 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 433 | // Try issuing an update without changing the envelope in the |
| 434 | // application repository: this should fail, and current link should be |
| 435 | // unchanged. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 436 | updateDeviceExpectError(t, ctx, "v2DM", impl.ErrUpdateNoOp.ID) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 437 | if evalLink() != scriptPathV2 { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 438 | t.Fatalf("script changed") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 441 | // Create a third version of the device manager and issue an update. |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 442 | *envelope = envelopeFromShell(sh, dmEnv, deviceManagerCmd, application.DeviceManagerTitle, "v3DM") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 443 | updateDevice(t, ctx, "v2DM") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 444 | |
| 445 | scriptPathV3 := evalLink() |
| 446 | if scriptPathV3 == scriptPathV2 { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 447 | t.Fatalf("current link didn't change") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 450 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 451 | dms.Expect("v2DM terminated") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 452 | |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 453 | dmh.Shutdown(os.Stderr, os.Stderr) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 454 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 455 | resolveExpectNotFound(t, ctx, "v3DM") // Ensure a clean slate. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 456 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 457 | // Re-lanuch the device manager from current link. We instruct the |
| 458 | // device manager to pause before stopping its server, so that we can |
| 459 | // verify that a second revert fails while a revert is in progress. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 460 | dmh, dms = mgmttest.RunShellCommand(t, sh, dmPauseBeforeStopEnv, execScriptCmd, currLink) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 461 | |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 462 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 463 | resolve(t, ctx, "v3DM", 1) // Current link should have been launching v3. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 464 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 465 | // Revert the device manager to its previous version (v2). |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 466 | revertDevice(t, ctx, "v3DM") |
| 467 | revertDeviceExpectError(t, ctx, "v3DM", impl.ErrOperationInProgress.ID) // Revert already in progress. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 468 | dmh.CloseStdin() |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 469 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 470 | dms.Expect("v3DM terminated") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 471 | if evalLink() != scriptPathV2 { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 472 | t.Fatalf("current link was not reverted correctly") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 473 | } |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 474 | dmh.Shutdown(os.Stderr, os.Stderr) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 475 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 476 | resolveExpectNotFound(t, ctx, "v2DM") // Ensure a clean slate. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 477 | |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 478 | dmh, dms = mgmttest.RunShellCommand(t, sh, dmEnv, execScriptCmd, currLink) |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 479 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 480 | resolve(t, ctx, "v2DM", 1) // Current link should have been launching v2. |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 481 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 482 | // Revert the device manager to its previous version (factory). |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 483 | revertDevice(t, ctx, "v2DM") |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 484 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 485 | dms.Expect("v2DM terminated") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 486 | if evalLink() != scriptPathFactory { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 487 | t.Fatalf("current link was not reverted correctly") |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 488 | } |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 489 | dmh.Shutdown(os.Stderr, os.Stderr) |
Bogdan Caprita | c87a914 | 2014-07-21 10:38:13 -0700 | [diff] [blame] | 490 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 491 | resolveExpectNotFound(t, ctx, "factoryDM") // Ensure a clean slate. |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 492 | |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 493 | dmh, dms = mgmttest.RunShellCommand(t, sh, dmEnv, execScriptCmd, currLink) |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 494 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 495 | resolve(t, ctx, "factoryDM", 1) // Current link should have been launching factory version. |
| 496 | stopDevice(t, ctx, "factoryDM") |
| 497 | dms.Expect("factoryDM terminated") |
Bogdan Caprita | 4ea9b03 | 2014-12-27 14:56:51 -0800 | [diff] [blame] | 498 | dms.ExpectEOF() |
| 499 | |
| 500 | // Re-launch the device manager, to exercise the behavior of Suspend. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 501 | resolveExpectNotFound(t, ctx, "factoryDM") // Ensure a clean slate. |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 502 | dmh, dms = mgmttest.RunShellCommand(t, sh, dmEnv, execScriptCmd, currLink) |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 503 | mgmttest.ReadPID(t, dms) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 504 | resolve(t, ctx, "factoryDM", 1) |
| 505 | suspendDevice(t, ctx, "factoryDM") |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 506 | dms.Expect("restart handler") |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 507 | dms.Expect("factoryDM terminated") |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 508 | dms.ExpectEOF() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 509 | } |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 510 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 511 | type pingServer chan<- pingArgs |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 512 | |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 513 | // TODO(caprita): Set the timeout in a more principled manner. |
Bogdan Caprita | a5dd989 | 2015-01-02 11:16:25 -0800 | [diff] [blame] | 514 | const pingTimeout = 60 * time.Second |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 515 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 516 | func (p pingServer) Ping(_ ipc.ServerContext, arg pingArgs) { |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 517 | p <- arg |
| 518 | } |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 519 | |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 520 | // setupPingServer creates a server listening for a ping from a child app; it |
| 521 | // returns a channel on which the app's ping message is returned, and a cleanup |
| 522 | // function. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 523 | func setupPingServer(t *testing.T, ctx *context.T) (<-chan pingArgs, func()) { |
| 524 | server, _ := mgmttest.NewServer(ctx) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 525 | pingCh := make(chan pingArgs, 1) |
Robin Thellend | 888f8cf | 2014-12-15 16:19:10 -0800 | [diff] [blame] | 526 | if err := server.Serve("pingserver", pingServer(pingCh), &openAuthorizer{}); err != nil { |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 527 | t.Fatalf("Serve(%q, <dispatcher>) failed: %v", "pingserver", err) |
| 528 | } |
| 529 | return pingCh, func() { |
| 530 | if err := server.Stop(); err != nil { |
| 531 | t.Fatalf("Stop() failed: %v", err) |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
Robert Kroeger | 124a04f | 2015-02-03 17:32:32 -0800 | [diff] [blame] | 536 | func instanceDirForApp(root, appID, instanceID string) string { |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 537 | applicationDirName := func(title string) string { |
| 538 | h := md5.New() |
| 539 | h.Write([]byte(title)) |
| 540 | hash := strings.TrimRight(base64.URLEncoding.EncodeToString(h.Sum(nil)), "=") |
| 541 | return "app-" + hash |
| 542 | } |
| 543 | components := strings.Split(appID, "/") |
| 544 | appTitle, installationID := components[0], components[1] |
Robert Kroeger | 124a04f | 2015-02-03 17:32:32 -0800 | [diff] [blame] | 545 | return filepath.Join(root, applicationDirName(appTitle), "installation-"+installationID, "instances", "instance-"+instanceID) |
| 546 | } |
| 547 | |
| 548 | func verifyAppWorkspace(t *testing.T, root, appID, instanceID string) { |
| 549 | // HACK ALERT: for now, we peek inside the device manager's directory |
| 550 | // structure (which ought to be opaque) to check for what the app has |
| 551 | // written to its local root. |
| 552 | // |
| 553 | // TODO(caprita): add support to device manager to browse logs/app local |
| 554 | // root. |
| 555 | rootDir := filepath.Join(instanceDirForApp(root, appID, instanceID), "root") |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 556 | testFile := filepath.Join(rootDir, "testfile") |
| 557 | if read, err := ioutil.ReadFile(testFile); err != nil { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 558 | t.Fatalf("Failed to read %v: %v", testFile, err) |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 559 | } else if want, got := "goodbye world", string(read); want != got { |
Jiri Simsa | 6351ee7 | 2014-08-18 16:44:41 -0700 | [diff] [blame] | 560 | t.Fatalf("Expected to read %v, got %v instead", want, got) |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 561 | } |
| 562 | // END HACK |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 563 | } |
Bogdan Caprita | 78b6216 | 2014-08-21 15:35:08 -0700 | [diff] [blame] | 564 | |
Robert Kroeger | 124a04f | 2015-02-03 17:32:32 -0800 | [diff] [blame] | 565 | func verifyAppState(t *testing.T, root, appID, instanceID, state string) { |
| 566 | // Same hack alert as verifyAppWorkspace |
| 567 | testFile := filepath.Join(instanceDirForApp(root, appID, instanceID), state) |
| 568 | if read, err := ioutil.ReadFile(testFile); err != nil { |
Robert Kroeger | 7464517 | 2015-02-11 14:22:21 -0800 | [diff] [blame] | 569 | t.Fatalf(testutil.FormatLogLine(2, "failed to get read %v: %v", testFile, err)) |
Robert Kroeger | 124a04f | 2015-02-03 17:32:32 -0800 | [diff] [blame] | 570 | } else if want, got := "status", string(read); want != got { |
Robert Kroeger | 7464517 | 2015-02-11 14:22:21 -0800 | [diff] [blame] | 571 | t.Fatalf(testutil.FormatLogLine(2, "Expected to read %v, got %v instead", want, got)) |
Robert Kroeger | 124a04f | 2015-02-03 17:32:32 -0800 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 575 | func verifyPingArgs(t *testing.T, pingCh <-chan pingArgs, username, flagValue, envValue string) { |
| 576 | var args pingArgs |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 577 | select { |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 578 | case args = <-pingCh: |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 579 | case <-time.After(pingTimeout): |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 580 | t.Fatalf(testutil.FormatLogLine(2, "failed to get ping")) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 581 | } |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 582 | wantArgs := pingArgs{ |
| 583 | Username: username, |
| 584 | FlagValue: flagValue, |
| 585 | EnvValue: envValue, |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 586 | } |
Bogdan Caprita | e1960f5 | 2015-01-14 09:57:19 -0800 | [diff] [blame] | 587 | if !reflect.DeepEqual(args, wantArgs) { |
| 588 | t.Fatalf(testutil.FormatLogLine(2, "got ping args %q, expected %q", args, wantArgs)) |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 592 | // TestAppLifeCycle installs an app, starts it, suspends it, resumes it, and |
| 593 | // then stops it. |
| 594 | func TestAppLifeCycle(t *testing.T) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 595 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 596 | defer shutdown() |
| 597 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 598 | |
| 599 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 600 | defer deferFn() |
| 601 | |
| 602 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 603 | envelope, cleanup := startMockRepos(t, ctx) |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 604 | defer cleanup() |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 605 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 606 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 607 | defer cleanup() |
| 608 | |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 609 | // Create a script wrapping the test target that implements suidhelper. |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 610 | helperPath := generateSuidHelperScript(t, root) |
Robert Kroeger | dd07b36 | 2014-09-18 17:34:42 -0700 | [diff] [blame] | 611 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 612 | // Set up the device manager. Since we won't do device manager updates, |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 613 | // don't worry about its application envelope and current link. |
Ryan Brown | 05de9ec | 2015-02-03 14:27:37 -0800 | [diff] [blame] | 614 | dmh, dms := mgmttest.RunShellCommand(t, sh, nil, deviceManagerCmd, "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 615 | mgmttest.ReadPID(t, dms) |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 616 | claimDevice(t, ctx, "dm", "mydevice", noPairingToken) |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 617 | |
| 618 | // Create the local server that the app uses to let us know it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 619 | pingCh, cleanup := setupPingServer(t, ctx) |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 620 | defer cleanup() |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 621 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 622 | resolve(t, ctx, "pingserver", 1) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 623 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 624 | // Create an envelope for a first version of the app. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 625 | *envelope = envelopeFromShell(sh, []string{testEnvVarName + "=env-val-envelope"}, appCmd, "google naps", fmt.Sprintf("--%s=flag-val-envelope", testFlagName), "appV1") |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 626 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 627 | // Install the app. The config-specified flag value for testFlagName |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 628 | // should override the value specified in the envelope above, and the |
| 629 | // config-specified value for origin should override the value in the |
| 630 | // Install rpc argument. |
| 631 | mtName, ok := sh.GetVar(consts.NamespaceRootPrefix) |
| 632 | if !ok { |
| 633 | t.Fatalf("failed to get namespace root var from shell") |
| 634 | } |
| 635 | // This rooted name should be equivalent to the relative name "ar", but |
| 636 | // we want to test that the config override for origin works. |
| 637 | rootedAppRepoName := naming.Join(mtName, "ar") |
| 638 | appID := installApp(t, ctx, device.Config{testFlagName: "flag-val-install", mgmt.AppOriginConfigKey: rootedAppRepoName}) |
Bogdan Caprita | d8373a1 | 2015-01-28 19:52:37 -0800 | [diff] [blame] | 639 | installationDebug := debug(t, ctx, appID) |
| 640 | // We spot-check a couple pieces of information we expect in the debug |
| 641 | // output. |
| 642 | // TODO(caprita): Is there a way to verify more without adding brittle |
| 643 | // logic that assumes too much about the format? This may be one |
| 644 | // argument in favor of making the output of Debug a struct instead of |
| 645 | // free-form string. |
Bogdan Caprita | 8964d3f | 2015-02-02 13:47:39 -0800 | [diff] [blame] | 646 | if !strings.Contains(installationDebug, fmt.Sprintf("Origin: %v", rootedAppRepoName)) { |
Bogdan Caprita | d8373a1 | 2015-01-28 19:52:37 -0800 | [diff] [blame] | 647 | t.Fatalf("debug response doesn't contain expected info: %v", installationDebug) |
| 648 | } |
| 649 | if !strings.Contains(installationDebug, "Config: map[random_test_flag:flag-val-install]") { |
| 650 | t.Fatalf("debug response doesn't contain expected info: %v", installationDebug) |
| 651 | } |
Bogdan Caprita | 730bde1 | 2014-11-08 15:35:43 -0800 | [diff] [blame] | 652 | |
| 653 | // Start requires the caller to grant a blessing for the app instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 654 | if _, err := startAppImpl(t, ctx, appID, ""); err == nil || !verror.Is(err, impl.ErrInvalidBlessing.ID) { |
Todd Wang | 34ed4c6 | 2014-11-26 15:15:52 -0800 | [diff] [blame] | 655 | t.Fatalf("Start(%v) expected to fail with %v, got %v instead", appID, impl.ErrInvalidBlessing.ID, err) |
Bogdan Caprita | 730bde1 | 2014-11-08 15:35:43 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 658 | // Start an instance of the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 659 | instance1ID := startApp(t, ctx, appID) |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 660 | |
Bogdan Caprita | d8373a1 | 2015-01-28 19:52:37 -0800 | [diff] [blame] | 661 | instanceDebug := debug(t, ctx, appID, instance1ID) |
Bogdan Caprita | fca013d | 2015-01-29 15:32:34 -0800 | [diff] [blame] | 662 | if !strings.Contains(instanceDebug, fmt.Sprintf("Blessing Store: Default blessings: %s/forapp/google naps", testutil.TestBlessing)) { |
Bogdan Caprita | d8373a1 | 2015-01-28 19:52:37 -0800 | [diff] [blame] | 663 | t.Fatalf("debug response doesn't contain expected info: %v", instanceDebug) |
| 664 | } |
| 665 | |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 666 | // Wait until the app pings us that it's ready. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 667 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") |
Robert Kroeger | 124a04f | 2015-02-03 17:32:32 -0800 | [diff] [blame] | 668 | verifyAppState(t, root, appID, instance1ID, "started") |
Robert Kroeger | 627a115 | 2014-10-01 14:57:55 -0700 | [diff] [blame] | 669 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 670 | v1EP1 := resolve(t, ctx, "appV1", 1)[0] |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 671 | |
| 672 | // Suspend the app instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 673 | suspendApp(t, ctx, appID, instance1ID) |
| 674 | resolveExpectNotFound(t, ctx, "appV1") |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 675 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 676 | resumeApp(t, ctx, appID, instance1ID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 677 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") // Wait until the app pings us that it's ready. |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 678 | oldV1EP1 := v1EP1 |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 679 | if v1EP1 = resolve(t, ctx, "appV1", 1)[0]; v1EP1 == oldV1EP1 { |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 680 | t.Fatalf("Expected a new endpoint for the app after suspend/resume") |
| 681 | } |
| 682 | |
| 683 | // Start a second instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 684 | instance2ID := startApp(t, ctx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 685 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") // Wait until the app pings us that it's ready. |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 686 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 687 | // There should be two endpoints mounted as "appV1", one for each |
| 688 | // instance of the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 689 | endpoints := resolve(t, ctx, "appV1", 2) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 690 | v1EP2 := endpoints[0] |
| 691 | if endpoints[0] == v1EP1 { |
| 692 | v1EP2 = endpoints[1] |
| 693 | if v1EP2 == v1EP1 { |
| 694 | t.Fatalf("Both endpoints are the same") |
| 695 | } |
| 696 | } else if endpoints[1] != v1EP1 { |
| 697 | t.Fatalf("Second endpoint should have been v1EP1: %v, %v", endpoints, v1EP1) |
| 698 | } |
Bogdan Caprita | 268b419 | 2014-08-28 10:04:44 -0700 | [diff] [blame] | 699 | |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 700 | // TODO(caprita): test Suspend and Resume, and verify various |
| 701 | // non-standard combinations (suspend when stopped; resume while still |
| 702 | // running; stop while suspended). |
| 703 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 704 | // Suspend the first instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 705 | suspendApp(t, ctx, appID, instance1ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 706 | // Only the second instance should still be running and mounted. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 707 | if want, got := v1EP2, resolve(t, ctx, "appV1", 1)[0]; want != got { |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 708 | t.Fatalf("Resolve(%v): want: %v, got %v", "appV1", want, got) |
| 709 | } |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 710 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 711 | // Updating the installation to itself is a no-op. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 712 | updateAppExpectError(t, ctx, appID, impl.ErrUpdateNoOp.ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 713 | |
| 714 | // Updating the installation should not work with a mismatched title. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 715 | *envelope = envelopeFromShell(sh, nil, appCmd, "bogus") |
| 716 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 717 | updateAppExpectError(t, ctx, appID, impl.ErrAppTitleMismatch.ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 718 | |
| 719 | // Create a second version of the app and update the app to it. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 720 | *envelope = envelopeFromShell(sh, []string{testEnvVarName + "=env-val-envelope"}, appCmd, "google naps", "appV2") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 721 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 722 | updateApp(t, ctx, appID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 723 | |
| 724 | // Second instance should still be running. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 725 | if want, got := v1EP2, resolve(t, ctx, "appV1", 1)[0]; want != got { |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 726 | t.Fatalf("Resolve(%v): want: %v, got %v", "appV1", want, got) |
| 727 | } |
| 728 | |
| 729 | // Resume first instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 730 | resumeApp(t, ctx, appID, instance1ID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 731 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") // Wait until the app pings us that it's ready. |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 732 | // Both instances should still be running the first version of the app. |
| 733 | // Check that the mounttable contains two endpoints, one of which is |
| 734 | // v1EP2. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 735 | endpoints = resolve(t, ctx, "appV1", 2) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 736 | if endpoints[0] == v1EP2 { |
| 737 | if endpoints[1] == v1EP2 { |
| 738 | t.Fatalf("Both endpoints are the same") |
| 739 | } |
| 740 | } else if endpoints[1] != v1EP2 { |
| 741 | t.Fatalf("Second endpoint should have been v1EP2: %v, %v", endpoints, v1EP2) |
| 742 | } |
| 743 | |
| 744 | // Stop first instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 745 | stopApp(t, ctx, appID, instance1ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 746 | verifyAppWorkspace(t, root, appID, instance1ID) |
| 747 | |
| 748 | // Only second instance is still running. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 749 | if want, got := v1EP2, resolve(t, ctx, "appV1", 1)[0]; want != got { |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 750 | t.Fatalf("Resolve(%v): want: %v, got %v", "appV1", want, got) |
| 751 | } |
| 752 | |
| 753 | // Start a third instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 754 | instance3ID := startApp(t, ctx, appID) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 755 | // Wait until the app pings us that it's ready. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 756 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 757 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 758 | resolve(t, ctx, "appV2", 1) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 759 | |
| 760 | // Stop second instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 761 | stopApp(t, ctx, appID, instance2ID) |
| 762 | resolveExpectNotFound(t, ctx, "appV1") |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 763 | |
| 764 | // Stop third instance. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 765 | stopApp(t, ctx, appID, instance3ID) |
| 766 | resolveExpectNotFound(t, ctx, "appV2") |
Bogdan Caprita | 9a59b8c | 2014-08-22 14:21:10 -0700 | [diff] [blame] | 767 | |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 768 | // Revert the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 769 | revertApp(t, ctx, appID) |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 770 | |
| 771 | // Start a fourth instance. It should be started from version 1. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 772 | instance4ID := startApp(t, ctx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 773 | verifyPingArgs(t, pingCh, userName(t), "flag-val-install", "env-val-envelope") // Wait until the app pings us that it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 774 | resolve(t, ctx, "appV1", 1) |
| 775 | stopApp(t, ctx, appID, instance4ID) |
| 776 | resolveExpectNotFound(t, ctx, "appV1") |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 777 | |
| 778 | // We are already on the first version, no further revert possible. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 779 | revertAppExpectError(t, ctx, appID, impl.ErrUpdateNoOp.ID) |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 780 | |
Bogdan Caprita | 8c776b2 | 2014-08-28 17:29:07 -0700 | [diff] [blame] | 781 | // Uninstall the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 782 | uninstallApp(t, ctx, appID) |
Bogdan Caprita | 8c776b2 | 2014-08-28 17:29:07 -0700 | [diff] [blame] | 783 | |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 784 | // Updating the installation should no longer be allowed. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 785 | updateAppExpectError(t, ctx, appID, impl.ErrInvalidOperation.ID) |
Bogdan Caprita | bce0a63 | 2014-09-03 16:15:26 -0700 | [diff] [blame] | 786 | |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 787 | // Reverting the installation should no longer be allowed. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 788 | revertAppExpectError(t, ctx, appID, impl.ErrInvalidOperation.ID) |
Bogdan Caprita | 53b7b7e | 2014-09-03 20:51:16 -0700 | [diff] [blame] | 789 | |
Bogdan Caprita | 8c776b2 | 2014-08-28 17:29:07 -0700 | [diff] [blame] | 790 | // Starting new instances should no longer be allowed. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 791 | startAppExpectError(t, ctx, appID, impl.ErrInvalidOperation.ID) |
Bogdan Caprita | 8c776b2 | 2014-08-28 17:29:07 -0700 | [diff] [blame] | 792 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 793 | // Cleanly shut down the device manager. |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 794 | syscall.Kill(dmh.Pid(), syscall.SIGINT) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 795 | dms.Expect("dm terminated") |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 796 | dms.ExpectEOF() |
Bogdan Caprita | 1e37913 | 2014-08-03 23:02:31 -0700 | [diff] [blame] | 797 | } |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 798 | |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 799 | func startRealBinaryRepository(t *testing.T, ctx *context.T, von string) func() { |
Jiri Simsa | 432cc2e | 2014-12-08 15:53:38 -0800 | [diff] [blame] | 800 | rootDir, err := binaryimpl.SetupRootDir("") |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 801 | if err != nil { |
Jiri Simsa | 432cc2e | 2014-12-08 15:53:38 -0800 | [diff] [blame] | 802 | t.Fatalf("binaryimpl.SetupRootDir failed: %v", err) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 803 | } |
Jiri Simsa | 432cc2e | 2014-12-08 15:53:38 -0800 | [diff] [blame] | 804 | state, err := binaryimpl.NewState(rootDir, "", 3) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 805 | if err != nil { |
| 806 | t.Fatalf("binaryimpl.NewState failed: %v", err) |
| 807 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 808 | server, _ := mgmttest.NewServer(ctx) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 809 | d, err := binaryimpl.NewDispatcher(veyron2.GetPrincipal(ctx), state) |
Robert Kroeger | 8d7a0ef | 2015-01-14 17:38:40 -0800 | [diff] [blame] | 810 | if err != nil { |
| 811 | t.Fatalf("server.NewDispatcher failed: %v", err) |
| 812 | } |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 813 | if err := server.ServeDispatcher(von, d); err != nil { |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 814 | t.Fatalf("server.ServeDispatcher failed: %v", err) |
| 815 | } |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 816 | return func() { |
| 817 | if err := server.Stop(); err != nil { |
| 818 | t.Fatalf("server.Stop failed: %v", err) |
| 819 | } |
Jiri Simsa | 432cc2e | 2014-12-08 15:53:38 -0800 | [diff] [blame] | 820 | if err := os.RemoveAll(rootDir); err != nil { |
| 821 | t.Fatalf("os.RemoveAll(%q) failed: %v", rootDir, err) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 826 | // TestDeviceManagerClaim claims a devicemanager and tests ACL permissions on |
| 827 | // its methods. |
| 828 | func TestDeviceManagerClaim(t *testing.T) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 829 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 830 | defer shutdown() |
| 831 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 832 | |
| 833 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 834 | defer deferFn() |
| 835 | |
| 836 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 837 | envelope, cleanup := startMockRepos(t, ctx) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 838 | defer cleanup() |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 839 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 840 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 841 | defer cleanup() |
| 842 | |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 843 | // Create a script wrapping the test target that implements suidhelper. |
| 844 | helperPath := generateSuidHelperScript(t, root) |
| 845 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 846 | // Set up the device manager. Since we won't do device manager updates, |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 847 | // don't worry about its application envelope and current link. |
gauthamt | 27f38ba | 2015-02-13 13:45:01 -0800 | [diff] [blame] | 848 | pairingToken := "abcxyz" |
| 849 | _, dms := mgmttest.RunShellCommand(t, sh, nil, deviceManagerCmd, "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link", pairingToken) |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 850 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 851 | defer syscall.Kill(pid, syscall.SIGINT) |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 852 | |
Bogdan Caprita | 2692910 | 2014-11-07 11:56:56 -0800 | [diff] [blame] | 853 | *envelope = envelopeFromShell(sh, nil, appCmd, "google naps", "trapp") |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 854 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 855 | claimantCtx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("claimant")) |
| 856 | if err != nil { |
| 857 | t.Fatalf("Could not create claimant principal: %v", err) |
| 858 | } |
| 859 | octx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("other")) |
| 860 | if err != nil { |
| 861 | t.Fatalf("Could not create other principal: %v", err) |
| 862 | } |
Matt Rosencrantz | 5180d16 | 2014-12-03 13:48:40 -0800 | [diff] [blame] | 863 | |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 864 | // Unclaimed devices cannot do anything but be claimed. |
| 865 | installAppExpectError(t, octx, impl.ErrUnclaimedDevice.ID) |
| 866 | // Claim the device with an incorrect pairing token should fail. |
| 867 | claimDeviceExpectError(t, claimantCtx, "dm", "mydevice", "badtoken", impl.ErrInvalidPairingToken.ID) |
| 868 | // But succeed with a valid pairing token |
| 869 | claimDevice(t, claimantCtx, "dm", "mydevice", pairingToken) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 870 | |
Robin Thellend | 888f8cf | 2014-12-15 16:19:10 -0800 | [diff] [blame] | 871 | // Installation should succeed since claimantRT is now the "owner" of |
| 872 | // the devicemanager. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 873 | appID := installApp(t, claimantCtx) |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 874 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 875 | // octx should be unable to install though, since the ACLs have |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 876 | // changed now. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 877 | installAppExpectError(t, octx, verror.NoAccess.ID) |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 878 | |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 879 | // Create the local server that the app uses to let us know it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 880 | pingCh, cleanup := setupPingServer(t, ctx) |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 881 | defer cleanup() |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 882 | |
| 883 | // Start an instance of the app. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 884 | instanceID := startApp(t, claimantCtx, appID) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 885 | |
| 886 | // Wait until the app pings us that it's ready. |
| 887 | select { |
| 888 | case <-pingCh: |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 889 | case <-time.After(pingTimeout): |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 890 | t.Fatalf("failed to get ping") |
| 891 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 892 | resolve(t, ctx, "trapp", 1) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 893 | suspendApp(t, claimantCtx, appID, instanceID) |
Bogdan Caprita | b9501d1 | 2014-10-10 15:02:03 -0700 | [diff] [blame] | 894 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 895 | // TODO(gauthamt): Test that ACLs persist across devicemanager restarts |
Gautham | 82bb995 | 2014-08-28 14:11:51 -0700 | [diff] [blame] | 896 | } |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 897 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 898 | func TestDeviceManagerUpdateACL(t *testing.T) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 899 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 900 | defer shutdown() |
| 901 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 902 | |
| 903 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 904 | defer deferFn() |
| 905 | |
| 906 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 907 | envelope, cleanup := startMockRepos(t, ctx) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 908 | defer cleanup() |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 909 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 910 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 911 | defer cleanup() |
| 912 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 913 | // The two "processes"/runtimes which will act as IPC clients to |
| 914 | // the devicemanager process. |
| 915 | selfCtx := ctx |
| 916 | octx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal()) |
| 917 | if err != nil { |
| 918 | t.Fatalf("Could not create other principal: %v", err) |
| 919 | } |
| 920 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 921 | // By default, selfCtx and octx will have blessings generated based on |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 922 | // the username/machine name running this process. Since these blessings |
| 923 | // will appear in ACLs, give them recognizable names. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 924 | idp := tsecurity.NewIDProvider("root") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 925 | if err := idp.Bless(veyron2.GetPrincipal(selfCtx), "self"); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 926 | t.Fatal(err) |
| 927 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 928 | if err := idp.Bless(veyron2.GetPrincipal(octx), "other"); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 929 | t.Fatal(err) |
| 930 | } |
| 931 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 932 | // Set up the device manager. Since we won't do device manager updates, |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 933 | // don't worry about its application envelope and current link. |
Ryan Brown | 05de9ec | 2015-02-03 14:27:37 -0800 | [diff] [blame] | 934 | _, dms := mgmttest.RunShellCommand(t, sh, nil, deviceManagerCmd, "dm", root, "unused_helper", "unused_app_repo_name", "unused_curr_link") |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 935 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 936 | defer syscall.Kill(pid, syscall.SIGINT) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 937 | |
| 938 | // Create an envelope for an app. |
Bogdan Caprita | 2692910 | 2014-11-07 11:56:56 -0800 | [diff] [blame] | 939 | *envelope = envelopeFromShell(sh, nil, appCmd, "google naps") |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 940 | |
Robert Kroeger | e95ed6d | 2015-01-14 17:41:04 -0800 | [diff] [blame] | 941 | // On an unclaimed device manager, there will be no ACLs. |
| 942 | deviceStub := device.DeviceClient("dm/device") |
| 943 | if _, _, err := deviceStub.GetACL(selfCtx); err == nil { |
| 944 | t.Fatalf("GetACL should have failed but didn't.") |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 947 | // Claim the devicemanager as "root/self/mydevice" |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 948 | claimDevice(t, selfCtx, "dm", "mydevice", noPairingToken) |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 949 | expectedACL := make(access.TaggedACLMap) |
| 950 | for _, tag := range access.AllTypicalTags() { |
Ankur | d6a793b | 2015-02-12 11:52:11 -0800 | [diff] [blame] | 951 | expectedACL[string(tag)] = access.ACL{In: []security.BlessingPattern{"root/$", "root/self/$", "root/self/mydevice/$"}} |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 952 | } |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 953 | var b bytes.Buffer |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 954 | if err := expectedACL.WriteTo(&b); err != nil { |
| 955 | t.Fatalf("Failed to save ACL:%v", err) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 956 | } |
| 957 | md5hash := md5.Sum(b.Bytes()) |
| 958 | expectedETAG := hex.EncodeToString(md5hash[:]) |
Robert Kroeger | e95ed6d | 2015-01-14 17:41:04 -0800 | [diff] [blame] | 959 | acl, etag, err := deviceStub.GetACL(selfCtx) |
| 960 | if err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 961 | t.Fatal(err) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 962 | } |
| 963 | if etag != expectedETAG { |
| 964 | t.Fatalf("getACL expected:%v(%v), got:%v(%v)", expectedACL, expectedETAG, acl, etag) |
| 965 | } |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 966 | // Install from octx should fail, since it does not match the ACL. |
| 967 | installAppExpectError(t, octx, verror.NoAccess.ID) |
| 968 | |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 969 | newACL := make(access.TaggedACLMap) |
| 970 | for _, tag := range access.AllTypicalTags() { |
| 971 | newACL.Add("root/other", string(tag)) |
| 972 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 973 | if err := deviceStub.SetACL(selfCtx, newACL, "invalid"); err == nil { |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 974 | t.Fatalf("SetACL should have failed with invalid etag") |
| 975 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 976 | if err := deviceStub.SetACL(selfCtx, newACL, etag); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 977 | t.Fatal(err) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 978 | } |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 979 | // Install should now fail with selfCtx, which no longer matches the |
| 980 | // ACLs but succeed with octx, which does. |
| 981 | installAppExpectError(t, selfCtx, verror.NoAccess.ID) |
| 982 | installApp(t, octx) |
Gautham | 6fe61e5 | 2014-09-16 13:58:17 -0700 | [diff] [blame] | 983 | } |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 984 | |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 985 | type simpleRW chan []byte |
| 986 | |
| 987 | func (s simpleRW) Write(p []byte) (n int, err error) { |
| 988 | s <- p |
| 989 | return len(p), nil |
| 990 | } |
| 991 | func (s simpleRW) Read(p []byte) (n int, err error) { |
| 992 | return copy(p, <-s), nil |
| 993 | } |
| 994 | |
| 995 | // TestDeviceManagerInstallation verifies the 'self install' and 'uninstall' |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 996 | // functionality of the device manager: it runs SelfInstall in a child process, |
| 997 | // then runs the executable from the soft link that the installation created. |
| 998 | // This should bring up a functioning device manager. In the end it runs |
| 999 | // Uninstall and verifies that the installation is gone. |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1000 | func TestDeviceManagerInstallation(t *testing.T) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 1001 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1002 | defer shutdown() |
| 1003 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1004 | |
| 1005 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1006 | defer deferFn() |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 1007 | testDir, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 1008 | defer cleanup() |
| 1009 | |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1010 | // Create a script wrapping the test target that implements suidhelper. |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 1011 | suidHelperPath := generateSuidHelperScript(t, testDir) |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1012 | // Create a dummy script mascarading as the security agent. |
| 1013 | agentPath := generateAgentScript(t, testDir) |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 1014 | initHelperPath := "" |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 1015 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1016 | // Create an 'envelope' for the device manager that we can pass to the |
| 1017 | // installer, to ensure that the device manager that the installer |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1018 | // configures can run. |
| 1019 | dmargs, dmenv := sh.CommandEnvelope(deviceManagerCmd, nil, "dm") |
| 1020 | dmDir := filepath.Join(testDir, "dm") |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 1021 | // TODO(caprita): Add test logic when initMode = true. |
| 1022 | singleUser, sessionMode, initMode := true, true, false |
Robin Thellend | d9ffea9 | 2015-01-29 09:47:37 -0800 | [diff] [blame] | 1023 | if err := impl.SelfInstall(dmDir, suidHelperPath, agentPath, initHelperPath, "", singleUser, sessionMode, initMode, dmargs[1:], dmenv, os.Stderr, os.Stdout); err != nil { |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1024 | t.Fatalf("SelfInstall failed: %v", err) |
| 1025 | } |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 1026 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1027 | resolveExpectNotFound(t, ctx, "dm") |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1028 | // Start the device manager. |
| 1029 | stdout := make(simpleRW, 100) |
Robin Thellend | f598628 | 2015-02-13 11:01:03 -0800 | [diff] [blame] | 1030 | defer os.Setenv(redirectEnv, os.Getenv(redirectEnv)) |
| 1031 | os.Setenv(redirectEnv, "1") |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1032 | if err := impl.Start(dmDir, os.Stderr, stdout); err != nil { |
| 1033 | t.Fatalf("Start failed: %v", err) |
| 1034 | } |
| 1035 | dms := expect.NewSession(t, stdout, mgmttest.ExpectTimeout) |
| 1036 | mgmttest.ReadPID(t, dms) |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 1037 | claimDevice(t, ctx, "dm", "mydevice", noPairingToken) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1038 | revertDeviceExpectError(t, ctx, "dm", impl.ErrUpdateNoOp.ID) // No previous version available. |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1039 | |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1040 | // Stop the device manager. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1041 | if err := impl.Stop(ctx, dmDir, os.Stderr, os.Stdout); err != nil { |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1042 | t.Fatalf("Stop failed: %v", err) |
| 1043 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1044 | dms.Expect("dm terminated") |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 1045 | |
| 1046 | // Uninstall. |
Bogdan Caprita | 29a3b35 | 2015-01-16 16:28:49 -0800 | [diff] [blame] | 1047 | if err := impl.Uninstall(dmDir, os.Stderr, os.Stdout); err != nil { |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1048 | t.Fatalf("Uninstall failed: %v", err) |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 1049 | } |
Bogdan Caprita | c7e72b6 | 2015-01-07 19:22:23 -0800 | [diff] [blame] | 1050 | // Ensure that the installation is gone. |
| 1051 | if files, err := ioutil.ReadDir(dmDir); err != nil || len(files) > 0 { |
| 1052 | var finfo []string |
| 1053 | for _, f := range files { |
| 1054 | finfo = append(finfo, f.Name()) |
| 1055 | } |
| 1056 | t.Fatalf("ReadDir returned (%v, %v)", err, finfo) |
Bogdan Caprita | a40d338 | 2014-12-19 16:30:26 -0800 | [diff] [blame] | 1057 | } |
Bogdan Caprita | 5420f17 | 2014-10-10 15:58:14 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1060 | func TestDeviceManagerGlobAndDebug(t *testing.T) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 1061 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1062 | defer shutdown() |
| 1063 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1064 | |
| 1065 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1066 | defer deferFn() |
| 1067 | |
| 1068 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1069 | envelope, cleanup := startMockRepos(t, ctx) |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1070 | defer cleanup() |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1071 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 1072 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1073 | defer cleanup() |
| 1074 | |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 1075 | // Create a script wrapping the test target that implements suidhelper. |
| 1076 | helperPath := generateSuidHelperScript(t, root) |
| 1077 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1078 | // Set up the device manager. Since we won't do device manager updates, |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1079 | // don't worry about its application envelope and current link. |
Ryan Brown | 05de9ec | 2015-02-03 14:27:37 -0800 | [diff] [blame] | 1080 | _, dms := mgmttest.RunShellCommand(t, sh, nil, deviceManagerCmd, "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 1081 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1082 | defer syscall.Kill(pid, syscall.SIGINT) |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1083 | |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1084 | // Create the local server that the app uses to let us know it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1085 | pingCh, cleanup := setupPingServer(t, ctx) |
Bogdan Caprita | d2b9f03 | 2014-10-10 17:43:29 -0700 | [diff] [blame] | 1086 | defer cleanup() |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1087 | |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1088 | // Create the envelope for the first version of the app. |
Bogdan Caprita | 2692910 | 2014-11-07 11:56:56 -0800 | [diff] [blame] | 1089 | *envelope = envelopeFromShell(sh, nil, appCmd, "google naps", "appV1") |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1090 | |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 1091 | // Device must be claimed before applications can be installed. |
| 1092 | claimDevice(t, ctx, "dm", "mydevice", noPairingToken) |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1093 | // Install the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1094 | appID := installApp(t, ctx) |
Bogdan Caprita | 43bc737 | 2014-12-03 21:51:12 -0800 | [diff] [blame] | 1095 | install1ID := path.Base(appID) |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1096 | |
| 1097 | // Start an instance of the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1098 | instance1ID := startApp(t, ctx, appID) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1099 | |
| 1100 | // Wait until the app pings us that it's ready. |
| 1101 | select { |
| 1102 | case <-pingCh: |
Bogdan Caprita | 916e99f | 2014-11-24 15:47:19 -0800 | [diff] [blame] | 1103 | case <-time.After(pingTimeout): |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1104 | t.Fatalf("failed to get ping") |
| 1105 | } |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1106 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1107 | app2ID := installApp(t, ctx) |
Bogdan Caprita | 43bc737 | 2014-12-03 21:51:12 -0800 | [diff] [blame] | 1108 | install2ID := path.Base(app2ID) |
| 1109 | |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1110 | testcases := []struct { |
| 1111 | name, pattern string |
| 1112 | expected []string |
| 1113 | }{ |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1114 | {"dm", "...", []string{ |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1115 | "", |
| 1116 | "apps", |
| 1117 | "apps/google naps", |
Bogdan Caprita | 43bc737 | 2014-12-03 21:51:12 -0800 | [diff] [blame] | 1118 | "apps/google naps/" + install1ID, |
| 1119 | "apps/google naps/" + install1ID + "/" + instance1ID, |
| 1120 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs", |
| 1121 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs/STDERR-<timestamp>", |
| 1122 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs/STDOUT-<timestamp>", |
| 1123 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs/bin.INFO", |
| 1124 | "apps/google naps/" + install1ID + "/" + instance1ID + "/logs/bin.<*>.INFO.<timestamp>", |
| 1125 | "apps/google naps/" + install1ID + "/" + instance1ID + "/pprof", |
| 1126 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats", |
| 1127 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats/ipc", |
| 1128 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats/system", |
| 1129 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats/system/start-time-rfc1123", |
| 1130 | "apps/google naps/" + install1ID + "/" + instance1ID + "/stats/system/start-time-unix", |
| 1131 | "apps/google naps/" + install2ID, |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1132 | "device", |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1133 | }}, |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1134 | {"dm/apps", "*", []string{"google naps"}}, |
| 1135 | {"dm/apps/google naps", "*", []string{install1ID, install2ID}}, |
| 1136 | {"dm/apps/google naps/" + install1ID, "*", []string{instance1ID}}, |
| 1137 | {"dm/apps/google naps/" + install1ID + "/" + instance1ID, "*", []string{"logs", "pprof", "stats"}}, |
| 1138 | {"dm/apps/google naps/" + install1ID + "/" + instance1ID + "/logs", "*", []string{ |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1139 | "STDERR-<timestamp>", |
| 1140 | "STDOUT-<timestamp>", |
| 1141 | "bin.INFO", |
| 1142 | "bin.<*>.INFO.<timestamp>", |
| 1143 | }}, |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1144 | {"dm/apps/google naps/" + install1ID + "/" + instance1ID + "/stats/system", "start-time*", []string{"start-time-rfc1123", "start-time-unix"}}, |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1145 | } |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1146 | logFileTimeStampRE := regexp.MustCompile("(STDOUT|STDERR)-[0-9]+$") |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1147 | logFileTrimInfoRE := regexp.MustCompile(`bin\..*\.INFO\.[0-9.-]+$`) |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1148 | logFileRemoveErrorFatalWarningRE := regexp.MustCompile("(ERROR|FATAL|WARNING)") |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1149 | statsTrimRE := regexp.MustCompile("/stats/(ipc|system(/start-time.*)?)$") |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1150 | for _, tc := range testcases { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1151 | results, err := testutil.GlobName(ctx, tc.name, tc.pattern) |
Robin Thellend | 5c95a6c | 2014-11-10 13:06:56 -0800 | [diff] [blame] | 1152 | if err != nil { |
| 1153 | t.Errorf("unexpected glob error for (%q, %q): %v", tc.name, tc.pattern, err) |
| 1154 | continue |
| 1155 | } |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1156 | filteredResults := []string{} |
| 1157 | for _, name := range results { |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1158 | // Keep only the stats object names that match this RE. |
| 1159 | if strings.Contains(name, "/stats/") && !statsTrimRE.MatchString(name) { |
| 1160 | continue |
| 1161 | } |
| 1162 | // Remove ERROR, WARNING, FATAL log files because |
| 1163 | // they're not consistently there. |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1164 | if logFileRemoveErrorFatalWarningRE.MatchString(name) { |
| 1165 | continue |
| 1166 | } |
| 1167 | name = logFileTimeStampRE.ReplaceAllString(name, "$1-<timestamp>") |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1168 | name = logFileTrimInfoRE.ReplaceAllString(name, "bin.<*>.INFO.<timestamp>") |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1169 | filteredResults = append(filteredResults, name) |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1170 | } |
Robin Thellend | 12937a2 | 2014-10-29 17:53:23 -0700 | [diff] [blame] | 1171 | sort.Strings(filteredResults) |
| 1172 | sort.Strings(tc.expected) |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1173 | if !reflect.DeepEqual(filteredResults, tc.expected) { |
| 1174 | t.Errorf("unexpected result for (%q, %q). Got %q, want %q", tc.name, tc.pattern, filteredResults, tc.expected) |
Robin Thellend | 9e523a6 | 2014-10-07 16:19:53 -0700 | [diff] [blame] | 1175 | } |
Robin Thellend | 09929f4 | 2014-10-01 10:18:13 -0700 | [diff] [blame] | 1176 | } |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 1177 | |
Robin Thellend | 5864732 | 2014-10-28 12:07:47 -0700 | [diff] [blame] | 1178 | // Call Size() on the log file objects. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1179 | files, err := testutil.GlobName(ctx, "dm", "apps/google naps/"+install1ID+"/"+instance1ID+"/logs/*") |
Robin Thellend | 5c95a6c | 2014-11-10 13:06:56 -0800 | [diff] [blame] | 1180 | if err != nil { |
| 1181 | t.Errorf("unexpected glob error: %v", err) |
| 1182 | } |
Robin Thellend | 12937a2 | 2014-10-29 17:53:23 -0700 | [diff] [blame] | 1183 | if want, got := 4, len(files); got < want { |
| 1184 | t.Errorf("Unexpected number of matches. Got %d, want at least %d", got, want) |
| 1185 | } |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 1186 | for _, file := range files { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1187 | name := naming.Join("dm", file) |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 1188 | c := logreader.LogFileClient(name) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1189 | if _, err := c.Size(ctx); err != nil { |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 1190 | t.Errorf("Size(%q) failed: %v", name, err) |
| 1191 | } |
| 1192 | } |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1193 | |
| 1194 | // Call Value() on some of the stats objects. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1195 | objects, err := testutil.GlobName(ctx, "dm", "apps/google naps/"+install1ID+"/"+instance1ID+"/stats/system/start-time*") |
Robin Thellend | 5c95a6c | 2014-11-10 13:06:56 -0800 | [diff] [blame] | 1196 | if err != nil { |
| 1197 | t.Errorf("unexpected glob error: %v", err) |
| 1198 | } |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1199 | if want, got := 2, len(objects); got != want { |
| 1200 | t.Errorf("Unexpected number of matches. Got %d, want %d", got, want) |
| 1201 | } |
| 1202 | for _, obj := range objects { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1203 | name := naming.Join("dm", obj) |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 1204 | c := stats.StatsClient(name) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1205 | if _, err := c.Value(ctx); err != nil { |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1206 | t.Errorf("Value(%q) failed: %v", name, err) |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | // Call CmdLine() on the pprof object. |
| 1211 | { |
Bogdan Caprita | 9c4aa22 | 2014-12-10 14:46:30 -0800 | [diff] [blame] | 1212 | name := "dm/apps/google naps/" + install1ID + "/" + instance1ID + "/pprof" |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 1213 | c := pprof.PProfClient(name) |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1214 | v, err := c.CmdLine(ctx) |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1215 | if err != nil { |
| 1216 | t.Errorf("CmdLine(%q) failed: %v", name, err) |
| 1217 | } |
| 1218 | if len(v) == 0 { |
| 1219 | t.Fatalf("Unexpected empty cmdline: %v", v) |
| 1220 | } |
| 1221 | if got, want := filepath.Base(v[0]), "bin"; got != want { |
| 1222 | t.Errorf("Unexpected value for argv[0]. Got %v, want %v", got, want) |
| 1223 | } |
| 1224 | } |
Robin Thellend | 4c5266e | 2014-10-27 13:19:29 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1227 | func TestDeviceManagerPackages(t *testing.T) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 1228 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1229 | defer shutdown() |
| 1230 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1231 | |
| 1232 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1233 | defer deferFn() |
| 1234 | |
| 1235 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1236 | envelope, cleanup := startMockRepos(t, ctx) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1237 | defer cleanup() |
| 1238 | |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1239 | binaryVON := "realbin" |
| 1240 | defer startRealBinaryRepository(t, ctx, binaryVON)() |
| 1241 | |
| 1242 | // upload package to binary repository |
| 1243 | tmpdir, err := ioutil.TempDir("", "test-package-") |
| 1244 | if err != nil { |
| 1245 | t.Fatalf("ioutil.TempDir failed: %v", err) |
| 1246 | } |
| 1247 | defer os.RemoveAll(tmpdir) |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1248 | createFile := func(name, contents string) { |
| 1249 | if err := ioutil.WriteFile(filepath.Join(tmpdir, name), []byte(contents), 0600); err != nil { |
| 1250 | t.Fatalf("ioutil.WriteFile failed: %v", err) |
| 1251 | } |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1252 | } |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1253 | createFile("hello.txt", "Hello World!") |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1254 | if _, err := libbinary.UploadFromDir(ctx, naming.Join(binaryVON, "testpkg"), tmpdir); err != nil { |
| 1255 | t.Fatalf("libbinary.UploadFromDir failed: %v", err) |
| 1256 | } |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1257 | createAndUpload := func(von, contents string) { |
| 1258 | createFile("tempfile", contents) |
| 1259 | if _, err := libbinary.UploadFromFile(ctx, naming.Join(binaryVON, von), filepath.Join(tmpdir, "tempfile")); err != nil { |
| 1260 | t.Fatalf("libbinary.UploadFromFile failed: %v", err) |
| 1261 | } |
Bogdan Caprita | 2751d67 | 2015-02-06 13:43:47 -0800 | [diff] [blame] | 1262 | } |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1263 | createAndUpload("testfile", "Goodbye World!") |
| 1264 | createAndUpload("leftshark", "Left shark") |
| 1265 | createAndUpload("rightshark", "Right shark") |
| 1266 | createAndUpload("beachball", "Beach ball") |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1267 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 1268 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1269 | defer cleanup() |
| 1270 | |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1271 | // Create a script wrapping the test target that implements suidhelper. |
| 1272 | helperPath := generateSuidHelperScript(t, root) |
| 1273 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1274 | // Set up the device manager. Since we won't do device manager updates, |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1275 | // don't worry about its application envelope and current link. |
Ryan Brown | 05de9ec | 2015-02-03 14:27:37 -0800 | [diff] [blame] | 1276 | _, dms := mgmttest.RunShellCommand(t, sh, nil, deviceManagerCmd, "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 1277 | pid := mgmttest.ReadPID(t, dms) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1278 | defer syscall.Kill(pid, syscall.SIGINT) |
| 1279 | |
| 1280 | // Create the local server that the app uses to let us know it's ready. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1281 | pingCh, cleanup := setupPingServer(t, ctx) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1282 | defer cleanup() |
| 1283 | |
| 1284 | // Create the envelope for the first version of the app. |
| 1285 | *envelope = envelopeFromShell(sh, nil, appCmd, "google naps", "appV1") |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1286 | envelope.Packages = map[string]application.SignedFile{ |
| 1287 | "test": application.SignedFile{ |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1288 | File: "realbin/testpkg", |
| 1289 | }, |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1290 | "test2": application.SignedFile{ |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1291 | File: "realbin/testfile", |
| 1292 | }, |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1293 | "shark": application.SignedFile{ |
| 1294 | File: "realbin/leftshark", |
| 1295 | }, |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1298 | // These are install-time overrides for packages. |
| 1299 | // Specifically, we override the 'shark' package and add a new |
| 1300 | // 'ball' package on top of what's specified in the envelope. |
| 1301 | packages := application.Packages{ |
| 1302 | "shark": application.SignedFile{ |
| 1303 | File: "realbin/rightshark", |
| 1304 | }, |
| 1305 | "ball": application.SignedFile{ |
| 1306 | File: "realbin/beachball", |
| 1307 | }, |
| 1308 | } |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 1309 | // Device must be claimed before apps can be installed. |
| 1310 | claimDevice(t, ctx, "dm", "mydevice", noPairingToken) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1311 | // Install the app. |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1312 | appID := installApp(t, ctx, packages) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1313 | |
| 1314 | // Start an instance of the app. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1315 | startApp(t, ctx, appID) |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1316 | |
| 1317 | // Wait until the app pings us that it's ready. |
| 1318 | select { |
| 1319 | case <-pingCh: |
| 1320 | case <-time.After(pingTimeout): |
| 1321 | t.Fatalf("failed to get ping") |
| 1322 | } |
| 1323 | |
Bogdan Caprita | 2751d67 | 2015-02-06 13:43:47 -0800 | [diff] [blame] | 1324 | for _, c := range []struct { |
| 1325 | path, content string |
| 1326 | }{ |
| 1327 | { |
| 1328 | filepath.Join("test", "hello.txt"), |
| 1329 | "Hello World!", |
| 1330 | }, |
| 1331 | { |
| 1332 | "test2", |
| 1333 | "Goodbye World!", |
| 1334 | }, |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1335 | { |
| 1336 | "shark", |
| 1337 | "Right shark", |
| 1338 | }, |
| 1339 | { |
| 1340 | "ball", |
| 1341 | "Beach ball", |
| 1342 | }, |
Bogdan Caprita | 2751d67 | 2015-02-06 13:43:47 -0800 | [diff] [blame] | 1343 | } { |
| 1344 | // Ask the app to cat the file. |
| 1345 | file := filepath.Join("packages", c.path) |
| 1346 | name := "appV1" |
| 1347 | content, err := cat(ctx, name, file) |
| 1348 | if err != nil { |
| 1349 | t.Errorf("cat(%q, %q) failed: %v", name, file, err) |
| 1350 | } |
| 1351 | if expected := c.content; content != expected { |
| 1352 | t.Errorf("unexpected content: expected %q, got %q", expected, content) |
| 1353 | } |
Robin Thellend | 875f259 | 2014-12-02 10:29:37 -0800 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1357 | func listAndVerifyAssociations(t *testing.T, ctx *context.T, stub device.DeviceClientMethods, expected []device.Association) { |
| 1358 | assocs, err := stub.ListAssociations(ctx) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1359 | if err != nil { |
| 1360 | t.Fatalf("ListAssociations failed %v", err) |
| 1361 | } |
Robert Kroeger | 1cb4a0d | 2014-10-20 11:55:38 -0700 | [diff] [blame] | 1362 | compareAssociations(t, assocs, expected) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1365 | // TODO(rjkroege): Verify that associations persist across restarts once |
| 1366 | // permanent storage is added. |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1367 | func TestAccountAssociation(t *testing.T) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 1368 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1369 | defer shutdown() |
| 1370 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1371 | |
| 1372 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1373 | defer deferFn() |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1374 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 1375 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1376 | defer cleanup() |
| 1377 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1378 | // The two "processes"/contexts which will act as IPC clients to |
| 1379 | // the devicemanager process. |
| 1380 | selfCtx := ctx |
| 1381 | otherCtx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal()) |
| 1382 | if err != nil { |
| 1383 | t.Fatalf("Could not create other principal: %v", err) |
| 1384 | } |
| 1385 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1386 | // By default, selfCtx and otherCtx will have blessings generated based |
| 1387 | // on the username/machine name running this process. Since these |
| 1388 | // blessings will appear in test expecations, give them readable names. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1389 | idp := tsecurity.NewIDProvider("root") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1390 | if err := idp.Bless(veyron2.GetPrincipal(selfCtx), "self"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1391 | t.Fatal(err) |
| 1392 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1393 | if err := idp.Bless(veyron2.GetPrincipal(otherCtx), "other"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1394 | t.Fatal(err) |
| 1395 | } |
| 1396 | |
Ryan Brown | 05de9ec | 2015-02-03 14:27:37 -0800 | [diff] [blame] | 1397 | _, dms := mgmttest.RunShellCommand(t, sh, nil, deviceManagerCmd, "dm", root, "unused_helper", "unused_app_repo_name", "unused_curr_link") |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 1398 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1399 | defer syscall.Kill(pid, syscall.SIGINT) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1400 | |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 1401 | deviceStub := device.DeviceClient("dm/device") |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1402 | // Attempt to list associations on the device manager without having |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1403 | // claimed it. |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 1404 | if list, err := deviceStub.ListAssociations(otherCtx); err == nil { |
| 1405 | t.Fatalf("ListAssociations should fail on unclaimed device manager but did not: (%v, %v)", list, err) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1406 | } |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1407 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1408 | // self claims the device manager. |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 1409 | claimDevice(t, ctx, "dm", "alice", noPairingToken) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1410 | |
| 1411 | vlog.VI(2).Info("Verify that associations start out empty.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1412 | listAndVerifyAssociations(t, selfCtx, deviceStub, []device.Association(nil)) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1413 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1414 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/self", "root/other"}, "alice_system_account"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1415 | t.Fatalf("ListAssociations failed %v", err) |
| 1416 | } |
| 1417 | vlog.VI(2).Info("Added association should appear.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1418 | listAndVerifyAssociations(t, selfCtx, deviceStub, []device.Association{ |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1419 | { |
| 1420 | "root/self", |
| 1421 | "alice_system_account", |
| 1422 | }, |
| 1423 | { |
| 1424 | "root/other", |
| 1425 | "alice_system_account", |
| 1426 | }, |
| 1427 | }) |
| 1428 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1429 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/self", "root/other"}, "alice_other_account"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1430 | t.Fatalf("AssociateAccount failed %v", err) |
| 1431 | } |
| 1432 | vlog.VI(2).Info("Change the associations and the change should appear.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1433 | listAndVerifyAssociations(t, selfCtx, deviceStub, []device.Association{ |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1434 | { |
| 1435 | "root/self", |
| 1436 | "alice_other_account", |
| 1437 | }, |
| 1438 | { |
| 1439 | "root/other", |
| 1440 | "alice_other_account", |
| 1441 | }, |
| 1442 | }) |
| 1443 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1444 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/other"}, ""); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1445 | t.Fatalf("AssociateAccount failed %v", err) |
| 1446 | } |
| 1447 | vlog.VI(2).Info("Verify that we can remove an association.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1448 | listAndVerifyAssociations(t, selfCtx, deviceStub, []device.Association{ |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1449 | { |
| 1450 | "root/self", |
| 1451 | "alice_other_account", |
| 1452 | }, |
| 1453 | }) |
| 1454 | } |
| 1455 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1456 | // userName is a helper function to determine the system name that the test is |
| 1457 | // running under. |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1458 | func userName(t *testing.T) string { |
| 1459 | u, err := user.Current() |
| 1460 | if err != nil { |
| 1461 | t.Fatalf("user.Current() failed: %v", err) |
| 1462 | } |
| 1463 | return u.Username |
| 1464 | } |
| 1465 | |
| 1466 | func TestAppWithSuidHelper(t *testing.T) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 1467 | ctx, shutdown := testutil.InitForTest() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1468 | defer shutdown() |
| 1469 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1470 | |
| 1471 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1472 | defer deferFn() |
| 1473 | |
| 1474 | // Set up mock application and binary repositories. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1475 | envelope, cleanup := startMockRepos(t, ctx) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1476 | defer cleanup() |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1477 | |
Robert Kroeger | d6e1d1a | 2014-12-10 15:08:45 -0800 | [diff] [blame] | 1478 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1479 | defer cleanup() |
| 1480 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1481 | // The two "processes"/runtimes which will act as IPC clients to |
| 1482 | // the devicemanager process. |
| 1483 | selfCtx := ctx |
| 1484 | otherCtx, err := veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal()) |
| 1485 | if err != nil { |
| 1486 | t.Fatalf("Could not create other principal: %v", err) |
| 1487 | } |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1488 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1489 | // By default, selfCtx and otherCtx will have blessings generated based |
| 1490 | // on the username/machine name running this process. Since these |
| 1491 | // blessings can appear in debugging output, give them recognizable |
| 1492 | // names. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1493 | idp := tsecurity.NewIDProvider("root") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1494 | if err := idp.Bless(veyron2.GetPrincipal(selfCtx), "self"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1495 | t.Fatal(err) |
| 1496 | } |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1497 | if err := idp.Bless(veyron2.GetPrincipal(otherCtx), "other"); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1498 | t.Fatal(err) |
| 1499 | } |
| 1500 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1501 | // Create a script wrapping the test target that implements suidhelper. |
Bogdan Caprita | 962d5e0 | 2014-10-28 18:36:09 -0700 | [diff] [blame] | 1502 | helperPath := generateSuidHelperScript(t, root) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1503 | |
Ryan Brown | 05de9ec | 2015-02-03 14:27:37 -0800 | [diff] [blame] | 1504 | _, dms := mgmttest.RunShellCommand(t, sh, nil, deviceManagerCmd, "-mocksetuid", "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
Robert Kroeger | ebfb62a | 2014-12-10 14:42:09 -0800 | [diff] [blame] | 1505 | pid := mgmttest.ReadPID(t, dms) |
Cosmos Nicolaou | ad2793f | 2014-10-27 16:24:15 -0700 | [diff] [blame] | 1506 | defer syscall.Kill(pid, syscall.SIGINT) |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 1507 | // Claim the devicemanager with selfCtx as root/self/alice |
| 1508 | claimDevice(t, selfCtx, "dm", "alice", noPairingToken) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1509 | |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 1510 | deviceStub := device.DeviceClient("dm/device") |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1511 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1512 | // Create the local server that the app uses to tell us which system |
| 1513 | // name the device manager wished to run it as. |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 1514 | pingCh, cleanup := setupPingServer(t, ctx) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1515 | defer cleanup() |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1516 | |
Bogdan Caprita | 2692910 | 2014-11-07 11:56:56 -0800 | [diff] [blame] | 1517 | // Create an envelope for a first version of the app. |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1518 | *envelope = envelopeFromShell(sh, []string{testEnvVarName + "=env-var"}, appCmd, "google naps", fmt.Sprintf("--%s=flag-val-envelope", testFlagName), "appV1") |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1519 | |
| 1520 | // Install and start the app as root/self. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1521 | appID := installApp(t, selfCtx) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1522 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1523 | // Start an instance of the app but this time it should fail: we do not |
| 1524 | // have an associated uname for the invoking identity. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1525 | startAppExpectError(t, selfCtx, appID, verror.NoAccess.ID) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1526 | |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1527 | // Create an association for selfCtx |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1528 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/self"}, testUserName); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1529 | t.Fatalf("AssociateAccount failed %v", err) |
| 1530 | } |
| 1531 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1532 | instance1ID := startApp(t, selfCtx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1533 | verifyPingArgs(t, pingCh, testUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1534 | stopApp(t, selfCtx, appID, instance1ID) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1535 | |
| 1536 | vlog.VI(2).Infof("other attempting to run an app without access. Should fail.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1537 | startAppExpectError(t, otherCtx, appID, verror.NoAccess.ID) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1538 | |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1539 | // Self will now let other also install apps. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1540 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/other"}, testUserName); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1541 | t.Fatalf("AssociateAccount failed %v", err) |
| 1542 | } |
| 1543 | // Add Start to the ACL list for root/other. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1544 | newACL, _, err := deviceStub.GetACL(selfCtx) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1545 | if err != nil { |
| 1546 | t.Fatalf("GetACL failed %v", err) |
| 1547 | } |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 1548 | newACL.Add("root/other", string(access.Write)) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1549 | if err := deviceStub.SetACL(selfCtx, newACL, ""); err != nil { |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1550 | t.Fatalf("SetACL failed %v", err) |
| 1551 | } |
| 1552 | |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 1553 | // With the introduction of per installation and per instance ACLs, |
| 1554 | // while other now has administrator permissions on the device manager, |
| 1555 | // other doesn't have execution permissions for the app. So this will |
| 1556 | // fail. |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1557 | vlog.VI(2).Infof("other attempting to run an app still without access. Should fail.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1558 | startAppExpectError(t, otherCtx, appID, verror.NoAccess.ID) |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1559 | |
| 1560 | // But self can give other permissions to start applications. |
| 1561 | vlog.VI(2).Infof("self attempting to give other permission to start %s", appID) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1562 | newACL, _, err = appStub(appID).GetACL(selfCtx) |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1563 | if err != nil { |
| 1564 | t.Fatalf("GetACL on appID: %v failed %v", appID, err) |
| 1565 | } |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 1566 | newACL.Add("root/other", string(access.Read)) |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1567 | if err = appStub(appID).SetACL(selfCtx, newACL, ""); err != nil { |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1568 | t.Fatalf("SetACL on appID: %v failed: %v", appID, err) |
| 1569 | } |
| 1570 | |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1571 | vlog.VI(2).Infof("other attempting to run an app with access. Should succeed.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1572 | instance2ID := startApp(t, otherCtx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1573 | verifyPingArgs(t, pingCh, testUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1574 | suspendApp(t, otherCtx, appID, instance2ID) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1575 | |
| 1576 | vlog.VI(2).Infof("Verify that Resume with the same systemName works.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1577 | resumeApp(t, otherCtx, appID, instance2ID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1578 | verifyPingArgs(t, pingCh, testUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1579 | suspendApp(t, otherCtx, appID, instance2ID) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1580 | |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1581 | vlog.VI(2).Infof("Verify that other can install and run applications.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1582 | otherAppID := installApp(t, otherCtx) |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1583 | |
| 1584 | vlog.VI(2).Infof("other attempting to run an app that other installed. Should succeed.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1585 | instance4ID := startApp(t, otherCtx, otherAppID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1586 | verifyPingArgs(t, pingCh, testUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1587 | |
| 1588 | // Clean up. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1589 | stopApp(t, otherCtx, otherAppID, instance4ID) |
Robert Kroeger | acc778b | 2014-11-03 17:17:21 -0800 | [diff] [blame] | 1590 | |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1591 | // Change the associated system name. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1592 | if err := deviceStub.AssociateAccount(selfCtx, []string{"root/other"}, anotherTestUserName); err != nil { |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1593 | t.Fatalf("AssociateAccount failed %v", err) |
| 1594 | } |
| 1595 | |
| 1596 | vlog.VI(2).Infof("Show that Resume with a different systemName fails.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1597 | resumeAppExpectError(t, otherCtx, appID, instance2ID, verror.NoAccess.ID) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1598 | |
| 1599 | // Clean up. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1600 | stopApp(t, otherCtx, appID, instance2ID) |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1601 | |
| 1602 | vlog.VI(2).Infof("Show that Start with different systemName works.") |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1603 | instance3ID := startApp(t, otherCtx, appID) |
Bogdan Caprita | 17666dd | 2015-01-14 09:27:46 -0800 | [diff] [blame] | 1604 | verifyPingArgs(t, pingCh, anotherTestUserName, "flag-val-envelope", "env-var") // Wait until the app pings us that it's ready. |
Robert Kroeger | 1ce0bd7 | 2014-10-22 13:57:14 -0700 | [diff] [blame] | 1605 | |
| 1606 | // Clean up. |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 1607 | stopApp(t, otherCtx, appID, instance3ID) |
Robert Kroeger | 362ff89 | 2014-09-29 14:23:47 -0700 | [diff] [blame] | 1608 | } |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1609 | |
| 1610 | func TestDownloadSignatureMatch(t *testing.T) { |
| 1611 | ctx, shutdown := testutil.InitForTest() |
| 1612 | defer shutdown() |
| 1613 | veyron2.GetNamespace(ctx).CacheCtl(naming.DisableCache(true)) |
| 1614 | |
| 1615 | sh, deferFn := mgmttest.CreateShellAndMountTable(t, ctx, nil) |
| 1616 | defer deferFn() |
| 1617 | |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1618 | binaryVON := "binary" |
| 1619 | pkgVON := naming.Join(binaryVON, "testpkg") |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1620 | defer startRealBinaryRepository(t, ctx, binaryVON)() |
| 1621 | |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1622 | up := testutil.RandomBytes(testutil.Rand.Intn(5 << 20)) |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1623 | mediaInfo := repository.MediaInfo{Type: "application/octet-stream"} |
| 1624 | sig, err := libbinary.Upload(ctx, naming.Join(binaryVON, "testbinary"), up, mediaInfo) |
| 1625 | if err != nil { |
| 1626 | t.Fatalf("Upload(%v) failed:%v", binaryVON, err) |
| 1627 | } |
| 1628 | |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1629 | // Upload packages for this application |
| 1630 | tmpdir, err := ioutil.TempDir("", "test-package-") |
| 1631 | if err != nil { |
| 1632 | t.Fatalf("ioutil.TempDir failed: %v", err) |
| 1633 | } |
| 1634 | defer os.RemoveAll(tmpdir) |
| 1635 | pkgContents := testutil.RandomBytes(testutil.Rand.Intn(5 << 20)) |
| 1636 | if err := ioutil.WriteFile(filepath.Join(tmpdir, "pkg.txt"), pkgContents, 0600); err != nil { |
| 1637 | t.Fatalf("ioutil.WriteFile failed: %v", err) |
| 1638 | } |
| 1639 | pkgSig, err := libbinary.UploadFromDir(ctx, pkgVON, tmpdir) |
| 1640 | if err != nil { |
| 1641 | t.Fatalf("libbinary.UploadFromDir failed: %v", err) |
| 1642 | } |
| 1643 | |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1644 | // Start the application repository |
| 1645 | envelope, serverStop := startApplicationRepository(ctx) |
| 1646 | defer serverStop() |
| 1647 | |
| 1648 | root, cleanup := mgmttest.SetupRootDir(t, "devicemanager") |
| 1649 | defer cleanup() |
| 1650 | |
| 1651 | // Create a script wrapping the test target that implements suidhelper. |
| 1652 | helperPath := generateSuidHelperScript(t, root) |
| 1653 | |
| 1654 | // Set up the device manager. Since we won't do device manager updates, |
| 1655 | // don't worry about its application envelope and current link. |
| 1656 | _, dms := mgmttest.RunShellCommand(t, sh, nil, deviceManagerCmd, "dm", root, helperPath, "unused_app_repo_name", "unused_curr_link") |
| 1657 | pid := mgmttest.ReadPID(t, dms) |
| 1658 | defer syscall.Kill(pid, syscall.SIGINT) |
Asim Shankar | 23dac32 | 2015-02-14 12:42:26 -0800 | [diff] [blame^] | 1659 | claimDevice(t, ctx, "dm", "mydevice", noPairingToken) |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1660 | |
| 1661 | publisher, err := veyron2.GetPrincipal(ctx).BlessSelf("publisher") |
| 1662 | if err != nil { |
| 1663 | t.Fatalf("Failed to generate publisher blessings:%v", err) |
| 1664 | } |
| 1665 | *envelope = application.Envelope{ |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1666 | Binary: application.SignedFile{ |
| 1667 | File: naming.Join(binaryVON, "testbinary"), |
| 1668 | Signature: *sig, |
| 1669 | }, |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1670 | Publisher: security.MarshalBlessings(publisher), |
Bogdan Caprita | c25a48c | 2015-02-12 13:45:51 -0800 | [diff] [blame] | 1671 | Packages: map[string]application.SignedFile{ |
| 1672 | "pkg": application.SignedFile{ |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1673 | File: pkgVON, |
| 1674 | Signature: *pkgSig, |
| 1675 | }, |
| 1676 | }, |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1677 | } |
Bogdan Caprita | 047fa45 | 2015-02-11 10:18:04 -0800 | [diff] [blame] | 1678 | if _, err := appStub().Install(ctx, mockApplicationRepoName, device.Config{}, nil); err != nil { |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1679 | t.Fatalf("Failed to Install app:%v", err) |
| 1680 | } |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1681 | |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1682 | // Verify that when the binary is corrupted, signature verification fails. |
| 1683 | up[0] = up[0] ^ 0xFF |
| 1684 | if err := libbinary.Delete(ctx, naming.Join(binaryVON, "testbinary")); err != nil { |
| 1685 | t.Fatalf("Delete(%v) failed:%v", binaryVON, err) |
| 1686 | } |
| 1687 | if _, err := libbinary.Upload(ctx, naming.Join(binaryVON, "testbinary"), up, mediaInfo); err != nil { |
| 1688 | t.Fatalf("Upload(%v) failed:%v", binaryVON, err) |
| 1689 | } |
Bogdan Caprita | 047fa45 | 2015-02-11 10:18:04 -0800 | [diff] [blame] | 1690 | if _, err := appStub().Install(ctx, mockApplicationRepoName, device.Config{}, nil); !verror.Is(err, impl.ErrOperationFailed.ID) { |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1691 | t.Fatalf("Failed to verify signature mismatch for binary:%v", binaryVON) |
| 1692 | } |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1693 | |
| 1694 | // Restore the binary and verify that installation succeeds. |
| 1695 | up[0] = up[0] ^ 0xFF |
| 1696 | if err := libbinary.Delete(ctx, naming.Join(binaryVON, "testbinary")); err != nil { |
| 1697 | t.Fatalf("Delete(%v) failed:%v", binaryVON, err) |
| 1698 | } |
| 1699 | if _, err := libbinary.Upload(ctx, naming.Join(binaryVON, "testbinary"), up, mediaInfo); err != nil { |
| 1700 | t.Fatalf("Upload(%v) failed:%v", binaryVON, err) |
| 1701 | } |
Bogdan Caprita | 047fa45 | 2015-02-11 10:18:04 -0800 | [diff] [blame] | 1702 | if _, err := appStub().Install(ctx, mockApplicationRepoName, device.Config{}, nil); err != nil { |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1703 | t.Fatalf("Failed to Install app:%v", err) |
| 1704 | } |
| 1705 | |
| 1706 | // Verify that when the package contents are corrupted, signature verification fails. |
| 1707 | pkgContents[0] = pkgContents[0] ^ 0xFF |
| 1708 | if err := libbinary.Delete(ctx, pkgVON); err != nil { |
| 1709 | t.Fatalf("Delete(%v) failed:%v", pkgVON, err) |
| 1710 | } |
| 1711 | if err := os.Remove(filepath.Join(tmpdir, "pkg.txt")); err != nil { |
| 1712 | t.Fatalf("Remove(%v) failed:%v", filepath.Join(tmpdir, "pkg.txt"), err) |
| 1713 | } |
| 1714 | if err := ioutil.WriteFile(filepath.Join(tmpdir, "pkg.txt"), pkgContents, 0600); err != nil { |
| 1715 | t.Fatalf("ioutil.WriteFile failed: %v", err) |
| 1716 | } |
| 1717 | if _, err = libbinary.UploadFromDir(ctx, pkgVON, tmpdir); err != nil { |
| 1718 | t.Fatalf("libbinary.UploadFromDir failed: %v", err) |
| 1719 | } |
Bogdan Caprita | 047fa45 | 2015-02-11 10:18:04 -0800 | [diff] [blame] | 1720 | if _, err := appStub().Install(ctx, mockApplicationRepoName, device.Config{}, nil); !verror.Is(err, impl.ErrOperationFailed.ID) { |
gauthamt | 3dbef0c | 2015-02-10 12:26:02 -0800 | [diff] [blame] | 1721 | t.Fatalf("Failed to verify signature mismatch for package:%v", pkgVON) |
| 1722 | } |
gauthamt | cb03d13 | 2015-02-05 18:05:22 -0800 | [diff] [blame] | 1723 | } |