Suharsh Sivakumar | 482695e | 2015-08-03 14:04:27 -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 | |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 5 | package manager |
Suharsh Sivakumar | 482695e | 2015-08-03 14:04:27 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "bufio" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "v.io/v23" |
| 13 | "v.io/v23/context" |
| 14 | "v.io/v23/flow" |
| 15 | "v.io/v23/naming" |
Suharsh Sivakumar | 482695e | 2015-08-03 14:04:27 -0700 | [diff] [blame] | 16 | |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 17 | _ "v.io/x/ref/runtime/factories/fake" |
Suharsh Sivakumar | e0feb27 | 2015-09-14 18:03:48 -0700 | [diff] [blame] | 18 | "v.io/x/ref/runtime/internal/flow/conn" |
Suharsh Sivakumar | 79b3179 | 2015-08-24 15:39:15 -0700 | [diff] [blame] | 19 | "v.io/x/ref/runtime/internal/flow/flowtest" |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 20 | "v.io/x/ref/test" |
Suharsh Sivakumar | 482695e | 2015-08-03 14:04:27 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 23 | func init() { |
| 24 | test.Init() |
| 25 | } |
| 26 | |
Suharsh Sivakumar | 482695e | 2015-08-03 14:04:27 -0700 | [diff] [blame] | 27 | func TestDirectConnection(t *testing.T) { |
| 28 | ctx, shutdown := v23.Init() |
| 29 | defer shutdown() |
| 30 | |
Suharsh Sivakumar | 482695e | 2015-08-03 14:04:27 -0700 | [diff] [blame] | 31 | rid := naming.FixedRoutingID(0x5555) |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 32 | m := New(ctx, rid) |
Suharsh Sivakumar | 482695e | 2015-08-03 14:04:27 -0700 | [diff] [blame] | 33 | |
| 34 | if err := m.Listen(ctx, "tcp", "127.0.0.1:0"); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | |
Suharsh Sivakumar | d065cd6 | 2015-09-08 16:44:30 -0700 | [diff] [blame] | 38 | testFlows(t, ctx, m, m, flowtest.BlessingsForPeer) |
Suharsh Sivakumar | 482695e | 2015-08-03 14:04:27 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 41 | func TestDialCachedConn(t *testing.T) { |
| 42 | ctx, shutdown := v23.Init() |
| 43 | defer shutdown() |
| 44 | |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 45 | am := New(ctx, naming.FixedRoutingID(0x5555)) |
| 46 | if err := am.Listen(ctx, "tcp", "127.0.0.1:0"); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 50 | dm := New(ctx, naming.FixedRoutingID(0x1111)) |
| 51 | // At first the cache should be empty. |
Suharsh Sivakumar | d9cb830 | 2015-09-04 13:35:11 -0700 | [diff] [blame] | 52 | if got, want := len(dm.(*manager).cache.addrCache), 0; got != want { |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 53 | t.Fatalf("got cache size %v, want %v", got, want) |
| 54 | } |
| 55 | // After dialing a connection the cache should hold one connection. |
Suharsh Sivakumar | d065cd6 | 2015-09-08 16:44:30 -0700 | [diff] [blame] | 56 | testFlows(t, ctx, dm, am, flowtest.BlessingsForPeer) |
Suharsh Sivakumar | d9cb830 | 2015-09-04 13:35:11 -0700 | [diff] [blame] | 57 | if got, want := len(dm.(*manager).cache.addrCache), 1; got != want { |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 58 | t.Fatalf("got cache size %v, want %v", got, want) |
| 59 | } |
| 60 | // After dialing another connection the cache should still hold one connection |
| 61 | // because the connections should be reused. |
Suharsh Sivakumar | d065cd6 | 2015-09-08 16:44:30 -0700 | [diff] [blame] | 62 | testFlows(t, ctx, dm, am, flowtest.BlessingsForPeer) |
Suharsh Sivakumar | d9cb830 | 2015-09-04 13:35:11 -0700 | [diff] [blame] | 63 | if got, want := len(dm.(*manager).cache.addrCache), 1; got != want { |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 64 | t.Fatalf("got cache size %v, want %v", got, want) |
| 65 | } |
| 66 | } |
| 67 | |
Suharsh Sivakumar | d065cd6 | 2015-09-08 16:44:30 -0700 | [diff] [blame] | 68 | func TestBidirectionalListeningEndpoint(t *testing.T) { |
| 69 | ctx, shutdown := v23.Init() |
| 70 | defer shutdown() |
| 71 | |
| 72 | am := New(ctx, naming.FixedRoutingID(0x5555)) |
| 73 | if err := am.Listen(ctx, "tcp", "127.0.0.1:0"); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | eps := am.ListeningEndpoints() |
| 77 | if len(eps) == 0 { |
| 78 | t.Fatalf("no endpoints listened on") |
| 79 | } |
| 80 | dm := New(ctx, naming.FixedRoutingID(0x1111)) |
| 81 | testFlows(t, ctx, dm, am, flowtest.BlessingsForPeer) |
| 82 | // Now am should be able to make a flow to dm even though dm is not listening. |
| 83 | testFlows(t, ctx, am, dm, flowtest.BlessingsForPeer) |
| 84 | } |
| 85 | |
Suharsh Sivakumar | e0feb27 | 2015-09-14 18:03:48 -0700 | [diff] [blame] | 86 | func TestNullClientBlessings(t *testing.T) { |
| 87 | ctx, shutdown := v23.Init() |
| 88 | defer shutdown() |
| 89 | |
| 90 | am := New(ctx, naming.FixedRoutingID(0x5555)) |
| 91 | if err := am.Listen(ctx, "tcp", "127.0.0.1:0"); err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | dm := New(ctx, naming.NullRoutingID) |
| 95 | _, af := testFlows(t, ctx, dm, am, flowtest.BlessingsForPeer) |
| 96 | // Ensure that the remote blessings of the underlying conn of the accepted flow are zero. |
| 97 | if rBlessings := af.Conn().(*conn.Conn).RemoteBlessings(); !rBlessings.IsZero() { |
| 98 | t.Errorf("got %v, want zero-value blessings", rBlessings) |
| 99 | } |
| 100 | dm = New(ctx, naming.FixedRoutingID(0x1111)) |
| 101 | _, af = testFlows(t, ctx, dm, am, flowtest.BlessingsForPeer) |
| 102 | // Ensure that the remote blessings of the underlying conn of the accepted flow are |
| 103 | // non-zero if we did specify a RoutingID. |
| 104 | if rBlessings := af.Conn().(*conn.Conn).RemoteBlessings(); rBlessings.IsZero() { |
| 105 | t.Errorf("got %v, want non-zero blessings", rBlessings) |
| 106 | } |
| 107 | } |
| 108 | |
Suharsh Sivakumar | d065cd6 | 2015-09-08 16:44:30 -0700 | [diff] [blame] | 109 | func testFlows(t *testing.T, ctx *context.T, dm, am flow.Manager, bFn flow.BlessingsForPeer) (df, af flow.Flow) { |
| 110 | eps := am.ListeningEndpoints() |
| 111 | if len(eps) == 0 { |
| 112 | t.Fatalf("no endpoints listened on") |
| 113 | } |
| 114 | ep := eps[0] |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 115 | var err error |
| 116 | df, err = dm.Dial(ctx, ep, bFn) |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
Suharsh Sivakumar | d065cd6 | 2015-09-08 16:44:30 -0700 | [diff] [blame] | 120 | want := "do you read me?" |
| 121 | writeLine(df, want) |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 122 | af, err = am.Accept(ctx) |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
Suharsh Sivakumar | d065cd6 | 2015-09-08 16:44:30 -0700 | [diff] [blame] | 126 | |
| 127 | got, err := readLine(af) |
| 128 | if err != nil { |
| 129 | t.Error(err) |
| 130 | } |
| 131 | if got != want { |
| 132 | t.Errorf("got %v, want %v", got, want) |
| 133 | } |
| 134 | |
| 135 | want = "i read you" |
| 136 | if err := writeLine(af, want); err != nil { |
| 137 | t.Error(err) |
| 138 | } |
| 139 | got, err = readLine(df) |
| 140 | if err != nil { |
| 141 | t.Error(err) |
| 142 | } |
| 143 | if got != want { |
| 144 | t.Errorf("got %v, want %v", got, want) |
| 145 | } |
Suharsh Sivakumar | ac2a8d1 | 2015-08-06 12:55:44 -0700 | [diff] [blame] | 146 | return |
| 147 | } |
| 148 | |
Suharsh Sivakumar | 482695e | 2015-08-03 14:04:27 -0700 | [diff] [blame] | 149 | func readLine(f flow.Flow) (string, error) { |
| 150 | s, err := bufio.NewReader(f).ReadString('\n') |
| 151 | return strings.TrimRight(s, "\n"), err |
| 152 | } |
| 153 | |
| 154 | func writeLine(f flow.Flow, data string) error { |
| 155 | data += "\n" |
| 156 | _, err := f.Write([]byte(data)) |
| 157 | return err |
| 158 | } |