2018-03-01 07:16:52 -05:00
|
|
|
package http_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-02-10 09:02:28 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2018-03-01 07:16:52 -05:00
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/net"
|
2018-04-10 06:42:02 -04:00
|
|
|
"v2ray.com/core/common/protocol/tls/cert"
|
2018-03-01 07:16:52 -05:00
|
|
|
"v2ray.com/core/testing/servers/tcp"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
. "v2ray.com/core/transport/internet/http"
|
|
|
|
"v2ray.com/core/transport/internet/tls"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTPConnection(t *testing.T) {
|
|
|
|
port := tcp.PickPort()
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
listener, err := Listen(context.Background(), net.LocalHostIP, port, &internet.MemoryStreamConfig{
|
2018-09-07 08:50:25 -04:00
|
|
|
ProtocolName: "http",
|
|
|
|
ProtocolSettings: &Config{},
|
|
|
|
SecurityType: "tls",
|
|
|
|
SecuritySettings: &tls.Config{
|
|
|
|
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("www.v2ray.com")))},
|
|
|
|
},
|
2018-11-21 08:54:40 -05:00
|
|
|
}, func(conn internet.Connection) {
|
2018-03-01 07:16:52 -05:00
|
|
|
go func() {
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
b := buf.New()
|
|
|
|
defer b.Release()
|
|
|
|
|
|
|
|
for {
|
2018-11-02 10:01:33 -04:00
|
|
|
if _, err := b.ReadFrom(conn); err != nil {
|
2018-03-01 07:16:52 -05:00
|
|
|
return
|
|
|
|
}
|
2019-02-10 09:02:28 -05:00
|
|
|
_, err := conn.Write(b.Bytes())
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
})
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2018-03-01 07:16:52 -05:00
|
|
|
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
dctx := context.Background()
|
|
|
|
conn, err := Dial(dctx, net.TCPDestination(net.LocalHostIP, port), &internet.MemoryStreamConfig{
|
2018-09-07 08:50:25 -04:00
|
|
|
ProtocolName: "http",
|
|
|
|
ProtocolSettings: &Config{},
|
|
|
|
SecurityType: "tls",
|
|
|
|
SecuritySettings: &tls.Config{
|
|
|
|
ServerName: "www.v2ray.com",
|
|
|
|
AllowInsecure: true,
|
|
|
|
},
|
2018-03-01 08:22:33 -05:00
|
|
|
})
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2018-03-01 07:16:52 -05:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
const N = 1024
|
|
|
|
b1 := make([]byte, N)
|
|
|
|
common.Must2(rand.Read(b1))
|
|
|
|
b2 := buf.New()
|
|
|
|
|
|
|
|
nBytes, err := conn.Write(b1)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-10 09:02:28 -05:00
|
|
|
if nBytes != N {
|
|
|
|
t.Error("write: ", nBytes)
|
|
|
|
}
|
2018-03-01 07:16:52 -05:00
|
|
|
|
2018-11-02 10:01:33 -04:00
|
|
|
b2.Clear()
|
|
|
|
common.Must2(b2.ReadFullFrom(conn, N))
|
2019-02-10 09:02:28 -05:00
|
|
|
if r := cmp.Diff(b2.Bytes(), b1); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
2018-03-01 07:16:52 -05:00
|
|
|
|
|
|
|
nBytes, err = conn.Write(b1)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-10 09:02:28 -05:00
|
|
|
if nBytes != N {
|
|
|
|
t.Error("write: ", nBytes)
|
|
|
|
}
|
2018-03-01 07:16:52 -05:00
|
|
|
|
2018-11-02 10:01:33 -04:00
|
|
|
b2.Clear()
|
|
|
|
common.Must2(b2.ReadFullFrom(conn, N))
|
2019-02-10 09:02:28 -05:00
|
|
|
if r := cmp.Diff(b2.Bytes(), b1); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|