1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
package dbus
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
. "gopkg.in/check.v1"
"net"
"path"
)
func (s *S) TestNewTransportUnix(c *C) {
trans, err := newTransport("unix:path=/tmp/dbus%3dsock")
c.Check(err, Equals, nil)
unixTrans, ok := trans.(*unixTransport)
c.Check(ok, Equals, true)
c.Check(unixTrans.Address, Equals, "/tmp/dbus=sock")
// And for abstract namespace sockets:
trans, err = newTransport("unix:abstract=/tmp/dbus%3dsock")
c.Check(err, Equals, nil)
unixTrans, ok = trans.(*unixTransport)
c.Check(ok, Equals, true)
c.Check(unixTrans.Address, Equals, "@/tmp/dbus=sock")
}
func (s *S) TestUnixTransportDial(c *C) {
socketFile := path.Join(c.MkDir(), "bus.sock")
listener, err := net.Listen("unix", socketFile)
c.Assert(err, IsNil)
trans, err := newTransport(fmt.Sprintf("unix:path=%s", socketFile))
c.Assert(err, IsNil)
errChan := make(chan error, 1)
go func() {
conn, err := listener.Accept()
if err == nil {
conn.Close()
}
errChan <- err
}()
conn, err := trans.Dial()
c.Assert(err, IsNil)
conn.Close()
// Was the other end of the connection established correctly?
err = <-errChan
c.Check(err, IsNil)
listener.Close()
}
func (s *S) TestNewTransportTcp(c *C) {
trans, err := newTransport("tcp:host=localhost,port=4444")
c.Check(err, Equals, nil)
tcpTrans, ok := trans.(*tcpTransport)
c.Check(ok, Equals, true)
c.Check(tcpTrans.Address, Equals, "localhost:4444")
c.Check(tcpTrans.Family, Equals, "tcp4")
// And with explicit family:
trans, err = newTransport("tcp:host=localhost,port=4444,family=ipv4")
c.Check(err, Equals, nil)
tcpTrans, ok = trans.(*tcpTransport)
c.Check(ok, Equals, true)
c.Check(tcpTrans.Address, Equals, "localhost:4444")
c.Check(tcpTrans.Family, Equals, "tcp4")
trans, err = newTransport("tcp:host=localhost,port=4444,family=ipv6")
c.Check(err, Equals, nil)
tcpTrans, ok = trans.(*tcpTransport)
c.Check(ok, Equals, true)
c.Check(tcpTrans.Address, Equals, "localhost:4444")
c.Check(tcpTrans.Family, Equals, "tcp6")
}
func (s *S) TestTcpTransportDial(c *C) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, IsNil)
addr := listener.Addr().(*net.TCPAddr)
address := fmt.Sprintf("tcp:host=%s,port=%d", addr.IP.String(), addr.Port)
trans, err := newTransport(address)
c.Assert(err, IsNil)
errChan := make(chan error, 1)
go func() {
conn, err := listener.Accept()
if err == nil {
conn.Close()
}
errChan <- err
}()
conn, err := trans.Dial()
c.Assert(err, IsNil)
conn.Close()
// Was the other end of the connection established correctly?
err = <-errChan
c.Check(err, IsNil)
listener.Close()
}
func (s *S) TestNewTransportNonceTcp(c *C) {
trans, err := newTransport("nonce-tcp:host=localhost,port=4444,noncefile=/tmp/foo")
c.Check(err, Equals, nil)
nonceTcpTrans, ok := trans.(*nonceTcpTransport)
c.Check(ok, Equals, true)
c.Check(nonceTcpTrans.Address, Equals, "localhost:4444")
c.Check(nonceTcpTrans.Family, Equals, "tcp4")
c.Check(nonceTcpTrans.NonceFile, Equals, "/tmp/foo")
}
func (s *S) TestNonceTcpTransportDial(c *C) {
nonceFile := path.Join(c.MkDir(), "nonce-file")
nonceData := []byte("nonce-data")
c.Assert(ioutil.WriteFile(nonceFile, nonceData, 0600), IsNil)
listener, err := net.Listen("tcp", "127.0.0.1:0")
c.Assert(err, IsNil)
addr := listener.Addr().(*net.TCPAddr)
address := fmt.Sprintf("nonce-tcp:host=%s,port=%d,noncefile=%s", addr.IP.String(), addr.Port, nonceFile)
trans, err := newTransport(address)
c.Assert(err, IsNil)
errChan := make(chan error, 1)
go func() {
conn, err := listener.Accept()
if err != nil {
errChan <- err
return
}
// The client starts by writing the nonce data to the socket.
data := make([]byte, 4096)
n, err := conn.Read(data)
if err != nil {
conn.Close()
errChan <- err
return
}
if !bytes.Equal(data[:n], nonceData) {
err = errors.New("Did not receive nonce data")
}
conn.Close()
errChan <- err
}()
conn, err := trans.Dial()
c.Assert(err, IsNil)
conn.Close()
// Was the other end of the connection established correctly?
err = <-errChan
c.Check(err, IsNil)
listener.Close()
}
|