blob: 62403da87567c89bae9b5b206d4c3f4dae35da78 [file] [log] [blame]
Suharsh Sivakumar482695e2015-08-03 14:04:27 -07001// 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 Sivakumarac2a8d12015-08-06 12:55:44 -07005package manager
Suharsh Sivakumar482695e2015-08-03 14:04:27 -07006
7import (
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 Sivakumar482695e2015-08-03 14:04:27 -070016
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -070017 _ "v.io/x/ref/runtime/factories/fake"
Suharsh Sivakumare0feb272015-09-14 18:03:48 -070018 "v.io/x/ref/runtime/internal/flow/conn"
Suharsh Sivakumar79b31792015-08-24 15:39:15 -070019 "v.io/x/ref/runtime/internal/flow/flowtest"
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -070020 "v.io/x/ref/test"
Suharsh Sivakumar482695e2015-08-03 14:04:27 -070021)
22
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -070023func init() {
24 test.Init()
25}
26
Suharsh Sivakumar482695e2015-08-03 14:04:27 -070027func TestDirectConnection(t *testing.T) {
28 ctx, shutdown := v23.Init()
29 defer shutdown()
30
Suharsh Sivakumar482695e2015-08-03 14:04:27 -070031 rid := naming.FixedRoutingID(0x5555)
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -070032 m := New(ctx, rid)
Suharsh Sivakumar482695e2015-08-03 14:04:27 -070033
34 if err := m.Listen(ctx, "tcp", "127.0.0.1:0"); err != nil {
35 t.Fatal(err)
36 }
37
Suharsh Sivakumard065cd62015-09-08 16:44:30 -070038 testFlows(t, ctx, m, m, flowtest.BlessingsForPeer)
Suharsh Sivakumar482695e2015-08-03 14:04:27 -070039}
40
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -070041func TestDialCachedConn(t *testing.T) {
42 ctx, shutdown := v23.Init()
43 defer shutdown()
44
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -070045 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 Sivakumarac2a8d12015-08-06 12:55:44 -070050 dm := New(ctx, naming.FixedRoutingID(0x1111))
51 // At first the cache should be empty.
Suharsh Sivakumard9cb8302015-09-04 13:35:11 -070052 if got, want := len(dm.(*manager).cache.addrCache), 0; got != want {
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -070053 t.Fatalf("got cache size %v, want %v", got, want)
54 }
55 // After dialing a connection the cache should hold one connection.
Suharsh Sivakumard065cd62015-09-08 16:44:30 -070056 testFlows(t, ctx, dm, am, flowtest.BlessingsForPeer)
Suharsh Sivakumard9cb8302015-09-04 13:35:11 -070057 if got, want := len(dm.(*manager).cache.addrCache), 1; got != want {
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -070058 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 Sivakumard065cd62015-09-08 16:44:30 -070062 testFlows(t, ctx, dm, am, flowtest.BlessingsForPeer)
Suharsh Sivakumard9cb8302015-09-04 13:35:11 -070063 if got, want := len(dm.(*manager).cache.addrCache), 1; got != want {
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -070064 t.Fatalf("got cache size %v, want %v", got, want)
65 }
66}
67
Suharsh Sivakumard065cd62015-09-08 16:44:30 -070068func 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 Sivakumare0feb272015-09-14 18:03:48 -070086func 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 Sivakumard065cd62015-09-08 16:44:30 -0700109func 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 Sivakumarac2a8d12015-08-06 12:55:44 -0700115 var err error
116 df, err = dm.Dial(ctx, ep, bFn)
117 if err != nil {
118 t.Fatal(err)
119 }
Suharsh Sivakumard065cd62015-09-08 16:44:30 -0700120 want := "do you read me?"
121 writeLine(df, want)
Suharsh Sivakumarac2a8d12015-08-06 12:55:44 -0700122 af, err = am.Accept(ctx)
123 if err != nil {
124 t.Fatal(err)
125 }
Suharsh Sivakumard065cd62015-09-08 16:44:30 -0700126
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 Sivakumarac2a8d12015-08-06 12:55:44 -0700146 return
147}
148
Suharsh Sivakumar482695e2015-08-03 14:04:27 -0700149func readLine(f flow.Flow) (string, error) {
150 s, err := bufio.NewReader(f).ReadString('\n')
151 return strings.TrimRight(s, "\n"), err
152}
153
154func writeLine(f flow.Flow, data string) error {
155 data += "\n"
156 _, err := f.Write([]byte(data))
157 return err
158}