From 905ac82bb14e7e6b8ae77a3d5af42a7392648df9 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Fri, 22 Nov 2019 16:26:59 +0800 Subject: [PATCH 1/3] more test to common/buf --- common/buf/buffer_test.go | 56 ++++++++++++++++++++++++ common/buf/data/test_ReadBuffer.dat | 39 +++++++++++++++++ common/buf/multi_buffer_test.go | 61 ++++++++++++++++++++++++++ common/buf/reader_test.go | 68 +++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 common/buf/data/test_ReadBuffer.dat diff --git a/common/buf/buffer_test.go b/common/buf/buffer_test.go index 61a25930..6231e449 100644 --- a/common/buf/buffer_test.go +++ b/common/buf/buffer_test.go @@ -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() diff --git a/common/buf/data/test_ReadBuffer.dat b/common/buf/data/test_ReadBuffer.dat new file mode 100644 index 00000000..83cb9032 --- /dev/null +++ b/common/buf/data/test_ReadBuffer.dat @@ -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.....................:~=,,,,,,,.. diff --git a/common/buf/multi_buffer_test.go b/common/buf/multi_buffer_test.go index 4e6923e7..6d838704 100644 --- a/common/buf/multi_buffer_test.go +++ b/common/buf/multi_buffer_test.go @@ -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) diff --git a/common/buf/reader_test.go b/common/buf/reader_test.go index 0f66c03a..74e4fd54 100644 --- a/common/buf/reader_test.go +++ b/common/buf/reader_test.go @@ -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)) From fa30713e76779e296ff0f5c23751c7cc300bff43 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Fri, 22 Nov 2019 17:37:03 +0800 Subject: [PATCH 2/3] minor changes to dns from pr #2037 --- app/dns/server.go | 15 ++++++--------- app/dns/server_test.go | 11 ----------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/app/dns/server.go b/app/dns/server.go index ca2d2314..86fbcae4 100644 --- a/app/dns/server.go +++ b/app/dns/server.go @@ -109,18 +109,16 @@ func New(ctx context.Context, config *Config) (*Server, error) { if len(config.NameServers) > 0 { features.PrintDeprecatedFeatureWarning("simple DNS server") + for _, destPB := range config.NameServers { + addNameServer(destPB) + } } - for _, destPB := range config.NameServers { - addNameServer(destPB) - } - - var geoIPMatcherContainer router.GeoIPMatcherContainer - if len(config.NameServer) > 0 { domainMatcher := &strmatcher.MatcherGroup{} domainIndexMap := make(map[uint32]uint32) ipIndexMap := make(map[uint32]*MultiGeoIPMatcher) + var geoIPMatcherContainer router.GeoIPMatcherContainer for _, ns := range config.NameServer { idx := addNameServer(ns.Address) @@ -141,7 +139,6 @@ func New(ctx context.Context, config *Config) (*Server, error) { return nil, newError("failed to create ip matcher").Base(err).AtWarning() } matchers = append(matchers, matcher) - } matcher := &MultiGeoIPMatcher{matchers: matchers} ipIndexMap[uint32(idx)] = matcher @@ -182,12 +179,12 @@ func (s *Server) IsOwnLink(ctx context.Context) bool { // Match check dns ip match geoip func (s *Server) Match(idx uint32, client Client, domain string, ips []net.IP) ([]net.IP, error) { matcher, exist := s.ipIndexMap[idx] - if exist == false { + if !exist { newError("domain ", domain, " server not in ipIndexMap: ", client.Name(), " idx:", idx, " just return").AtDebug().WriteToLog() return ips, nil } - if matcher.HasMatcher() == false { + if !matcher.HasMatcher() { newError("domain ", domain, " server has not valid matcher: ", client.Name(), " idx:", idx, " just return").AtDebug().WriteToLog() return ips, nil } diff --git a/app/dns/server_test.go b/app/dns/server_test.go index 5c98e5f0..05617411 100644 --- a/app/dns/server_test.go +++ b/app/dns/server_test.go @@ -444,17 +444,6 @@ func TestIPMatch(t *testing.T) { config := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&Config{ - NameServers: []*net.Endpoint{ - { - Network: net.Network_UDP, - Address: &net.IPOrDomain{ - Address: &net.IPOrDomain_Ip{ - Ip: []byte{127, 0, 0, 1}, - }, - }, - Port: 9999, /* unreachable */ - }, - }, NameServer: []*NameServer{ // private dns, not match { From 3b9514ec9f22b1e79c2d98aa0fda353c2418e915 Mon Sep 17 00:00:00 2001 From: vcptr <51714622+vcptr@users.noreply.github.com> Date: Fri, 22 Nov 2019 17:57:16 +0800 Subject: [PATCH 3/3] minor optm --- app/dns/server.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/dns/server.go b/app/dns/server.go index 86fbcae4..c1c85e93 100644 --- a/app/dns/server.go +++ b/app/dns/server.go @@ -53,10 +53,7 @@ func (c *MultiGeoIPMatcher) Match(ip net.IP) bool { // HasMatcher check has matcher func (c *MultiGeoIPMatcher) HasMatcher() bool { - if len(c.matchers) > 0 { - return true - } - return false + return len(c.matchers) > 0 } func generateRandomTag() string {