1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 07:26:24 -05:00

remove usage on assert lib

This commit is contained in:
Darien Raymond 2019-01-06 23:30:38 +01:00
parent a1b33c3bd6
commit 4468c60b95
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 160 additions and 116 deletions

View File

@ -1,10 +1,11 @@
package dns_test package dns_test
import ( import (
"runtime"
"testing" "testing"
"time" "time"
"github.com/google/go-cmp/cmp"
"v2ray.com/core" "v2ray.com/core"
"v2ray.com/core/app/dispatcher" "v2ray.com/core/app/dispatcher"
. "v2ray.com/core/app/dns" . "v2ray.com/core/app/dns"
@ -17,7 +18,6 @@ import (
feature_dns "v2ray.com/core/features/dns" feature_dns "v2ray.com/core/features/dns"
"v2ray.com/core/proxy/freedom" "v2ray.com/core/proxy/freedom"
"v2ray.com/core/testing/servers/udp" "v2ray.com/core/testing/servers/udp"
. "v2ray.com/ext/assert"
"github.com/miekg/dns" "github.com/miekg/dns"
) )
@ -67,11 +67,6 @@ func (*staticHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
} }
func TestUDPServerSubnet(t *testing.T) { func TestUDPServerSubnet(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("doesn't work on Windows due to miekg/dns changes.")
}
assert := With(t)
port := udp.PickPort() port := udp.PickPort()
dnsServer := dns.Server{ dnsServer := dns.Server{
@ -112,22 +107,21 @@ func TestUDPServerSubnet(t *testing.T) {
} }
v, err := core.New(config) v, err := core.New(config)
assert(err, IsNil) common.Must(err)
client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client) client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
ips, err := client.LookupIP("google.com") ips, err := client.LookupIP("google.com")
assert(err, IsNil) if err != nil {
assert(len(ips), Equals, 1) t.Fatal("unexpected error: ", err)
assert([]byte(ips[0]), Equals, []byte{8, 8, 4, 4}) }
if r := cmp.Diff(ips, []net.IP{{8, 8, 4, 4}}); r != "" {
t.Fatal(r)
}
} }
func TestUDPServer(t *testing.T) { func TestUDPServer(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("doesn't work on Windows due to miekg/dns changes.")
}
assert := With(t)
port := udp.PickPort() port := udp.PickPort()
dnsServer := dns.Server{ dnsServer := dns.Server{
@ -167,34 +161,47 @@ func TestUDPServer(t *testing.T) {
} }
v, err := core.New(config) v, err := core.New(config)
assert(err, IsNil) common.Must(err)
client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client) client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
ips, err := client.LookupIP("google.com") {
assert(err, IsNil) ips, err := client.LookupIP("google.com")
assert(len(ips), Equals, 1) if err != nil {
assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8}) t.Fatal("unexpected error: ", err)
}
ips, err = client.LookupIP("facebook.com") if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
assert(err, IsNil) t.Fatal(r)
assert(len(ips), Equals, 1) }
assert([]byte(ips[0]), Equals, []byte{9, 9, 9, 9}) }
{
ips, err := client.LookupIP("facebook.com")
if err != nil {
t.Fatal("unexpected error: ", err)
}
if r := cmp.Diff(ips, []net.IP{{9, 9, 9, 9}}); r != "" {
t.Fatal(r)
}
}
dnsServer.Shutdown() dnsServer.Shutdown()
ips, err = client.LookupIP("google.com") {
assert(err, IsNil) ips, err := client.LookupIP("google.com")
assert(len(ips), Equals, 1) if err != nil {
assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8}) t.Fatal("unexpected error: ", err)
}
if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
t.Fatal(r)
}
}
} }
func TestPrioritizedDomain(t *testing.T) { func TestPrioritizedDomain(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("doesn't work on Windows due to miekg/dns changes.")
}
assert := With(t)
port := udp.PickPort() port := udp.PickPort()
dnsServer := dns.Server{ dnsServer := dns.Server{
@ -253,15 +260,22 @@ func TestPrioritizedDomain(t *testing.T) {
} }
v, err := core.New(config) v, err := core.New(config)
assert(err, IsNil) common.Must(err)
client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client) client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
startTime := time.Now() startTime := time.Now()
ips, err := client.LookupIP("google.com")
assert(err, IsNil) {
assert(len(ips), Equals, 1) ips, err := client.LookupIP("google.com")
assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8}) if err != nil {
t.Fatal("unexpected error: ", err)
}
if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" {
t.Fatal(r)
}
}
endTime := time.Now() endTime := time.Now()
if startTime.After(endTime.Add(time.Second * 2)) { if startTime.After(endTime.Add(time.Second * 2)) {
@ -270,11 +284,6 @@ func TestPrioritizedDomain(t *testing.T) {
} }
func TestUDPServerIPv6(t *testing.T) { func TestUDPServerIPv6(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("doesn't work on Windows due to miekg/dns changes.")
}
assert := With(t)
port := udp.PickPort() port := udp.PickPort()
dnsServer := dns.Server{ dnsServer := dns.Server{
@ -314,13 +323,19 @@ func TestUDPServerIPv6(t *testing.T) {
} }
v, err := core.New(config) v, err := core.New(config)
assert(err, IsNil) common.Must(err)
client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client) client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client)
client6 := client.(feature_dns.IPv6Lookup) client6 := client.(feature_dns.IPv6Lookup)
ips, err := client6.LookupIPv6("ipv6.google.com") {
assert(err, IsNil) ips, err := client6.LookupIPv6("ipv6.google.com")
assert(len(ips), Equals, 1) if err != nil {
assert([]byte(ips[0]), Equals, []byte{32, 1, 72, 96, 72, 96, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136}) t.Fatal("unexpected error: ", err)
}
if r := cmp.Diff(ips, []net.IP{{32, 1, 72, 96, 72, 96, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136}}); r != "" {
t.Fatal(r)
}
}
} }

