Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package xproxyd_test |
| 6 | |
| 7 | import ( |
| 8 | "bufio" |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 9 | "fmt" |
Suharsh Sivakumar | a5ba0f8 | 2015-09-25 18:26:52 -0700 | [diff] [blame] | 10 | "math/rand" |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 11 | "strings" |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 12 | "sync" |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 13 | "testing" |
Suharsh Sivakumar | 21373dd | 2015-09-15 16:31:00 -0700 | [diff] [blame] | 14 | "time" |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 15 | |
Suharsh Sivakumar | 0d917c4 | 2015-09-25 16:06:19 -0700 | [diff] [blame] | 16 | "v.io/x/ref" |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 17 | _ "v.io/x/ref/runtime/factories/generic" |
| 18 | "v.io/x/ref/services/xproxyd" |
Suharsh Sivakumar | 21373dd | 2015-09-15 16:31:00 -0700 | [diff] [blame] | 19 | "v.io/x/ref/test/goroutines" |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 20 | |
| 21 | "v.io/v23" |
| 22 | "v.io/v23/context" |
| 23 | "v.io/v23/flow" |
| 24 | "v.io/v23/naming" |
| 25 | "v.io/v23/rpc" |
| 26 | "v.io/v23/security" |
| 27 | ) |
| 28 | |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 29 | const ( |
Suharsh Sivakumar | b73dd63 | 2015-09-18 13:07:37 -0700 | [diff] [blame] | 30 | leakWaitTime = 250 * time.Millisecond |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 31 | pollTime = 50 * time.Millisecond |
| 32 | ) |
Suharsh Sivakumar | 21373dd | 2015-09-15 16:31:00 -0700 | [diff] [blame] | 33 | |
Suharsh Sivakumar | 63dab49 | 2015-09-24 18:01:31 -0700 | [diff] [blame] | 34 | type testService struct{} |
| 35 | |
| 36 | func (t *testService) Echo(ctx *context.T, call rpc.ServerCall, arg string) (string, error) { |
| 37 | return "response:" + arg, nil |
| 38 | } |
| 39 | |
| 40 | func TestProxyRPC(t *testing.T) { |
Suharsh Sivakumar | 0d917c4 | 2015-09-25 16:06:19 -0700 | [diff] [blame] | 41 | if ref.RPCTransitionState() != ref.XServers { |
Suharsh Sivakumar | 63dab49 | 2015-09-24 18:01:31 -0700 | [diff] [blame] | 42 | t.Skip("Test only runs under 'V23_RPC_TRANSITION_STATE==xservers'") |
| 43 | } |
| 44 | defer goroutines.NoLeaks(t, leakWaitTime)() |
| 45 | ctx, shutdown := v23.Init() |
| 46 | defer shutdown() |
| 47 | |
| 48 | // Start the proxy. |
| 49 | pep := startProxy(t, ctx, address{"tcp", "127.0.0.1:0"}) |
| 50 | |
| 51 | // Start the server listening through the proxy. |
| 52 | ctx = v23.WithListenSpec(ctx, rpc.ListenSpec{Proxy: pep.Name()}) |
| 53 | _, s, err := v23.WithNewServer(ctx, "", &testService{}, nil) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | // Wait for the server to finish listening through the proxy. |
| 58 | eps := s.Status().Endpoints |
| 59 | for ; len(eps) < 2 || eps[1].Addr().Network() == ""; eps = s.Status().Endpoints { |
| 60 | time.Sleep(pollTime) |
| 61 | } |
| 62 | |
| 63 | var got string |
| 64 | if err := v23.GetClient(ctx).Call(ctx, eps[1].Name(), "Echo", []interface{}{"hello"}, []interface{}{&got}); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | if want := "response:hello"; got != want { |
| 68 | t.Errorf("got %v, want %v", got, want) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestMultipleProxyRPC(t *testing.T) { |
Suharsh Sivakumar | 0d917c4 | 2015-09-25 16:06:19 -0700 | [diff] [blame] | 73 | if ref.RPCTransitionState() != ref.XServers { |
Suharsh Sivakumar | 63dab49 | 2015-09-24 18:01:31 -0700 | [diff] [blame] | 74 | t.Skip("Test only runs under 'V23_RPC_TRANSITION_STATE==xservers'") |
| 75 | } |
| 76 | defer goroutines.NoLeaks(t, leakWaitTime)() |
| 77 | kp := newKillProtocol() |
| 78 | flow.RegisterProtocol("kill", kp) |
| 79 | ctx, shutdown := v23.Init() |
| 80 | defer shutdown() |
| 81 | |
| 82 | // Start the proxies. |
| 83 | pep := startProxy(t, ctx, address{"kill", "127.0.0.1:0"}) |
| 84 | p2ep := startProxy(t, ctx, address{"v23", pep.String()}, address{"kill", "127.0.0.1:0"}) |
| 85 | |
| 86 | // Start the server listening through the proxy. |
| 87 | ctx = v23.WithListenSpec(ctx, rpc.ListenSpec{Proxy: p2ep.Name()}) |
| 88 | _, s, err := v23.WithNewServer(ctx, "", &testService{}, nil) |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | // Create a new flow manager for the client. |
| 93 | cctx, _, err := v23.ExperimentalWithNewFlowManager(ctx) |
| 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | // Wait for the server to finish listening through the proxy. |
| 98 | eps := s.Status().Endpoints |
| 99 | for ; len(eps) == 0 || eps[0].Addr().Network() == ""; eps = s.Status().Endpoints { |
| 100 | time.Sleep(pollTime) |
| 101 | } |
| 102 | |
| 103 | var got string |
| 104 | if err := v23.GetClient(cctx).Call(ctx, eps[0].Name(), "Echo", []interface{}{"hello"}, []interface{}{&got}); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | if want := "response:hello"; got != want { |
| 108 | t.Errorf("got %v, want %v", got, want) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // TODO(suharshs): Remove the below tests when the transition is complete. |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 113 | func TestSingleProxy(t *testing.T) { |
Suharsh Sivakumar | 21373dd | 2015-09-15 16:31:00 -0700 | [diff] [blame] | 114 | defer goroutines.NoLeaks(t, leakWaitTime)() |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 115 | kp := newKillProtocol() |
| 116 | flow.RegisterProtocol("kill", kp) |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 117 | pctx, shutdown := v23.Init() |
| 118 | defer shutdown() |
| 119 | actx, am, err := v23.ExperimentalWithNewFlowManager(pctx) |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | dctx, dm, err := v23.ExperimentalWithNewFlowManager(pctx) |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 128 | pep := startProxy(t, pctx, address{"kill", "127.0.0.1:0"}) |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 129 | |
Suharsh Sivakumar | 63dab49 | 2015-09-24 18:01:31 -0700 | [diff] [blame] | 130 | done := make(chan struct{}) |
| 131 | update := func(eps []naming.Endpoint) { |
| 132 | if len(eps) > 0 { |
| 133 | if err := testEndToEndConnection(t, dctx, actx, dm, am, eps[0]); err != nil { |
| 134 | t.Error(err) |
| 135 | } |
| 136 | close(done) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if err := am.ProxyListen(actx, pep, update); err != nil { |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 141 | t.Fatal(err) |
| 142 | } |
Suharsh Sivakumar | 63dab49 | 2015-09-24 18:01:31 -0700 | [diff] [blame] | 143 | <-done |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | func TestMultipleProxies(t *testing.T) { |
Suharsh Sivakumar | 21373dd | 2015-09-15 16:31:00 -0700 | [diff] [blame] | 147 | defer goroutines.NoLeaks(t, leakWaitTime)() |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 148 | kp := newKillProtocol() |
| 149 | flow.RegisterProtocol("kill", kp) |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 150 | pctx, shutdown := v23.Init() |
| 151 | defer shutdown() |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 152 | actx, am, err := v23.ExperimentalWithNewFlowManager(pctx) |
| 153 | if err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | dctx, dm, err := v23.ExperimentalWithNewFlowManager(pctx) |
| 157 | if err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 161 | pep := startProxy(t, pctx, address{"kill", "127.0.0.1:0"}) |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 162 | |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 163 | p2ep := startProxy(t, pctx, address{"v23", pep.String()}, address{"kill", "127.0.0.1:0"}) |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 164 | |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 165 | p3ep := startProxy(t, pctx, address{"v23", p2ep.String()}, address{"kill", "127.0.0.1:0"}) |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 166 | |
Suharsh Sivakumar | a5ba0f8 | 2015-09-25 18:26:52 -0700 | [diff] [blame] | 167 | done := make(chan struct{}) |
Suharsh Sivakumar | 63dab49 | 2015-09-24 18:01:31 -0700 | [diff] [blame] | 168 | update := func(eps []naming.Endpoint) { |
| 169 | // TODO(suharshs): Fix this test once we have the proxy send update messages to the |
Suharsh Sivakumar | a5ba0f8 | 2015-09-25 18:26:52 -0700 | [diff] [blame] | 170 | // server when it reconnects to a proxy. This test only really tests the first connection |
| 171 | // currently because the connections are cached. So we need to kill connections and |
| 172 | // wait for them to reestablish but we need proxies to update communicate their new endpoints |
| 173 | // to each other and to the server. For now we at least check a random endpoint so the |
| 174 | // test will at least fail over many runs if something is wrong. |
Suharsh Sivakumar | 63dab49 | 2015-09-24 18:01:31 -0700 | [diff] [blame] | 175 | if len(eps) > 0 { |
Suharsh Sivakumar | a5ba0f8 | 2015-09-25 18:26:52 -0700 | [diff] [blame] | 176 | if err := testEndToEndConnection(t, dctx, actx, dm, am, eps[rand.Int()%3]); err != nil { |
Suharsh Sivakumar | 63dab49 | 2015-09-24 18:01:31 -0700 | [diff] [blame] | 177 | t.Error(err) |
| 178 | } |
Suharsh Sivakumar | a5ba0f8 | 2015-09-25 18:26:52 -0700 | [diff] [blame] | 179 | close(done) |
Suharsh Sivakumar | 63dab49 | 2015-09-24 18:01:31 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | if err := am.ProxyListen(actx, p3ep, update); err != nil { |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 184 | t.Fatal(err) |
| 185 | } |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 186 | |
Suharsh Sivakumar | a5ba0f8 | 2015-09-25 18:26:52 -0700 | [diff] [blame] | 187 | <-done |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 188 | } |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 189 | |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 190 | func testEndToEndConnection(t *testing.T, dctx, actx *context.T, dm, am flow.Manager, aep naming.Endpoint) error { |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 191 | // The dialing flow.Manager dials a flow to the accepting flow.Manager. |
| 192 | want := "Do you read me?" |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 193 | df, err := dm.Dial(dctx, aep, bfp) |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 194 | if err != nil { |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 195 | return err |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 196 | } |
| 197 | // We write before accepting to ensure that the openFlow message is sent. |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 198 | if err := writeLine(df, want); err != nil { |
| 199 | return err |
| 200 | } |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 201 | af, err := am.Accept(actx) |
| 202 | if err != nil { |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 203 | return err |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 204 | } |
| 205 | got, err := readLine(af) |
| 206 | if err != nil { |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 207 | return err |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 208 | } |
| 209 | if got != want { |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 210 | return fmt.Errorf("got %v, want %v", got, want) |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Writes in the opposite direction should work as well. |
| 214 | want = "I read you loud and clear." |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 215 | if err := writeLine(af, want); err != nil { |
| 216 | return err |
| 217 | } |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 218 | got, err = readLine(df) |
| 219 | if err != nil { |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 220 | return err |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 221 | } |
| 222 | if got != want { |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 223 | return fmt.Errorf("got %v, want %v", got, want) |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 224 | } |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 225 | return nil |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 228 | // TODO(suharshs): Add test for bidirectional RPC. |
Suharsh Sivakumar | fb4af95 | 2015-08-21 17:51:01 -0700 | [diff] [blame] | 229 | |
| 230 | func readLine(f flow.Flow) (string, error) { |
| 231 | s, err := bufio.NewReader(f).ReadString('\n') |
| 232 | return strings.TrimRight(s, "\n"), err |
| 233 | } |
| 234 | |
| 235 | func writeLine(f flow.Flow, data string) error { |
| 236 | data += "\n" |
| 237 | _, err := f.Write([]byte(data)) |
| 238 | return err |
| 239 | } |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 240 | |
| 241 | func bfp( |
| 242 | ctx *context.T, |
| 243 | localEndpoint, remoteEndpoint naming.Endpoint, |
| 244 | remoteBlessings security.Blessings, |
| 245 | remoteDischarges map[string]security.Discharge, |
| 246 | ) (security.Blessings, map[string]security.Discharge, error) { |
| 247 | return v23.GetPrincipal(ctx).BlessingStore().Default(), nil, nil |
| 248 | } |
| 249 | |
| 250 | type address struct { |
| 251 | Protocol, Address string |
| 252 | } |
| 253 | |
| 254 | func startProxy(t *testing.T, ctx *context.T, addrs ...address) naming.Endpoint { |
| 255 | var ls rpc.ListenSpec |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 256 | hasProxies := false |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 257 | for _, addr := range addrs { |
| 258 | ls.Addrs = append(ls.Addrs, addr) |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 259 | if addr.Protocol == "v23" { |
| 260 | hasProxies = true |
| 261 | } |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 262 | } |
| 263 | ctx = v23.WithListenSpec(ctx, ls) |
Suharsh Sivakumar | e0feb27 | 2015-09-14 18:03:48 -0700 | [diff] [blame] | 264 | proxy, _, err := xproxyd.New(ctx) |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 265 | if err != nil { |
| 266 | t.Fatal(err) |
| 267 | } |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 268 | // Wait for the proxy to connect to its proxies. |
| 269 | if hasProxies { |
| 270 | for len(proxy.MultipleProxyEndpoints()) == 0 { |
| 271 | time.Sleep(pollTime) |
| 272 | } |
| 273 | } |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 274 | peps := proxy.ListeningEndpoints() |
| 275 | for _, pep := range peps { |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 276 | if pep.Addr().Network() == "tcp" || pep.Addr().Network() == "kill" { |
Suharsh Sivakumar | 6734a79 | 2015-09-04 12:39:15 -0700 | [diff] [blame] | 277 | return pep |
| 278 | } |
| 279 | } |
| 280 | t.Fatal("Proxy not listening on network address.") |
| 281 | return nil |
| 282 | } |
Suharsh Sivakumar | f82e2b4 | 2015-09-16 19:04:53 -0700 | [diff] [blame] | 283 | |
| 284 | type killProtocol struct { |
| 285 | protocol flow.Protocol |
| 286 | mu sync.Mutex |
| 287 | conns []flow.Conn |
| 288 | } |
| 289 | |
| 290 | func newKillProtocol() *killProtocol { |
| 291 | p, _ := flow.RegisteredProtocol("tcp") |
| 292 | return &killProtocol{protocol: p} |
| 293 | } |
| 294 | |
| 295 | func (p *killProtocol) KillConnections() { |
| 296 | p.mu.Lock() |
| 297 | for _, c := range p.conns { |
| 298 | c.Close() |
| 299 | } |
| 300 | p.conns = nil |
| 301 | p.mu.Unlock() |
| 302 | } |
| 303 | |
| 304 | func (p *killProtocol) Dial(ctx *context.T, protocol, address string, timeout time.Duration) (flow.Conn, error) { |
| 305 | c, err := p.protocol.Dial(ctx, "tcp", address, timeout) |
| 306 | if err != nil { |
| 307 | return nil, err |
| 308 | } |
| 309 | p.mu.Lock() |
| 310 | p.conns = append(p.conns, c) |
| 311 | p.mu.Unlock() |
| 312 | return c, nil |
| 313 | } |
| 314 | |
| 315 | func (p *killProtocol) Listen(ctx *context.T, protocol, address string) (flow.Listener, error) { |
| 316 | return p.protocol.Listen(ctx, "tcp", address) |
| 317 | } |
| 318 | |
| 319 | func (p *killProtocol) Resolve(ctx *context.T, protocol, address string) (string, string, error) { |
| 320 | return p.protocol.Resolve(ctx, "tcp", address) |
| 321 | } |