mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 23:06:30 -05:00
more test to common/buf
This commit is contained in:
parent
ec9cf059e8
commit
905ac82bb1
@ -46,6 +46,62 @@ func TestBufferString(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufferByte(t *testing.T) {
|
||||
{
|
||||
buffer := New()
|
||||
common.Must(buffer.WriteByte('m'))
|
||||
if buffer.String() != "m" {
|
||||
t.Error("expect buffer content as ", "m", " but actually ", buffer.String())
|
||||
}
|
||||
buffer.Release()
|
||||
}
|
||||
{
|
||||
buffer := StackNew()
|
||||
common.Must(buffer.WriteByte('n'))
|
||||
if buffer.String() != "n" {
|
||||
t.Error("expect buffer content as ", "n", " but actually ", buffer.String())
|
||||
}
|
||||
buffer.Release()
|
||||
}
|
||||
{
|
||||
buffer := StackNew()
|
||||
common.Must2(buffer.WriteString("HELLOWORLD"))
|
||||
if b := buffer.Byte(5); b != 'W' {
|
||||
t.Error("unexpected byte ", b)
|
||||
}
|
||||
|
||||
buffer.SetByte(5, 'M')
|
||||
if buffer.String() != "HELLOMORLD" {
|
||||
t.Error("expect buffer content as ", "n", " but actually ", buffer.String())
|
||||
}
|
||||
buffer.Release()
|
||||
}
|
||||
}
|
||||
func TestBufferResize(t *testing.T) {
|
||||
buffer := New()
|
||||
defer buffer.Release()
|
||||
|
||||
const payload = "Test String"
|
||||
common.Must2(buffer.WriteString(payload))
|
||||
if buffer.String() != payload {
|
||||
t.Error("expect buffer content as ", payload, " but actually ", buffer.String())
|
||||
}
|
||||
|
||||
buffer.Resize(-6, -3)
|
||||
if l := buffer.Len(); int(l) != 3 {
|
||||
t.Error("len error ", l)
|
||||
}
|
||||
|
||||
if s := buffer.String(); s != "Str" {
|
||||
t.Error("unexpect buffer ", s)
|
||||
}
|
||||
|
||||
buffer.Resize(int32(len(payload)), 200)
|
||||
if l := buffer.Len(); int(l) != 200-len(payload) {
|
||||
t.Error("len error ", l)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBufferSlice(t *testing.T) {
|
||||
{
|
||||
b := New()
|
||||
|
39
common/buf/data/test_ReadBuffer.dat
Normal file
39
common/buf/data/test_ReadBuffer.dat
Normal file
@ -0,0 +1,39 @@
|
||||
|
||||
???8+?$??I????+??????+?+++?IO7$ZD88ZDMD8OZ$7II7+++++++++++++
|
||||
+??7++???I????????+?+++?+I?IZI$OND7ODDDDDD7Z$IZI++++++++++++
|
||||
???I????????????~,...~?++I?777$DD8O8DDD88O$O7$7I++++++++++?+
|
||||
???????????????.,::~...,+?I77ZZD8ZDNDDDDD8ZZ7$$7+++++++?+?+?
|
||||
??????????????.,,:~~~==,I?7$$ZOD8ODNDD8DDZ$87777++?+++?????+
|
||||
?????????I?=...:~~~~=~=+I?$$ZODD88ND8N8DDOZOZ77?????++??????
|
||||
???II?????.,,,:==~~===I?IIZ$O$88ODD8ODNDDDOO$7$??I?++?++++??
|
||||
???I????+..,,~=+???+?????7OOZZ8O$$778DDDDDO87I$I++++++++????
|
||||
I??????..,,:~=??????+=,~?ZZZ$$I??II$DDDDD8Z8I~,+=?II$777IIII
|
||||
II???,.,,::~??I?I?....,,~==I?+===+?$ODN8DD$O=,......+?????II
|
||||
I?I?..,,:~~????,...,,::::~~~~~~~~=+$88ODD88=~,,,.......IIIII
|
||||
II,..,,:~~I?:..,,,::::~~~~~~~~~~~~~+IOZ87?~~~::::,,,,...=?II
|
||||
I,...,:::....,:::::::~~~~~~~~~~~~~~~=++=~~~~~~~~~~~:::,,,?II
|
||||
,,,,~....,,,::::::::::::::~~~~~~~~~~~~~~~~~~~~~~~~~~~::,,,??
|
||||
:~:...,,,:::::::::::::::::::~~~~~~~~~~~~~~~~~~~~~~~~~~::,,II
|
||||
:::::::::::::::::::~+++::::::~~~~~~~~~~~~~~~~~~~~~~~~::::,,7
|
||||
::::::::::::::~IIII?????:::::::~~~~~~~~~~~~~~~~~~~~~::::::,I
|
||||
:,,,,,,,:+ZIIIIIIIIIIIII:::~::~~~~~~~~~~~~~~~~~~~~=~::::::::
|
||||
7I777IIZI7ZIIIIIIIIIIII7?:~~~~~~~~~~~~~~~~~~~~~~~~~=~:::::::
|
||||
$$$77$7Z77$7I77IIII7III$$:~~~~~~~~~~~~~~~~~~~~~~~=II~:::::::
|
||||
$$$8$Z7$7$Z777777777777Z7~:~~~~~~~~~~~~~~~~~~~~~~$777::::::,
|
||||
ZOZOZOZZ$7$$ZZ$8DDDZ777$$=~~~~~~~~~~~~~~~~~~~~~~~$$$7~:::::,
|
||||
OOZOOOZZOOZO$ZZZ$O$$$$7ZZ$~~~~~~~~~~~~~~~~~~~~~~~ZZ$ZZ:::::,
|
||||
O88OOOOO8ODOZZZZZOOZ8OOOOO:~~~~~~~~~~~~~~~~~~~~~ZOZZZZ~:::::
|
||||
8888O8OODZ8ZOZOZZOOZOOOOOZ:::~~~~~~~~~~~~~~~~~~~,Z$ZOOO:::::
|
||||
Z88O88D8Z88ZZOOZZOZ$$Z$$OZ:::~~~~~~~~~~~~~~~~~~~,,ZOOOOO::::
|
||||
888D88OODD8DNDNDNNDDDD88OI:::::~~~~~~~~~~~~~~~~~.,:8ZO8O::::
|
||||
D8D88DO88ZOOZOO8DDDNOZ$$O8~::::~~~~~~~~~~~~~===~..,88O8OO:::
|
||||
8OD8O8OODO$D8DO88DO8O8888O~~::~~::~~~~~~~~~~~===...:8OOOZ~::
|
||||
:..................,~,..~,~~:~:~~~~~~~~~~~~~~===...,+.....~~
|
||||
.........................~~~:~~~~~~~~~~~~~~~~~==:..,......:~
|
||||
.Made with love.........,~~~~~~~~:~~~~::~~~~~~~==..,,......:
|
||||
........................~~~~~~~~~~~~~~:~~~~~~~~===,.,......~
|
||||
...................,,..~~~~~~~~~~~~~~~~~~~~~~~~~==~,,.......
|
||||
..................,,::~~~~~~~~~~~~~~~~~~~~~~~~~====~.,....,.
|
||||
....................:~~~~~~~~~~~~~~~~~~~~~~~~~~~~==~:......,
|
||||
......................,~================,.==~~~=~===~,......
|
||||
.Thank you for your support.....................:~=,,,,,,,..
|
@ -1,6 +1,7 @@
|
||||
package buf_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"testing"
|
||||
@ -96,6 +97,66 @@ 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)
|
||||
|
||||
if l := len(b); l != 8*1024 {
|
||||
t.Error("unexpceted length from ReadAllToBytes", l)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiBufferCopy(t *testing.T) {
|
||||
lb := make([]byte, 8*1024)
|
||||
common.Must2(io.ReadFull(rand.Reader, lb))
|
||||
reader := bytes.NewBuffer(lb)
|
||||
|
||||
mb, err := ReadFrom(reader)
|
||||
common.Must(err)
|
||||
|
||||
lbdst := make([]byte, 8*1024)
|
||||
mb.Copy(lbdst)
|
||||
|
||||
if d := cmp.Diff(lb, lbdst); d != "" {
|
||||
t.Error("unexpceted different from MultiBufferCopy ", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplitFirstBytes(t *testing.T) {
|
||||
a := New()
|
||||
common.Must2(a.WriteString("ab"))
|
||||
b := New()
|
||||
common.Must2(b.WriteString("bc"))
|
||||
|
||||
mb := MultiBuffer{a, b}
|
||||
|
||||
o := make([]byte, 2)
|
||||
_, cnt := SplitFirstBytes(mb, o)
|
||||
if cnt != 2 {
|
||||
t.Error("unexpected cnt from SplitFirstBytes ", cnt)
|
||||
}
|
||||
if d := cmp.Diff(string(o), "ab"); d != "" {
|
||||
t.Error("unexpected splited result from SplitFirstBytes ", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompact(t *testing.T) {
|
||||
a := New()
|
||||
common.Must2(a.WriteString("ab"))
|
||||
b := New()
|
||||
common.Must2(b.WriteString("bc"))
|
||||
|
||||
mb := MultiBuffer{a, b}
|
||||
cmb := Compact(mb)
|
||||
|
||||
if w := cmb.String(); w != "abbc" {
|
||||
t.Error("unexpected Compact result ", w)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSplitBytes(b *testing.B) {
|
||||
var mb MultiBuffer
|
||||
raw := make([]byte, Size)
|
||||
|
@ -1,10 +1,14 @@
|
||||
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"
|
||||
@ -65,6 +69,9 @@ func TestReadByte(t *testing.T) {
|
||||
if b != 'a' {
|
||||
t.Error("unexpected byte: ", b, " want a")
|
||||
}
|
||||
if reader.BufferedBytes() != 3 { // 3 bytes left in buffer
|
||||
t.Error("unexpected buffered Bytes: ", reader.BufferedBytes())
|
||||
}
|
||||
|
||||
nBytes, err := reader.WriteTo(DiscardBytes)
|
||||
common.Must(err)
|
||||
@ -73,6 +80,67 @@ func TestReadByte(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadBuffer(t *testing.T) {
|
||||
{
|
||||
sr := strings.NewReader("abcd")
|
||||
buf, err := ReadBuffer(sr)
|
||||
common.Must(err)
|
||||
|
||||
if s := buf.String(); s != "abcd" {
|
||||
t.Error("unexpected str: ", s, " want abcd")
|
||||
}
|
||||
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) {
|
||||
sr := strings.NewReader("abcd")
|
||||
reader := &BufferedReader{
|
||||
Reader: NewReader(sr),
|
||||
}
|
||||
|
||||
mb, err := reader.ReadAtMost(3)
|
||||
common.Must(err)
|
||||
if s := mb.String(); s != "abc" {
|
||||
t.Error("unexpected read result: ", s)
|
||||
}
|
||||
|
||||
nBytes, err := reader.WriteTo(DiscardBytes)
|
||||
common.Must(err)
|
||||
if nBytes != 1 {
|
||||
t.Error("unexpect bytes written: ", nBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPacketReader_ReadMultiBuffer(t *testing.T) {
|
||||
const alpha = "abcefg"
|
||||
buf := bytes.NewBufferString(alpha)
|
||||
reader := &PacketReader{buf}
|
||||
mb, err := reader.ReadMultiBuffer()
|
||||
common.Must(err)
|
||||
if s := mb.String(); s != alpha {
|
||||
t.Error("content: ", s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReaderInterface(t *testing.T) {
|
||||
_ = (io.Reader)(new(ReadVReader))
|
||||
_ = (Reader)(new(ReadVReader))
|
||||
|
Loading…
Reference in New Issue
Block a user