massive refactoring against unit test lib

This commit is contained in:
Darien Raymond 2015-12-02 14:27:18 +00:00
parent cee85bdf26
commit 0a2e4343bc
44 changed files with 327 additions and 317 deletions

View File

@ -6,11 +6,12 @@ import (
v2net "github.com/v2ray/v2ray-core/common/net"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestStringListParsingList(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawJson := `["a", "b", "c", "d"]`
var strList StringList
@ -20,7 +21,7 @@ func TestStringListParsingList(t *testing.T) {
}
func TestStringListParsingString(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawJson := `"abcd"`
var strList StringList
@ -30,7 +31,7 @@ func TestStringListParsingString(t *testing.T) {
}
func TestDomainMatching(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rule := &FieldRule{
Domain: NewStringList("v2ray.com"),
@ -40,7 +41,7 @@ func TestDomainMatching(t *testing.T) {
}
func TestPortMatching(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rule := &FieldRule{
Port: &v2nettesting.PortRange{
@ -53,7 +54,7 @@ func TestPortMatching(t *testing.T) {
}
func TestIPMatching(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawJson := `{
"type": "field",
@ -66,7 +67,7 @@ func TestIPMatching(t *testing.T) {
}
func TestIPListMatching(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawJson := `{
"type": "field",
@ -79,7 +80,7 @@ func TestIPListMatching(t *testing.T) {
}
func TestPortNotMatching(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawJson := `{
"type": "field",
@ -92,7 +93,7 @@ func TestPortNotMatching(t *testing.T) {
}
func TestDomainNotMatching(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawJson := `{
"type": "field",
@ -105,7 +106,7 @@ func TestDomainNotMatching(t *testing.T) {
}
func TestDomainNotMatchingDomain(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawJson := `{
"type": "field",

View File

@ -6,11 +6,12 @@ import (
"github.com/v2ray/v2ray-core/app/router/rules/config"
testinconfig "github.com/v2ray/v2ray-core/app/router/rules/config/testing"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestSimpleRouter(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
router := &Router{
rules: []config.Rule{

View File

@ -3,11 +3,12 @@ package alloc
import (
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestBufferClear(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
buffer := NewBuffer().Clear()
defer buffer.Release()
@ -21,7 +22,7 @@ func TestBufferClear(t *testing.T) {
}
func TestBufferIsFull(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
buffer := NewBuffer()
defer buffer.Release()

View File

@ -4,11 +4,12 @@ import (
"testing"
"time"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestTimedQueue(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
removed := make(map[string]bool)

View File

@ -7,11 +7,12 @@ import (
"testing"
"time"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestAccessLog(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
filename := "/tmp/test_access_log.log"
InitAccessLogger(filename)

View File

@ -5,11 +5,12 @@ import (
"log"
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestLogLevelSetting(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
assert.Pointer(debugLogger).Equals(noOpLoggerInstance)
SetLogLevel(DebugLevel)
@ -21,7 +22,7 @@ func TestLogLevelSetting(t *testing.T) {
}
func TestStreamLogger(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
buffer := bytes.NewBuffer(make([]byte, 0, 1024))
logger := &streamLogger{

View File

@ -4,11 +4,12 @@ import (
"net"
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestIPv4Address(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
port := NewPort(80)
@ -23,7 +24,7 @@ func TestIPv4Address(t *testing.T) {
}
func TestIPv6Address(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
ip := []byte{
byte(1), byte(2), byte(3), byte(4),
@ -43,7 +44,7 @@ func TestIPv6Address(t *testing.T) {
}
func TestDomainAddress(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
domain := "v2ray.com"
port := NewPort(443)
@ -58,7 +59,7 @@ func TestDomainAddress(t *testing.T) {
}
func TestNetIPv4Address(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
ip := net.IPv4(1, 2, 3, 4)
port := NewPort(80)

View File

@ -3,11 +3,12 @@ package net
import (
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestTCPDestination(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
dest := NewTCPDestination(IPAddress([]byte{1, 2, 3, 4}, 80))
assert.Bool(dest.IsTCP()).IsTrue()
@ -16,7 +17,7 @@ func TestTCPDestination(t *testing.T) {
}
func TestUDPDestination(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
dest := NewUDPDestination(IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}, 53))
assert.Bool(dest.IsTCP()).IsFalse()

View File

@ -4,11 +4,12 @@ import (
"encoding/json"
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestArrayNetworkList(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var list NetworkList
err := json.Unmarshal([]byte("[\"Tcp\"]"), &list)
@ -18,7 +19,7 @@ func TestArrayNetworkList(t *testing.T) {
}
func TestStringNetworkList(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var list NetworkList
err := json.Unmarshal([]byte("\"TCP, ip\""), &list)

View File

@ -4,11 +4,12 @@ import (
"encoding/json"
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestIntPort(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var portRange PortRange
err := json.Unmarshal([]byte("1234"), &portRange)
@ -19,7 +20,7 @@ func TestIntPort(t *testing.T) {
}
func TestOverRangeIntPort(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var portRange PortRange
err := json.Unmarshal([]byte("70000"), &portRange)
@ -30,7 +31,7 @@ func TestOverRangeIntPort(t *testing.T) {
}
func TestSingleStringPort(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var portRange PortRange
err := json.Unmarshal([]byte("\"1234\""), &portRange)
@ -41,7 +42,7 @@ func TestSingleStringPort(t *testing.T) {
}
func TestStringPairPort(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var portRange PortRange
err := json.Unmarshal([]byte("\"1234-5678\""), &portRange)
@ -52,7 +53,7 @@ func TestStringPairPort(t *testing.T) {
}
func TestOverRangeStringPort(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var portRange PortRange
err := json.Unmarshal([]byte("\"65536\""), &portRange)

View File

@ -0,0 +1,46 @@
package unit
import (
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/testing/assert"
)
func Port(value v2net.Port) *PortSubject {
return &PortSubject{value: value}
}
type PortSubject struct {
*assert.Subject
value v2net.Port
}
func (subject *PortSubject) Named(name string) *PortSubject {
subject.Subject.Named(name)
return subject
}
func (subject *PortSubject) Fail(verb string, other v2net.Port) {
subject.FailWithMessage("Not true that " + subject.DisplayString() + " " + verb + " <" + other.String() + ">.")
}
func (subject *PortSubject) DisplayString() string {
return subject.Subject.DisplayString(subject.value.String())
}
func (subject *PortSubject) Equals(expectation v2net.Port) {
if subject.value.Value() != expectation.Value() {
subject.Fail("is equal to", expectation)
}
}
func (subject *PortSubject) GreaterThan(expectation v2net.Port) {
if subject.value.Value() <= expectation.Value() {
subject.Fail("is greater than", expectation)
}
}
func (subject *PortSubject) LessThan(expectation v2net.Port) {
if subject.value.Value() >= expectation.Value() {
subject.Fail("is less than", expectation)
}
}

View File

@ -8,11 +8,12 @@ import (
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestReaderAndWrite(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
size := 1024 * 1024
buffer := make([]byte, size)

View File

@ -5,7 +5,8 @@ import (
"testing"
"time"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
var (
@ -13,7 +14,7 @@ var (
)
func TestNoRetry(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
startTime := time.Now().Unix()
err := Timed(10, 100000).On(func() error {
@ -26,7 +27,7 @@ func TestNoRetry(t *testing.T) {
}
func TestRetryOnce(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
startTime := time.Now()
called := 0
@ -44,7 +45,7 @@ func TestRetryOnce(t *testing.T) {
}
func TestRetryMultiple(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
startTime := time.Now()
called := 0
@ -62,7 +63,7 @@ func TestRetryMultiple(t *testing.T) {
}
func TestRetryExhausted(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
startTime := time.Now()
called := 0

View File

@ -4,11 +4,11 @@ import (
"testing"
"github.com/v2ray/v2ray-core/proxy/common/config"
"github.com/v2ray/v2ray-core/testing/unit"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestRegisterInboundConfig(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
initializeConfigCache()
protocol := "test_protocol"
@ -27,7 +27,7 @@ func TestRegisterInboundConfig(t *testing.T) {
}
func TestRegisterOutboundConfig(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
initializeConfigCache()
protocol := "test_protocol"

View File

@ -10,13 +10,14 @@ import (
_ "github.com/v2ray/v2ray-core/proxy/freedom"
"github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/config/testing/mocks"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
"github.com/v2ray/v2ray-core/testing/servers/udp"
"github.com/v2ray/v2ray-core/testing/unit"
)
func TestDokodemoTCP(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
port := v2nettesting.PickPort()
@ -78,7 +79,7 @@ func TestDokodemoTCP(t *testing.T) {
}
func TestDokodemoUDP(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
port := v2nettesting.PickPort()

View File

@ -18,13 +18,14 @@ import (
proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
"github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/config/testing/mocks"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
"github.com/v2ray/v2ray-core/testing/servers/udp"
"github.com/v2ray/v2ray-core/testing/unit"
)
func TestUDPSend(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
data2Send := "Data to be sent to remote"
@ -76,7 +77,7 @@ func TestUDPSend(t *testing.T) {
}
func TestSocksTcpConnect(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
port := v2nettesting.PickPort()
data2Send := "Data to be sent to remote"

View File

@ -7,11 +7,11 @@ import (
"github.com/v2ray/v2ray-core/proxy/common/config"
jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json"
"github.com/v2ray/v2ray-core/testing/unit"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestAccountMapParsing(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var accountMap SocksAccountMap
err := json.Unmarshal([]byte("[{\"user\": \"a\", \"pass\":\"b\"}, {\"user\": \"c\", \"pass\":\"d\"}]"), &accountMap)
@ -24,14 +24,14 @@ func TestAccountMapParsing(t *testing.T) {
}
func TestDefaultIPAddress(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
socksConfig := jsonconfig.CreateConfig("socks", config.TypeInbound).(*SocksConfig)
assert.String(socksConfig.IP().String()).Equals("127.0.0.1")
}
func TestIPAddressParsing(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var ipAddress IPAddress
err := json.Unmarshal([]byte("\"1.2.3.4\""), &ipAddress)
@ -40,7 +40,7 @@ func TestIPAddressParsing(t *testing.T) {
}
func TestNoAuthConfig(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var config SocksConfig
err := json.Unmarshal([]byte("{\"auth\":\"noauth\", \"ip\":\"8.8.8.8\"}"), &config)
@ -52,7 +52,7 @@ func TestNoAuthConfig(t *testing.T) {
}
func TestUserPassConfig(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
var config SocksConfig
err := json.Unmarshal([]byte("{\"auth\":\"password\", \"accounts\":[{\"user\":\"x\", \"pass\":\"y\"}], \"udp\":true}"), &config)

View File

@ -5,11 +5,12 @@ import (
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestSocks4AuthenticationRequestRead(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawRequest := []byte{
0x04, // version
@ -26,7 +27,7 @@ func TestSocks4AuthenticationRequestRead(t *testing.T) {
}
func TestSocks4AuthenticationResponseToBytes(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
response := NewSocks4AuthenticationResponse(byte(0x10), 443, []byte{1, 2, 3, 4})

View File

@ -6,12 +6,13 @@ import (
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/transport"
)
func TestHasAuthenticationMethod(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
request := Socks5AuthenticationRequest{
version: socksVersion,
@ -26,7 +27,7 @@ func TestHasAuthenticationMethod(t *testing.T) {
}
func TestAuthenticationRequestRead(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawRequest := []byte{
0x05, // version
@ -41,7 +42,7 @@ func TestAuthenticationRequestRead(t *testing.T) {
}
func TestAuthenticationResponseWrite(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
response := NewAuthenticationResponse(byte(0x05))
@ -51,7 +52,7 @@ func TestAuthenticationResponseWrite(t *testing.T) {
}
func TestRequestRead(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rawRequest := []byte{
0x05, // version
@ -71,7 +72,7 @@ func TestRequestRead(t *testing.T) {
}
func TestResponseWrite(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
response := Socks5Response{
socksVersion,
@ -98,14 +99,14 @@ func TestResponseWrite(t *testing.T) {
}
func TestEOF(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
_, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 0)))
assert.Error(err).Equals(io.EOF)
}
func TestSignleByte(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
_, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1)))
assert.Error(err).Equals(transport.CorruptedPacket)

View File

@ -3,12 +3,12 @@ package protocol
import (
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/transport"
)
func TestSingleByteRequest(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
request, err := ReadUDPRequest(make([]byte, 1))
if request != nil {
@ -18,7 +18,7 @@ func TestSingleByteRequest(t *testing.T) {
}
func TestDomainAddressRequest(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
payload := make([]byte, 0, 1024)
payload = append(payload, 0, 0, 1, AddrTypeDomain, byte(len("v2ray.com")))

View File

@ -15,11 +15,12 @@ import (
proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
"github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/config/testing/mocks"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestSocksTcpConnect(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.")
@ -74,7 +75,7 @@ func TestSocksTcpConnect(t *testing.T) {
}
func TestSocksTcpConnectWithUserPass(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.")
@ -132,7 +133,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
}
func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.")
@ -176,7 +177,7 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
}
func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.")
@ -220,7 +221,7 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
}
func TestSocksUdpSend(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.")

View File

@ -3,11 +3,11 @@ package config
import (
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestUUIDToID(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
uuid := "2418d087-648d-4990-86e8-19dca1d006d3"
expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}

View File

@ -4,11 +4,11 @@ import (
"testing"
"time"
"github.com/v2ray/v2ray-core/testing/unit"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestGenerateRandomInt64InRange(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
base := time.Now().Unix()
delta := 100

View File

@ -10,7 +10,8 @@ import (
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user/testing/mocks"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
type TestUser struct {
@ -27,7 +28,7 @@ func (this *TestUser) Level() config.UserLevel {
}
func TestVMessSerialization(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
userId, err := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
if err != nil {
@ -81,7 +82,7 @@ func TestVMessSerialization(t *testing.T) {
}
func TestReadSingleByte(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
reader := NewVMessRequestReader(nil)
_, err := reader.Read(bytes.NewReader(make([]byte, 1)))

View File

@ -12,11 +12,12 @@ import (
"github.com/v2ray/v2ray-core/proxy/vmess/config/json"
"github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/config/testing/mocks"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestVMessInAndOut(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
assert.Error(err).IsNil()

View File

@ -10,11 +10,12 @@ import (
_ "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
"github.com/v2ray/v2ray-core/shell/point/config/json"
"github.com/v2ray/v2ray-core/testing/unit"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestClientSampleConfig(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
// TODO: fix for Windows
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
@ -34,7 +35,7 @@ func TestClientSampleConfig(t *testing.T) {
}
func TestServerSampleConfig(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
// TODO: fix for Windows
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"
@ -54,7 +55,7 @@ func TestServerSampleConfig(t *testing.T) {
}
func TestDetourConfig(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
// TODO: fix for Windows
baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config"

View File

@ -1,19 +1,16 @@
package unit
package assert
import (
"strconv"
)
type BoolSubject struct {
*Subject
value bool
func Bool(value bool) *BoolSubject {
return &BoolSubject{value: value}
}
func NewBoolSubject(base *Subject, value bool) *BoolSubject {
return &BoolSubject{
Subject: base,
value: value,
}
type BoolSubject struct {
Subject
value bool
}
func (subject *BoolSubject) Named(name string) *BoolSubject {

View File

@ -1,20 +1,17 @@
package unit
package assert
import (
"bytes"
"fmt"
)
type BytesSubject struct {
*Subject
value []byte
func Bytes(value []byte) *BytesSubject {
return &BytesSubject{value: value}
}
func NewBytesSubject(base *Subject, value []byte) *BytesSubject {
return &BytesSubject{
Subject: base,
value: value,
}
type BytesSubject struct {
Subject
value []byte
}
func (subject *BytesSubject) Named(name string) *BytesSubject {

View File

@ -1,19 +1,16 @@
package unit
package assert
import (
"strconv"
)
type ByteSubject struct {
*Subject
value byte
func Byte(value byte) *ByteSubject {
return &ByteSubject{value: value}
}
func NewByteSubject(base *Subject, value byte) *ByteSubject {
return &ByteSubject{
Subject: base,
value: value,
}
type ByteSubject struct {
Subject
value byte
}
func (subject *ByteSubject) Named(name string) *ByteSubject {

View File

@ -1,15 +1,12 @@
package unit
package assert
type ErrorSubject struct {
*Subject
value error
func Error(value error) *ErrorSubject {
return &ErrorSubject{value: value}
}
func NewErrorSubject(base *Subject, value error) *ErrorSubject {
return &ErrorSubject{
Subject: base,
value: value,
}
type ErrorSubject struct {
Subject
value error
}
func (subject *ErrorSubject) Named(name string) *ErrorSubject {

View File

@ -1,19 +1,16 @@
package unit
package assert
import (
"strconv"
)
type Int64Subject struct {
*Subject
value int64
func Int64(value int64) *Int64Subject {
return &Int64Subject{value: value}
}
func NewInt64Subject(base *Subject, value int64) *Int64Subject {
return &Int64Subject{
Subject: base,
value: value,
}
type Int64Subject struct {
Subject
value int64
}
func (subject *Int64Subject) Named(name string) *Int64Subject {

View File

@ -1,21 +1,18 @@
package unit
package assert
import (
"strconv"
)
func Int(value int) *IntSubject {
return &IntSubject{value: value}
}
type IntSubject struct {
*Subject
value int
}
func NewIntSubject(base *Subject, value int) *IntSubject {
return &IntSubject{
Subject: base,
value: value,
}
}
func (subject *IntSubject) Named(name string) *IntSubject {
subject.Subject.Named(name)
return subject

View File

@ -1,19 +1,16 @@
package unit
package assert
import (
"fmt"
)
type PointerSubject struct {
*Subject
value interface{}
func Pointer(value interface{}) *PointerSubject {
return &PointerSubject{value: value}
}
func NewPointerSubject(base *Subject, value interface{}) *PointerSubject {
return &PointerSubject{
Subject: base,
value: value,
}
type PointerSubject struct {
Subject
value interface{}
}
func (subject *PointerSubject) Named(name string) *PointerSubject {

View File

@ -1,15 +1,12 @@
package unit
package assert
type StringSubject struct {
*Subject
value string
func String(value string) *StringSubject {
return &StringSubject{value: value}
}
func NewStringSubject(base *Subject, value string) *StringSubject {
return &StringSubject{
Subject: base,
value: value,
}
type StringSubject struct {
Subject
value string
}
func (subject *StringSubject) Named(name string) *StringSubject {

33
testing/assert/subject.go Normal file
View File

@ -0,0 +1,33 @@
package assert
import (
v2testing "github.com/v2ray/v2ray-core/testing"
)
type Subject struct {
name string
}
func NewSubject() *Subject {
return &Subject{
name: "",
}
}
func (subject *Subject) FailWithMessage(message string) {
v2testing.Fail(message)
}
func (subject *Subject) Named(name string) {
subject.name = name
}
func (subject *Subject) DisplayString(value string) string {
if len(value) == 0 {
value = "unknown"
}
if len(subject.name) == 0 {
return "<" + value + ">"
}
return subject.name + "(<" + value + ">)"
}

View File

@ -1,19 +1,16 @@
package unit
package assert
import (
"strconv"
)
type Uint16Subject struct {
*Subject
value uint16
func Uint16(value uint16) *Uint16Subject {
return &Uint16Subject{value: value}
}
func NewUint16Subject(base *Subject, value uint16) *Uint16Subject {
return &Uint16Subject{
Subject: base,
value: value,
}
type Uint16Subject struct {
Subject
value uint16
}
func (subject *Uint16Subject) Named(name string) *Uint16Subject {

66
testing/global.go Normal file
View File

@ -0,0 +1,66 @@
package unit
import (
"bytes"
"fmt"
"runtime"
"strings"
"testing"
)
var tGlobal *testing.T
func Current(t *testing.T) {
tGlobal = t
}
func getCaller() (string, int) {
stackLevel := 3
for {
_, file, line, ok := runtime.Caller(stackLevel)
if strings.Contains(file, "assert") {
stackLevel++
} else {
if ok {
// Truncate file name at last file name separator.
if index := strings.LastIndex(file, "/"); index >= 0 {
file = file[index+1:]
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
file = file[index+1:]
}
} else {
file = "???"
line = 1
}
return file, line
}
}
}
// decorate prefixes the string with the file and line of the call site
// and inserts the final newline if needed and indentation tabs for formatting.
func decorate(s string) string {
file, line := getCaller()
buf := new(bytes.Buffer)
// Every line is indented at least one tab.
buf.WriteString(" ")
fmt.Fprintf(buf, "%s:%d: ", file, line)
lines := strings.Split(s, "\n")
if l := len(lines); l > 1 && lines[l-1] == "" {
lines = lines[:l-1]
}
for i, line := range lines {
if i > 0 {
// Second and subsequent lines are indented an extra tab.
buf.WriteString("\n\t\t")
}
buf.WriteString(line)
}
buf.WriteByte('\n')
return buf.String()
}
func Fail(message string) {
fmt.Println(decorate(message))
tGlobal.Fail()
}

View File

@ -6,13 +6,14 @@ import (
v2net "github.com/v2ray/v2ray-core/common/net"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
"github.com/v2ray/v2ray-core/testing/servers/udp"
"github.com/v2ray/v2ray-core/testing/unit"
)
func TestTCPConnection(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
targetPort := v2nettesting.PickPort()
tcpServer := &tcp.Server{
@ -78,7 +79,7 @@ func TestTCPConnection(t *testing.T) {
}
func TestTCPBind(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
targetPort := v2nettesting.PickPort()
tcpServer := &tcp.Server{
@ -125,7 +126,7 @@ func TestTCPBind(t *testing.T) {
}
func TestUDPAssociate(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
targetPort := v2nettesting.PickPort()
udpServer := &udp.Server{

View File

@ -1,53 +0,0 @@
package unit
import (
"testing"
)
// Assertion is an assertion library inspired by Truth.
// See http://google.github.io/truth/
type Assertion struct {
t *testing.T
}
func Assert(t *testing.T) *Assertion {
assert := new(Assertion)
assert.t = t
return assert
}
func (a *Assertion) Int64(value int64) *Int64Subject {
return NewInt64Subject(NewSubject(a), value)
}
func (a *Assertion) Int(value int) *IntSubject {
return NewIntSubject(NewSubject(a), value)
}
func (a *Assertion) Uint16(value uint16) *Uint16Subject {
return NewUint16Subject(NewSubject(a), value)
}
func (a *Assertion) Byte(value byte) *ByteSubject {
return NewByteSubject(NewSubject(a), value)
}
func (a *Assertion) Bytes(value []byte) *BytesSubject {
return NewBytesSubject(NewSubject(a), value)
}
func (a *Assertion) String(value string) *StringSubject {
return NewStringSubject(NewSubject(a), value)
}
func (a *Assertion) Error(value error) *ErrorSubject {
return NewErrorSubject(NewSubject(a), value)
}
func (a *Assertion) Bool(value bool) *BoolSubject {
return NewBoolSubject(NewSubject(a), value)
}
func (a *Assertion) Pointer(value interface{}) *PointerSubject {
return NewPointerSubject(NewSubject(a), value)
}

View File

@ -1,76 +0,0 @@
package unit
import (
"bytes"
"fmt"
"runtime"
"strings"
)
type Subject struct {
assert *Assertion
name string
}
func NewSubject(assert *Assertion) *Subject {
return &Subject{
assert: assert,
name: "",
}
}
// decorate prefixes the string with the file and line of the call site
// and inserts the final newline if needed and indentation tabs for formatting.
func decorate(s string) string {
_, file, line, ok := runtime.Caller(3)
if strings.Contains(file, "testing") {
_, file, line, ok = runtime.Caller(4)
}
if ok {
// Truncate file name at last file name separator.
if index := strings.LastIndex(file, "/"); index >= 0 {
file = file[index+1:]
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
file = file[index+1:]
}
} else {
file = "???"
line = 1
}
buf := new(bytes.Buffer)
// Every line is indented at least one tab.
buf.WriteString(" ")
fmt.Fprintf(buf, "%s:%d: ", file, line)
lines := strings.Split(s, "\n")
if l := len(lines); l > 1 && lines[l-1] == "" {
lines = lines[:l-1]
}
for i, line := range lines {
if i > 0 {
// Second and subsequent lines are indented an extra tab.
buf.WriteString("\n\t\t")
}
buf.WriteString(line)
}
buf.WriteByte('\n')
return buf.String()
}
func (subject *Subject) FailWithMessage(message string) {
fmt.Println(decorate(message))
subject.assert.t.Fail()
}
func (subject *Subject) Named(name string) {
subject.name = name
}
func (subject *Subject) DisplayString(value string) string {
if len(value) == 0 {
value = "unknown"
}
if len(subject.name) == 0 {
return "<" + value + ">"
}
return subject.name + "(<" + value + ">)"
}

View File

@ -6,7 +6,7 @@ import (
"path/filepath"
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
"github.com/v2ray/v2ray-core/testing/assert"
)
func cleanBinPath() {
@ -31,7 +31,7 @@ func allFilesExists(files ...string) bool {
}
func TestBuildMacOS(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
binPath = filepath.Join(os.Getenv("GOPATH"), "testing")
cleanBinPath()

View File

@ -3,11 +3,11 @@ package main
import (
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestParseOS(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
assert.Pointer(parseOS("windows")).Equals(Windows)
assert.Pointer(parseOS("macos")).Equals(MacOS)
@ -16,7 +16,7 @@ func TestParseOS(t *testing.T) {
}
func TestParseArch(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
assert.Pointer(parseArch("x86")).Equals(X86)
assert.Pointer(parseArch("x64")).Equals(Amd64)

View File

@ -11,11 +11,11 @@ import (
"testing"
"time"
"github.com/v2ray/v2ray-core/testing/unit"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestBuildAndRun(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
gopath := os.Getenv("GOPATH")
target := filepath.Join(gopath, "src", "v2ray_test")

View File

@ -3,11 +3,11 @@ package git
import (
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestRevParse(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
rev, err := RevParse("HEAD")
assert.Error(err).IsNil()
@ -15,7 +15,7 @@ func TestRevParse(t *testing.T) {
}
func TestRepoVersion(t *testing.T) {
assert := unit.Assert(t)
v2testing.Current(t)
version, err := RepoVersionHead()
assert.Error(err).IsNil()