func TestInvalidNetworkJson(t *testing.T) { assert := assert.On(t) var list NetworkList err := json.Unmarshal([]byte("0"), &list) assert.Error(err).IsNotNil() }
func TestInvalidUserJson(t *testing.T) { assert := assert.On(t) user := new(User) err := json.Unmarshal([]byte(`{"email": 1234}`), user) assert.Error(err).IsNotNil() }
func TestRecivingWindow(t *testing.T) { assert := assert.On(t) window := NewReceivingWindow(3) seg0 := &DataSegment{} seg1 := &DataSegment{} seg2 := &DataSegment{} seg3 := &DataSegment{} assert.Bool(window.Set(0, seg0)).IsTrue() assert.Pointer(window.RemoveFirst()).Equals(seg0) e := window.RemoveFirst() if e != nil { assert.Fail("Expecting nil.") } assert.Bool(window.Set(1, seg1)).IsTrue() assert.Bool(window.Set(2, seg2)).IsTrue() window.Advance() assert.Bool(window.Set(2, seg3)).IsTrue() assert.Pointer(window.RemoveFirst()).Equals(seg1) assert.Pointer(window.Remove(1)).Equals(seg2) assert.Pointer(window.Remove(2)).Equals(seg3) }
func TestWrongAddressType(t *testing.T) { assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear().AppendBytes(5) _, err := ReadRequest(buffer, nil, false) assert.Error(err).Equals(transport.ErrCorruptedPacket) }
func TestInsufficientPortRequest(t *testing.T) { assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear().AppendBytes(1, 1, 2, 3, 4, 5) _, err := ReadRequest(buffer, nil, false) assert.Error(err).Equals(transport.ErrCorruptedPacket) }
func TestSingleBytePayload(t *testing.T) { assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear().AppendBytes(1) _, err := ReadRequest(buffer, nil, false) assert.Error(err).Equals(transport.ErrCorruptedPacket) }
func TestEmptyPayload(t *testing.T) { assert := assert.On(t) buffer := alloc.NewSmallBuffer().Clear() _, err := ReadRequest(buffer, nil, false) assert.Error(err).Equals(io.EOF) }
func TestReceiverUser(t *testing.T) { assert := assert.On(t) id := protocol.NewID(uuid.New()) alters := protocol.NewAlterIDs(id, 100) account := &protocol.VMessAccount{ ID: id, AlterIDs: alters, } user := protocol.NewUser(account, protocol.UserLevel(0), "") rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user) assert.Bool(rec.HasUser(user)).IsTrue() assert.Int(len(rec.Accounts)).Equals(1) id2 := protocol.NewID(uuid.New()) alters2 := protocol.NewAlterIDs(id2, 100) account2 := &protocol.VMessAccount{ ID: id2, AlterIDs: alters2, } user2 := protocol.NewUser(account2, protocol.UserLevel(0), "") assert.Bool(rec.HasUser(user2)).IsFalse() rec.AddUser(user2) assert.Bool(rec.HasUser(user2)).IsTrue() assert.Int(len(rec.Accounts)).Equals(2) }
func TestSendingQueue(t *testing.T) { assert := assert.On(t) queue := NewSendingQueue(3) seg0 := alloc.NewBuffer() seg1 := alloc.NewBuffer() seg2 := alloc.NewBuffer() seg3 := alloc.NewBuffer() assert.Bool(queue.IsEmpty()).IsTrue() assert.Bool(queue.IsFull()).IsFalse() queue.Push(seg0) assert.Bool(queue.IsEmpty()).IsFalse() queue.Push(seg1) queue.Push(seg2) assert.Bool(queue.IsFull()).IsTrue() assert.Pointer(queue.Pop()).Equals(seg0) queue.Push(seg3) assert.Bool(queue.IsFull()).IsTrue() assert.Pointer(queue.Pop()).Equals(seg1) assert.Pointer(queue.Pop()).Equals(seg2) assert.Pointer(queue.Pop()).Equals(seg3) assert.Int(int(queue.Len())).Equals(0) }
func TestHopByHopHeadersStrip(t *testing.T) { assert := assert.On(t) rawRequest := `GET /pkg/net/http/ HTTP/1.1 Host: golang.org Connection: keep-alive,Foo, Bar Foo: foo Bar: bar Proxy-Connection: keep-alive Proxy-Authenticate: abc User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10 Accept-Encoding: gzip Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7 Cache-Control: no-cache Accept-Language: de,en;q=0.7,en-us;q=0.3 ` b := bufio.NewReader(strings.NewReader(rawRequest)) req, err := http.ReadRequest(b) assert.Error(err).IsNil() assert.String(req.Header.Get("Foo")).Equals("foo") assert.String(req.Header.Get("Bar")).Equals("bar") assert.String(req.Header.Get("Connection")).Equals("keep-alive,Foo, Bar") assert.String(req.Header.Get("Proxy-Connection")).Equals("keep-alive") assert.String(req.Header.Get("Proxy-Authenticate")).Equals("abc") StripHopByHopHeaders(req) assert.String(req.Header.Get("Connection")).Equals("close") assert.String(req.Header.Get("Foo")).Equals("") assert.String(req.Header.Get("Bar")).Equals("") assert.String(req.Header.Get("Proxy-Connection")).Equals("") assert.String(req.Header.Get("Proxy-Authenticate")).Equals("") }
func TestBufferedReader(t *testing.T) { assert := assert.On(t) content := alloc.NewLargeBuffer() len := content.Len() reader := NewBufferedReader(content) assert.Bool(reader.Cached()).IsTrue() payload := make([]byte, 16) nBytes, err := reader.Read(payload) assert.Int(nBytes).Equals(16) assert.Error(err).IsNil() len2 := content.Len() assert.Int(len - len2).GreaterThan(16) nBytes, err = reader.Read(payload) assert.Int(nBytes).Equals(16) assert.Error(err).IsNil() assert.Int(content.Len()).Equals(len2) reader.SetCached(false) payload2 := alloc.NewBuffer() reader.Read(payload2.Value) assert.Int(content.Len()).Equals(len2) reader.Read(payload2.Value) assert.Int(content.Len()).LessThan(len2) }
func TestBuildAndRun(t *testing.T) { assert := assert.On(t) gopath := os.Getenv("GOPATH") goOS := parseOS(runtime.GOOS) goArch := parseArch(runtime.GOARCH) target := filepath.Join(gopath, "src", "v2ray_test") if goOS == Windows { target += ".exe" } err := buildV2Ray(target, "v1.0", goOS, goArch) assert.Error(err).IsNil() outBuffer := bytes.NewBuffer(make([]byte, 0, 1024)) errBuffer := bytes.NewBuffer(make([]byte, 0, 1024)) configFile := filepath.Join(gopath, "src", "github.com", "v2ray", "v2ray-core", "release", "config", "vpoint_socks_vmess.json") cmd := exec.Command(target, "--config="+configFile) cmd.Stdout = outBuffer cmd.Stderr = errBuffer cmd.Start() <-time.After(1 * time.Second) cmd.Process.Kill() outStr := string(outBuffer.Bytes()) errStr := string(errBuffer.Bytes()) assert.Bool(strings.Contains(outStr, "v1.0")).IsTrue() assert.String(errStr).Equals("") os.Remove(target) }
func TestResponseWrite(t *testing.T) { assert := assert.On(t) response := Socks5Response{ socksVersion, ErrorSuccess, AddrTypeIPv4, [4]byte{0x72, 0x72, 0x72, 0x72}, "", [16]byte{}, v2net.Port(53), } buffer := alloc.NewSmallBuffer().Clear() defer buffer.Release() response.Write(buffer) expectedBytes := []byte{ socksVersion, ErrorSuccess, byte(0x00), AddrTypeIPv4, 0x72, 0x72, 0x72, 0x72, byte(0x00), byte(0x035), } assert.Bytes(buffer.Value).Equals(expectedBytes) }
func TestBadSegment(t *testing.T) { assert := assert.On(t) seg, buf := ReadSegment(nil) assert.Pointer(seg).IsNil() assert.Int(len(buf)).Equals(0) }
func TestSwitchAccount(t *testing.T) { assert := assert.On(t) sa := &protocol.CommandSwitchAccount{ Port: 1234, ID: uuid.New(), AlterIds: 1024, Level: 128, ValidMin: 16, } buffer := alloc.NewBuffer().Clear() err := MarshalCommand(sa, buffer) assert.Error(err).IsNil() cmd, err := UnmarshalCommand(1, buffer.Value[2:]) assert.Error(err).IsNil() sa2, ok := cmd.(*protocol.CommandSwitchAccount) assert.Bool(ok).IsTrue() assert.Pointer(sa.Host).IsNil() assert.Pointer(sa2.Host).IsNil() assert.Port(sa.Port).Equals(sa2.Port) assert.String(sa.ID.String()).Equals(sa2.ID.String()) assert.Uint16(sa.AlterIds).Equals(sa2.AlterIds) assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) }
func TestIPResolution(t *testing.T) { assert := assert.On(t) space := app.NewSpace() space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager()) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) r, _ := router.CreateRouter("rules", &rules.RouterRuleConfig{}, space) space.BindApp(router.APP_ID, r) dnsServer := dns.NewCacheServer(space, &dns.Config{ Hosts: map[string]net.IP{ "v2ray.com": net.IP([]byte{127, 0, 0, 1}), }, }) space.BindApp(dns.APP_ID, dnsServer) freedom := NewFreedomConnection( &Config{DomainStrategy: DomainStrategyUseIP}, space, &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamSettings{ Type: internet.StreamConnectionTypeRawTCP, }, }) space.Initialize() ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80))) assert.Destination(ipDest).IsTCP() assert.Address(ipDest.Address()).Equals(v2net.LocalHostIP) }
func TestACKSegment(t *testing.T) { assert := assert.On(t) seg := &AckSegment{ Conv: 1, ReceivingWindow: 2, ReceivingNext: 3, Count: 5, NumberList: []uint32{1, 3, 5, 7, 9}, TimestampList: []uint32{2, 4, 6, 8, 10}, } nBytes := seg.ByteSize() bytes := seg.Bytes(nil) assert.Int(len(bytes)).Equals(nBytes) iseg, _ := ReadSegment(bytes) seg2 := iseg.(*AckSegment) assert.Uint16(seg2.Conv).Equals(seg.Conv) assert.Uint32(seg2.ReceivingWindow).Equals(seg.ReceivingWindow) assert.Uint32(seg2.ReceivingNext).Equals(seg.ReceivingNext) assert.Byte(seg2.Count).Equals(seg.Count) for i := byte(0); i < seg2.Count; i++ { assert.Uint32(seg2.TimestampList[i]).Equals(seg.TimestampList[i]) assert.Uint32(seg2.NumberList[i]).Equals(seg.NumberList[i]) } }
func TestInvalidIdJson(t *testing.T) { assert := assert.On(t) user := new(User) err := json.Unmarshal([]byte(`{"id": "1234"}`), user) assert.Error(err).IsNotNil() }
func TestNext(t *testing.T) { assert := assert.On(t) uuid := New() uuid2 := uuid.Next() assert.Bool(uuid.Equals(uuid2)).IsFalse() }
func TestWrongProtocolVersion(t *testing.T) { assert := assert.On(t) buffer := alloc.NewBuffer().Clear().AppendBytes(6, 1, 0) _, _, err := ReadAuthentication(buffer) assert.Error(err).Equals(proxy.ErrorInvalidProtocolVersion) }
func TestZeroAuthenticationMethod(t *testing.T) { assert := assert.On(t) buffer := alloc.NewBuffer().Clear().AppendBytes(5, 0) _, _, err := ReadAuthentication(buffer) assert.Error(err).Equals(proxy.ErrorInvalidAuthentication) }
func TestNormalGetRequest(t *testing.T) { assert := assert.On(t) testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil) port := v2nettesting.PickPort() httpProxy := NewServer( &Config{}, testPacketDispatcher, &proxy.InboundHandlerMeta{ Address: v2net.LocalHostIP, Port: port, StreamSettings: &internet.StreamSettings{ Type: internet.StreamConnectionTypeRawTCP, }}) defer httpProxy.Close() err := httpProxy.Start() assert.Error(err).IsNil() assert.Port(port).Equals(httpProxy.Port()) httpClient := &http.Client{} resp, err := httpClient.Get("http://127.0.0.1:" + port.String() + "/") assert.Error(err).IsNil() assert.Int(resp.StatusCode).Equals(400) }
func TestParseOS(t *testing.T) { assert := assert.On(t) assert.Pointer(parseOS("windows")).Equals(Windows) assert.Pointer(parseOS("macos")).Equals(MacOS) assert.Pointer(parseOS("linux")).Equals(Linux) assert.Pointer(parseOS("test")).Equals(UnknownOS) }
func TestTCPDestination(t *testing.T) { assert := assert.On(t) dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) assert.Destination(dest).IsTCP() assert.Destination(dest).IsNotUDP() assert.Destination(dest).EqualsString("tcp:1.2.3.4:80") }
func TestUDPDestination(t *testing.T) { assert := assert.On(t) dest := v2net.UDPDestination(v2net.IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}), 53) assert.Destination(dest).IsNotTCP() assert.Destination(dest).IsUDP() assert.Destination(dest).EqualsString("udp:[2001:4860:4860::8888]:53") }
func TestStringListUnmarshalError(t *testing.T) { assert := assert.On(t) rawJson := `1234` list := new(StringList) err := json.Unmarshal([]byte(rawJson), list) assert.Error(err).IsNotNil() }
func TestAlwaysValidStrategy(t *testing.T) { assert := assert.On(t) strategy := AlwaysValid() assert.Bool(strategy.IsValid()).IsTrue() strategy.Invalidate() assert.Bool(strategy.IsValid()).IsTrue() }
func TestNetIPv4Address(t *testing.T) { assert := assert.On(t) ip := net.IPv4(1, 2, 3, 4) addr := v2net.IPAddress(ip) assert.Address(addr).IsIPv4() assert.Address(addr).EqualsString("1.2.3.4") }
func TestInvalidAddressJson(t *testing.T) { assert := assert.On(t) rawJson := "1234" var address AddressJson err := json.Unmarshal([]byte(rawJson), &address) assert.Error(err).IsNotNil() }
func TestEquals(t *testing.T) { assert := assert.On(t) var uuid *UUID = nil var uuid2 *UUID = nil assert.Bool(uuid.Equals(uuid2)).IsTrue() assert.Bool(uuid.Equals(New())).IsFalse() }