Browse Source

Merge pull request #2043 from vcptr/testbuf

more test code
Kslr 6 years ago
parent
commit
7313089539

+ 7 - 13
app/dns/server.go

@@ -53,10 +53,7 @@ func (c *MultiGeoIPMatcher) Match(ip net.IP) bool {
 
 
 // HasMatcher check has matcher
 // HasMatcher check has matcher
 func (c *MultiGeoIPMatcher) HasMatcher() bool {
 func (c *MultiGeoIPMatcher) HasMatcher() bool {
-	if len(c.matchers) > 0 {
-		return true
-	}
-	return false
+	return len(c.matchers) > 0
 }
 }
 
 
 func generateRandomTag() string {
 func generateRandomTag() string {
@@ -109,18 +106,16 @@ func New(ctx context.Context, config *Config) (*Server, error) {
 
 
 	if len(config.NameServers) > 0 {
 	if len(config.NameServers) > 0 {
 		features.PrintDeprecatedFeatureWarning("simple DNS server")
 		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 {
 	if len(config.NameServer) > 0 {
 		domainMatcher := &strmatcher.MatcherGroup{}
 		domainMatcher := &strmatcher.MatcherGroup{}
 		domainIndexMap := make(map[uint32]uint32)
 		domainIndexMap := make(map[uint32]uint32)
 		ipIndexMap := make(map[uint32]*MultiGeoIPMatcher)
 		ipIndexMap := make(map[uint32]*MultiGeoIPMatcher)
+		var geoIPMatcherContainer router.GeoIPMatcherContainer
 
 
 		for _, ns := range config.NameServer {
 		for _, ns := range config.NameServer {
 			idx := addNameServer(ns.Address)
 			idx := addNameServer(ns.Address)
@@ -141,7 +136,6 @@ func New(ctx context.Context, config *Config) (*Server, error) {
 					return nil, newError("failed to create ip matcher").Base(err).AtWarning()
 					return nil, newError("failed to create ip matcher").Base(err).AtWarning()
 				}
 				}
 				matchers = append(matchers, matcher)
 				matchers = append(matchers, matcher)
-
 			}
 			}
 			matcher := &MultiGeoIPMatcher{matchers: matchers}
 			matcher := &MultiGeoIPMatcher{matchers: matchers}
 			ipIndexMap[uint32(idx)] = matcher
 			ipIndexMap[uint32(idx)] = matcher
@@ -182,12 +176,12 @@ func (s *Server) IsOwnLink(ctx context.Context) bool {
 // Match check dns ip match geoip
 // Match check dns ip match geoip
 func (s *Server) Match(idx uint32, client Client, domain string, ips []net.IP) ([]net.IP, error) {
 func (s *Server) Match(idx uint32, client Client, domain string, ips []net.IP) ([]net.IP, error) {
 	matcher, exist := s.ipIndexMap[idx]
 	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()
 		newError("domain ", domain, " server not in ipIndexMap: ", client.Name(), " idx:", idx, " just return").AtDebug().WriteToLog()
 		return ips, nil
 		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()
 		newError("domain ", domain, " server has not valid matcher: ", client.Name(), " idx:", idx, " just return").AtDebug().WriteToLog()
 		return ips, nil
 		return ips, nil
 	}
 	}

+ 0 - 11
app/dns/server_test.go

@@ -444,17 +444,6 @@ func TestIPMatch(t *testing.T) {
 	config := &core.Config{
 	config := &core.Config{
 		App: []*serial.TypedMessage{
 		App: []*serial.TypedMessage{
 			serial.ToTypedMessage(&Config{
 			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{
 				NameServer: []*NameServer{
 					// private dns, not match
 					// private dns, not match
 					{
 					{

+ 56 - 0
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) {
 func TestBufferSlice(t *testing.T) {
 	{
 	{
 		b := New()
 		b := New()

+ 39 - 0
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.....................:~=,,,,,,,..

+ 61 - 0
common/buf/multi_buffer_test.go

@@ -1,6 +1,7 @@
 package buf_test
 package buf_test
 
 
 import (
 import (
+	"bytes"
 	"crypto/rand"
 	"crypto/rand"
 	"io"
 	"io"
 	"testing"
 	"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) {
 func BenchmarkSplitBytes(b *testing.B) {
 	var mb MultiBuffer
 	var mb MultiBuffer
 	raw := make([]byte, Size)
 	raw := make([]byte, Size)

+ 68 - 0
common/buf/reader_test.go

@@ -1,10 +1,14 @@
 package buf_test
 package buf_test
 
 
 import (
 import (
+	"bytes"
 	"io"
 	"io"
+	"io/ioutil"
+	"os"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 
 
+	"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/transport/pipe"
 	"v2ray.com/core/transport/pipe"
@@ -65,6 +69,9 @@ func TestReadByte(t *testing.T) {
 	if b != 'a' {
 	if b != 'a' {
 		t.Error("unexpected byte: ", b, " want 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)
 	nBytes, err := reader.WriteTo(DiscardBytes)
 	common.Must(err)
 	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) {
 func TestReaderInterface(t *testing.T) {
 	_ = (io.Reader)(new(ReadVReader))
 	_ = (io.Reader)(new(ReadVReader))
 	_ = (Reader)(new(ReadVReader))
 	_ = (Reader)(new(ReadVReader))