1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

remove dep of assert lib

This commit is contained in:
Darien Raymond 2019-02-09 15:46:48 +01:00
parent 481b3fd294
commit 932e09a388
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
7 changed files with 133 additions and 143 deletions

View File

@ -4,15 +4,15 @@ import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"v2ray.com/core/app/stats"
. "v2ray.com/core/app/stats/command"
"v2ray.com/core/common"
. "v2ray.com/ext/assert"
)
func TestGetStats(t *testing.T) {
assert := With(t)
m, err := stats.NewManager(context.Background(), &stats.Config{})
common.Must(err)
@ -49,18 +49,19 @@ func TestGetStats(t *testing.T) {
Reset_: tc.reset,
})
if tc.err {
assert(err, IsNotNil)
if err == nil {
t.Error("nil error: ", tc.name)
}
} else {
common.Must(err)
assert(resp.Stat.Name, Equals, tc.name)
assert(resp.Stat.Value, Equals, tc.value)
if r := cmp.Diff(resp.Stat, &Stat{Name: tc.name, Value: tc.value}); r != "" {
t.Error(r)
}
}
}
}
func TestQueryStats(t *testing.T) {
assert := With(t)
m, err := stats.NewManager(context.Background(), &stats.Config{})
common.Must(err)
@ -81,23 +82,10 @@ func TestQueryStats(t *testing.T) {
Pattern: "counter_",
})
common.Must(err)
assert(len(resp.Stat), Equals, 2)
v2 := false
v3 := false
for _, sc := range resp.Stat {
switch sc.Name {
case "test_counter_2":
assert(sc.Value, Equals, int64(2))
v2 = true
case "test_counter_3":
assert(sc.Value, Equals, int64(3))
v3 = true
default:
t.Error("unexpected stat name: ", sc.Name)
t.Fail()
if r := cmp.Diff(resp.Stat, []*Stat{
{Name: "test_counter_2", Value: 2},
{Name: "test_counter_3", Value: 3},
}, cmpopts.SortSlices(func(s1, s2 *Stat) bool { return s1.Name < s2.Name })); r != "" {
t.Error(r)
}
}
assert(v2, IsTrue)
assert(v3, IsTrue)
}

View File

