From c060f9f11fa9eadd16e3750b73e87c223c422875 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Wed, 27 Nov 2019 09:12:02 +0800 Subject: [PATCH] fix test read buf --- ....dat => test_MultiBufferReadAllToByte.dat} | 0 common/buf/multi_buffer_test.go | 35 +++++++++++++++---- common/buf/reader_test.go | 20 ----------- 3 files changed, 28 insertions(+), 27 deletions(-) rename common/buf/data/{test_ReadBuffer.dat => test_MultiBufferReadAllToByte.dat} (100%) diff --git a/common/buf/data/test_ReadBuffer.dat b/common/buf/data/test_MultiBufferReadAllToByte.dat similarity index 100% rename from common/buf/data/test_ReadBuffer.dat rename to common/buf/data/test_MultiBufferReadAllToByte.dat diff --git a/common/buf/multi_buffer_test.go b/common/buf/multi_buffer_test.go index 6d838704..add7a1e1 100644 --- a/common/buf/multi_buffer_test.go +++ b/common/buf/multi_buffer_test.go @@ -7,6 +7,8 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "io/ioutil" + "os" "v2ray.com/core/common" . "v2ray.com/core/common/buf" @@ -98,14 +100,33 @@ func TestMultiBufferSplitFirst(t *testing.T) { } func TestMultiBufferReadAllToByte(t *testing.T) { - lb := make([]byte, 8*1024) - common.Must2(io.ReadFull(rand.Reader, lb)) - rd := bytes.NewBuffer(lb) - b, err := ReadAllToBytes(rd) - common.Must(err) + { + lb := make([]byte, 8*1024) + common.Must2(io.ReadFull(rand.Reader, lb)) + rd := bytes.NewBuffer(lb) + b, err := ReadAllToBytes(rd) + common.Must(err) - if l := len(b); l != 8*1024 { - t.Error("unexpceted length from ReadAllToBytes", l) + if l := len(b); l != 8*1024 { + t.Error("unexpceted length from ReadAllToBytes", l) + } + + } + { + const dat = "data/test_MultiBufferReadAllToByte.dat" + f, err := os.Open(dat) + common.Must(err) + + buf2, err := ReadAllToBytes(f) + common.Must(err) + f.Close() + + cnt, err := ioutil.ReadFile(dat) + common.Must(err) + + if d := cmp.Diff(buf2, cnt); d != "" { + t.Error("fail to read from file: ", d) + } } } diff --git a/common/buf/reader_test.go b/common/buf/reader_test.go index 74e4fd54..95622eef 100644 --- a/common/buf/reader_test.go +++ b/common/buf/reader_test.go @@ -3,12 +3,9 @@ package buf_test import ( "bytes" "io" - "io/ioutil" - "os" "strings" "testing" - "github.com/google/go-cmp/cmp" "v2ray.com/core/common" . "v2ray.com/core/common/buf" "v2ray.com/core/transport/pipe" @@ -92,23 +89,6 @@ func TestReadBuffer(t *testing.T) { buf.Release() } - { - const dat = "data/test_ReadBuffer.dat" - f, err := os.Open(dat) - common.Must(err) - defer f.Close() - - buf2, err := ReadBuffer(f) - common.Must(err) - - cnt, err := ioutil.ReadFile(dat) - common.Must(err) - - if cmp.Diff(buf2.Bytes(), cnt) != "" { - t.Error("fail to read from file") - } - buf2.Release() - } } func TestReadAtMost(t *testing.T) {