2018-04-12 11:44:23 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2018-03-29 00:35:02 -04:00
|
|
|
package domainsocket_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-04-09 11:09:24 -04:00
|
|
|
"runtime"
|
2018-03-29 00:35:02 -04:00
|
|
|
"testing"
|
|
|
|
|
2018-04-09 11:09:24 -04:00
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
. "v2ray.com/core/transport/internet/domainsocket"
|
|
|
|
. "v2ray.com/ext/assert"
|
2018-03-29 00:35:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestListen(t *testing.T) {
|
2018-04-09 11:09:24 -04:00
|
|
|
assert := With(t)
|
|
|
|
|
2018-09-07 08:50:25 -04:00
|
|
|
ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "domainsocket",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "/tmp/ts3",
|
|
|
|
},
|
2018-04-09 11:09:24 -04:00
|
|
|
})
|
|
|
|
listener, err := Listen(ctx, nil, net.Port(0), func(conn internet.Connection) {
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
b := buf.New()
|
2018-11-02 10:01:33 -04:00
|
|
|
common.Must2(b.ReadFrom(conn))
|
2018-04-09 11:09:24 -04:00
|
|
|
assert(b.String(), Equals, "Request")
|
|
|
|
|
|
|
|
common.Must2(conn.Write([]byte("Response")))
|
|
|
|
})
|
|
|
|
assert(err, IsNil)
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
conn, err := Dial(ctx, net.Destination{})
|
|
|
|
assert(err, IsNil)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
_, err = conn.Write([]byte("Request"))
|
|
|
|
assert(err, IsNil)
|
|
|
|
|
|
|
|
b := buf.New()
|
2018-11-02 10:01:33 -04:00
|
|
|
common.Must2(b.ReadFrom(conn))
|
2018-03-29 00:35:02 -04:00
|
|
|
|
2018-04-09 11:09:24 -04:00
|
|
|
assert(b.String(), Equals, "Response")
|
2018-03-29 00:35:02 -04:00
|
|
|
}
|
2018-04-05 04:17:17 -04:00
|
|
|
|
2018-04-09 11:09:24 -04:00
|
|
|
func TestListenAbstract(t *testing.T) {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert := With(t)
|
|
|
|
|
2018-09-07 08:50:25 -04:00
|
|
|
ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
|
|
|
ProtocolName: "domainsocket",
|
|
|
|
ProtocolSettings: &Config{
|
|
|
|
Path: "/tmp/ts3",
|
|
|
|
Abstract: true,
|
|
|
|
},
|
2018-04-09 11:09:24 -04:00
|
|
|
})
|
|
|
|
listener, err := Listen(ctx, nil, net.Port(0), func(conn internet.Connection) {
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
b := buf.New()
|
2018-11-02 10:01:33 -04:00
|
|
|
common.Must2(b.ReadFrom(conn))
|
2018-04-09 11:09:24 -04:00
|
|
|
assert(b.String(), Equals, "Request")
|
|
|
|
|
|
|
|
common.Must2(conn.Write([]byte("Response")))
|
|
|
|
})
|
|
|
|
assert(err, IsNil)
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
conn, err := Dial(ctx, net.Destination{})
|
|
|
|
assert(err, IsNil)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
_, err = conn.Write([]byte("Request"))
|
|
|
|
assert(err, IsNil)
|
|
|
|
|
|
|
|
b := buf.New()
|
2018-11-02 10:01:33 -04:00
|
|
|
common.Must2(b.ReadFrom(conn))
|
2018-04-09 11:09:24 -04:00
|
|
|
|
|
|
|
assert(b.String(), Equals, "Response")
|
2018-04-05 04:17:17 -04:00
|
|
|
}
|