From 4468c60b9522c60d1eacb9845e8790cf86e22bd4 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sun, 6 Jan 2019 23:30:38 +0100 Subject: [PATCH] remove usage on assert lib --- app/dns/server_test.go | 113 +++++++++++++++----------- app/policy/manager_test.go | 26 ++++-- testing/scenarios/shadowsocks_test.go | 111 ++++++++++++++----------- transport/pipe/pipe_test.go | 26 +++--- 4 files changed, 160 insertions(+), 116 deletions(-) diff --git a/app/dns/server_test.go b/app/dns/server_test.go index 1654dfed0..0d548baec 100644 --- a/app/dns/server_test.go +++ b/app/dns/server_test.go @@ -1,10 +1,11 @@ package dns_test import ( - "runtime" "testing" "time" + "github.com/google/go-cmp/cmp" + "v2ray.com/core" "v2ray.com/core/app/dispatcher" . "v2ray.com/core/app/dns" @@ -17,7 +18,6 @@ import ( feature_dns "v2ray.com/core/features/dns" "v2ray.com/core/proxy/freedom" "v2ray.com/core/testing/servers/udp" - . "v2ray.com/ext/assert" "github.com/miekg/dns" ) @@ -67,11 +67,6 @@ func (*staticHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { } 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() dnsServer := dns.Server{ @@ -112,22 +107,21 @@ func TestUDPServerSubnet(t *testing.T) { } v, err := core.New(config) - assert(err, IsNil) + common.Must(err) client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client) ips, err := client.LookupIP("google.com") - assert(err, IsNil) - assert(len(ips), Equals, 1) - assert([]byte(ips[0]), Equals, []byte{8, 8, 4, 4}) + if err != nil { + t.Fatal("unexpected error: ", err) + } + + if r := cmp.Diff(ips, []net.IP{{8, 8, 4, 4}}); r != "" { + t.Fatal(r) + } } 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() dnsServer := dns.Server{ @@ -167,34 +161,47 @@ func TestUDPServer(t *testing.T) { } v, err := core.New(config) - assert(err, IsNil) + common.Must(err) client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client) - ips, err := client.LookupIP("google.com") - assert(err, IsNil) - assert(len(ips), Equals, 1) - assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8}) + { + ips, err := client.LookupIP("google.com") + if err != nil { + t.Fatal("unexpected error: ", err) + } - ips, err = client.LookupIP("facebook.com") - assert(err, IsNil) - assert(len(ips), Equals, 1) - assert([]byte(ips[0]), Equals, []byte{9, 9, 9, 9}) + if r := cmp.Diff(ips, []net.IP{{8, 8, 8, 8}}); r != "" { + t.Fatal(r) + } + } + + { + 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() - ips, err = client.LookupIP("google.com") - assert(err, IsNil) - assert(len(ips), Equals, 1) - assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8}) + { + ips, err := client.LookupIP("google.com") + if err != nil { + 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) { - if runtime.GOOS == "windows" { - t.Skip("doesn't work on Windows due to miekg/dns changes.") - } - assert := With(t) - port := udp.PickPort() dnsServer := dns.Server{ @@ -253,15 +260,22 @@ func TestPrioritizedDomain(t *testing.T) { } v, err := core.New(config) - assert(err, IsNil) + common.Must(err) client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client) startTime := time.Now() - ips, err := client.LookupIP("google.com") - assert(err, IsNil) - assert(len(ips), Equals, 1) - assert([]byte(ips[0]), Equals, []byte{8, 8, 8, 8}) + + { + ips, err := client.LookupIP("google.com") + 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() if startTime.After(endTime.Add(time.Second * 2)) { @@ -270,11 +284,6 @@ func TestPrioritizedDomain(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() dnsServer := dns.Server{ @@ -314,13 +323,19 @@ func TestUDPServerIPv6(t *testing.T) { } v, err := core.New(config) - assert(err, IsNil) + common.Must(err) client := v.GetFeature(feature_dns.ClientType()).(feature_dns.Client) client6 := client.(feature_dns.IPv6Lookup) - ips, err := client6.LookupIPv6("ipv6.google.com") - assert(err, IsNil) - assert(len(ips), Equals, 1) - assert([]byte(ips[0]), Equals, []byte{32, 1, 72, 96, 72, 96, 0, 0, 0, 0, 0, 0, 0, 0, 136, 136}) + { + ips, err := client6.LookupIPv6("ipv6.google.com") + if err != nil { + 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) + } + } } diff --git a/app/policy/manager_test.go b/app/policy/manager_test.go index f830aa813..8f2a61759 100644 --- a/app/policy/manager_test.go +++ b/app/policy/manager_test.go @@ -6,13 +6,11 @@ import ( "time" . "v2ray.com/core/app/policy" + "v2ray.com/core/common" "v2ray.com/core/features/policy" - . "v2ray.com/ext/assert" ) func TestPolicy(t *testing.T) { - assert := With(t) - manager, err := New(context.Background(), &Config{ Level: map[uint32]*Policy{ 0: { @@ -24,14 +22,24 @@ func TestPolicy(t *testing.T) { }, }, }) - assert(err, IsNil) + common.Must(err) pDefault := policy.SessionDefault() - p0 := manager.ForLevel(0) - assert(p0.Timeouts.Handshake, Equals, 2*time.Second) - assert(p0.Timeouts.ConnectionIdle, Equals, pDefault.Timeouts.ConnectionIdle) + { + p := manager.ForLevel(0) + 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) + } + } } diff --git a/testing/scenarios/shadowsocks_test.go b/testing/scenarios/shadowsocks_test.go index 9214e807c..207d02e71 100644 --- a/testing/scenarios/shadowsocks_test.go +++ b/testing/scenarios/shadowsocks_test.go @@ -6,12 +6,15 @@ import ( "testing" "time" - "v2ray.com/core/common" - "v2ray.com/core/common/compare" + "github.com/google/go-cmp/cmp" + "golang.org/x/sync/errgroup" "v2ray.com/core" "v2ray.com/core/app/log" "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" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" @@ -25,13 +28,11 @@ import ( ) func TestShadowsocksAES256TCP(t *testing.T) { - assert := With(t) - tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert(err, IsNil) + common.Must(err) defer tcpServer.Close() account := serial.ToTypedMessage(&shadowsocks.Account{ @@ -113,44 +114,50 @@ func TestShadowsocksAES256TCP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert(err, IsNil) + common.Must(err) + defer CloseAllServers(servers) - var wg sync.WaitGroup - wg.Add(10) + var errg errgroup.Group for i := 0; i < 10; i++ { - go func() { + errg.Go(func() error { conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert(err, IsNil) + if err != nil { + return err + } + defer conn.Close() payload := make([]byte, 10240*1024) - rand.Read(payload) + common.Must2(rand.Read(payload)) nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) + if err != nil { + return err + } + if nBytes != len(payload) { + return errors.New("expect ", len(payload), " written, but actually ", nBytes) + } response := readFrom(conn, time.Second*20, 10240*1024) - assert(response, Equals, xor([]byte(payload))) - assert(conn.Close(), IsNil) - wg.Done() - }() + if r := cmp.Diff(response, xor([]byte(payload))); r != "" { + return errors.New(r) + } + return nil + }) + } + if err := errg.Wait(); err != nil { + t.Fatal(err) } - wg.Wait() - - CloseAllServers(servers) } func TestShadowsocksAES128UDP(t *testing.T) { - assert := With(t) - udpServer := udp.Server{ MsgProcessor: xor, } dest, err := udpServer.Start() - assert(err, IsNil) + common.Must(err) defer udpServer.Close() account := serial.ToTypedMessage(&shadowsocks.Account{ @@ -232,34 +239,43 @@ func TestShadowsocksAES128UDP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert(err, IsNil) + common.Must(err) + defer CloseAllServers(servers) - var wg sync.WaitGroup - wg.Add(10) + var errg errgroup.Group for i := 0; i < 10; i++ { - go func() { + errg.Go(func() error { conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert(err, IsNil) + if err != nil { + return err + } + defer conn.Close() payload := make([]byte, 1024) - rand.Read(payload) + common.Must2(rand.Read(payload)) nBytes, err := conn.Write([]byte(payload)) - assert(err, IsNil) - assert(nBytes, Equals, len(payload)) + if err != nil { + return err + } + if nBytes != len(payload) { + return errors.New("expect ", len(payload), " written, but actually ", nBytes) + } response := readFrom(conn, time.Second*5, 1024) - assert(response, Equals, xor([]byte(payload))) - assert(conn.Close(), IsNil) - wg.Done() - }() + if r := cmp.Diff(response, xor(payload)); r != "" { + return errors.New(r) + } + return nil + }) } - wg.Wait() - CloseAllServers(servers) + if err := errg.Wait(); err != nil { + t.Fatal(err) + } } func TestShadowsocksChacha20TCP(t *testing.T) { @@ -938,12 +954,9 @@ func TestShadowsocksNone(t *testing.T) { defer CloseAllServers(servers) - var wg sync.WaitGroup - wg.Add(10) + var errg errgroup.Group for i := 0; i < 10; i++ { - go func() { - defer wg.Done() - + errg.Go(func() error { conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), @@ -958,14 +971,18 @@ func TestShadowsocksNone(t *testing.T) { common.Must(err) 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) - if err := compare.BytesEqualWithDetail(response, xor([]byte(payload))); err != nil { - t.Error(err) + if r := cmp.Diff(response, xor(payload)); r != "" { + return errors.New(r) } - }() + return nil + }) + } + + if err := errg.Wait(); err != nil { + t.Fatal(err) } - wg.Wait() } diff --git a/transport/pipe/pipe_test.go b/transport/pipe/pipe_test.go index 0960eca35..1087a637e 100644 --- a/transport/pipe/pipe_test.go +++ b/transport/pipe/pipe_test.go @@ -7,6 +7,8 @@ import ( "testing" "time" + "github.com/google/go-cmp/cmp" + "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/task" @@ -15,36 +17,38 @@ import ( ) func TestPipeReadWrite(t *testing.T) { - assert := With(t) - pReader, pWriter := New(WithSizeLimit(1024)) b := buf.New() b.WriteString("abcd") - assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil) + common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b})) b2 := buf.New() b2.WriteString("efg") - assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b2}), IsNil) + common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b2})) rb, err := pReader.ReadMultiBuffer() - assert(err, IsNil) - assert(rb.String(), Equals, "abcdefg") + common.Must(err) + if r := cmp.Diff(rb.String(), "abcdefg"); r != "" { + t.Error(r) + } } func TestPipeInterrupt(t *testing.T) { - assert := With(t) - pReader, pWriter := New(WithSizeLimit(1024)) payload := []byte{'a', 'b', 'c', 'd'} b := buf.New() b.Write(payload) - assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil) + common.Must(pWriter.WriteMultiBuffer(buf.MultiBuffer{b})) pWriter.Interrupt() rb, err := pReader.ReadMultiBuffer() - assert(err, Equals, io.ErrClosedPipe) - assert(rb.IsEmpty(), IsTrue) + if err != io.ErrClosedPipe { + 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) {