blob: e7a7aa2739f32de17f0e485a8f28e68828680038 [file] [log] [blame]
Asim Shankarecc72a32014-08-21 22:49:40 -07001package vif_test
2
3import (
Jason Hickey96d30e82014-11-13 07:40:00 -08004 "fmt"
Asim Shankarecc72a32014-08-21 22:49:40 -07005 "io/ioutil"
6 "net"
Robin Thellendcf140c02014-12-08 14:56:24 -08007 "os"
Asim Shankarecc72a32014-08-21 22:49:40 -07008 "path"
9 "testing"
10
Jiri Simsa519c5072014-09-17 21:37:57 -070011 "veyron.io/veyron/veyron/runtimes/google/ipc/stream/vif"
Jason Hickey96d30e82014-11-13 07:40:00 -080012 "veyron.io/veyron/veyron2/naming"
Asim Shankarecc72a32014-08-21 22:49:40 -070013)
14
15func TestSetWithPipes(t *testing.T) {
16 var (
Jason Hickey96d30e82014-11-13 07:40:00 -080017 conn1, conn1s = net.Pipe()
18 conn2, _ = net.Pipe()
19 addr1 = conn1.RemoteAddr()
20 addr2 = conn2.RemoteAddr()
Asim Shankarecc72a32014-08-21 22:49:40 -070021
Jason Hickey96d30e82014-11-13 07:40:00 -080022 set = vif.NewSet()
Asim Shankarecc72a32014-08-21 22:49:40 -070023 )
Jason Hickey96d30e82014-11-13 07:40:00 -080024 // The server is needed to negotiate with the dialed VIF. Otherwise, the
25 // InternalNewDialedVIF below will block.
26 go func() {
27 _, err := vif.InternalNewAcceptedVIF(conn1s, naming.FixedRoutingID(2), nil)
28 if err != nil {
29 panic(fmt.Sprintf("failed to create server: %s", err))
30 }
31 }()
32 vf, err := vif.InternalNewDialedVIF(conn1, naming.FixedRoutingID(1), nil, nil, nil)
Asim Shankarecc72a32014-08-21 22:49:40 -070033 if err != nil {
34 t.Fatal(err)
35 }
36 if addr1.Network() != addr2.Network() || addr1.String() != addr2.String() {
37 t.Fatalf("This test was intended for distinct connections that have duplicate RemoteAddrs. "+
38 "That does not seem to be the case with (%q, %q) and (%q, %q)",
39 addr1.Network(), addr1, addr2.Network(), addr2)
40 }
41 set.Insert(vf)
42 if found := set.Find(addr2.Network(), addr2.String()); found != nil {
43 t.Fatalf("Got [%v] want nil on Find(%q, %q))", found, addr2.Network(), addr2)
44 }
45}
46
47func TestSetWithUnixSocket(t *testing.T) {
48 dir, err := ioutil.TempDir("", "TestSetWithFileConn")
49 if err != nil {
50 t.Fatal(err)
51 }
Robin Thellendcf140c02014-12-08 14:56:24 -080052 defer os.RemoveAll(dir)
Asim Shankarecc72a32014-08-21 22:49:40 -070053 sockname := path.Join(dir, "socket")
54 ln, err := net.Listen("unix", sockname)
55 if err != nil {
56 t.Fatal(err)
57 }
58 // Setup the creation of two separate connections.
59 // At the listener, they will end up with two distinct connections
60 // with the same "address" (empty string).
61 go func() {
62 for i := 0; i < 2; i++ {
Jason Hickey96d30e82014-11-13 07:40:00 -080063 conn, err := net.Dial("unix", sockname)
64 if err != nil {
Asim Shankarecc72a32014-08-21 22:49:40 -070065 ln.Close()
Jason Hickey96d30e82014-11-13 07:40:00 -080066 panic(fmt.Sprintf("dial failure: %s", err))
Asim Shankarecc72a32014-08-21 22:49:40 -070067 }
Jason Hickey96d30e82014-11-13 07:40:00 -080068 go func() {
69 if _, err := vif.InternalNewDialedVIF(conn, naming.FixedRoutingID(1), nil, nil, nil); err != nil {
70 panic(fmt.Sprintf("failed to dial VIF: %s", err))
71 }
72 }()
Asim Shankarecc72a32014-08-21 22:49:40 -070073 }
74 }()
75
76 conn1, err := ln.Accept()
77 if err != nil {
78 t.Fatal(err)
79 }
80 conn2, err := ln.Accept()
81 if err != nil {
82 t.Fatal(err)
83 }
84 addr1 := conn1.RemoteAddr()
85 addr2 := conn2.RemoteAddr()
86 if addr1.Network() != addr2.Network() || addr1.String() != addr2.String() {
87 t.Fatalf("This test was intended for distinct connections that have duplicate RemoteAddrs. "+
88 "That does not seem to be the case with (%q, %q) and (%q, %q)",
89 addr1.Network(), addr1, addr2.Network(), addr2)
90 }
91 vif1, err := vif.InternalNewAcceptedVIF(conn1, naming.FixedRoutingID(1), nil)
92 if err != nil {
93 t.Fatal(err)
94 }
Jason Hickey96d30e82014-11-13 07:40:00 -080095 if _, err := vif.InternalNewAcceptedVIF(conn2, naming.FixedRoutingID(1), nil); err != nil {
96 t.Fatal(err)
97 }
Asim Shankarecc72a32014-08-21 22:49:40 -070098 set := vif.NewSet()
99 set.Insert(vif1)
100 if found := set.Find(addr2.Network(), addr2.String()); found != nil {
101 t.Errorf("Got [%v] want nil on Find(%q, %q)", found, addr2.Network(), addr2)
102 }
103}