func TestSwitchAccount(t *testing.T) { v2testing.Current(t) sa := &SwitchAccount{ Port: 1234, ID: uuid.New(), AlterIds: 1024, Level: 128, ValidMin: 16, } cmd, err := CreateResponseCommand(1) assert.Error(err).IsNil() buffer := bytes.NewBuffer(make([]byte, 0, 1024)) sa.Marshal(buffer) cmd.Unmarshal(buffer.Bytes()) sa2, ok := cmd.(*SwitchAccount) assert.Bool(ok).IsTrue() assert.Pointer(sa.Host).IsNil() assert.Pointer(sa2.Host).IsNil() netassert.Port(sa.Port).Equals(sa2.Port) assert.String(sa.ID).Equals(sa2.ID.String()) assert.Uint16(sa.AlterIds.Value()).Equals(sa2.AlterIds.Value()) assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) }
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 TestSwitchAccount(t *testing.T) { v2testing.Current(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() netassert.Port(sa.Port).Equals(sa2.Port) assert.String(sa.ID).Equals(sa2.ID.String()) assert.Uint16(sa.AlterIds.Value()).Equals(sa2.AlterIds.Value()) assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) }
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 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 TestSizedQueue(t *testing.T) { v2testing.Current(t) queue := collect.NewSizedQueue(2) assert.Pointer(queue.Put(1)).IsNil() assert.Pointer(queue.Put(2)).IsNil() assert.Int(queue.Put(3).(int)).Equals(1) }
func TestParseArch(t *testing.T) { assert := assert.On(t) assert.Pointer(parseArch("x86")).Equals(X86) assert.Pointer(parseArch("x64")).Equals(Amd64) assert.Pointer(parseArch("arm")).Equals(Arm) assert.Pointer(parseArch("arm64")).Equals(Arm64) assert.Pointer(parseArch("test")).Equals(UnknownArch) }
func TestLogLevelSetting(t *testing.T) { v2testing.Current(t) assert.Pointer(debugLogger).Equals(noOpLoggerInstance) SetLogLevel(DebugLevel) assert.Pointer(debugLogger).Equals(streamLoggerInstance) SetLogLevel(InfoLevel) assert.Pointer(debugLogger).Equals(noOpLoggerInstance) assert.Pointer(infoLogger).Equals(streamLoggerInstance) }
func TestBadSegment(t *testing.T) { assert := assert.On(t) seg, buf := ReadSegment(nil) assert.Pointer(seg).IsNil() assert.Int(len(buf)).Equals(0) }
func TestServerSampleConfig(t *testing.T) { v2testing.Current(t) // TODO: fix for Windows baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json")) assert.Error(err).IsNil() netassert.Port(pointConfig.Port).IsValid() assert.Pointer(pointConfig.InboundConfig).IsNotNil() assert.Pointer(pointConfig.OutboundConfig).IsNotNil() assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("vmess") assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("freedom") assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() }
func TestServerSampleConfig(t *testing.T) { v2testing.Current(t) GOPATH := os.Getenv("GOPATH") baseDir := filepath.Join(GOPATH, "src", "github.com", "v2ray", "v2ray-core", "release", "config") pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json")) assert.Error(err).IsNil() netassert.Port(pointConfig.Port).IsValid() assert.Pointer(pointConfig.InboundConfig).IsNotNil() assert.Pointer(pointConfig.OutboundConfig).IsNotNil() assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("vmess") assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("freedom") assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() }
func TestClientSampleConfig(t *testing.T) { assert := assert.On(t) GOPATH := os.Getenv("GOPATH") baseDir := filepath.Join(GOPATH, "src", "github.com", "v2ray", "v2ray-core", "release", "config") pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) assert.Error(err).IsNil() assert.Pointer(pointConfig.InboundConfig).IsNotNil() assert.Port(pointConfig.InboundConfig.Port).IsValid() assert.Pointer(pointConfig.OutboundConfig).IsNotNil() assert.String(pointConfig.InboundConfig.Protocol).Equals("socks") assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() assert.String(pointConfig.OutboundConfig.Protocol).Equals("vmess") assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() }
func TestIPRule(t *testing.T) { assert := assert.On(t) rule := ParseRule([]byte(`{ "type": "field", "ip": [ "10.0.0.0/8", "192.0.0.0/24" ], "network": "tcp", "outboundTag": "direct" }`)) assert.Pointer(rule).IsNotNil() assert.Bool(rule.Apply(v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80))).IsFalse() assert.Bool(rule.Apply(v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80))).IsTrue() assert.Bool(rule.Apply(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80))).IsFalse() assert.Bool(rule.Apply(v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80))).IsTrue() }
func TestRegisterOutboundConfig(t *testing.T) { v2testing.Current(t) initializeConfigCache() protocol := "test_protocol" creator := func() interface{} { return true } err := RegisterOutboundConnectionConfig(protocol, creator) assert.Error(err).IsNil() configObj := CreateConfig(protocol, config.TypeOutbound) assert.Bool(configObj.(bool)).IsTrue() configObj = CreateConfig(protocol, config.TypeInbound) assert.Pointer(configObj).IsNil() }
func TestServerList(t *testing.T) { assert := assert.On(t) list := NewServerList() list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(1)), AlwaysValid())) assert.Uint32(list.Size()).Equals(1) list.AddServer(NewServerSpec(v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(2)), BeforeTime(time.Now().Add(time.Second)))) assert.Uint32(list.Size()).Equals(2) server := list.GetServer(1) assert.Port(server.Destination().Port()).Equals(2) time.Sleep(2 * time.Second) server = list.GetServer(1) assert.Pointer(server).IsNil() server = list.GetServer(0) assert.Port(server.Destination().Port()).Equals(1) }
func TestRegisterInboundConfig(t *testing.T) { v2testing.Current(t) initializeConfigCache() protocol := "test_protocol" creator := func([]byte) (interface{}, error) { return true, nil } err := RegisterInboundConfig(protocol, creator) assert.Error(err).IsNil() configObj, err := CreateInboundConfig(protocol, nil) assert.Bool(configObj.(bool)).IsTrue() assert.Error(err).IsNil() configObj, err = CreateOutboundConfig(protocol, nil) assert.Pointer(configObj).IsNil() }
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() assert.Bool(rule.Apply(v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80))).IsTrue() assert.Bool(rule.Apply(v2net.TCPDestination(v2net.DomainAddress("www.aabb.com"), 80))).IsFalse() assert.Bool(rule.Apply(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80))).IsFalse() assert.Bool(rule.Apply(v2net.TCPDestination(v2net.DomainAddress("www.12306.cn"), 80))).IsTrue() assert.Bool(rule.Apply(v2net.TCPDestination(v2net.DomainAddress("www.acn.com"), 80))).IsFalse() }