Browse Source

update tests

pull/1435/head
Darien Raymond 6 years ago
parent
commit
24288a74a2
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 24
      app/dns/hosts_test.go
  2. 10
      app/dns/nameserver_test.go
  3. 12
      app/log/command/command_test.go
  4. 10
      common/compare/string.go
  5. 7
      common/errors/errors_test.go
  6. 7
      common/log/log_test.go
  7. 5
      common/protocol/address_test.go
  8. 5
      common/protocol/http/sniff_test.go
  9. 5
      common/protocol/tls/sniff_test.go
  10. 7
      common/serial/serial_test.go
  11. 6
      common/uuid/uuid_test.go

24
app/dns/hosts_test.go

@ -3,13 +3,13 @@ package dns_test
import (
"testing"
"github.com/google/go-cmp/cmp"
. "v2ray.com/core/app/dns"
. "v2ray.com/ext/assert"
"v2ray.com/core/common"
)
func TestStaticHosts(t *testing.T) {
assert := With(t)
pb := []*Config_HostMapping{
{
Type: DomainMatchingType_Full,
@ -28,17 +28,25 @@ func TestStaticHosts(t *testing.T) {
}
hosts, err := NewStaticHosts(pb, nil)
assert(err, IsNil)
common.Must(err)
{
ips := hosts.LookupIP("v2ray.com")
assert(len(ips), Equals, 1)
assert([]byte(ips[0]), Equals, []byte{1, 1, 1, 1})
if len(ips) != 1 {
t.Error("expect 1 IP, but got ", len(ips))
}
if diff := cmp.Diff([]byte(ips[0]), []byte{1, 1, 1, 1}); diff != "" {
t.Error(diff)
}
}
{
ips := hosts.LookupIP("www.v2ray.cn")
assert(len(ips), Equals, 1)
assert([]byte(ips[0]), Equals, []byte{2, 2, 2, 2})
if len(ips) != 1 {
t.Error("expect 1 IP, but got ", len(ips))
}
if diff := cmp.Diff([]byte(ips[0]), []byte{2, 2, 2, 2}); diff != "" {
t.Error(diff)
}
}
}

10
app/dns/nameserver_test.go

@ -6,16 +6,16 @@ import (
"time"
. "v2ray.com/core/app/dns"
. "v2ray.com/ext/assert"
"v2ray.com/core/common"
)
func TestLocalNameServer(t *testing.T) {
assert := With(t)
s := NewLocalNameServer()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
ips, err := s.QueryIP(ctx, "google.com")
cancel()
assert(err, IsNil)
assert(len(ips), GreaterThan, 0)
common.Must(err)
if len(ips) == 0 {
t.Error("expect some ips, but got 0")
}
}

12
app/log/command/command_test.go

@ -11,13 +11,11 @@ import (
"v2ray.com/core/app/proxyman"
_ "v2ray.com/core/app/proxyman/inbound"
_ "v2ray.com/core/app/proxyman/outbound"
"v2ray.com/core/common"
"v2ray.com/core/common/serial"
. "v2ray.com/ext/assert"
)
func TestLoggerRestart(t *testing.T) {
assert := With(t)
v, err := core.New(&core.Config{
App: []*serial.TypedMessage{
serial.ToTypedMessage(&log.Config{}),
@ -26,13 +24,11 @@ func TestLoggerRestart(t *testing.T) {
serial.ToTypedMessage(&proxyman.OutboundConfig{}),
},
})
assert(err, IsNil)
assert(v.Start(), IsNil)
common.Must(err)
common.Must(v.Start())
server := &LoggerServer{
V: v,
}
_, err = server.RestartLogger(context.Background(), &RestartLoggerRequest{})
assert(err, IsNil)
common.Must2(server.RestartLogger(context.Background(), &RestartLoggerRequest{}))
}

10
common/compare/string.go

@ -1,10 +0,0 @@
package compare
import "v2ray.com/core/common/errors"
func StringEqualWithDetail(a string, b string) error {
if a != b {
return errors.New("Got ", b, " but want ", a)
}
return nil
}

7
common/errors/errors_test.go

@ -4,7 +4,8 @@ import (
"io"
"testing"
"v2ray.com/core/common/compare"
"github.com/google/go-cmp/cmp"
. "v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
. "v2ray.com/ext/assert"
@ -46,8 +47,8 @@ func TestErrorMessage(t *testing.T) {
}
for _, d := range data {
if err := compare.StringEqualWithDetail(d.msg, d.err.Error()); err != nil {
t.Fatal(err)
if diff := cmp.Diff(d.msg, d.err.Error()); diff != "" {
t.Error(diff)
}
}
}

7
common/log/log_test.go

@ -3,7 +3,8 @@ package log_test
import (
"testing"
"v2ray.com/core/common/compare"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common/log"
"v2ray.com/core/common/net"
)
@ -26,7 +27,7 @@ func TestLogRecord(t *testing.T) {
Content: net.ParseAddress(ip),
})
if err := compare.StringEqualWithDetail("[Error] "+ip, logger.value); err != nil {
t.Fatal(err)
if diff := cmp.Diff("[Error] "+ip, logger.value); diff != "" {
t.Error(diff)
}
}

5
common/protocol/address_test.go

@ -4,9 +4,10 @@ import (
"bytes"
"testing"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/compare"
"v2ray.com/core/common/net"
. "v2ray.com/core/common/protocol"
)
@ -134,7 +135,7 @@ func TestAddressWriting(t *testing.T) {
}
} else {
common.Must(err)
if err := compare.BytesEqualWithDetail(tc.Bytes, b.Bytes()); err != nil {
if diff := cmp.Diff(tc.Bytes, b.Bytes()); diff != "" {
t.Error(err)
}
}

5
common/protocol/http/sniff_test.go

@ -3,7 +3,6 @@ package http_test
import (
"testing"
"v2ray.com/core/common/compare"
. "v2ray.com/core/common/protocol/http"
)
@ -98,8 +97,8 @@ first_name=John&last_name=Doe&action=Submit`,
if err != nil {
t.Errorf("Expect no error but actually %s in test %v", err.Error(), test)
}
if err := compare.StringEqualWithDetail(header.Domain(), test.domain); err != nil {
t.Error(err)
if header.Domain() != test.domain {
t.Error("expected domain ", test.domain, " but got ", header.Domain())
}
}
}

5
common/protocol/tls/sniff_test.go

@ -3,7 +3,6 @@ package tls_test
import (
"testing"
"v2ray.com/core/common/compare"
. "v2ray.com/core/common/protocol/tls"
)
@ -94,8 +93,8 @@ func TestTLSHeaders(t *testing.T) {
if err != nil {
t.Errorf("Expect no error but actually %s in test %v", err.Error(), test)
}
if err := compare.StringEqualWithDetail(header.Domain(), test.domain); err != nil {
t.Error(err)
if header.Domain() != test.domain {
t.Error("expect domain ", test.domain, " but got ", header.Domain())
}
}
}

7
common/serial/serial_test.go

@ -3,9 +3,10 @@ package serial_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/compare"
"v2ray.com/core/common/serial"
)
@ -18,7 +19,7 @@ func TestUint32Serial(t *testing.T) {
if n != 4 {
t.Error("expect 4 bytes writtng, but actually ", n)
}
if err := compare.BytesEqualWithDetail(b.Bytes(), []byte{0, 0, 0, 10}); err != nil {
t.Error(err)
if diff := cmp.Diff(b.Bytes(), []byte{0, 0, 0, 10}); diff != "" {
t.Error(diff)
}
}

6
common/uuid/uuid_test.go

@ -3,6 +3,8 @@ package uuid_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"v2ray.com/core/common"
"v2ray.com/core/common/compare"
. "v2ray.com/core/common/uuid"
@ -15,8 +17,8 @@ func TestParseBytes(t *testing.T) {
uuid, err := ParseBytes(bytes)
common.Must(err)
if err := compare.StringEqualWithDetail(uuid.String(), str); err != nil {
t.Fatal(err)
if diff := cmp.Diff(uuid.String(), str); diff != "" {
t.Error(diff)
}
_, err = ParseBytes([]byte{1, 3, 2, 4})

Loading…
Cancel
Save