View File

@ -6,13 +6,11 @@ import (
"time" "time"
. "v2ray.com/core/app/policy" . "v2ray.com/core/app/policy"
"v2ray.com/core/common"
"v2ray.com/core/features/policy" "v2ray.com/core/features/policy"
. "v2ray.com/ext/assert"
) )
func TestPolicy(t *testing.T) { func TestPolicy(t *testing.T) {
assert := With(t)
manager, err := New(context.Background(), &Config{ manager, err := New(context.Background(), &Config{
Level: map[uint32]*Policy{ Level: map[uint32]*Policy{
0: { 0: {
@ -24,14 +22,24 @@ func TestPolicy(t *testing.T) {
}, },
}, },
}) })
assert(err, IsNil) common.Must(err)
pDefault := policy.SessionDefault() pDefault := policy.SessionDefault()
p0 := manager.ForLevel(0) {
assert(p0.Timeouts.Handshake, Equals, 2*time.Second) p := manager.ForLevel(0)
assert(p0.Timeouts.ConnectionIdle, Equals, pDefault.Timeouts.ConnectionIdle) if p.Timeouts.Handshake != 2*time.Second {
t.Error("expect 2 sec timeout, but got ", p.Timeouts.Handshake)
}
if p.Timeouts.ConnectionIdle != pDefault.Timeouts.ConnectionIdle {
t.Error("expect ", pDefault.Timeouts.ConnectionIdle, " sec timeout, but got ", p.Timeouts.ConnectionIdle)
}
}
p1 := manager.ForLevel(1) {
assert(p1.Timeouts.Handshake, Equals, pDefault.Timeouts.Handshake) p := manager.ForLevel(1)
if p.Timeouts.Handshake != pDefault.Timeouts.Handshake {
t.Error("expect ", pDefault.Timeouts.Handshake, " sec timeout, but got ", p.Timeouts.Handshake)
}
}
} }

View File

@ -6,12 +6,15 @@ import (
"testing" "testing"
"time" "time"
"v2ray.com/core/common" "github.com/google/go-cmp/cmp"
"v2ray.com/core/common/compare" "golang.org/x/sync/errgroup"
"v2ray.com/core" "v2ray.com/core"
"v2ray.com/core/app/log" "v2ray.com/core/app/log"
"v2ray.com/core/app/proxyman" "v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
"v2ray.com/core/common/compare"
"v2ray.com/core/common/errors"
clog "v2ray.com/core/common/log" clog "v2ray.com/core/common/log"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
@ -25,13 +28,11 @@ import (
) )
func TestShadowsocksAES256TCP(t *testing.T) { func TestShadowsocksAES256TCP(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{ tcpServer := tcp.Server{
MsgProcessor: xor, MsgProcessor: xor,
} }
dest, err := tcpServer.Start() dest, err := tcpServer.Start()
assert(err, IsNil) common.Must(err)
defer tcpServer.Close() defer tcpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{ account := serial.ToTypedMessage(&shadowsocks.Account{
@ -113,44 +114,50 @@ func TestShadowsocksAES256TCP(t *testing.T) {
} }
servers, err := InitializeServerConfigs(serverConfig, clientConfig) servers, err := InitializeServerConfigs(serverConfig, clientConfig)
assert(err, IsNil) common.Must(err)
defer CloseAllServers(servers)
var wg sync.WaitGroup var errg errgroup.Group
wg.Add(10)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
go func() { errg.Go(func() error {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1}, IP: []byte{127, 0, 0, 1},
Port: int(clientPort), Port: int(clientPort),
}) })
assert(err, IsNil) if err != nil {
return err
}
defer conn.Close()
payload := make([]byte, 10240*1024) payload := make([]byte, 10240*1024)
rand.Read(payload) common.Must2(rand.Read(payload))
nBytes, err := conn.Write([]byte(payload)) nBytes, err := conn.Write([]byte(payload))
assert(err, IsNil) if err != nil {
assert(nBytes, Equals, len(payload)) return err
}
if nBytes != len(payload) {
return errors.New("expect ", len(payload), " written, but actually ", nBytes)
}
response := readFrom(conn, time.Second*20, 10240*1024) response := readFrom(conn, time.Second*20, 10240*1024)
assert(response, Equals, xor([]byte(payload))) if r := cmp.Diff(response, xor([]byte(payload))); r != "" {
assert(conn.Close(), IsNil) return errors.New(r)
wg.Done() }
}() return nil
})
}
if err := errg.Wait(); err != nil {
t.Fatal(err)
} }
wg.Wait()
CloseAllServers(servers)
} }
func TestShadowsocksAES128UDP(t *testing.T) { func TestShadowsocksAES128UDP(t *testing.T) {
assert := With(t)
udpServer := udp.Server{ udpServer := udp.Server{
MsgProcessor: xor, MsgProcessor: xor,
} }
dest, err := udpServer.Start() dest, err := udpServer.Start()
assert(err, IsNil) common.Must(err)
defer udpServer.Close() defer udpServer.Close()
account := serial.ToTypedMessage(&shadowsocks.Account{ account := serial.ToTypedMessage(&shadowsocks.Account{
@ -232,34 +239,43 @@ func TestShadowsocksAES128UDP(t *testing.T) {
} }
servers, err := InitializeServerConfigs(serverConfig, clientConfig) servers, err := InitializeServerConfigs(serverConfig, clientConfig)
assert(err, IsNil) common.Must(err)
defer CloseAllServers(servers)
var wg sync.WaitGroup var errg errgroup.Group
wg.Add(10)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
go func() { errg.Go(func() error {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1}, IP: []byte{127, 0, 0, 1},
Port: int(clientPort), Port: int(clientPort),
}) })
assert(err, IsNil) if err != nil {
return err
}
defer conn.Close()
payload := make([]byte, 1024) payload := make([]byte, 1024)
rand.Read(payload) common.Must2(rand.Read(payload))
nBytes, err := conn.Write([]byte(payload)) nBytes, err := conn.Write([]byte(payload))
assert(err, IsNil) if err != nil {
assert(nBytes, Equals, len(payload)) return err
}
if nBytes != len(payload) {
return errors.New("expect ", len(payload), " written, but actually ", nBytes)
}
response := readFrom(conn, time.Second*5, 1024) response := readFrom(conn, time.Second*5, 1024)
assert(response, Equals, xor([]byte(payload))) if r := cmp.Diff(response, xor(payload)); r != "" {
assert(conn.Close(), IsNil) return errors.New(r)
wg.Done() }
}() return nil
})
} }
wg.Wait()
CloseAllServers(servers) if err := errg.Wait(); err != nil {
t.Fatal(err)
}
} }
func TestShadowsocksChacha20TCP(t *testing.T) { func TestShadowsocksChacha20TCP(t *testing.T) {
@ -938,12 +954,9 @@ func TestShadowsocksNone(t *testing.T) {
defer CloseAllServers(servers) defer CloseAllServers(servers)
var wg sync.WaitGroup var errg errgroup.Group
wg.Add(10)
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
go func() { errg.Go(func() error {
defer wg.Done()
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: []byte{127, 0, 0, 1}, IP: []byte{127, 0, 0, 1},
Port: int(clientPort), Port: int(clientPort),
@ -958,14 +971,18 @@ func TestShadowsocksNone(t *testing.T) {
common.Must(err) common.Must(err)
if nBytes != len(payload) { if nBytes != len(payload) {
t.Error("only part of payload is written: ", nBytes) return errors.New("only part of payload is written: ", nBytes)
} }
response := readFrom(conn, time.Second*20, 10240*1024) response := readFrom(conn, time.Second*20, 10240*1024)
if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { if r := cmp.Diff(response, xor(payload)); r != "" {
t.Error(err) return errors.New(r)
} }
}() return nil
})
}
if err := errg.Wait(); err != nil {
t.Fatal(err)
} }
wg.Wait()
} }