@ -7,15 +7,14 @@ import (
"io"
"testing"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common"
. "v2ray.com/core/common/buf"
"v2ray.com/core/transport/pipe"
. "v2ray.com/ext/assert"
)
func TestWriter(t *testing.T) {
assert := With(t)
lb := New()
common.Must2(lb.ReadFrom(rand.Reader))
@ -25,53 +24,57 @@ func TestWriter(t *testing.T) {
writer := NewBufferedWriter(NewWriter(writeBuffer))
writer.SetBuffered(false)
err := writer.WriteMultiBuffer(MultiBuffer{lb})
common.Must(err)
assert(writer.Flush(), IsNil)
assert(expectedBytes, Equals, writeBuffer.Bytes())
common.Must(writer.WriteMultiBuffer(MultiBuffer{lb}))
common.Must(writer.Flush())
if r := cmp.Diff(expectedBytes, writeBuffer.Bytes()); r != "" {
t.Error(r)
}
}
func TestBytesWriterReadFrom(t *testing.T) {
assert := With(t)
const size = 50000
pReader, pWriter := pipe.New(pipe.WithSizeLimit(size))
reader := bufio.NewReader(io.LimitReader(rand.Reader, size))
writer := NewBufferedWriter(pWriter)
writer.SetBuffered(false)
nBytes, err := reader.WriteTo(writer)
assert(nBytes, Equals, int64(size))
if nBytes != size {
t.Fatal("unexpected size of bytes written: ", nBytes)
}
if err != nil {
t.Fatal("expect success, but actually error: ", err.Error())
}
mb, err := pReader.ReadMultiBuffer()
common.Must(err)
assert(mb.Len(), Equals, int32(size))
if mb.Len() != size {
t.Fatal("unexpected size read: ", mb.Len())
}
}
func TestDiscardBytes(t *testing.T) {
assert := With(t)
b := New()
common.Must2(b.ReadFullFrom(rand.Reader, Size))
nBytes, err := io.Copy(DiscardBytes, b)
assert(nBytes, Equals, int64(Size))
common.Must(err)
if nBytes != Size {
t.Error("copy size: ", nBytes)
}
}
func TestDiscardBytesMultiBuffer(t *testing.T) {
assert := With(t)
const size = 10240*1024 + 1
buffer := bytes.NewBuffer(make([]byte, 0, size))
common.Must2(buffer.ReadFrom(io.LimitReader(rand.Reader, size)))
r := NewReader(buffer)
nBytes, err := io.Copy(DiscardBytes, &BufferedReader{Reader: r})
assert(nBytes, Equals, int64(size))
common.Must(err)
if nBytes != size {
t.Error("copy size: ", nBytes)
}
}
func TestWriterInterface(t *testing.T) {

View File

@ -8,12 +8,9 @@ import (
"v2ray.com/core/common"
. "v2ray.com/core/common/platform"
. "v2ray.com/ext/assert"
)
func TestNormalizeEnvName(t *testing.T) {
assert := With(t)
cases := []struct {
input string
output string
@ -32,44 +29,37 @@ func TestNormalizeEnvName(t *testing.T) {
},
}
for _, test := range cases {
assert(NormalizeEnvName(test.input), Equals, test.output)
if v := NormalizeEnvName(test.input); v != test.output {
t.Error("unexpected output: ", v, " want ", test.output)
}
}
}
func TestEnvFlag(t *testing.T) {
assert := With(t)
assert(EnvFlag{
if v := EnvFlag{
Name: "xxxxx.y",
}.GetValueAsInt(10), Equals, 10)
}.GetValueAsInt(10); v != 10 {
t.Error("env value: ", v)
}
}
func TestGetAssetLocation(t *testing.T) {
assert := With(t)
exec, err := os.Executable()
common.Must(err)
loc := GetAssetLocation("t")
assert(filepath.Dir(loc), Equals, filepath.Dir(exec))
if filepath.Dir(loc) != filepath.Dir(exec) {
t.Error("asset dir: ", loc, " not in ", exec)
}
os.Setenv("v2ray.location.asset", "/v2ray")
if runtime.GOOS == "windows" {
assert(GetAssetLocation("t"), Equals, "\\v2ray\\t")
if v := GetAssetLocation("t"); v != "\\v2ray\\t" {
t.Error("asset loc: ", v)
}
} else {
assert(GetAssetLocation("t"), Equals, "/v2ray/t")
if v := GetAssetLocation("t"); v != "/v2ray/t" {
t.Error("asset loc: ", v)
}
}
func TestGetPluginLocation(t *testing.T) {
assert := With(t)
exec, err := os.Executable()
common.Must(err)
loc := GetPluginDirectory()
assert(loc, Equals, filepath.Join(filepath.Dir(exec), "plugins"))
os.Setenv("V2RAY_LOCATION_PLUGIN", "/v2ray")
assert(GetPluginDirectory(), Equals, "/v2ray")
}

View File

@ -7,7 +7,6 @@ import (
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
. "v2ray.com/core/common/retry"
. "v2ray.com/ext/assert"
)
var (
@ -15,8 +14,6 @@ var (
)
func TestNoRetry(t *testing.T) {
assert := With(t)
startTime := time.Now().Unix()
err := Timed(10, 100000).On(func() error {
return nil
@ -24,12 +21,12 @@ func TestNoRetry(t *testing.T) {
endTime := time.Now().Unix()
common.Must(err)
assert(endTime-startTime, AtLeast, int64(0))
if endTime < startTime {
t.Error("endTime < startTime: ", startTime, " -> ", endTime)
}
}
func TestRetryOnce(t *testing.T) {
assert := With(t)
startTime := time.Now()
called := 0
err := Timed(10, 1000).On(func() error {
@ -42,12 +39,12 @@ func TestRetryOnce(t *testing.T) {
duration := time.Since(startTime)
common.Must(err)
assert(int64(duration/time.Millisecond), AtLeast, int64(900))
if v := int64(duration / time.Millisecond); v < 900 {
t.Error("duration: ", v)
}
}
func TestRetryMultiple(t *testing.T) {
assert := With(t)
startTime := time.Now()
called := 0
err := Timed(10, 1000).On(func() error {
@ -60,12 +57,12 @@ func TestRetryMultiple(t *testing.T) {
duration := time.Since(startTime)
common.Must(err)
assert(int64(duration/time.Millisecond), AtLeast, int64(4900))
if v := int64(duration / time.Millisecond); v < 4900 {
t.Error("duration: ", v)
}
}
func TestRetryExhausted(t *testing.T) {
assert := With(t)
startTime := time.Now()
called := 0
err := Timed(2, 1000).On(func() error {
@ -74,13 +71,16 @@ func TestRetryExhausted(t *testing.T) {
})
duration := time.Since(startTime)
assert(errors.Cause(err), Equals, ErrRetryFailed)
assert(int64(duration/time.Millisecond), AtLeast, int64(1900))
if errors.Cause(err) != ErrRetryFailed {
t.Error("cause: ", err)
}
if v := int64(duration / time.Millisecond); v < 1900 {
t.Error("duration: ", v)
}
}
func TestExponentialBackoff(t *testing.T) {
assert := With(t)
startTime := time.Now()
called := 0
err := ExponentialBackoff(10, 100).On(func() error {
@ -89,6 +89,10 @@ func TestExponentialBackoff(t *testing.T) {
})
duration := time.Since(startTime)
assert(errors.Cause(err), Equals, ErrRetryFailed)
assert(int64(duration/time.Millisecond), AtLeast, int64(4000))
if errors.Cause(err) != ErrRetryFailed {
t.Error("cause: ", err)
}
if v := int64(duration / time.Millisecond); v < 4000 {
t.Error("duration: ", v)
}
}

View File

@ -7,7 +7,6 @@ import (
"v2ray.com/core/common"
. "v2ray.com/core/common/uuid"
. "v2ray.com/ext/assert"
)
func TestParseBytes(t *testing.T) {
@ -48,33 +47,36 @@ func TestParseString(t *testing.T) {
}
func TestNewUUID(t *testing.T) {
assert := With(t)
uuid := New()
uuid2, err := ParseString(uuid.String())
common.Must(err)
assert(uuid.String(), Equals, uuid2.String())
assert(uuid.Bytes(), Equals, uuid2.Bytes())
if uuid.String() != uuid2.String() {
t.Error("uuid string: ", uuid.String(), " != ", uuid2.String())
}
if r := cmp.Diff(uuid.Bytes(), uuid2.Bytes()); r != "" {
t.Error(r)
}
}
func TestRandom(t *testing.T) {
assert := With(t)
uuid := New()
uuid2 := New()
assert(uuid.String(), NotEquals, uuid2.String())
assert(uuid.Bytes(), NotEquals, uuid2.Bytes())
if uuid.String() == uuid2.String() {
t.Error("duplicated uuid")
}
}
func TestEquals(t *testing.T) {
assert := With(t)
var uuid *UUID
var uuid2 *UUID
assert(uuid.Equals(uuid2), IsTrue)
if !uuid.Equals(uuid2) {
t.Error("empty uuid should equal")
}
uuid3 := New()
assert(uuid.Equals(&uuid3), IsFalse)
if uuid.Equals(&uuid3) {
t.Error("nil uuid equals non-nil uuid")
}
}

View File

@ -10,6 +10,8 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"v2ray.com/core"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
@ -20,12 +22,9 @@ import (
v2http "v2ray.com/core/proxy/http"
v2httptest "v2ray.com/core/testing/servers/http"
"v2ray.com/core/testing/servers/tcp"
. "v2ray.com/ext/assert"
)
func TestHttpConformance(t *testing.T) {
assert := With(t)
httpServerPort := tcp.PickPort()
httpServer := &v2httptest.Server{
Port: httpServerPort,
@ -55,6 +54,7 @@ func TestHttpConformance(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
transport := &http.Transport{
@ -69,20 +69,19 @@ func TestHttpConformance(t *testing.T) {
resp, err := client.Get("http://127.0.0.1:" + httpServerPort.String())
common.Must(err)
assert(resp.StatusCode, Equals, 200)
if resp.StatusCode != 200 {
t.Fatal("status: ", resp.StatusCode)
}
content, err := ioutil.ReadAll(resp.Body)
common.Must(err)
assert(string(content), Equals, "Home")
if string(content) != "Home" {
t.Fatal("body: ", string(content))
}
}
CloseAllServers(servers)
}
func TestHttpError(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: func(msg []byte) []byte {
return []byte{}
@ -116,6 +115,7 @@ func TestHttpError(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
transport := &http.Transport{
@ -130,15 +130,13 @@ func TestHttpError(t *testing.T) {
resp, err := client.Get("http://127.0.0.1:" + dest.Port.String())
common.Must(err)
assert(resp.StatusCode, Equals, 503)
if resp.StatusCode != 503 {
t.Error("status: ", resp.StatusCode)
}
}
CloseAllServers(servers)
}
func TestHttpConnectMethod(t *testing.T) {
assert := With(t)
tcpServer := tcp.Server{
MsgProcessor: xor,
}
@ -166,6 +164,7 @@ func TestHttpConnectMethod(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
transport := &http.Transport{
@ -187,21 +186,19 @@ func TestHttpConnectMethod(t *testing.T) {
resp, err := client.Do(req)
common.Must(err)
assert(resp.StatusCode, Equals, 200)
if resp.StatusCode != 200 {
t.Fatal("status: ", resp.StatusCode)
}
content := make([]byte, len(payload))
common.Must2(io.ReadFull(resp.Body, content))
common.Must(err)
assert(content, Equals, xor(payload))
if r := cmp.Diff(content, xor(payload)); r != "" {
t.Fatal(r)
}
}
CloseAllServers(servers)
}
func TestHttpPost(t *testing.T) {
assert := With(t)
httpServerPort := tcp.PickPort()
httpServer := &v2httptest.Server{
Port: httpServerPort,
@ -245,6 +242,7 @@ func TestHttpPost(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
transport := &http.Transport{
@ -262,15 +260,16 @@ func TestHttpPost(t *testing.T) {
resp, err := client.Post("http://127.0.0.1:"+httpServerPort.String()+"/testpost", "application/x-www-form-urlencoded", bytes.NewReader(payload))
common.Must(err)
assert(resp.StatusCode, Equals, 200)
if resp.StatusCode != 200 {
t.Fatal("status: ", resp.StatusCode)
}
content, err := ioutil.ReadAll(resp.Body)
common.Must(err)
assert(content, Equals, xor(payload))
if r := cmp.Diff(content, xor(payload)); r != "" {
t.Fatal(r)
}
}
CloseAllServers(servers)
}
func setProxyBasicAuth(req *http.Request, user, pass string) {
@ -280,8 +279,6 @@ func setProxyBasicAuth(req *http.Request, user, pass string) {
}
func TestHttpBasicAuth(t *testing.T) {
assert := With(t)
httpServerPort := tcp.PickPort()
httpServer := &v2httptest.Server{
Port: httpServerPort,
@ -315,6 +312,7 @@ func TestHttpBasicAuth(t *testing.T) {
servers, err := InitializeServerConfigs(serverConfig)
common.Must(err)
defer CloseAllServers(servers)
{
transport := &http.Transport{
@ -330,7 +328,9 @@ func TestHttpBasicAuth(t *testing.T) {
{
resp, err := client.Get("http://127.0.0.1:" + httpServerPort.String())
common.Must(err)
assert(resp.StatusCode, Equals, 407)
if resp.StatusCode != 407 {
t.Fatal("status: ", resp.StatusCode)
}
}
{
@ -340,7 +340,9 @@ func TestHttpBasicAuth(t *testing.T) {
setProxyBasicAuth(req, "a", "c")
resp, err := client.Do(req)
common.Must(err)
assert(resp.StatusCode, Equals, 407)
if resp.StatusCode != 407 {
t.Fatal("status: ", resp.StatusCode)
}
}
{
@ -350,13 +352,15 @@ func TestHttpBasicAuth(t *testing.T) {
setProxyBasicAuth(req, "a", "b")
resp, err := client.Do(req)
common.Must(err)
assert(resp.StatusCode, Equals, 200)
if resp.StatusCode != 200 {
t.Fatal("status: ", resp.StatusCode)
}
content, err := ioutil.ReadAll(resp.Body)
common.Must(err)
assert(string(content), Equals, "Home")
if string(content) != "Home" {
t.Fatal("body: ", string(content))
}
}
}
CloseAllServers(servers)
}

View File

@ -9,12 +9,9 @@ import (
"v2ray.com/core/common"
"v2ray.com/core/common/protocol/tls/cert"
. "v2ray.com/core/transport/internet/tls"
. "v2ray.com/ext/assert"
)
func TestCertificateIssuing(t *testing.T) {
assert := With(t)
certificate := ParseCertificate(cert.MustGenerate(nil, cert.Authority(true), cert.KeyUsage(x509.KeyUsageCertSign)))
certificate.Usage = Certificate_AUTHORITY_ISSUE
@ -32,12 +29,12 @@ func TestCertificateIssuing(t *testing.T) {
x509Cert, err := x509.ParseCertificate(v2rayCert.Certificate[0])
common.Must(err)
assert(x509Cert.NotAfter.After(time.Now()), IsTrue)
if !x509Cert.NotAfter.After(time.Now()) {
t.Error("NotAfter: ", x509Cert.NotAfter)
}
}
func TestExpiredCertificate(t *testing.T) {
assert := With(t)
caCert := cert.MustGenerate(nil, cert.Authority(true), cert.KeyUsage(x509.KeyUsageCertSign))
expiredCert := cert.MustGenerate(caCert, cert.NotAfter(time.Now().Add(time.Minute*-2)), cert.CommonName("www.v2ray.com"), cert.DNSNames("www.v2ray.com"))
@ -61,7 +58,9 @@ func TestExpiredCertificate(t *testing.T) {
x509Cert, err := x509.ParseCertificate(v2rayCert.Certificate[0])
common.Must(err)
assert(x509Cert.NotAfter.After(time.Now()), IsTrue)
if !x509Cert.NotAfter.After(time.Now()) {
t.Error("NotAfter: ", x509Cert.NotAfter)
}
}
func TestInsecureCertificates(t *testing.T) {