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 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 TestBuffer(t *testing.T) { assert := assert.On(t) b := NewBuffer() for i := 0; i < NumDistro; i++ { x := b.Allocate() assert.Pointer(x).IsNotNil() x.Release() } assert.Pointer(b.Allocate()).IsNil() b.Release() }
func TestBadSegment(t *testing.T) { assert := assert.On(t) seg, buf := ReadSegment(nil) assert.Pointer(seg).IsNil() assert.Int(len(buf)).Equals(0) }
func TestSingleRelease(t *testing.T) { assert := assert.On(t) b := NewBuffer() x := b.Allocate() x.Release() y := b.Allocate() assert.Pointer(y.Value).IsNotNil() b.Release() y.Release() z := b.Allocate() assert.Pointer(z).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() 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 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 TestServerSampleConfig(t *testing.T) { assert := assert.On(t) GOPATH := os.Getenv("GOPATH") baseDir := filepath.Join(GOPATH, "src", "v2ray.com", "core", "tools", "release", "config") pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.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("vmess") assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() assert.String(pointConfig.OutboundConfig.Protocol).Equals("freedom") assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() }
func TestConnectionRecycle(t *testing.T) { assert := assert.On(t) pool := NewConnectionPool() c := &TestConnection{id: "test"} pool.Put(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80))), c) time.Sleep(6 * time.Second) assert.Bool(c.closed).IsTrue() conn := pool.Get(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80)))) assert.Pointer(conn).IsNil() }
func TestConnectionCache(t *testing.T) { assert := assert.On(t) pool := NewConnectionPool() conn := pool.Get(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80)))) assert.Pointer(conn).IsNil() pool.Put(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80))), &TestConnection{id: "test"}) conn = pool.Get(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80)))) assert.String(conn.(*TestConnection).id).Equals("test") }
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 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 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() cond, err := rule.BuildCondition() assert.Error(err).IsNil() assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.DomainAddress("www.ooxx.com"), 80)))).IsFalse() assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{10, 0, 0, 1}), 80)))).IsTrue() assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 80)))).IsFalse() assert.Bool(cond.Apply(proxy.ContextWithDestination(context.Background(), v2net.TCPDestination(v2net.IPAddress([]byte{192, 0, 0, 1}), 80)))).IsTrue() }
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() }