示例#1
0
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)
}
示例#2
0
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)
}
示例#3
0
func TestPubsub(t *testing.T) {
	v2testing.Current(t)

	messages := make(map[string]app.PubsubMessage)

	pubsub := New()
	pubsub.Subscribe(&apptesting.Context{}, "t1", func(message app.PubsubMessage) {
		messages["t1"] = message
	})

	pubsub.Subscribe(&apptesting.Context{}, "t2", func(message app.PubsubMessage) {
		messages["t2"] = message
	})

	message := app.PubsubMessage([]byte("This is a pubsub message."))
	pubsub.Publish(&apptesting.Context{}, "t2", message)
	<-time.Tick(time.Second)

	_, found := messages["t1"]
	assert.Bool(found).IsFalse()

	actualMessage, found := messages["t2"]
	assert.Bool(found).IsTrue()
	assert.StringLiteral(string(actualMessage)).Equals(string(message))
}
示例#4
0
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)
}
示例#5
0
func TestBuildMacOS(t *testing.T) {
	v2testing.Current(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()
}
示例#6
0
func TestAlwaysValidStrategy(t *testing.T) {
	assert := assert.On(t)

	strategy := AlwaysValid()
	assert.Bool(strategy.IsValid()).IsTrue()
	strategy.Invalidate()
	assert.Bool(strategy.IsValid()).IsTrue()
}
示例#7
0
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()
}
示例#8
0
func TestEquals(t *testing.T) {
	v2testing.Current(t)

	var uuid *UUID = nil
	var uuid2 *UUID = nil
	assert.Bool(uuid.Equals(uuid2)).IsTrue()
	assert.Bool(uuid.Equals(New())).IsFalse()
}
示例#9
0
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()
}
示例#10
0
func TestStringNetworkList(t *testing.T) {
	v2testing.Current(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()
}
示例#11
0
func TestArrayNetworkList(t *testing.T) {
	assert := assert.On(t)

	var list NetworkList
	err := json.Unmarshal([]byte("[\"Tcp\"]"), &list)
	assert.Error(err).IsNil()
	assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue()
	assert.Bool(list.HasNetwork(Network("udp"))).IsFalse()
}
func TestChinaSites(t *testing.T) {
	v2testing.Current(t)

	rule := &ChinaSitesRule{}
	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()
}
示例#13
0
func TestBufferIsFull(t *testing.T) {
	v2testing.Current(t)

	buffer := NewBuffer()
	defer buffer.Release()

	assert.Bool(buffer.IsFull()).IsTrue()

	buffer.Clear()
	assert.Bool(buffer.IsFull()).IsFalse()
}
示例#14
0
func TestChinaIP(t *testing.T) {
	v2testing.Current(t)

	rule := NewIPv4Matcher(chinaIPNet)
	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()
}
示例#15
0
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.IsIPv4()).IsTrue()
	assert.Bool(address.Address.IsDomain()).IsFalse()
	assert.Bool(address.Address.IP().Equal(net.ParseIP("8.8.8.8"))).IsTrue()
}
示例#16
0
func TestUserPassConfig(t *testing.T) {
	v2testing.Current(t)

	var config SocksConfig
	err := json.Unmarshal([]byte("{\"auth\":\"password\", \"accounts\":[{\"user\":\"x\", \"pass\":\"y\"}], \"udp\":true}"), &config)
	assert.Error(err).IsNil()
	assert.Bool(config.IsNoAuth()).IsFalse()
	assert.Bool(config.IsPassword()).IsTrue()
	assert.Bool(config.HasAccount("x", "y")).IsTrue()
	assert.Bool(config.UDPEnabled()).IsTrue()
}
示例#17
0
func TestNoAuthConfig(t *testing.T) {
	v2testing.Current(t)

	var config SocksConfig
	err := json.Unmarshal([]byte("{\"auth\":\"noauth\", \"ip\":\"8.8.8.8\"}"), &config)
	assert.Error(err).IsNil()
	assert.Bool(config.IsNoAuth()).IsTrue()
	assert.Bool(config.IsPassword()).IsFalse()
	assert.String(config.IP()).Equals("8.8.8.8")
	assert.Bool(config.UDPEnabled()).IsFalse()
}
示例#18
0
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.IsIPv4()).IsFalse()
	assert.Bool(address.Address.IsDomain()).IsTrue()
	assert.String(address.Address.Domain()).Equals("v2ray.com")
}
示例#19
0
func TestIPParsing(t *testing.T) {
	v2testing.Current(t)

	rawJson := "\"8.8.8.8\""
	host := &Host{}
	err := json.Unmarshal([]byte(rawJson), host)
	assert.Error(err).IsNil()
	assert.Bool(host.IsIP()).IsTrue()
	assert.Bool(host.IsDomain()).IsFalse()
	assert.Bool(host.IP().Equal(net.ParseIP("8.8.8.8"))).IsTrue()
}
示例#20
0
func TestDomainParsing(t *testing.T) {
	v2testing.Current(t)

	rawJson := "\"v2ray.com\""
	host := &Host{}
	err := json.Unmarshal([]byte(rawJson), host)
	assert.Error(err).IsNil()
	assert.Bool(host.IsIP()).IsFalse()
	assert.Bool(host.IsDomain()).IsTrue()
	assert.StringLiteral(host.Domain()).Equals("v2ray.com")
}
示例#21
0
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()
}
示例#22
0
func TestAccountMapParsing(t *testing.T) {
	v2testing.Current(t)

	var accountMap SocksAccountMap
	err := json.Unmarshal([]byte("[{\"user\": \"a\", \"pass\":\"b\"}, {\"user\": \"c\", \"pass\":\"d\"}]"), &accountMap)
	assert.Error(err).IsNil()

	assert.Bool(accountMap.HasAccount("a", "b")).IsTrue()
	assert.Bool(accountMap.HasAccount("a", "c")).IsFalse()
	assert.Bool(accountMap.HasAccount("c", "d")).IsTrue()
	assert.Bool(accountMap.HasAccount("e", "d")).IsFalse()
}
示例#23
0
func TestChinaSites(t *testing.T) {
	v2testing.Current(t)

	rule := ParseRule([]byte(`{
    "type": "chinasites"
  }`))
	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()
}
示例#24
0
func TestChinaIP(t *testing.T) {
	v2testing.Current(t)

	rule := ParseRule([]byte(`{
    "type": "chinaip"
  }`))
	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 TestTimedQueue(t *testing.T) {
	v2testing.Current(t)

	removed := make(map[string]bool)

	nowSec := time.Now().Unix()
	q := NewTimedQueue(2)

	go func() {
		for {
			entry := <-q.RemovedEntries()
			removed[entry.(string)] = true
		}
	}()

	q.Add("Value1", nowSec)
	q.Add("Value2", nowSec+5)

	v1, ok := removed["Value1"]
	assert.Bool(ok).IsFalse()

	v2, ok := removed["Value2"]
	assert.Bool(ok).IsFalse()

	tick := time.Tick(4 * time.Second)
	<-tick

	v1, ok = removed["Value1"]
	assert.Bool(ok).IsTrue()
	assert.Bool(v1).IsTrue()
	removed["Value1"] = false

	v2, ok = removed["Value2"]
	assert.Bool(ok).IsFalse()

	<-tick
	v2, ok = removed["Value2"]
	assert.Bool(ok).IsTrue()
	assert.Bool(v2).IsTrue()
	removed["Value2"] = false

	<-tick
	assert.Bool(removed["Values"]).IsFalse()

	q.Add("Value1", time.Now().Unix()+10)

	<-tick
	v1, ok = removed["Value1"]
	assert.Bool(ok).IsTrue()
	assert.Bool(v1).IsFalse()
}
示例#26
0
func TestIPNet(t *testing.T) {
	v2testing.Current(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()
}
示例#27
0
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()
}
示例#28
0
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 TestChinaSitesJson(t *testing.T) {
	v2testing.Current(t)

	rule := ParseRule([]byte(`{
    "type": "chinasites",
    "outboundTag": "y"
  }`))
	assert.StringLiteral(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()
}
示例#30
0
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()
}