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 TestChinaSitesJson(t *testing.T) { assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "chinasites", "outboundTag": "y" }`)) assert.String(rule.Tag).Equals("y") cond, err := rule.BuildCondition() assert.Error(err).IsNil() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("v.qq.com"), 80), })).IsTrue() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("www.163.com"), 80), })).IsTrue() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("ngacn.cc"), 80), })).IsTrue() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("12306.cn"), 80), })).IsTrue() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("v2ray.com"), 80), })).IsFalse() }
func TestDomainRule(t *testing.T) { assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "field", "domain": [ "ooxx.com", "oxox.com", "regexp:\\.cn$" ], "network": "tcp", "outboundTag": "direct" }`)) assert.Pointer(rule).IsNotNil() cond, err := rule.BuildCondition() assert.Error(err).IsNil() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("www.ooxx.com"), 80), })).IsTrue() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("www.aabb.com"), 80), })).IsFalse() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80), })).IsFalse() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("www.12306.cn"), 80), })).IsTrue() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("www.acn.com"), 80), })).IsFalse() }
func TestChinaIPJson(t *testing.T) { assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "chinaip", "outboundTag": "x" }`)) assert.String(rule.Tag).Equals("x") cond, err := rule.BuildCondition() assert.Error(err).IsNil() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("121.14.1.189"), 80), })).IsTrue() // sina.com.cn assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("101.226.103.106"), 80), })).IsTrue() // qq.com assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("115.239.210.36"), 80), })).IsTrue() // image.baidu.com assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("120.135.126.1"), 80), })).IsTrue() assert.Bool(cond.Apply(&proxy.SessionInfo{ Destination: v2net.TCPDestination(v2net.ParseAddress("8.8.8.8"), 80), })).IsFalse() }
func TestSourceIPRule(t *testing.T) { assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "field", "source": [ "10.0.0.0/8", "192.0.0.0/24" ], "outboundTag": "direct" }`)) assert.Pointer(rule).IsNotNil() cond, err := rule.BuildCondition() assert.Error(err).IsNil() assert.Bool(cond.Apply(&proxy.SessionInfo{ Source: v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80), })).IsFalse() assert.Bool(cond.Apply(&proxy.SessionInfo{ Source: v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80), })).IsTrue() assert.Bool(cond.Apply(&proxy.SessionInfo{ Source: v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80), })).IsFalse() assert.Bool(cond.Apply(&proxy.SessionInfo{ Source: v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80), })).IsTrue() }
func TestSocksInboundConfig(t *testing.T) { assert := assert.On(t) rawJson := `{ "auth": "password", "accounts": [ { "user": "******", "pass": "******" } ], "udp": false, "ip": "127.0.0.1", "timeout": 5 }` config := new(SocksServerConfig) err := json.Unmarshal([]byte(rawJson), &config) assert.Error(err).IsNil() message, err := config.Build() assert.Error(err).IsNil() iConfig, err := message.GetInstance() assert.Error(err).IsNil() socksConfig := iConfig.(*socks.ServerConfig) assert.Bool(socksConfig.AuthType == socks.AuthType_PASSWORD).IsTrue() assert.Int(len(socksConfig.Accounts)).Equals(1) assert.String(socksConfig.Accounts["my-username"]).Equals("my-password") assert.Bool(socksConfig.UdpEnabled).IsFalse() assert.Address(socksConfig.Address.AsAddress()).Equals(net.LocalHostIP) assert.Uint32(socksConfig.Timeout).Equals(5) }
func TestBuildMacOS(t *testing.T) { assert := assert.On(t) binPath = filepath.Join(os.Getenv("GOPATH"), "testing") cleanBinPath() build("macos", "amd64", true, "test", "metadata.txt") assert.Bool(allFilesExists( "v2ray-macos.zip", "v2ray-test-macos", filepath.Join("v2ray-test-macos", "config.json"), filepath.Join("v2ray-test-macos", "v2ray"))).IsTrue() build("windows", "amd64", true, "test", "metadata.txt") assert.Bool(allFilesExists( "v2ray-windows-64.zip", "v2ray-test-windows-64", filepath.Join("v2ray-test-windows-64", "config.json"), filepath.Join("v2ray-test-windows-64", "v2ray.exe"))).IsTrue() build("linux", "amd64", true, "test", "metadata.txt") assert.Bool(allFilesExists( "v2ray-linux-64.zip", "v2ray-test-linux-64", filepath.Join("v2ray-test-linux-64", "vpoint_socks_vmess.json"), filepath.Join("v2ray-test-linux-64", "vpoint_vmess_freedom.json"), filepath.Join("v2ray-test-linux-64", "v2ray"))).IsTrue() }
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() }
func TestAlwaysValidStrategy(t *testing.T) { assert := assert.On(t) strategy := AlwaysValid() assert.Bool(strategy.IsValid()).IsTrue() strategy.Invalidate() assert.Bool(strategy.IsValid()).IsTrue() }
func TestStringNetworkList(t *testing.T) { assert := assert.On(t) var list NetworkList err := json.Unmarshal([]byte("\"TCP, ip\""), &list) assert.Error(err).IsNil() assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue() assert.Bool(list.HasNetwork(Network("udp"))).IsFalse() }
func TestRawConnection(t *testing.T) { assert := assert.On(t) rawConn := RawConnection{net.TCPConn{}} assert.Bool(rawConn.Reusable()).IsFalse() rawConn.SetReusable(true) assert.Bool(rawConn.Reusable()).IsFalse() }
func TestBufferIsFull(t *testing.T) { assert := assert.On(t) buffer := NewBuffer() defer buffer.Release() assert.Bool(buffer.IsFull()).IsTrue() buffer.Clear() assert.Bool(buffer.IsFull()).IsFalse() }
func TestRequestOptionClear(t *testing.T) { assert := assert.On(t) var option RequestOption option.Set(RequestOptionChunkStream) option.Set(RequestOptionConnectionReuse) option.Clear(RequestOptionChunkStream) assert.Bool(option.Has(RequestOptionChunkStream)).IsFalse() assert.Bool(option.Has(RequestOptionConnectionReuse)).IsTrue() }
func TestArrayNetworkList(t *testing.T) { assert := assert.On(t) var list NetworkList err := json.Unmarshal([]byte("[\"Tcp\"]"), &list) assert.Error(err).IsNil() nlist := list.Build() assert.Bool(nlist.HasNetwork(v2net.ParseNetwork("tcp"))).IsTrue() assert.Bool(nlist.HasNetwork(v2net.ParseNetwork("udp"))).IsFalse() }
func TestDomainParsing(t *testing.T) { assert := assert.On(t) rawJson := "\"v2ray.com\"" var address AddressJson err := json.Unmarshal([]byte(rawJson), &address) assert.Error(err).IsNil() assert.Bool(address.Address.Family().Either(AddressFamilyIPv4)).IsFalse() assert.Bool(address.Address.Family().Either(AddressFamilyDomain)).IsTrue() assert.String(address.Address.Domain()).Equals("v2ray.com") }
func TestChinaSites(t *testing.T) { assert := assert.On(t) rule := NewChinaSitesRule("tag") assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("12306.cn"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("v2ray.com"))).IsFalse() }
func TestIPParsing(t *testing.T) { assert := assert.On(t) rawJson := "\"8.8.8.8\"" var address AddressJson err := json.Unmarshal([]byte(rawJson), &address) assert.Error(err).IsNil() assert.Bool(address.Address.Family().Either(AddressFamilyIPv4)).IsTrue() assert.Bool(address.Address.Family().Either(AddressFamilyDomain)).IsFalse() assert.Bool(address.Address.IP().Equal(net.ParseIP("8.8.8.8"))).IsTrue() }
func TestTimeoutValidStrategy(t *testing.T) { assert := assert.On(t) strategy := BeforeTime(time.Now().Add(2 * time.Second)) assert.Bool(strategy.IsValid()).IsTrue() time.Sleep(3 * time.Second) assert.Bool(strategy.IsValid()).IsFalse() strategy = BeforeTime(time.Now().Add(2 * time.Second)) strategy.Invalidate() assert.Bool(strategy.IsValid()).IsFalse() }
func TestHasAuthenticationMethod(t *testing.T) { assert := assert.On(t) request := Socks5AuthenticationRequest{ version: socksVersion, nMethods: byte(0x02), authMethods: [256]byte{0x01, 0x02}, } assert.Bool(request.HasAuthMethod(byte(0x01))).IsTrue() request.authMethods[0] = byte(0x03) assert.Bool(request.HasAuthMethod(byte(0x01))).IsFalse() }
func TestIPNet(t *testing.T) { assert := assert.On(t) ipNet := NewIPNet() ipNet.Add(parseCIDR(("0.0.0.0/8"))) ipNet.Add(parseCIDR(("10.0.0.0/8"))) ipNet.Add(parseCIDR(("100.64.0.0/10"))) ipNet.Add(parseCIDR(("127.0.0.0/8"))) ipNet.Add(parseCIDR(("169.254.0.0/16"))) ipNet.Add(parseCIDR(("172.16.0.0/12"))) ipNet.Add(parseCIDR(("192.0.0.0/24"))) ipNet.Add(parseCIDR(("192.0.2.0/24"))) ipNet.Add(parseCIDR(("192.168.0.0/16"))) ipNet.Add(parseCIDR(("198.18.0.0/15"))) ipNet.Add(parseCIDR(("198.51.100.0/24"))) ipNet.Add(parseCIDR(("203.0.113.0/24"))) ipNet.Add(parseCIDR(("8.8.8.8/32"))) assert.Bool(ipNet.Contains(net.ParseIP("192.168.1.1"))).IsTrue() assert.Bool(ipNet.Contains(net.ParseIP("192.0.0.0"))).IsTrue() assert.Bool(ipNet.Contains(net.ParseIP("192.0.1.0"))).IsFalse() assert.Bool(ipNet.Contains(net.ParseIP("0.1.0.0"))).IsTrue() assert.Bool(ipNet.Contains(net.ParseIP("1.0.0.1"))).IsFalse() assert.Bool(ipNet.Contains(net.ParseIP("8.8.8.7"))).IsFalse() assert.Bool(ipNet.Contains(net.ParseIP("8.8.8.8"))).IsTrue() assert.Bool(ipNet.Contains(net.ParseIP("2001:cdba::3257:9652"))).IsFalse() }
func TestChinaSitesJson(t *testing.T) { assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "chinasites", "outboundTag": "y" }`)) assert.String(rule.Tag).Equals("y") assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("12306.cn"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("v2ray.com"))).IsFalse() }
func TestUDPDestinationEquals(t *testing.T) { assert := assert.On(t) dest := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) assert.Bool(dest.Equals(nil)).IsFalse() dest2 := v2net.UDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) assert.Bool(dest.Equals(dest2)).IsTrue() dest3 := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) assert.Bool(dest.Equals(dest3)).IsFalse() dest4 := v2net.UDPDestination(v2net.DomainAddress("v2ray.com"), 80) assert.Bool(dest.Equals(dest4)).IsFalse() }
func TestChinaIPJson(t *testing.T) { assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "chinaip", "outboundTag": "x" }`)) assert.String(rule.Tag).Equals("x") assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn assert.Bool(rule.Apply(makeDestination("101.226.103.106"))).IsTrue() // qq.com assert.Bool(rule.Apply(makeDestination("115.239.210.36"))).IsTrue() // image.baidu.com assert.Bool(rule.Apply(makeDestination("120.135.126.1"))).IsTrue() assert.Bool(rule.Apply(makeDestination("8.8.8.8"))).IsFalse() }
func TestIPv6AddressEquals(t *testing.T) { assert := assert.On(t) addr := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) assert.Bool(addr.Equals(nil)).IsFalse() addr2 := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) assert.Bool(addr.Equals(addr2)).IsTrue() addr3 := v2net.IPAddress([]byte{1, 2, 3, 4}) assert.Bool(addr.Equals(addr3)).IsFalse() addr4 := v2net.IPAddress([]byte{1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) assert.Bool(addr.Equals(addr4)).IsFalse() }
func TestDomainAddressEquals(t *testing.T) { assert := assert.On(t) addr := v2net.DomainAddress("v2ray.com") assert.Bool(addr.Equals(nil)).IsFalse() addr2 := v2net.DomainAddress("v2ray.com") assert.Bool(addr.Equals(addr2)).IsTrue() addr3 := v2net.DomainAddress("www.v2ray.com") assert.Bool(addr.Equals(addr3)).IsFalse() addr4 := v2net.IPAddress([]byte{1, 3, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) assert.Bool(addr.Equals(addr4)).IsFalse() }
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", "tools", "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 TestBufferedReader(t *testing.T) { assert := assert.On(t) content := buf.New() content.AppendSupplier(buf.ReadFrom(rand.Reader)) len := content.Len() reader := NewReader(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) }
func TestAdaptiveReader(t *testing.T) { assert := assert.On(t) rawContent := make([]byte, 1024*1024) reader := NewAdaptiveReader(bytes.NewBuffer(rawContent)) b1, err := reader.Read() assert.Error(err).IsNil() assert.Bool(b1.IsFull()).IsTrue() assert.Int(b1.Len()).Equals(alloc.BufferSize) b2, err := reader.Read() assert.Error(err).IsNil() assert.Bool(b2.IsFull()).IsTrue() assert.Int(b2.Len()).Equals(alloc.LargeBufferSize) }
func TestSwitchAccount(t *testing.T) { assert := assert.On(t) sa := &protocol.CommandSwitchAccount{ Port: 1234, ID: uuid.New(), AlterIds: 1024, Level: 128, ValidMin: 16, } buffer := buf.New() err := MarshalCommand(sa, buffer) assert.Error(err).IsNil() cmd, err := UnmarshalCommand(1, buffer.BytesFrom(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 TestNext(t *testing.T) { assert := assert.On(t) uuid := New() uuid2 := uuid.Next() assert.Bool(uuid.Equals(uuid2)).IsFalse() }