2016-07-12 17:54:54 -04:00
|
|
|
package kcp_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
2016-08-15 16:12:11 -04:00
|
|
|
"net"
|
2016-07-12 17:54:54 -04:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/testing/assert"
|
2016-09-30 10:53:40 -04:00
|
|
|
"v2ray.com/core/transport/internet"
|
2016-08-20 14:55:45 -04:00
|
|
|
. "v2ray.com/core/transport/internet/kcp"
|
2016-10-02 17:43:58 -04:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2016-07-12 17:54:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDialAndListen(t *testing.T) {
|
|
|
|
assert := assert.On(t)
|
|
|
|
|
2016-10-02 17:43:58 -04:00
|
|
|
kcpSettings := new(Config)
|
|
|
|
anySettings, err := ptypes.MarshalAny(kcpSettings)
|
|
|
|
assert.Error(err).IsNil()
|
|
|
|
|
|
|
|
listerner, err := NewListener(v2net.LocalHostIP, v2net.Port(0), internet.ListenOptions{
|
|
|
|
Stream: &internet.StreamConfig{
|
|
|
|
Network: v2net.Network_KCP,
|
|
|
|
NetworkSettings: []*internet.NetworkSettings{
|
|
|
|
{
|
|
|
|
Network: v2net.Network_KCP,
|
|
|
|
Settings: anySettings,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2016-07-12 17:54:54 -04:00
|
|
|
assert.Error(err).IsNil()
|
2016-08-15 16:12:11 -04:00
|
|
|
port := v2net.Port(listerner.Addr().(*net.UDPAddr).Port)
|
2016-07-12 17:54:54 -04:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := listerner.Accept()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
go func() {
|
2016-07-13 03:19:51 -04:00
|
|
|
payload := make([]byte, 4096)
|
2016-07-12 17:54:54 -04:00
|
|
|
for {
|
|
|
|
nBytes, err := conn.Read(payload)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for idx, b := range payload[:nBytes] {
|
|
|
|
payload[idx] = b ^ 'c'
|
|
|
|
}
|
|
|
|
conn.Write(payload[:nBytes])
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
for i := 0; i < 10; i++ {
|
2016-10-02 17:43:58 -04:00
|
|
|
clientConn, err := DialKCP(v2net.LocalHostIP, v2net.UDPDestination(v2net.LocalHostIP, port), internet.DialerOptions{
|
|
|
|
Stream: &internet.StreamConfig{
|
|
|
|
Network: v2net.Network_KCP,
|
|
|
|
NetworkSettings: []*internet.NetworkSettings{
|
|
|
|
{
|
|
|
|
Network: v2net.Network_KCP,
|
|
|
|
Settings: anySettings,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2016-07-12 17:54:54 -04:00
|
|
|
assert.Error(err).IsNil()
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
clientSend := make([]byte, 1024*1024)
|
|
|
|
rand.Read(clientSend)
|
2016-07-14 15:31:54 -04:00
|
|
|
go clientConn.Write(clientSend)
|
2016-07-12 17:54:54 -04:00
|
|
|
|
|
|
|
clientReceived := make([]byte, 1024*1024)
|
|
|
|
nBytes, _ := io.ReadFull(clientConn, clientReceived)
|
|
|
|
assert.Int(nBytes).Equals(len(clientReceived))
|
|
|
|
clientConn.Close()
|
|
|
|
|
|
|
|
clientExpected := make([]byte, 1024*1024)
|
|
|
|
for idx, b := range clientSend {
|
|
|
|
clientExpected[idx] = b ^ 'c'
|
|
|
|
}
|
|
|
|
assert.Bytes(clientReceived).Equals(clientExpected)
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
assert.Int(listerner.ActiveConnections()).Equals(0)
|
|
|
|
|
|
|
|
listerner.Close()
|
|
|
|
}
|