View File

@ -7,6 +7,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/task" "v2ray.com/core/common/task"
@ -15,36 +17,38 @@ import (
) )
func TestPipeReadWrite(t *testing.T) { func TestPipeReadWrite(t *testing.T) {
assert := With(t)
pReader, pWriter := New(WithSizeLimit(1024)) pReader, pWriter := New(WithSizeLimit(1024))
b := buf.New() b := buf.New()
b.WriteString("abcd") b.WriteString("abcd")
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil) common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}))
b2 := buf.New() b2 := buf.New()
b2.WriteString("efg") b2.WriteString("efg")
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b2}), IsNil) common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b2}))
rb, err := pReader.ReadMultiBuffer() rb, err := pReader.ReadMultiBuffer()
assert(err, IsNil) common.Must(err)
assert(rb.String(), Equals, "abcdefg") if r := cmp.Diff(rb.String(), "abcdefg"); r != "" {
t.Error(r)
}
} }
func TestPipeInterrupt(t *testing.T) { func TestPipeInterrupt(t *testing.T) {
assert := With(t)
pReader, pWriter := New(WithSizeLimit(1024)) pReader, pWriter := New(WithSizeLimit(1024))
payload := []byte{'a', 'b', 'c', 'd'} payload := []byte{'a', 'b', 'c', 'd'}
b := buf.New() b := buf.New()
b.Write(payload) b.Write(payload)
assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil) common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}))
pWriter.Interrupt() pWriter.Interrupt()
rb, err := pReader.ReadMultiBuffer() rb, err := pReader.ReadMultiBuffer()
assert(err, Equals, io.ErrClosedPipe) if err != io.ErrClosedPipe {
assert(rb.IsEmpty(), IsTrue) t.Fatal("expect io.ErrClosePipe, but got ", err)
}
if !rb.IsEmpty() {
t.Fatal("expect empty buffer, but got ", rb.Len())
}
} }
func TestPipeClose(t *testing.T) { func TestPipeClose(t *testing.T) {