1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-09 09:20:45 +00:00

remove dependency on assert lib

This commit is contained in:
Darien Raymond 2019-01-31 20:57:01 +01:00
parent 25f0546228
commit c9958681f7
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
7 changed files with 81 additions and 53 deletions

View File

@ -18,7 +18,6 @@ import (
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/protocol/http"
"v2ray.com/core/common/session"
. "v2ray.com/ext/assert"
"v2ray.com/ext/sysio"
)
@ -266,23 +265,46 @@ func loadGeoSite(country string) ([]*Domain, error) {
}
func TestChinaSites(t *testing.T) {
assert := With(t)
common.Must(sysio.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(os.Getenv("GOPATH"), "src", "v2ray.com", "core", "release", "config", "geosite.dat")))
domains, err := loadGeoSite("CN")
assert(err, IsNil)
common.Must(err)
matcher, err := NewDomainMatcher(domains)
common.Must(err)
assert(matcher.ApplyDomain("163.com"), IsTrue)
assert(matcher.ApplyDomain("163.com"), IsTrue)
assert(matcher.ApplyDomain("164.com"), IsFalse)
assert(matcher.ApplyDomain("164.com"), IsFalse)
type TestCase struct {
Domain string
Output bool
}
testCases := []TestCase{
{
Domain: "163.com",
Output: true,
},
{
Domain: "163.com",
Output: true,
},
{
Domain: "164.com",
Output: false,
},
{
Domain: "164.com",
Output: false,
},
}
for i := 0; i < 1024; i++ {
assert(matcher.ApplyDomain(strconv.Itoa(i)+".not-exists.com"), IsFalse)
testCases = append(testCases, TestCase{Domain: strconv.Itoa(i) + ".not-exists.com", Output: false})
}
for _, testCase := range testCases {
r := matcher.ApplyDomain(testCase.Domain)
if r != testCase.Output {
t.Error("expected output ", testCase.Output, " for domain ", testCase.Domain, " but got ", r)
}
}
}

View File

@ -4,24 +4,33 @@ import (
"testing"
. "v2ray.com/core/common/bitmask"
. "v2ray.com/ext/assert"
)
func TestBitmaskByte(t *testing.T) {
assert := With(t)
b := Byte(0)
b.Set(Byte(1))
assert(b.Has(1), IsTrue)
if !b.Has(1) {
t.Fatal("expected ", b, " to contain 1, but actually not")
}
b.Set(Byte(2))
assert(b.Has(2), IsTrue)
assert(b.Has(1), IsTrue)
if !b.Has(2) {
t.Fatal("expected ", b, " to contain 2, but actually not")
}
if !b.Has(1) {
t.Fatal("expected ", b, " to contain 1, but actually not")
}
b.Clear(Byte(1))
assert(b.Has(2), IsTrue)
assert(b.Has(1), IsFalse)
if !b.Has(2) {
t.Fatal("expected ", b, " to contain 2, but actually not")
}
if b.Has(1) {
t.Fatal("expected ", b, " to not contain 1, but actually did")
}
b.Toggle(Byte(2))
assert(b.Has(2), IsFalse)
if b.Has(2) {
t.Fatal("expected ", b, " to not contain 2, but actually did")
}
}

View File

@ -4,15 +4,16 @@ import (
"testing"
. "v2ray.com/core/common/serial"
. "v2ray.com/ext/assert"
)
func TestGetInstance(t *testing.T) {
assert := With(t)
p, err := GetInstance("")
assert(p, IsNil)
assert(err, IsNotNil)
if p != nil {
t.Error("expected nil instance, but got ", p)
}
if err == nil {
t.Error("expect non-nil error, but got nil")
}
}
func TestConvertingNilMessage(t *testing.T) {

View File

@ -4,12 +4,9 @@ import (
"testing"
. "v2ray.com/core/common/signal/pubsub"
. "v2ray.com/ext/assert"
)
func TestPubsub(t *testing.T) {
assert := With(t)
service := NewService()
sub := service.Subscribe("a")
@ -17,7 +14,9 @@ func TestPubsub(t *testing.T) {
select {
case v := <-sub.Wait():
assert(v.(int), Equals, 1)
if v != 1 {
t.Error("expected subscribed value 1, but got ", v)
}
default:
t.Fail()
}

View File

@ -4,15 +4,11 @@ import (
"testing"
"time"
. "v2ray.com/core/common/task"
. "v2ray.com/ext/assert"
"v2ray.com/core/common"
. "v2ray.com/core/common/task"
)
func TestPeriodicTaskStop(t *testing.T) {
assert := With(t)
value := 0
task := &Periodic{
Interval: time.Second * 2,
@ -24,9 +20,13 @@ func TestPeriodicTaskStop(t *testing.T) {
common.Must(task.Start())
time.Sleep(time.Second * 5)
common.Must(task.Close())
assert(value, Equals, 3)
if value != 3 {
t.Fatal("expected 3, but got ", value)
}
time.Sleep(time.Second * 4)
assert(value, Equals, 3)
if value != 3 {
t.Fatal("expected 3, but got ", value)
}
common.Must(task.Start())
time.Sleep(time.Second * 3)
if value != 5 {

View File

@ -3,16 +3,16 @@ package encoding_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/uuid"
. "v2ray.com/core/proxy/vmess/encoding"
. "v2ray.com/ext/assert"
)
func TestSwitchAccount(t *testing.T) {
assert := With(t)
sa := &protocol.CommandSwitchAccount{
Port: 1234,
ID: uuid.New(),
@ -22,19 +22,16 @@ func TestSwitchAccount(t *testing.T) {
}
buffer := buf.New()
err := MarshalCommand(sa, buffer)
assert(err, IsNil)
common.Must(MarshalCommand(sa, buffer))
cmd, err := UnmarshalCommand(1, buffer.BytesFrom(2))
assert(err, IsNil)
common.Must(err)
sa2, ok := cmd.(*protocol.CommandSwitchAccount)
assert(ok, IsTrue)
assert(sa.Host, IsNil)
assert(sa2.Host, IsNil)
assert(sa.Port, Equals, sa2.Port)
assert(sa.ID.String(), Equals, sa2.ID.String())
assert(sa.AlterIds, Equals, sa2.AlterIds)
assert(byte(sa.Level), Equals, byte(sa2.Level))
assert(sa.ValidMin, Equals, sa2.ValidMin)
if !ok {
t.Fatal("failed to convert command to CommandSwitchAccount")
}
if r := cmp.Diff(sa2, sa); r != "" {
t.Error(r)
}
}

View File

@ -4,21 +4,21 @@ import (
"context"
"testing"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
. "v2ray.com/core/transport/internet/headers/wechat"
. "v2ray.com/ext/assert"
)
func TestUTPWrite(t *testing.T) {
assert := With(t)
videoRaw, err := NewVideoChat(context.Background(), &VideoConfig{})
assert(err, IsNil)
common.Must(err)
video := videoRaw.(*VideoChat)
payload := buf.New()
video.Serialize(payload.Extend(video.Size()))
assert(payload.Len(), Equals, video.Size())
if payload.Len() != video.Size() {
t.Error("expected payload size ", video.Size(), " but got ", payload.Len())
}
}