diff --git a/app/dispatcher/impl/sniffer_test.go b/app/dispatcher/impl/sniffer_test.go index 6b10c17a3..ce252de4b 100644 --- a/app/dispatcher/impl/sniffer_test.go +++ b/app/dispatcher/impl/sniffer_test.go @@ -4,11 +4,11 @@ import ( "testing" . "v2ray.com/core/app/dispatcher/impl" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestHTTPHeaders(t *testing.T) { - assert := assert.On(t) + assert := With(t) cases := []struct { input string @@ -94,13 +94,13 @@ first_name=John&last_name=Doe&action=Submit`, for _, test := range cases { domain, err := SniffHTTP([]byte(test.input)) - assert.String(domain).Equals(test.domain) - assert.Error(err).Equals(test.err) + assert(domain, Equals, test.domain) + assert(err, Equals, test.err) } } func TestTLSHeaders(t *testing.T) { - assert := assert.On(t) + assert := With(t) cases := []struct { input []byte @@ -180,7 +180,7 @@ func TestTLSHeaders(t *testing.T) { for _, test := range cases { domain, err := SniffTLS(test.input) - assert.String(domain).Equals(test.domain) - assert.Error(err).Equals(test.err) + assert(domain, Equals, test.domain) + assert(err, Equals, test.err) } } diff --git a/app/dns/server/querier.go b/app/dns/server/querier.go new file mode 100644 index 000000000..722c81632 --- /dev/null +++ b/app/dns/server/querier.go @@ -0,0 +1,20 @@ +package server + +import ( + "time" + + "v2ray.com/core/common/net" +) + +type IPResult struct { + IP []net.IP + TTL time.Duration +} + +type Querier interface { + QueryDomain(domain string) <-chan *IPResult +} + +type UDPQuerier struct { + server net.Destination +} diff --git a/app/dns/server/server.go b/app/dns/server/server.go index 2fedbedf5..567cf4576 100644 --- a/app/dns/server/server.go +++ b/app/dns/server/server.go @@ -24,6 +24,17 @@ type DomainRecord struct { A *ARecord } +type Record struct { + IP []net.IP + Expire time.Time + LastAccess time.Time +} + +func (r *Record) Expired() bool { + now := time.Now() + return r.Expire.Before(now) || r.LastAccess.Add(time.Hour).Before(now) +} + type CacheServer struct { sync.RWMutex hosts map[string]net.IP diff --git a/app/log/internal/log_entry_test.go b/app/log/internal/log_entry_test.go index c20b45eb5..c67185a03 100644 --- a/app/log/internal/log_entry_test.go +++ b/app/log/internal/log_entry_test.go @@ -4,11 +4,11 @@ import ( "testing" . "v2ray.com/core/app/log/internal" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestAccessLog(t *testing.T) { - assert := assert.On(t) + assert := With(t) entry := &AccessLog{ From: "test_from", @@ -18,8 +18,8 @@ func TestAccessLog(t *testing.T) { } entryStr := entry.String() - assert.String(entryStr).Contains("test_from") - assert.String(entryStr).Contains("test_to") - assert.String(entryStr).Contains("test_reason") - assert.String(entryStr).Contains("Accepted") + assert(entryStr, HasSubstring, "test_from") + assert(entryStr, HasSubstring, "test_to") + assert(entryStr, HasSubstring, "test_reason") + assert(entryStr, HasSubstring, "Accepted") } diff --git a/app/proxyman/mux/mux_test.go b/app/proxyman/mux/mux_test.go index 4276fbbfe..775f87f82 100644 --- a/app/proxyman/mux/mux_test.go +++ b/app/proxyman/mux/mux_test.go @@ -9,8 +9,8 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" - "v2ray.com/core/testing/assert" "v2ray.com/core/transport/ray" + . "v2ray.com/ext/assert" ) func readAll(reader buf.Reader) (buf.MultiBuffer, error) { @@ -29,7 +29,7 @@ func readAll(reader buf.Reader) (buf.MultiBuffer, error) { } func TestReaderWriter(t *testing.T) { - assert := assert.On(t) + assert := With(t) stream := ray.NewStream(context.Background()) @@ -48,16 +48,16 @@ func TestReaderWriter(t *testing.T) { return writer.Write(buf.NewMultiBufferValue(b)) } - assert.Error(writePayload(writer, 'a', 'b', 'c', 'd')).IsNil() - assert.Error(writePayload(writer2)).IsNil() + assert(writePayload(writer, 'a', 'b', 'c', 'd'), IsNil) + assert(writePayload(writer2), IsNil) - assert.Error(writePayload(writer, 'e', 'f', 'g', 'h')).IsNil() - assert.Error(writePayload(writer3, 'x')).IsNil() + assert(writePayload(writer, 'e', 'f', 'g', 'h'), IsNil) + assert(writePayload(writer3, 'x'), IsNil) writer.Close() writer3.Close() - assert.Error(writePayload(writer2, 'y')).IsNil() + assert(writePayload(writer2, 'y'), IsNil) writer2.Close() bytesReader := buf.ToBytesReader(stream) @@ -65,79 +65,79 @@ func TestReaderWriter(t *testing.T) { streamReader := NewStreamReader(bytesReader) meta, err := metaReader.Read() - assert.Error(err).IsNil() - assert.Uint16(meta.SessionID).Equals(1) - assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusNew)) - assert.Destination(meta.Target).Equals(dest) - assert.Byte(byte(meta.Option)).Equals(byte(OptionData)) + assert(err, IsNil) + assert(meta.SessionID, Equals, uint16(1)) + assert(byte(meta.SessionStatus), Equals, byte(SessionStatusNew)) + assert(meta.Target, Equals, dest) + assert(byte(meta.Option), Equals, byte(OptionData)) data, err := readAll(streamReader) - assert.Error(err).IsNil() - assert.Int(len(data)).Equals(1) - assert.String(data[0].String()).Equals("abcd") + assert(err, IsNil) + assert(len(data), Equals, 1) + assert(data[0].String(), Equals, "abcd") meta, err = metaReader.Read() - assert.Error(err).IsNil() - assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusNew)) - assert.Uint16(meta.SessionID).Equals(2) - assert.Byte(byte(meta.Option)).Equals(0) - assert.Destination(meta.Target).Equals(dest2) + assert(err, IsNil) + assert(byte(meta.SessionStatus), Equals, byte(SessionStatusNew)) + assert(meta.SessionID, Equals, uint16(2)) + assert(byte(meta.Option), Equals, byte(0)) + assert(meta.Target, Equals, dest2) meta, err = metaReader.Read() - assert.Error(err).IsNil() - assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusKeep)) - assert.Uint16(meta.SessionID).Equals(1) - assert.Byte(byte(meta.Option)).Equals(1) + assert(err, IsNil) + assert(byte(meta.SessionStatus), Equals, byte(SessionStatusKeep)) + assert(meta.SessionID, Equals, uint16(1)) + assert(byte(meta.Option), Equals, byte(1)) data, err = readAll(streamReader) - assert.Error(err).IsNil() - assert.Int(len(data)).Equals(1) - assert.String(data[0].String()).Equals("efgh") + assert(err, IsNil) + assert(len(data), Equals, 1) + assert(data[0].String(), Equals, "efgh") meta, err = metaReader.Read() - assert.Error(err).IsNil() - assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusNew)) - assert.Uint16(meta.SessionID).Equals(3) - assert.Byte(byte(meta.Option)).Equals(1) - assert.Destination(meta.Target).Equals(dest3) + assert(err, IsNil) + assert(byte(meta.SessionStatus), Equals, byte(SessionStatusNew)) + assert(meta.SessionID, Equals, uint16(3)) + assert(byte(meta.Option), Equals, byte(1)) + assert(meta.Target, Equals, dest3) data, err = readAll(streamReader) - assert.Error(err).IsNil() - assert.Int(len(data)).Equals(1) - assert.String(data[0].String()).Equals("x") + assert(err, IsNil) + assert(len(data), Equals, 1) + assert(data[0].String(), Equals, "x") meta, err = metaReader.Read() - assert.Error(err).IsNil() - assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusEnd)) - assert.Uint16(meta.SessionID).Equals(1) - assert.Byte(byte(meta.Option)).Equals(0) + assert(err, IsNil) + assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd)) + assert(meta.SessionID, Equals, uint16(1)) + assert(byte(meta.Option), Equals, byte(0)) meta, err = metaReader.Read() - assert.Error(err).IsNil() - assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusEnd)) - assert.Uint16(meta.SessionID).Equals(3) - assert.Byte(byte(meta.Option)).Equals(0) + assert(err, IsNil) + assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd)) + assert(meta.SessionID, Equals, uint16(3)) + assert(byte(meta.Option), Equals, byte(0)) meta, err = metaReader.Read() - assert.Error(err).IsNil() - assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusKeep)) - assert.Uint16(meta.SessionID).Equals(2) - assert.Byte(byte(meta.Option)).Equals(1) + assert(err, IsNil) + assert(byte(meta.SessionStatus), Equals, byte(SessionStatusKeep)) + assert(meta.SessionID, Equals, uint16(2)) + assert(byte(meta.Option), Equals, byte(1)) data, err = readAll(streamReader) - assert.Error(err).IsNil() - assert.Int(len(data)).Equals(1) - assert.String(data[0].String()).Equals("y") + assert(err, IsNil) + assert(len(data), Equals, 1) + assert(data[0].String(), Equals, "y") meta, err = metaReader.Read() - assert.Error(err).IsNil() - assert.Byte(byte(meta.SessionStatus)).Equals(byte(SessionStatusEnd)) - assert.Uint16(meta.SessionID).Equals(2) - assert.Byte(byte(meta.Option)).Equals(0) + assert(err, IsNil) + assert(byte(meta.SessionStatus), Equals, byte(SessionStatusEnd)) + assert(meta.SessionID, Equals, uint16(2)) + assert(byte(meta.Option), Equals, byte(0)) stream.Close() meta, err = metaReader.Read() - assert.Error(err).IsNotNil() - assert.Pointer(meta).IsNil() + assert(err, IsNotNil) + assert(meta, IsNil) } diff --git a/app/proxyman/mux/session_test.go b/app/proxyman/mux/session_test.go index 63953ecf6..4acffd8f0 100644 --- a/app/proxyman/mux/session_test.go +++ b/app/proxyman/mux/session_test.go @@ -4,36 +4,36 @@ import ( "testing" . "v2ray.com/core/app/proxyman/mux" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestSessionManagerAdd(t *testing.T) { - assert := assert.On(t) + assert := With(t) m := NewSessionManager() s := m.Allocate() - assert.Uint16(s.ID).Equals(1) - assert.Int(m.Size()).Equals(1) + assert(s.ID, Equals, uint16(1)) + assert(m.Size(), Equals, 1) s = m.Allocate() - assert.Uint16(s.ID).Equals(2) - assert.Int(m.Size()).Equals(2) + assert(s.ID, Equals, uint16(2)) + assert(m.Size(), Equals, 2) s = &Session{ ID: 4, } m.Add(s) - assert.Uint16(s.ID).Equals(4) + assert(s.ID, Equals, uint16(4)) } func TestSessionManagerClose(t *testing.T) { - assert := assert.On(t) + assert := With(t) m := NewSessionManager() s := m.Allocate() - assert.Bool(m.CloseIfNoSession()).IsFalse() + assert(m.CloseIfNoSession(), IsFalse) m.Remove(s.ID) - assert.Bool(m.CloseIfNoSession()).IsTrue() + assert(m.CloseIfNoSession(), IsTrue) } diff --git a/app/router/condition_test.go b/app/router/condition_test.go index 1fec521fe..f0283dc3d 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -8,11 +8,11 @@ import ( "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" "v2ray.com/core/proxy" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestSubDomainMatcher(t *testing.T) { - assert := assert.On(t) + assert := With(t) cases := []struct { pattern string @@ -47,12 +47,12 @@ func TestSubDomainMatcher(t *testing.T) { } for _, test := range cases { matcher := NewSubDomainMatcher(test.pattern) - assert.Bool(matcher.Apply(test.input) == test.output).IsTrue() + assert(matcher.Apply(test.input) == test.output, IsTrue) } } func TestRoutingRule(t *testing.T) { - assert := assert.On(t) + assert := With(t) type ruleTest struct { input context.Context @@ -172,10 +172,10 @@ func TestRoutingRule(t *testing.T) { for _, test := range cases { cond, err := test.rule.BuildCondition() - assert.Error(err).IsNil() + assert(err, IsNil) for _, t := range test.test { - assert.Bool(cond.Apply(t.input)).Equals(t.output) + assert(cond.Apply(t.input), Equals, t.output) } } } diff --git a/app/router/router_test.go b/app/router/router_test.go index e3db5cd0e..12350fb94 100644 --- a/app/router/router_test.go +++ b/app/router/router_test.go @@ -14,11 +14,11 @@ import ( . "v2ray.com/core/app/router" "v2ray.com/core/common/net" "v2ray.com/core/proxy" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestSimpleRouter(t *testing.T) { - assert := assert.On(t) + assert := With(t) config := &Config{ Rule: []*RoutingRule{ @@ -33,16 +33,16 @@ func TestSimpleRouter(t *testing.T) { space := app.NewSpace() ctx := app.ContextWithSpace(context.Background(), space) - assert.Error(app.AddApplicationToSpace(ctx, new(dns.Config))).IsNil() - assert.Error(app.AddApplicationToSpace(ctx, new(dispatcher.Config))).IsNil() - assert.Error(app.AddApplicationToSpace(ctx, new(proxyman.OutboundConfig))).IsNil() - assert.Error(app.AddApplicationToSpace(ctx, config)).IsNil() - assert.Error(space.Initialize()).IsNil() + assert(app.AddApplicationToSpace(ctx, new(dns.Config)), IsNil) + assert(app.AddApplicationToSpace(ctx, new(dispatcher.Config)), IsNil) + assert(app.AddApplicationToSpace(ctx, new(proxyman.OutboundConfig)), IsNil) + assert(app.AddApplicationToSpace(ctx, config), IsNil) + assert(space.Initialize(), IsNil) r := FromSpace(space) ctx = proxy.ContextWithTarget(ctx, net.TCPDestination(net.DomainAddress("v2ray.com"), 80)) tag, err := r.TakeDetour(ctx) - assert.Error(err).IsNil() - assert.String(tag).Equals("test") + assert(err, IsNil) + assert(tag, Equals, "test") } diff --git a/common/bitmask/byte_test.go b/common/bitmask/byte_test.go index 6e7e2e8b2..2ca2a7e32 100644 --- a/common/bitmask/byte_test.go +++ b/common/bitmask/byte_test.go @@ -4,24 +4,24 @@ import ( "testing" . "v2ray.com/core/common/bitmask" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestBitmaskByte(t *testing.T) { - assert := assert.On(t) + assert := With(t) b := Byte(0) b.Set(Byte(1)) - assert.Bool(b.Has(1)).IsTrue() + assert(b.Has(1), IsTrue) b.Set(Byte(2)) - assert.Bool(b.Has(2)).IsTrue() - assert.Bool(b.Has(1)).IsTrue() + assert(b.Has(2), IsTrue) + assert(b.Has(1), IsTrue) b.Clear(Byte(1)) - assert.Bool(b.Has(2)).IsTrue() - assert.Bool(b.Has(1)).IsFalse() + assert(b.Has(2), IsTrue) + assert(b.Has(1), IsFalse) b.Toggle(Byte(2)) - assert.Bool(b.Has(2)).IsFalse() + assert(b.Has(2), IsFalse) } diff --git a/common/buf/buffer_test.go b/common/buf/buffer_test.go index 6c0d2a50f..af05057ec 100644 --- a/common/buf/buffer_test.go +++ b/common/buf/buffer_test.go @@ -6,64 +6,64 @@ import ( . "v2ray.com/core/common/buf" "v2ray.com/core/common/serial" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestBufferClear(t *testing.T) { - assert := assert.On(t) + assert := With(t) buffer := New() defer buffer.Release() payload := "Bytes" buffer.Append([]byte(payload)) - assert.Int(buffer.Len()).Equals(len(payload)) + assert(buffer.Len(), Equals, len(payload)) buffer.Clear() - assert.Int(buffer.Len()).Equals(0) + assert(buffer.Len(), Equals, 0) } func TestBufferIsEmpty(t *testing.T) { - assert := assert.On(t) + assert := With(t) buffer := New() defer buffer.Release() - assert.Bool(buffer.IsEmpty()).IsTrue() + assert(buffer.IsEmpty(), IsTrue) } func TestBufferString(t *testing.T) { - assert := assert.On(t) + assert := With(t) buffer := New() defer buffer.Release() - assert.Error(buffer.AppendSupplier(serial.WriteString("Test String"))).IsNil() - assert.String(buffer.String()).Equals("Test String") + assert(buffer.AppendSupplier(serial.WriteString("Test String")), IsNil) + assert(buffer.String(), Equals, "Test String") } func TestBufferWrite(t *testing.T) { - assert := assert.On(t) + assert := With(t) buffer := NewLocal(8) nBytes, err := buffer.Write([]byte("abcd")) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(4) + assert(err, IsNil) + assert(nBytes, Equals, 4) nBytes, err = buffer.Write([]byte("abcde")) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(4) - assert.String(buffer.String()).Equals("abcdabcd") + assert(err, IsNil) + assert(nBytes, Equals, 4) + assert(buffer.String(), Equals, "abcdabcd") } func TestSyncPool(t *testing.T) { - assert := assert.On(t) + assert := With(t) p := NewSyncPool(32) b := p.Allocate() - assert.Int(b.Len()).Equals(0) + assert(b.Len(), Equals, 0) - assert.Error(b.AppendSupplier(ReadFrom(rand.Reader))).IsNil() - assert.Int(b.Len()).Equals(32) + assert(b.AppendSupplier(ReadFrom(rand.Reader)), IsNil) + assert(b.Len(), Equals, 32) b.Release() } diff --git a/common/buf/buffered_reader_test.go b/common/buf/buffered_reader_test.go index 1c0731535..7d23e4076 100644 --- a/common/buf/buffered_reader_test.go +++ b/common/buf/buffered_reader_test.go @@ -5,32 +5,32 @@ import ( "testing" . "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestBufferedReader(t *testing.T) { - assert := assert.On(t) + assert := With(t) content := New() - assert.Error(content.AppendSupplier(ReadFrom(rand.Reader))).IsNil() + assert(content.AppendSupplier(ReadFrom(rand.Reader)), IsNil) len := content.Len() reader := NewBufferedReader(content) - assert.Bool(reader.IsBuffered()).IsTrue() + assert(reader.IsBuffered(), IsTrue) payload := make([]byte, 16) nBytes, err := reader.Read(payload) - assert.Int(nBytes).Equals(16) - assert.Error(err).IsNil() + assert(nBytes, Equals, 16) + assert(err, IsNil) len2 := content.Len() - assert.Int(len - len2).GreaterThan(16) + assert(len - len2, GreaterThan, 16) nBytes, err = reader.Read(payload) - assert.Int(nBytes).Equals(16) - assert.Error(err).IsNil() + assert(nBytes, Equals, 16) + assert(err, IsNil) - assert.Int(content.Len()).Equals(len2) + assert(content.Len(), Equals, len2) } diff --git a/common/buf/buffered_writer_test.go b/common/buf/buffered_writer_test.go index 97cb923be..12154ee6c 100644 --- a/common/buf/buffered_writer_test.go +++ b/common/buf/buffered_writer_test.go @@ -6,49 +6,49 @@ import ( "v2ray.com/core/common" . "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestBufferedWriter(t *testing.T) { - assert := assert.On(t) + assert := With(t) content := New() writer := NewBufferedWriter(content) - assert.Bool(writer.IsBuffered()).IsTrue() + assert(writer.IsBuffered(), IsTrue) payload := make([]byte, 16) nBytes, err := writer.Write(payload) - assert.Int(nBytes).Equals(16) - assert.Error(err).IsNil() + assert(nBytes, Equals, 16) + assert(err, IsNil) - assert.Bool(content.IsEmpty()).IsTrue() + assert(content.IsEmpty(), IsTrue) - assert.Error(writer.SetBuffered(false)).IsNil() - assert.Int(content.Len()).Equals(16) + assert(writer.SetBuffered(false), IsNil) + assert(content.Len(), Equals, 16) } func TestBufferedWriterLargePayload(t *testing.T) { - assert := assert.On(t) + assert := With(t) content := NewLocal(128 * 1024) writer := NewBufferedWriter(content) - assert.Bool(writer.IsBuffered()).IsTrue() + assert(writer.IsBuffered(), IsTrue) payload := make([]byte, 64*1024) common.Must2(rand.Read(payload)) nBytes, err := writer.Write(payload[:512]) - assert.Int(nBytes).Equals(512) - assert.Error(err).IsNil() + assert(nBytes, Equals, 512) + assert(err, IsNil) - assert.Bool(content.IsEmpty()).IsTrue() + assert(content.IsEmpty(), IsTrue) nBytes, err = writer.Write(payload[512:]) - assert.Error(err).IsNil() - assert.Error(writer.Flush()).IsNil() - assert.Int(nBytes).Equals(64*1024 - 512) - assert.Bytes(content.Bytes()).Equals(payload) + assert(err, IsNil) + assert(writer.Flush(), IsNil) + assert(nBytes, Equals, 64*1024 - 512) + assert(content.Bytes(), Equals, payload) } diff --git a/common/buf/multi_buffer_test.go b/common/buf/multi_buffer_test.go index bf7059d44..92df32f02 100644 --- a/common/buf/multi_buffer_test.go +++ b/common/buf/multi_buffer_test.go @@ -4,11 +4,11 @@ import ( "testing" . "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestMultiBufferRead(t *testing.T) { - assert := assert.On(t) + assert := With(t) b1 := New() b1.AppendBytes('a', 'b') @@ -19,17 +19,17 @@ func TestMultiBufferRead(t *testing.T) { bs := make([]byte, 32) nBytes, err := mb.Read(bs) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(4) - assert.Bytes(bs[:nBytes]).Equals([]byte("abcd")) + assert(err, IsNil) + assert(nBytes, Equals, 4) + assert(bs[:nBytes], Equals, []byte("abcd")) } func TestMultiBufferAppend(t *testing.T) { - assert := assert.On(t) + assert := With(t) var mb MultiBuffer b := New() b.AppendBytes('a', 'b') mb.Append(b) - assert.Int(mb.Len()).Equals(2) + assert(mb.Len(), Equals, 2) } diff --git a/common/buf/reader_test.go b/common/buf/reader_test.go index cb1e6567f..7c79d0fdc 100644 --- a/common/buf/reader_test.go +++ b/common/buf/reader_test.go @@ -7,24 +7,24 @@ import ( "testing" . "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" "v2ray.com/core/transport/ray" + . "v2ray.com/ext/assert" ) func TestAdaptiveReader(t *testing.T) { - assert := assert.On(t) + assert := With(t) rawContent := make([]byte, 1024*1024) buffer := bytes.NewBuffer(rawContent) reader := NewReader(buffer) b, err := reader.Read() - assert.Error(err).IsNil() - assert.Int(b.Len()).Equals(32 * 1024) + assert(err, IsNil) + assert(b.Len(), Equals, 32*1024) } func TestBytesReaderWriteTo(t *testing.T) { - assert := assert.On(t) + assert := With(t) stream := ray.NewStream(context.Background()) reader := ToBytesReader(stream) @@ -32,25 +32,25 @@ func TestBytesReaderWriteTo(t *testing.T) { b1.AppendBytes('a', 'b', 'c') b2 := New() b2.AppendBytes('e', 'f', 'g') - assert.Error(stream.Write(NewMultiBufferValue(b1, b2))).IsNil() + assert(stream.Write(NewMultiBufferValue(b1, b2)), IsNil) stream.Close() stream2 := ray.NewStream(context.Background()) writer := ToBytesWriter(stream2) nBytes, err := io.Copy(writer, reader) - assert.Error(err).IsNil() - assert.Int64(nBytes).Equals(6) + assert(err, IsNil) + assert(nBytes, Equals, int64(6)) mb, err := stream2.Read() - assert.Error(err).IsNil() - assert.Int(len(mb)).Equals(2) - assert.String(mb[0].String()).Equals("abc") - assert.String(mb[1].String()).Equals("efg") + assert(err, IsNil) + assert(len(mb), Equals, 2) + assert(mb[0].String(), Equals, "abc") + assert(mb[1].String(), Equals, "efg") } func TestBytesReaderMultiBuffer(t *testing.T) { - assert := assert.On(t) + assert := With(t) stream := ray.NewStream(context.Background()) reader := ToBytesReader(stream) @@ -58,13 +58,13 @@ func TestBytesReaderMultiBuffer(t *testing.T) { b1.AppendBytes('a', 'b', 'c') b2 := New() b2.AppendBytes('e', 'f', 'g') - assert.Error(stream.Write(NewMultiBufferValue(b1, b2))).IsNil() + assert(stream.Write(NewMultiBufferValue(b1, b2)), IsNil) stream.Close() mbReader := NewReader(reader) mb, err := mbReader.Read() - assert.Error(err).IsNil() - assert.Int(len(mb)).Equals(2) - assert.String(mb[0].String()).Equals("abc") - assert.String(mb[1].String()).Equals("efg") + assert(err, IsNil) + assert(len(mb), Equals, 2) + assert(mb[0].String(), Equals, "abc") + assert(mb[1].String(), Equals, "efg") } diff --git a/common/buf/writer_test.go b/common/buf/writer_test.go index afa0d8cd2..b91006904 100644 --- a/common/buf/writer_test.go +++ b/common/buf/writer_test.go @@ -10,15 +10,15 @@ import ( "io" . "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/transport/ray" ) func TestWriter(t *testing.T) { - assert := assert.On(t) + assert := With(t) lb := New() - assert.Error(lb.AppendSupplier(ReadFrom(rand.Reader))).IsNil() + assert(lb.AppendSupplier(ReadFrom(rand.Reader)), IsNil) expectedBytes := append([]byte(nil), lb.Bytes()...) @@ -26,20 +26,20 @@ func TestWriter(t *testing.T) { writer := NewWriter(NewBufferedWriter(writeBuffer)) err := writer.Write(NewMultiBufferValue(lb)) - assert.Error(err).IsNil() - assert.Bytes(expectedBytes).Equals(writeBuffer.Bytes()) + assert(err, IsNil) + assert(expectedBytes, Equals, writeBuffer.Bytes()) } func TestBytesWriterReadFrom(t *testing.T) { - assert := assert.On(t) + assert := With(t) cache := ray.NewStream(context.Background()) reader := bufio.NewReader(io.LimitReader(rand.Reader, 8192)) _, err := reader.WriteTo(ToBytesWriter(cache)) - assert.Error(err).IsNil() + assert(err, IsNil) mb, err := cache.Read() - assert.Error(err).IsNil() - assert.Int(mb.Len()).Equals(8192) - assert.Int(len(mb)).Equals(4) + assert(err, IsNil) + assert(mb.Len(), Equals, 8192) + assert(len(mb), Equals, 4) } diff --git a/common/crypto/auth_test.go b/common/crypto/auth_test.go index a44ecfbf3..cd10b1b63 100644 --- a/common/crypto/auth_test.go +++ b/common/crypto/auth_test.go @@ -10,19 +10,19 @@ import ( "v2ray.com/core/common/buf" . "v2ray.com/core/common/crypto" "v2ray.com/core/common/protocol" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestAuthenticationReaderWriter(t *testing.T) { - assert := assert.On(t) + assert := With(t) key := make([]byte, 16) rand.Read(key) block, err := aes.NewCipher(key) - assert.Error(err).IsNil() + assert(err, IsNil) aead, err := cipher.NewGCM(block) - assert.Error(err).IsNil() + assert(err, IsNil) rawPayload := make([]byte, 8192*10) rand.Read(rawPayload) @@ -42,10 +42,10 @@ func TestAuthenticationReaderWriter(t *testing.T) { AdditionalDataGenerator: &NoOpBytesGenerator{}, }, PlainChunkSizeParser{}, cache, protocol.TransferTypeStream) - assert.Error(writer.Write(buf.NewMultiBufferValue(payload))).IsNil() - assert.Int(cache.Len()).Equals(83360) - assert.Error(writer.Write(buf.NewMultiBuffer())).IsNil() - assert.Error(err).IsNil() + assert(writer.Write(buf.NewMultiBufferValue(payload)), IsNil) + assert(cache.Len(), Equals, 83360) + assert(writer.Write(buf.NewMultiBuffer()), IsNil) + assert(err, IsNil) reader := NewAuthenticationReader(&AEADAuthenticator{ AEAD: aead, @@ -59,29 +59,29 @@ func TestAuthenticationReaderWriter(t *testing.T) { for mb.Len() < len(rawPayload) { mb2, err := reader.Read() - assert.Error(err).IsNil() + assert(err, IsNil) mb.AppendMulti(mb2) } mbContent := make([]byte, 8192*10) mb.Read(mbContent) - assert.Bytes(mbContent).Equals(rawPayload) + assert(mbContent, Equals, rawPayload) _, err = reader.Read() - assert.Error(err).Equals(io.EOF) + assert(err, Equals, io.EOF) } func TestAuthenticationReaderWriterPacket(t *testing.T) { - assert := assert.On(t) + assert := With(t) key := make([]byte, 16) rand.Read(key) block, err := aes.NewCipher(key) - assert.Error(err).IsNil() + assert(err, IsNil) aead, err := cipher.NewGCM(block) - assert.Error(err).IsNil() + assert(err, IsNil) cache := buf.NewLocal(1024) iv := make([]byte, 12) @@ -104,10 +104,10 @@ func TestAuthenticationReaderWriterPacket(t *testing.T) { pb2.Append([]byte("efgh")) payload.Append(pb2) - assert.Error(writer.Write(payload)).IsNil() - assert.Int(cache.Len()).GreaterThan(0) - assert.Error(writer.Write(buf.NewMultiBuffer())).IsNil() - assert.Error(err).IsNil() + assert(writer.Write(payload), IsNil) + assert(cache.Len(), GreaterThan, 0) + assert(writer.Write(buf.NewMultiBuffer()), IsNil) + assert(err, IsNil) reader := NewAuthenticationReader(&AEADAuthenticator{ AEAD: aead, @@ -118,14 +118,14 @@ func TestAuthenticationReaderWriterPacket(t *testing.T) { }, PlainChunkSizeParser{}, cache, protocol.TransferTypePacket) mb, err := reader.Read() - assert.Error(err).IsNil() + assert(err, IsNil) b1 := mb.SplitFirst() - assert.String(b1.String()).Equals("abcd") + assert(b1.String(), Equals, "abcd") b2 := mb.SplitFirst() - assert.String(b2.String()).Equals("efgh") - assert.Bool(mb.IsEmpty()).IsTrue() + assert(b2.String(), Equals, "efgh") + assert(mb.IsEmpty(), IsTrue) _, err = reader.Read() - assert.Error(err).Equals(io.EOF) + assert(err, Equals, io.EOF) } diff --git a/common/crypto/chacha20_test.go b/common/crypto/chacha20_test.go index 7bca32a91..40ea44c3b 100644 --- a/common/crypto/chacha20_test.go +++ b/common/crypto/chacha20_test.go @@ -7,7 +7,7 @@ import ( "v2ray.com/core/common" . "v2ray.com/core/common/crypto" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func mustDecodeHex(s string) []byte { @@ -17,7 +17,7 @@ func mustDecodeHex(s string) []byte { } func TestChaCha20Stream(t *testing.T) { - assert := assert.On(t) + assert := With(t) var cases = []struct { key []byte @@ -51,12 +51,12 @@ func TestChaCha20Stream(t *testing.T) { input := make([]byte, len(c.output)) actualOutout := make([]byte, len(c.output)) s.XORKeyStream(actualOutout, input) - assert.Bytes(c.output).Equals(actualOutout) + assert(c.output, Equals, actualOutout) } } func TestChaCha20Decoding(t *testing.T) { - assert := assert.On(t) + assert := With(t) key := make([]byte, 32) rand.Read(key) @@ -72,5 +72,5 @@ func TestChaCha20Decoding(t *testing.T) { stream2 := NewChaCha20Stream(key, iv) stream2.XORKeyStream(x, x) - assert.Bytes(x).Equals(payload) + assert(x, Equals, payload) } diff --git a/common/crypto/chunk_test.go b/common/crypto/chunk_test.go index e9f399b0f..67144dcef 100644 --- a/common/crypto/chunk_test.go +++ b/common/crypto/chunk_test.go @@ -6,11 +6,11 @@ import ( "v2ray.com/core/common/buf" . "v2ray.com/core/common/crypto" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestChunkStreamIO(t *testing.T) { - assert := assert.On(t) + assert := With(t) cache := buf.NewLocal(8192) @@ -19,26 +19,26 @@ func TestChunkStreamIO(t *testing.T) { b := buf.New() b.AppendBytes('a', 'b', 'c', 'd') - assert.Error(writer.Write(buf.NewMultiBufferValue(b))).IsNil() + assert(writer.Write(buf.NewMultiBufferValue(b)), IsNil) b = buf.New() b.AppendBytes('e', 'f', 'g') - assert.Error(writer.Write(buf.NewMultiBufferValue(b))).IsNil() + assert(writer.Write(buf.NewMultiBufferValue(b)), IsNil) - assert.Error(writer.Write(buf.NewMultiBuffer())).IsNil() + assert(writer.Write(buf.NewMultiBuffer()), IsNil) - assert.Int(cache.Len()).Equals(13) + assert(cache.Len(), Equals, 13) mb, err := reader.Read() - assert.Error(err).IsNil() - assert.Int(mb.Len()).Equals(4) - assert.Bytes(mb[0].Bytes()).Equals([]byte("abcd")) + assert(err, IsNil) + assert(mb.Len(), Equals, 4) + assert(mb[0].Bytes(), Equals, []byte("abcd")) mb, err = reader.Read() - assert.Error(err).IsNil() - assert.Int(mb.Len()).Equals(3) - assert.Bytes(mb[0].Bytes()).Equals([]byte("efg")) + assert(err, IsNil) + assert(mb.Len(), Equals, 3) + assert(mb[0].Bytes(), Equals, []byte("efg")) _, err = reader.Read() - assert.Error(err).Equals(io.EOF) + assert(err, Equals, io.EOF) } diff --git a/common/errors/errors_test.go b/common/errors/errors_test.go index cb385fbd9..337e3c9d2 100644 --- a/common/errors/errors_test.go +++ b/common/errors/errors_test.go @@ -5,29 +5,29 @@ import ( "testing" . "v2ray.com/core/common/errors" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestError(t *testing.T) { - assert := assert.On(t) + assert := With(t) err := New("TestError") - assert.Bool(GetSeverity(err) == SeverityInfo).IsTrue() + assert(GetSeverity(err), Equals, SeverityInfo) err = New("TestError2").Base(io.EOF) - assert.Bool(GetSeverity(err) == SeverityInfo).IsTrue() + assert(GetSeverity(err), Equals, SeverityInfo) err = New("TestError3").Base(io.EOF).AtWarning() - assert.Bool(GetSeverity(err) == SeverityWarning).IsTrue() + assert(GetSeverity(err), Equals, SeverityWarning) err = New("TestError4").Base(io.EOF).AtWarning() err = New("TestError5").Base(err) - assert.Bool(GetSeverity(err) == SeverityWarning).IsTrue() - assert.String(err.Error()).Contains("EOF") + assert(GetSeverity(err), Equals, SeverityWarning) + assert(err.Error(), HasSubstring, "EOF") } func TestErrorMessage(t *testing.T) { - assert := assert.On(t) + assert := With(t) data := []struct { err error @@ -44,6 +44,6 @@ func TestErrorMessage(t *testing.T) { } for _, d := range data { - assert.String(d.err.Error()).Equals(d.msg) + assert(d.err.Error(), Equals, d.msg) } } diff --git a/common/net/address_test.go b/common/net/address_test.go index 58583d248..3f5fcb428 100644 --- a/common/net/address_test.go +++ b/common/net/address_test.go @@ -6,7 +6,6 @@ import ( . "v2ray.com/core/common/net" . "v2ray.com/core/common/net/testing" - "v2ray.com/core/testing/assert" . "v2ray.com/ext/assert" ) @@ -24,7 +23,7 @@ func TestIPv4Address(t *testing.T) { } func TestIPv6Address(t *testing.T) { - assert := assert.On(t) + assert := With(t) ip := []byte{ byte(1), byte(2), byte(3), byte(4), @@ -34,15 +33,15 @@ func TestIPv6Address(t *testing.T) { } addr := IPAddress(ip) - assert.Address(addr).IsIPv6() - assert.Address(addr).IsNotIPv4() - assert.Address(addr).IsNotDomain() - assert.IP(addr.IP()).Equals(net.IP(ip)) - assert.Address(addr).EqualsString("[102:304:102:304:102:304:102:304]") + assert(addr, IsIPv6) + assert(addr, Not(IsIPv4)) + assert(addr, Not(IsDomain)) + assert(addr.IP(), Equals, net.IP(ip)) + assert(addr.String(), Equals, "[102:304:102:304:102:304:102:304]") } func TestIPv4Asv6(t *testing.T) { - assert := assert.On(t) + assert := With(t) ip := []byte{ byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), @@ -50,27 +49,27 @@ func TestIPv4Asv6(t *testing.T) { byte(1), byte(2), byte(3), byte(4), } addr := IPAddress(ip) - assert.Address(addr).EqualsString("1.2.3.4") + assert(addr.String(), Equals, "1.2.3.4") } func TestDomainAddress(t *testing.T) { - assert := assert.On(t) + assert := With(t) domain := "v2ray.com" addr := DomainAddress(domain) - assert.Address(addr).IsDomain() - assert.Address(addr).IsNotIPv6() - assert.Address(addr).IsNotIPv4() - assert.String(addr.Domain()).Equals(domain) - assert.Address(addr).EqualsString("v2ray.com") + assert(addr, IsDomain) + assert(addr, Not(IsIPv6)) + assert(addr, Not(IsIPv4)) + assert(addr.Domain(), Equals, domain) + assert(addr.String(), Equals, "v2ray.com") } func TestNetIPv4Address(t *testing.T) { - assert := assert.On(t) + assert := With(t) ip := net.IPv4(1, 2, 3, 4) addr := IPAddress(ip) - assert.Address(addr).IsIPv4() - assert.Address(addr).EqualsString("1.2.3.4") + assert(addr, IsIPv4) + assert(addr.String(), Equals, "1.2.3.4") } diff --git a/common/net/destination_test.go b/common/net/destination_test.go index f3b0f1317..b68e9ccbe 100644 --- a/common/net/destination_test.go +++ b/common/net/destination_test.go @@ -4,23 +4,24 @@ import ( "testing" . "v2ray.com/core/common/net" - "v2ray.com/core/testing/assert" + . "v2ray.com/core/common/net/testing" + . "v2ray.com/ext/assert" ) func TestTCPDestination(t *testing.T) { - assert := assert.On(t) + assert := With(t) dest := TCPDestination(IPAddress([]byte{1, 2, 3, 4}), 80) - assert.Destination(dest).IsTCP() - assert.Destination(dest).IsNotUDP() - assert.Destination(dest).EqualsString("tcp:1.2.3.4:80") + assert(dest, IsTCP) + assert(dest, Not(IsUDP)) + assert(dest.String(), Equals, "tcp:1.2.3.4:80") } func TestUDPDestination(t *testing.T) { - assert := assert.On(t) + assert := With(t) dest := UDPDestination(IPAddress([]byte{0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88}), 53) - assert.Destination(dest).IsNotTCP() - assert.Destination(dest).IsUDP() - assert.Destination(dest).EqualsString("udp:[2001:4860:4860::8888]:53") + assert(dest, Not(IsTCP)) + assert(dest, IsUDP) + assert(dest.String(), Equals, "udp:[2001:4860:4860::8888]:53") } diff --git a/common/net/ipnet_test.go b/common/net/ipnet_test.go index e92616037..132dca9b7 100644 --- a/common/net/ipnet_test.go +++ b/common/net/ipnet_test.go @@ -6,7 +6,7 @@ import ( "v2ray.com/core/common" . "v2ray.com/core/common/net" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func parseCIDR(str string) *net.IPNet { @@ -16,7 +16,7 @@ func parseCIDR(str string) *net.IPNet { } func TestIPNet(t *testing.T) { - assert := assert.On(t) + assert := With(t) ipNet := NewIPNetTable() ipNet.Add(parseCIDR(("0.0.0.0/8"))) @@ -32,12 +32,12 @@ func TestIPNet(t *testing.T) { 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(ParseIP("192.168.1.1"))).IsTrue() - assert.Bool(ipNet.Contains(ParseIP("192.0.0.0"))).IsTrue() - assert.Bool(ipNet.Contains(ParseIP("192.0.1.0"))).IsFalse() - assert.Bool(ipNet.Contains(ParseIP("0.1.0.0"))).IsTrue() - assert.Bool(ipNet.Contains(ParseIP("1.0.0.1"))).IsFalse() - assert.Bool(ipNet.Contains(ParseIP("8.8.8.7"))).IsFalse() - assert.Bool(ipNet.Contains(ParseIP("8.8.8.8"))).IsTrue() - assert.Bool(ipNet.Contains(ParseIP("2001:cdba::3257:9652"))).IsFalse() + assert(ipNet.Contains(ParseIP("192.168.1.1")), IsTrue) + assert(ipNet.Contains(ParseIP("192.0.0.0")), IsTrue) + assert(ipNet.Contains(ParseIP("192.0.1.0")), IsFalse) + assert(ipNet.Contains(ParseIP("0.1.0.0")), IsTrue) + assert(ipNet.Contains(ParseIP("1.0.0.1")), IsFalse) + assert(ipNet.Contains(ParseIP("8.8.8.7")), IsFalse) + assert(ipNet.Contains(ParseIP("8.8.8.8")), IsTrue) + assert(ipNet.Contains(ParseIP("2001:cdba::3257:9652")), IsFalse) } diff --git a/common/net/port_test.go b/common/net/port_test.go index a1f1f4e21..187cdf1f3 100644 --- a/common/net/port_test.go +++ b/common/net/port_test.go @@ -4,15 +4,15 @@ import ( "testing" . "v2ray.com/core/common/net" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestPortRangeContains(t *testing.T) { - assert := assert.On(t) + assert := With(t) portRange := &PortRange{ From: 53, To: 53, } - assert.Bool(portRange.Contains(Port(53))).IsTrue() + assert(portRange.Contains(Port(53)), IsTrue) } diff --git a/common/net/testing/assert.go b/common/net/testing/assert.go index 16c9f5aa7..b6eb2fb9c 100644 --- a/common/net/testing/assert.go +++ b/common/net/testing/assert.go @@ -1,46 +1,48 @@ package testing import ( - "reflect" - "v2ray.com/core/common/net" "v2ray.com/ext/assert" ) -var IsIPv4 = assert.CreateMatcher(reflect.TypeOf(net.Address(nil)), reflect.ValueOf(func(a net.Address) bool { +var IsIPv4 = assert.CreateMatcher(func(a net.Address) bool { return a.Family().IsIPv4() -}), 1, "is IPv4") +}, "is IPv4") -var IsIPv6 = assert.CreateMatcher(reflect.TypeOf(net.Address(nil)), reflect.ValueOf(func(a net.Address) bool { +var IsIPv6 = assert.CreateMatcher(func(a net.Address) bool { return a.Family().IsIPv6() -}), 1, "is IPv6") +}, "is IPv6") -var IsIP = assert.CreateMatcher(reflect.TypeOf(net.Address(nil)), reflect.ValueOf(func(a net.Address) bool { +var IsIP = assert.CreateMatcher(func(a net.Address) bool { return a.Family().IsIPv4() || a.Family().IsIPv6() -}), 1, "is IP") +}, "is IP") -var IsTCP = assert.CreateMatcher(reflect.TypeOf(net.Destination{}), reflect.ValueOf(func(a net.Destination) bool { +var IsTCP = assert.CreateMatcher(func(a net.Destination) bool { return a.Network == net.Network_TCP -}), 1, "is TCP") +}, "is TCP") -var IsUDP = assert.CreateMatcher(reflect.TypeOf(net.Destination{}), reflect.ValueOf(func(a net.Destination) bool { +var IsUDP = assert.CreateMatcher(func(a net.Destination) bool { return a.Network == net.Network_UDP -}), 1, "is UDP") +}, "is UDP") -var IsDomain = assert.CreateMatcher(reflect.TypeOf(net.Address(nil)), reflect.ValueOf(func(a net.Address) bool { +var IsDomain = assert.CreateMatcher(func(a net.Address) bool { return a.Family().IsDomain() -}), 1, "is Domain") +}, "is Domain") func init() { - assert.RegisterEqualsMatcher(reflect.TypeOf((*net.Address)(nil)).Elem(), reflect.ValueOf(func(a, b net.Address) bool { + assert.RegisterEqualsMatcher(func(a, b net.Address) bool { return a == b - })) + }) - assert.RegisterEqualsMatcher(reflect.TypeOf(net.Destination{}), reflect.ValueOf(func(a, b net.Destination) bool { + assert.RegisterEqualsMatcher(func(a, b net.Destination) bool { return a == b - })) + }) - assert.RegisterEqualsMatcher(reflect.TypeOf(net.Port(0)), reflect.ValueOf(func(a, b net.Port) bool { + assert.RegisterEqualsMatcher(func(a, b net.Port) bool { return a == b - })) + }) + + assert.RegisterEqualsMatcher(func(a, b net.IP) bool { + return a.Equal(b) + }) } diff --git a/common/platform/platform_test.go b/common/platform/platform_test.go index 4b3d6e6fe..6eb52acd6 100644 --- a/common/platform/platform_test.go +++ b/common/platform/platform_test.go @@ -4,11 +4,11 @@ import ( "testing" . "v2ray.com/core/common/platform" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestNormalizeEnvName(t *testing.T) { - assert := assert.On(t) + assert := With(t) cases := []struct { input string @@ -28,14 +28,14 @@ func TestNormalizeEnvName(t *testing.T) { }, } for _, test := range cases { - assert.String(NormalizeEnvName(test.input)).Equals(test.output) + assert(NormalizeEnvName(test.input), Equals, test.output) } } func TestEnvFlag(t *testing.T) { - assert := assert.On(t) + assert := With(t) - assert.Int(EnvFlag{ + assert(EnvFlag{ Name: "xxxxx.y", - }.GetValueAsInt(10)).Equals(10) + }.GetValueAsInt(10), Equals, 10) } diff --git a/common/protocol/id_test.go b/common/protocol/id_test.go index 12788529a..81bf50638 100644 --- a/common/protocol/id_test.go +++ b/common/protocol/id_test.go @@ -6,12 +6,12 @@ import ( "v2ray.com/core/common/predicate" . "v2ray.com/core/common/protocol" "v2ray.com/core/common/uuid" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestCmdKey(t *testing.T) { - assert := assert.On(t) + assert := With(t) id := NewID(uuid.New()) - assert.Bool(predicate.BytesAll(id.CmdKey(), 0)).IsFalse() + assert(predicate.BytesAll(id.CmdKey(), 0), IsFalse) } diff --git a/common/protocol/server_picker_test.go b/common/protocol/server_picker_test.go index 2b1bd6fe5..72e789ee8 100644 --- a/common/protocol/server_picker_test.go +++ b/common/protocol/server_picker_test.go @@ -6,30 +6,30 @@ import ( "v2ray.com/core/common/net" . "v2ray.com/core/common/protocol" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestServerList(t *testing.T) { - assert := assert.On(t) + assert := With(t) list := NewServerList() list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(1)), AlwaysValid())) - assert.Uint32(list.Size()).Equals(1) + assert(list.Size(), Equals, uint32(1)) list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(2)), BeforeTime(time.Now().Add(time.Second)))) - assert.Uint32(list.Size()).Equals(2) + assert(list.Size(), Equals, uint32(2)) server := list.GetServer(1) - assert.Port(server.Destination().Port).Equals(2) + assert(server.Destination().Port, Equals, net.Port(2)) time.Sleep(2 * time.Second) server = list.GetServer(1) - assert.Pointer(server).IsNil() + assert(server, IsNil) server = list.GetServer(0) - assert.Port(server.Destination().Port).Equals(1) + assert(server.Destination().Port, Equals, net.Port(1)) } func TestServerPicker(t *testing.T) { - assert := assert.On(t) + assert := With(t) list := NewServerList() list.AddServer(NewServerSpec(net.TCPDestination(net.LocalHostIP, net.Port(1)), AlwaysValid())) @@ -38,17 +38,17 @@ func TestServerPicker(t *testing.T) { picker := NewRoundRobinServerPicker(list) server := picker.PickServer() - assert.Port(server.Destination().Port).Equals(1) + assert(server.Destination().Port, Equals, net.Port(1)) server = picker.PickServer() - assert.Port(server.Destination().Port).Equals(2) + assert(server.Destination().Port, Equals, net.Port(2)) server = picker.PickServer() - assert.Port(server.Destination().Port).Equals(3) + assert(server.Destination().Port, Equals, net.Port(3)) server = picker.PickServer() - assert.Port(server.Destination().Port).Equals(1) + assert(server.Destination().Port, Equals, net.Port(1)) time.Sleep(2 * time.Second) server = picker.PickServer() - assert.Port(server.Destination().Port).Equals(1) + assert(server.Destination().Port, Equals, net.Port(1)) server = picker.PickServer() - assert.Port(server.Destination().Port).Equals(1) + assert(server.Destination().Port, Equals, net.Port(1)) } diff --git a/common/protocol/server_spec_test.go b/common/protocol/server_spec_test.go index beca89b9c..0c1a6252b 100644 --- a/common/protocol/server_spec_test.go +++ b/common/protocol/server_spec_test.go @@ -5,27 +5,27 @@ import ( "time" . "v2ray.com/core/common/protocol" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestAlwaysValidStrategy(t *testing.T) { - assert := assert.On(t) + assert := With(t) strategy := AlwaysValid() - assert.Bool(strategy.IsValid()).IsTrue() + assert(strategy.IsValid(), IsTrue) strategy.Invalidate() - assert.Bool(strategy.IsValid()).IsTrue() + assert(strategy.IsValid(), IsTrue) } func TestTimeoutValidStrategy(t *testing.T) { - assert := assert.On(t) + assert := With(t) strategy := BeforeTime(time.Now().Add(2 * time.Second)) - assert.Bool(strategy.IsValid()).IsTrue() + assert(strategy.IsValid(), IsTrue) time.Sleep(3 * time.Second) - assert.Bool(strategy.IsValid()).IsFalse() + assert(strategy.IsValid(), IsFalse) strategy = BeforeTime(time.Now().Add(2 * time.Second)) strategy.Invalidate() - assert.Bool(strategy.IsValid()).IsFalse() + assert(strategy.IsValid(), IsFalse) } diff --git a/common/protocol/time_test.go b/common/protocol/time_test.go index 1ee1084f9..8ed761e53 100644 --- a/common/protocol/time_test.go +++ b/common/protocol/time_test.go @@ -5,11 +5,11 @@ import ( "time" . "v2ray.com/core/common/protocol" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestGenerateRandomInt64InRange(t *testing.T) { - assert := assert.On(t) + assert := With(t) base := time.Now().Unix() delta := 100 @@ -17,7 +17,7 @@ func TestGenerateRandomInt64InRange(t *testing.T) { for i := 0; i < 100; i++ { val := int64(generator()) - assert.Int64(val).AtMost(base + int64(delta)) - assert.Int64(val).AtLeast(base - int64(delta)) + assert(val, AtMost, base + int64(delta)) + assert(val, AtLeast, base - int64(delta)) } } diff --git a/common/retry/retry_test.go b/common/retry/retry_test.go index 23dd249ee..a08ec8411 100644 --- a/common/retry/retry_test.go +++ b/common/retry/retry_test.go @@ -6,7 +6,7 @@ import ( "v2ray.com/core/common/errors" . "v2ray.com/core/common/retry" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) var ( @@ -14,7 +14,7 @@ var ( ) func TestNoRetry(t *testing.T) { - assert := assert.On(t) + assert := With(t) startTime := time.Now().Unix() err := Timed(10, 100000).On(func() error { @@ -22,12 +22,12 @@ func TestNoRetry(t *testing.T) { }) endTime := time.Now().Unix() - assert.Error(err).IsNil() - assert.Int64(endTime - startTime).AtLeast(0) + assert(err, IsNil) + assert(endTime-startTime, AtLeast, int64(0)) } func TestRetryOnce(t *testing.T) { - assert := assert.On(t) + assert := With(t) startTime := time.Now() called := 0 @@ -40,12 +40,12 @@ func TestRetryOnce(t *testing.T) { }) duration := time.Since(startTime) - assert.Error(err).IsNil() - assert.Int64(int64(duration / time.Millisecond)).AtLeast(900) + assert(err, IsNil) + assert(int64(duration/time.Millisecond), AtLeast, int64(900)) } func TestRetryMultiple(t *testing.T) { - assert := assert.On(t) + assert := With(t) startTime := time.Now() called := 0 @@ -58,12 +58,12 @@ func TestRetryMultiple(t *testing.T) { }) duration := time.Since(startTime) - assert.Error(err).IsNil() - assert.Int64(int64(duration / time.Millisecond)).AtLeast(4900) + assert(err, IsNil) + assert(int64(duration/time.Millisecond), AtLeast, int64(4900)) } func TestRetryExhausted(t *testing.T) { - assert := assert.On(t) + assert := With(t) startTime := time.Now() called := 0 @@ -73,12 +73,12 @@ func TestRetryExhausted(t *testing.T) { }) duration := time.Since(startTime) - assert.Error(errors.Cause(err)).Equals(ErrRetryFailed) - assert.Int64(int64(duration / time.Millisecond)).AtLeast(1900) + assert(errors.Cause(err), Equals, ErrRetryFailed) + assert(int64(duration/time.Millisecond), AtLeast, int64(1900)) } func TestExponentialBackoff(t *testing.T) { - assert := assert.On(t) + assert := With(t) startTime := time.Now() called := 0 @@ -88,6 +88,6 @@ func TestExponentialBackoff(t *testing.T) { }) duration := time.Since(startTime) - assert.Error(errors.Cause(err)).Equals(ErrRetryFailed) - assert.Int64(int64(duration / time.Millisecond)).AtLeast(4000) + assert(errors.Cause(err), Equals, ErrRetryFailed) + assert(int64(duration/time.Millisecond), AtLeast, int64(4000)) } diff --git a/common/serial/bytes_test.go b/common/serial/bytes_test.go index a873395c7..65245f9e0 100644 --- a/common/serial/bytes_test.go +++ b/common/serial/bytes_test.go @@ -4,11 +4,11 @@ import ( "testing" . "v2ray.com/core/common/serial" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestBytesToHex(t *testing.T) { - assert := assert.On(t) + assert := With(t) cases := []struct { input []byte @@ -21,15 +21,15 @@ func TestBytesToHex(t *testing.T) { } for _, test := range cases { - assert.String(test.output).Equals(BytesToHexString(test.input)) + assert(test.output, Equals, BytesToHexString(test.input)) } } func TestInt64(t *testing.T) { - assert := assert.On(t) + assert := With(t) x := int64(375134875348) b := Int64ToBytes(x, []byte{}) v := BytesToInt64(b) - assert.Int64(x).Equals(v) + assert(x, Equals, v) } diff --git a/common/serial/numbers_test.go b/common/serial/numbers_test.go index 9464df5b4..7d67f1edf 100644 --- a/common/serial/numbers_test.go +++ b/common/serial/numbers_test.go @@ -6,15 +6,15 @@ import ( "v2ray.com/core/common" "v2ray.com/core/common/buf" . "v2ray.com/core/common/serial" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestUint32(t *testing.T) { - assert := assert.On(t) + assert := With(t) x := uint32(458634234) s1 := Uint32ToBytes(x, []byte{}) s2 := buf.New() common.Must(s2.AppendSupplier(WriteUint32(x))) - assert.Bytes(s1).Equals(s2.Bytes()) + assert(s1, Equals, s2.Bytes()) } diff --git a/common/serial/typed_message_test.go b/common/serial/typed_message_test.go index 74da1b906..4bf7c540a 100644 --- a/common/serial/typed_message_test.go +++ b/common/serial/typed_message_test.go @@ -4,13 +4,13 @@ import ( "testing" . "v2ray.com/core/common/serial" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestGetInstance(t *testing.T) { - assert := assert.On(t) + assert := With(t) p, err := GetInstance("") - assert.Pointer(p).IsNil() - assert.Error(err).IsNotNil() + assert(p, IsNil) + assert(err, IsNotNil) } diff --git a/common/signal/exec_test.go b/common/signal/exec_test.go index e335054fb..d6bac83b5 100644 --- a/common/signal/exec_test.go +++ b/common/signal/exec_test.go @@ -6,11 +6,11 @@ import ( "testing" . "v2ray.com/core/common/signal" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestErrorOrFinish2_Error(t *testing.T) { - assert := assert.On(t) + assert := With(t) c1 := make(chan error, 1) c2 := make(chan error, 2) @@ -22,11 +22,11 @@ func TestErrorOrFinish2_Error(t *testing.T) { c1 <- errors.New("test") err := <-c - assert.String(err.Error()).Equals("test") + assert(err.Error(), Equals, "test") } func TestErrorOrFinish2_Error2(t *testing.T) { - assert := assert.On(t) + assert := With(t) c1 := make(chan error, 1) c2 := make(chan error, 2) @@ -38,11 +38,11 @@ func TestErrorOrFinish2_Error2(t *testing.T) { c2 <- errors.New("test") err := <-c - assert.String(err.Error()).Equals("test") + assert(err.Error(), Equals, "test") } func TestErrorOrFinish2_NoneError(t *testing.T) { - assert := assert.On(t) + assert := With(t) c1 := make(chan error, 1) c2 := make(chan error, 2) @@ -61,11 +61,11 @@ func TestErrorOrFinish2_NoneError(t *testing.T) { close(c2) err := <-c - assert.Error(err).IsNil() + assert(err, IsNil) } func TestErrorOrFinish2_NoneError2(t *testing.T) { - assert := assert.On(t) + assert := With(t) c1 := make(chan error, 1) c2 := make(chan error, 2) @@ -84,5 +84,5 @@ func TestErrorOrFinish2_NoneError2(t *testing.T) { close(c1) err := <-c - assert.Error(err).IsNil() + assert(err, IsNil) } diff --git a/common/signal/timer_test.go b/common/signal/timer_test.go index 8c5f57e6b..abaa9f6a5 100644 --- a/common/signal/timer_test.go +++ b/common/signal/timer_test.go @@ -7,26 +7,26 @@ import ( "time" . "v2ray.com/core/common/signal" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestActivityTimer(t *testing.T) { - assert := assert.On(t) + assert := With(t) ctx, timer := CancelAfterInactivity(context.Background(), time.Second*5) time.Sleep(time.Second * 6) - assert.Error(ctx.Err()).IsNotNil() + assert(ctx.Err(), IsNotNil) runtime.KeepAlive(timer) } func TestActivityTimerUpdate(t *testing.T) { - assert := assert.On(t) + assert := With(t) ctx, timer := CancelAfterInactivity(context.Background(), time.Second*10) time.Sleep(time.Second * 3) - assert.Error(ctx.Err()).IsNil() + assert(ctx.Err(), IsNil) timer.SetTimeout(time.Second * 1) time.Sleep(time.Second * 2) - assert.Error(ctx.Err()).IsNotNil() + assert(ctx.Err(), IsNotNil) runtime.KeepAlive(timer) } diff --git a/common/uuid/uuid_test.go b/common/uuid/uuid_test.go index 27d1e1499..f8b90085e 100644 --- a/common/uuid/uuid_test.go +++ b/common/uuid/uuid_test.go @@ -4,74 +4,74 @@ import ( "testing" . "v2ray.com/core/common/uuid" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestParseBytes(t *testing.T) { - assert := assert.On(t) + assert := With(t) str := "2418d087-648d-4990-86e8-19dca1d006d3" bytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3} uuid, err := ParseBytes(bytes) - assert.Error(err).IsNil() - assert.String(uuid.String()).Equals(str) + assert(err, IsNil) + assert(uuid.String(), Equals, str) _, err = ParseBytes([]byte{1, 3, 2, 4}) - assert.Error(err).IsNotNil() + assert(err, IsNotNil) } func TestParseString(t *testing.T) { - assert := assert.On(t) + assert := With(t) str := "2418d087-648d-4990-86e8-19dca1d006d3" expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3} uuid, err := ParseString(str) - assert.Error(err).IsNil() - assert.Bytes(uuid.Bytes()).Equals(expectedBytes) + assert(err, IsNil) + assert(uuid.Bytes(), Equals, expectedBytes) uuid, err = ParseString("2418d087") - assert.Error(err).IsNotNil() + assert(err, IsNotNil) uuid, err = ParseString("2418d087-648k-4990-86e8-19dca1d006d3") - assert.Error(err).IsNotNil() + assert(err, IsNotNil) } func TestNewUUID(t *testing.T) { - assert := assert.On(t) + assert := With(t) uuid := New() uuid2, err := ParseString(uuid.String()) - assert.Error(err).IsNil() - assert.String(uuid.String()).Equals(uuid2.String()) - assert.Bytes(uuid.Bytes()).Equals(uuid2.Bytes()) + assert(err, IsNil) + assert(uuid.String(), Equals, uuid2.String()) + assert(uuid.Bytes(), Equals, uuid2.Bytes()) } func TestRandom(t *testing.T) { - assert := assert.On(t) + assert := With(t) uuid := New() uuid2 := New() - assert.String(uuid.String()).NotEquals(uuid2.String()) - assert.Bytes(uuid.Bytes()).NotEquals(uuid2.Bytes()) + assert(uuid.String(), NotEquals, uuid2.String()) + assert(uuid.Bytes(), NotEquals, uuid2.Bytes()) } func TestEquals(t *testing.T) { - assert := assert.On(t) + assert := With(t) var uuid *UUID = nil var uuid2 *UUID = nil - assert.Bool(uuid.Equals(uuid2)).IsTrue() - assert.Bool(uuid.Equals(New())).IsFalse() + assert(uuid.Equals(uuid2), IsTrue) + assert(uuid.Equals(New()), IsFalse) } func TestNext(t *testing.T) { - assert := assert.On(t) + assert := With(t) uuid := New() uuid2 := uuid.Next() - assert.Bool(uuid.Equals(uuid2)).IsFalse() + assert(uuid.Equals(uuid2), IsFalse) } diff --git a/proxy/blackhole/config_test.go b/proxy/blackhole/config_test.go index 7678d7cf6..1e5ad3525 100644 --- a/proxy/blackhole/config_test.go +++ b/proxy/blackhole/config_test.go @@ -7,11 +7,11 @@ import ( "v2ray.com/core/common/buf" . "v2ray.com/core/proxy/blackhole" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestHTTPResponse(t *testing.T) { - assert := assert.On(t) + assert := With(t) buffer := buf.New() @@ -20,6 +20,6 @@ func TestHTTPResponse(t *testing.T) { reader := bufio.NewReader(buffer) response, err := http.ReadResponse(reader, nil) - assert.Error(err).IsNil() - assert.Int(response.StatusCode).Equals(403) + assert(err, IsNil) + assert(response.StatusCode, Equals, 403) } diff --git a/proxy/http/server_test.go b/proxy/http/server_test.go index 2bc370a2e..f2d1dafc0 100644 --- a/proxy/http/server_test.go +++ b/proxy/http/server_test.go @@ -7,13 +7,13 @@ import ( "testing" . "v2ray.com/core/proxy/http" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" _ "v2ray.com/core/transport/internet/tcp" ) func TestHopByHopHeadersStrip(t *testing.T) { - assert := assert.On(t) + assert := With(t) rawRequest := `GET /pkg/net/http/ HTTP/1.1 Host: golang.org @@ -31,17 +31,17 @@ Accept-Language: de,en;q=0.7,en-us;q=0.3 ` b := bufio.NewReader(strings.NewReader(rawRequest)) req, err := http.ReadRequest(b) - assert.Error(err).IsNil() - assert.String(req.Header.Get("Foo")).Equals("foo") - assert.String(req.Header.Get("Bar")).Equals("bar") - assert.String(req.Header.Get("Connection")).Equals("keep-alive,Foo, Bar") - assert.String(req.Header.Get("Proxy-Connection")).Equals("keep-alive") - assert.String(req.Header.Get("Proxy-Authenticate")).Equals("abc") + assert(err, IsNil) + assert(req.Header.Get("Foo"), Equals, "foo") + assert(req.Header.Get("Bar"), Equals, "bar") + assert(req.Header.Get("Connection"), Equals, "keep-alive,Foo, Bar") + assert(req.Header.Get("Proxy-Connection"), Equals, "keep-alive") + assert(req.Header.Get("Proxy-Authenticate"), Equals, "abc") StripHopByHopHeaders(req.Header) - assert.String(req.Header.Get("Connection")).IsEmpty() - assert.String(req.Header.Get("Foo")).IsEmpty() - assert.String(req.Header.Get("Bar")).IsEmpty() - assert.String(req.Header.Get("Proxy-Connection")).IsEmpty() - assert.String(req.Header.Get("Proxy-Authenticate")).IsEmpty() + assert(req.Header.Get("Connection"), IsEmpty) + assert(req.Header.Get("Foo"), IsEmpty) + assert(req.Header.Get("Bar"), IsEmpty) + assert(req.Header.Get("Proxy-Connection"), IsEmpty) + assert(req.Header.Get("Proxy-Authenticate"), IsEmpty) } diff --git a/proxy/shadowsocks/ota_test.go b/proxy/shadowsocks/ota_test.go index dda777e1d..b93d55a69 100644 --- a/proxy/shadowsocks/ota_test.go +++ b/proxy/shadowsocks/ota_test.go @@ -5,11 +5,11 @@ import ( "v2ray.com/core/common/buf" . "v2ray.com/core/proxy/shadowsocks" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestNormalChunkReading(t *testing.T) { - assert := assert.On(t) + assert := With(t) buffer := buf.New() buffer.AppendBytes( @@ -17,12 +17,12 @@ func TestNormalChunkReading(t *testing.T) { reader := NewChunkReader(buffer, NewAuthenticator(ChunkKeyGenerator( []byte{21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}))) payload, err := reader.Read() - assert.Error(err).IsNil() - assert.Bytes(payload[0].Bytes()).Equals([]byte{11, 12, 13, 14, 15, 16, 17, 18}) + assert(err, IsNil) + assert(payload[0].Bytes(), Equals, []byte{11, 12, 13, 14, 15, 16, 17, 18}) } func TestNormalChunkWriting(t *testing.T) { - assert := assert.On(t) + assert := With(t) buffer := buf.NewLocal(512) writer := NewChunkWriter(buffer, NewAuthenticator(ChunkKeyGenerator( @@ -31,6 +31,6 @@ func TestNormalChunkWriting(t *testing.T) { b := buf.NewLocal(256) b.Append([]byte{11, 12, 13, 14, 15, 16, 17, 18}) err := writer.Write(buf.NewMultiBufferValue(b)) - assert.Error(err).IsNil() - assert.Bytes(buffer.Bytes()).Equals([]byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18}) + assert(err, IsNil) + assert(buffer.Bytes(), Equals, []byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18}) } diff --git a/proxy/shadowsocks/protocol_test.go b/proxy/shadowsocks/protocol_test.go index 5371868c8..763bd26b2 100644 --- a/proxy/shadowsocks/protocol_test.go +++ b/proxy/shadowsocks/protocol_test.go @@ -8,11 +8,11 @@ import ( "v2ray.com/core/common/protocol" "v2ray.com/core/common/serial" . "v2ray.com/core/proxy/shadowsocks" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestUDPEncoding(t *testing.T) { - assert := assert.On(t) + assert := With(t) request := &protocol.RequestHeader{ Version: Version, @@ -32,17 +32,17 @@ func TestUDPEncoding(t *testing.T) { data := buf.NewLocal(256) data.AppendSupplier(serial.WriteString("test string")) encodedData, err := EncodeUDPPacket(request, data.Bytes()) - assert.Error(err).IsNil() + assert(err, IsNil) decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData) - assert.Error(err).IsNil() - assert.Bytes(decodedData.Bytes()).Equals(data.Bytes()) - assert.Address(decodedRequest.Address).Equals(request.Address) - assert.Port(decodedRequest.Port).Equals(request.Port) + assert(err, IsNil) + assert(decodedData.Bytes(), Equals, data.Bytes()) + assert(decodedRequest.Address, Equals, request.Address) + assert(decodedRequest.Port, Equals, request.Port) } func TestTCPRequest(t *testing.T) { - assert := assert.On(t) + assert := With(t) cases := []struct { request *protocol.RequestHeader @@ -110,18 +110,18 @@ func TestTCPRequest(t *testing.T) { defer cache.Release() writer, err := WriteTCPRequest(request, cache) - assert.Error(err).IsNil() + assert(err, IsNil) - assert.Error(writer.Write(buf.NewMultiBufferValue(data))).IsNil() + assert(writer.Write(buf.NewMultiBufferValue(data)), IsNil) decodedRequest, reader, err := ReadTCPSession(request.User, cache) - assert.Error(err).IsNil() - assert.Address(decodedRequest.Address).Equals(request.Address) - assert.Port(decodedRequest.Port).Equals(request.Port) + assert(err, IsNil) + assert(decodedRequest.Address, Equals, request.Address) + assert(decodedRequest.Port, Equals, request.Port) decodedData, err := reader.Read() - assert.Error(err).IsNil() - assert.String(decodedData[0].String()).Equals(string(payload)) + assert(err, IsNil) + assert(decodedData[0].String(), Equals, string(payload)) } for _, test := range cases { @@ -131,7 +131,7 @@ func TestTCPRequest(t *testing.T) { } func TestUDPReaderWriter(t *testing.T) { - assert := assert.On(t) + assert := With(t) user := &protocol.User{ Account: serial.ToTypedMessage(&Account{ @@ -159,18 +159,18 @@ func TestUDPReaderWriter(t *testing.T) { b := buf.New() b.AppendSupplier(serial.WriteString("test payload")) err := writer.Write(buf.NewMultiBufferValue(b)) - assert.Error(err).IsNil() + assert(err, IsNil) payload, err := reader.Read() - assert.Error(err).IsNil() - assert.String(payload[0].String()).Equals("test payload") + assert(err, IsNil) + assert(payload[0].String(), Equals, "test payload") b = buf.New() b.AppendSupplier(serial.WriteString("test payload 2")) err = writer.Write(buf.NewMultiBufferValue(b)) - assert.Error(err).IsNil() + assert(err, IsNil) payload, err = reader.Read() - assert.Error(err).IsNil() - assert.String(payload[0].String()).Equals("test payload 2") + assert(err, IsNil) + assert(payload[0].String(), Equals, "test payload 2") } diff --git a/proxy/socks/protocol_test.go b/proxy/socks/protocol_test.go index f0111abc4..61f2d8dbd 100644 --- a/proxy/socks/protocol_test.go +++ b/proxy/socks/protocol_test.go @@ -7,11 +7,11 @@ import ( "v2ray.com/core/common/net" "v2ray.com/core/common/protocol" . "v2ray.com/core/proxy/socks" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestUDPEncoding(t *testing.T) { - assert := assert.On(t) + assert := With(t) b := buf.New() @@ -24,11 +24,11 @@ func TestUDPEncoding(t *testing.T) { content := []byte{'a'} payload := buf.New() payload.Append(content) - assert.Error(writer.Write(buf.NewMultiBufferValue(payload))).IsNil() + assert(writer.Write(buf.NewMultiBufferValue(payload)), IsNil) reader := NewUDPReader(b) decodedPayload, err := reader.Read() - assert.Error(err).IsNil() - assert.Bytes(decodedPayload[0].Bytes()).Equals(content) + assert(err, IsNil) + assert(decodedPayload[0].Bytes(), Equals, content) } diff --git a/proxy/vmess/encoding/auth_test.go b/proxy/vmess/encoding/auth_test.go index a160fdea9..c8abb584b 100644 --- a/proxy/vmess/encoding/auth_test.go +++ b/proxy/vmess/encoding/auth_test.go @@ -6,11 +6,11 @@ import ( "v2ray.com/core/common" . "v2ray.com/core/proxy/vmess/encoding" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestFnvAuth(t *testing.T) { - assert := assert.On(t) + assert := With(t) fnvAuth := new(FnvAuthenticator) expectedText := make([]byte, 256) @@ -20,7 +20,7 @@ func TestFnvAuth(t *testing.T) { buffer := make([]byte, 512) b := fnvAuth.Seal(buffer[:0], nil, expectedText, nil) b, err = fnvAuth.Open(buffer[:0], nil, b, nil) - assert.Error(err).IsNil() - assert.Int(len(b)).Equals(256) - assert.Bytes(b).Equals(expectedText) + assert(err, IsNil) + assert(len(b), Equals, 256) + assert(b, Equals, expectedText) } diff --git a/proxy/vmess/encoding/commands_test.go b/proxy/vmess/encoding/commands_test.go index 142c73cb7..f0d93bde5 100644 --- a/proxy/vmess/encoding/commands_test.go +++ b/proxy/vmess/encoding/commands_test.go @@ -7,11 +7,11 @@ import ( "v2ray.com/core/common/protocol" "v2ray.com/core/common/uuid" . "v2ray.com/core/proxy/vmess/encoding" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestSwitchAccount(t *testing.T) { - assert := assert.On(t) + assert := With(t) sa := &protocol.CommandSwitchAccount{ Port: 1234, @@ -23,18 +23,18 @@ func TestSwitchAccount(t *testing.T) { buffer := buf.New() err := MarshalCommand(sa, buffer) - assert.Error(err).IsNil() + assert(err, IsNil) cmd, err := UnmarshalCommand(1, buffer.BytesFrom(2)) - assert.Error(err).IsNil() + assert(err, IsNil) sa2, ok := cmd.(*protocol.CommandSwitchAccount) - assert.Bool(ok).IsTrue() - assert.Pointer(sa.Host).IsNil() - assert.Pointer(sa2.Host).IsNil() - assert.Port(sa.Port).Equals(sa2.Port) - assert.String(sa.ID.String()).Equals(sa2.ID.String()) - assert.Uint16(sa.AlterIds).Equals(sa2.AlterIds) - assert.Byte(byte(sa.Level)).Equals(byte(sa2.Level)) - assert.Byte(sa.ValidMin).Equals(sa2.ValidMin) + 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) } diff --git a/proxy/vmess/encoding/encoding_test.go b/proxy/vmess/encoding/encoding_test.go index 043e33840..6ed0edc1f 100644 --- a/proxy/vmess/encoding/encoding_test.go +++ b/proxy/vmess/encoding/encoding_test.go @@ -12,11 +12,11 @@ import ( "v2ray.com/core/common/uuid" "v2ray.com/core/proxy/vmess" . "v2ray.com/core/proxy/vmess/encoding" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestRequestSerialization(t *testing.T) { - assert := assert.On(t) + assert := With(t) user := &protocol.User{ Level: 0, @@ -52,18 +52,18 @@ func TestRequestSerialization(t *testing.T) { server := NewServerSession(userValidator, sessionHistory) actualRequest, err := server.DecodeRequestHeader(buffer) - assert.Error(err).IsNil() + assert(err, IsNil) - assert.Byte(expectedRequest.Version).Equals(actualRequest.Version) - assert.Byte(byte(expectedRequest.Command)).Equals(byte(actualRequest.Command)) - assert.Byte(byte(expectedRequest.Option)).Equals(byte(actualRequest.Option)) - assert.Address(expectedRequest.Address).Equals(actualRequest.Address) - assert.Port(expectedRequest.Port).Equals(actualRequest.Port) - assert.Byte(byte(expectedRequest.Security)).Equals(byte(actualRequest.Security)) + assert(expectedRequest.Version, Equals, actualRequest.Version) + assert(byte(expectedRequest.Command), Equals, byte(actualRequest.Command)) + assert(byte(expectedRequest.Option), Equals, byte(actualRequest.Option)) + assert(expectedRequest.Address, Equals, actualRequest.Address) + assert(expectedRequest.Port, Equals, actualRequest.Port) + assert(byte(expectedRequest.Security), Equals, byte(actualRequest.Security)) _, err = server.DecodeRequestHeader(buffer2) // anti replay attack - assert.Error(err).IsNotNil() + assert(err, IsNotNil) cancel() } diff --git a/testing/scenarios/dns_test.go b/testing/scenarios/dns_test.go index aadb66205..0360d1578 100644 --- a/testing/scenarios/dns_test.go +++ b/testing/scenarios/dns_test.go @@ -14,18 +14,18 @@ import ( "v2ray.com/core/proxy/blackhole" "v2ray.com/core/proxy/freedom" "v2ray.com/core/proxy/socks" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" ) func TestResolveIP(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() serverPort := pickPort() @@ -81,24 +81,24 @@ func TestResolveIP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig) - assert.Error(err).IsNil() + assert(err, IsNil) { noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port)) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "test payload" nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } CloseAllServers(servers) diff --git a/testing/scenarios/dokodemo_test.go b/testing/scenarios/dokodemo_test.go index 0c88d0eb4..9deee05bc 100644 --- a/testing/scenarios/dokodemo_test.go +++ b/testing/scenarios/dokodemo_test.go @@ -15,19 +15,19 @@ import ( "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/inbound" "v2ray.com/core/proxy/vmess/outbound" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/udp" ) func TestDokodemoTCP(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -109,38 +109,38 @@ func TestDokodemoTCP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) for port := clientPort; port <= clientPort+clientPortRange; port++ { conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } CloseAllServers(servers) } func TestDokodemoUDP(t *testing.T) { - assert := assert.On(t) + assert := With(t) udpServer := udp.Server{ MsgProcessor: xor, } dest, err := udpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer udpServer.Close() userID := protocol.NewID(uuid.New()) @@ -210,25 +210,25 @@ func TestDokodemoUDP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) for port := clientPort; port <= clientPort+clientPortRange; port++ { conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } CloseAllServers(servers) diff --git a/testing/scenarios/feature_test.go b/testing/scenarios/feature_test.go index 282bcbe1a..903fd943e 100644 --- a/testing/scenarios/feature_test.go +++ b/testing/scenarios/feature_test.go @@ -24,21 +24,21 @@ import ( "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/inbound" "v2ray.com/core/proxy/vmess/outbound" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/udp" "v2ray.com/core/transport/internet" ) func TestPassiveConnection(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, SendFirst: []byte("send first"), } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() serverPort := pickPort() @@ -66,49 +66,49 @@ func TestPassiveConnection(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(serverPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) { response := make([]byte, 1024) nBytes, err := conn.Read(response) - assert.Error(err).IsNil() - assert.String(string(response[:nBytes])).Equals("send first") + assert(err, IsNil) + assert(string(response[:nBytes]), Equals, "send first") } payload := "dokodemo request." { nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) } { response := make([]byte, 1024) nBytes, err := conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) } - assert.Error(conn.Close()).IsNil() + assert(conn.Close(), IsNil) CloseAllServers(servers) } func TestProxy(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() serverUserID := protocol.NewID(uuid.New()) @@ -227,36 +227,36 @@ func TestProxy(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) CloseAllServers(servers) } func TestProxyOverKCP(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() serverUserID := protocol.NewID(uuid.New()) @@ -386,43 +386,43 @@ func TestProxyOverKCP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) CloseAllServers(servers) } func TestBlackhole(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() tcpServer2 := tcp.Server{ MsgProcessor: xor, } dest2, err := tcpServer2.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer2.Close() serverPort := pickPort() @@ -479,41 +479,41 @@ func TestBlackhole(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(serverPort2), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." { nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) } { response := make([]byte, 1024) _, err := conn.Read(response) - assert.Error(err).IsNotNil() + assert(err, IsNotNil) } - assert.Error(conn.Close()).IsNil() + assert(conn.Close(), IsNil) CloseAllServers(servers) } func TestForward(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() serverPort := pickPort() @@ -549,37 +549,37 @@ func TestForward(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig) - assert.Error(err).IsNil() + assert(err, IsNil) { noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := noAuthDialer.Dial("tcp", "google.com:80") - assert.Error(err).IsNil() + assert(err, IsNil) payload := "test payload" nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } CloseAllServers(servers) } func TestUDPConnection(t *testing.T) { - assert := assert.On(t) + assert := With(t) udpServer := udp.Server{ MsgProcessor: xor, } dest, err := udpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer udpServer.Close() clientPort := pickPort() @@ -607,28 +607,28 @@ func TestUDPConnection(t *testing.T) { } servers, err := InitializeServerConfigs(clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) { conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." for i := 0; i < 5; i++ { nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) } - assert.Error(conn.Close()).IsNil() + assert(conn.Close(), IsNil) } time.Sleep(20 * time.Second) @@ -638,25 +638,25 @@ func TestUDPConnection(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } CloseAllServers(servers) } func TestDomainSniffing(t *testing.T) { - assert := assert.On(t) + assert := With(t) sniffingPort := pickPort() httpPort := pickPort() @@ -725,7 +725,7 @@ func TestDomainSniffing(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig) - assert.Error(err).IsNil() + assert(err, IsNil) { transport := &http.Transport{ @@ -739,10 +739,10 @@ func TestDomainSniffing(t *testing.T) { } resp, err := client.Get("https://www.github.com/") - assert.Error(err).IsNil() - assert.Int(resp.StatusCode).Equals(200) + assert(err, IsNil) + assert(resp.StatusCode, Equals, 200) - assert.Error(resp.Write(ioutil.Discard)).IsNil() + assert(resp.Write(ioutil.Discard), IsNil) } CloseAllServers(servers) diff --git a/testing/scenarios/http_test.go b/testing/scenarios/http_test.go index 181cc21cb..ad7ede42c 100644 --- a/testing/scenarios/http_test.go +++ b/testing/scenarios/http_test.go @@ -12,12 +12,12 @@ import ( "v2ray.com/core/common/serial" "v2ray.com/core/proxy/freedom" v2http "v2ray.com/core/proxy/http" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" v2httptest "v2ray.com/core/testing/servers/http" ) func TestHttpConformance(t *testing.T) { - assert := assert.On(t) + assert := With(t) httpServerPort := pickPort() httpServer := &v2httptest.Server{ @@ -25,7 +25,7 @@ func TestHttpConformance(t *testing.T) { PathHandler: make(map[string]http.HandlerFunc), } _, err := httpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer httpServer.Close() serverPort := pickPort() @@ -47,7 +47,7 @@ func TestHttpConformance(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig) - assert.Error(err).IsNil() + assert(err, IsNil) { transport := &http.Transport{ @@ -61,12 +61,12 @@ func TestHttpConformance(t *testing.T) { } resp, err := client.Get("http://127.0.0.1:" + httpServerPort.String()) - assert.Error(err).IsNil() - assert.Int(resp.StatusCode).Equals(200) + assert(err, IsNil) + assert(resp.StatusCode, Equals, 200) content, err := ioutil.ReadAll(resp.Body) - assert.Error(err).IsNil() - assert.String(string(content)).Equals("Home") + assert(err, IsNil) + assert(string(content), Equals, "Home") } diff --git a/testing/scenarios/shadowsocks_test.go b/testing/scenarios/shadowsocks_test.go index 3bfd948c5..94d8a0e96 100644 --- a/testing/scenarios/shadowsocks_test.go +++ b/testing/scenarios/shadowsocks_test.go @@ -15,19 +15,19 @@ import ( "v2ray.com/core/proxy/dokodemo" "v2ray.com/core/proxy/freedom" "v2ray.com/core/proxy/shadowsocks" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/udp" ) func TestShadowsocksAES256TCP(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() account := serial.ToTypedMessage(&shadowsocks.Account{ @@ -108,7 +108,7 @@ func TestShadowsocksAES256TCP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) var wg sync.WaitGroup wg.Add(10) @@ -118,18 +118,18 @@ func TestShadowsocksAES256TCP(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 10240*1024) rand.Read(payload) nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 10240*1024) - assert.Bytes(response).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) wg.Done() }() } @@ -139,13 +139,13 @@ func TestShadowsocksAES256TCP(t *testing.T) { } func TestShadowsocksAES128UDP(t *testing.T) { - assert := assert.On(t) + assert := With(t) udpServer := udp.Server{ MsgProcessor: xor, } dest, err := udpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer udpServer.Close() account := serial.ToTypedMessage(&shadowsocks.Account{ @@ -227,7 +227,7 @@ func TestShadowsocksAES128UDP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) var wg sync.WaitGroup wg.Add(10) @@ -237,18 +237,18 @@ func TestShadowsocksAES128UDP(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 1024) rand.Read(payload) nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*5, 1024) - assert.Bytes(response).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) wg.Done() }() } @@ -258,13 +258,13 @@ func TestShadowsocksAES128UDP(t *testing.T) { } func TestShadowsocksChacha20TCP(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() account := serial.ToTypedMessage(&shadowsocks.Account{ @@ -345,7 +345,7 @@ func TestShadowsocksChacha20TCP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) var wg sync.WaitGroup wg.Add(10) @@ -355,18 +355,18 @@ func TestShadowsocksChacha20TCP(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 10240*1024) rand.Read(payload) nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 10240*1024) - assert.Bytes(response).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) wg.Done() }() } diff --git a/testing/scenarios/socks_test.go b/testing/scenarios/socks_test.go index 11f3a1e40..264ab2cdd 100644 --- a/testing/scenarios/socks_test.go +++ b/testing/scenarios/socks_test.go @@ -13,19 +13,19 @@ import ( "v2ray.com/core/proxy/dokodemo" "v2ray.com/core/proxy/freedom" "v2ray.com/core/proxy/socks" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/udp" ) func TestSocksBridgeTCP(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() serverPort := pickPort() @@ -93,36 +93,36 @@ func TestSocksBridgeTCP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "test payload" nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) CloseAllServers(servers) } func TestSocksBridageUDP(t *testing.T) { - assert := assert.On(t) + assert := With(t) udpServer := udp.Server{ MsgProcessor: xor, } dest, err := udpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer udpServer.Close() serverPort := pickPort() @@ -190,36 +190,36 @@ func TestSocksBridageUDP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) CloseAllServers(servers) } func TestSocksConformance(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() authPort := pickPort() @@ -263,76 +263,76 @@ func TestSocksConformance(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig) - assert.Error(err).IsNil() + assert(err, IsNil) { noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr(), nil, xproxy.Direct) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := noAuthDialer.Dial("tcp", dest.NetAddr()) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "test payload" nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } { authDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, authPort).NetAddr(), &xproxy.Auth{User: "Test Account", Password: "Test Password"}, xproxy.Direct) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := authDialer.Dial("tcp", dest.NetAddr()) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "test payload" nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } { dialer := socks4.DialSocksProxy(socks4.SOCKS4, net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr()) conn, err := dialer("tcp", dest.NetAddr()) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "test payload" nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } { dialer := socks4.DialSocksProxy(socks4.SOCKS4A, net.TCPDestination(net.LocalHostIP, noAuthPort).NetAddr()) conn, err := dialer("tcp", net.TCPDestination(net.LocalHostDomain, tcpServer.Port).NetAddr()) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "test payload" nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } CloseAllServers(servers) diff --git a/testing/scenarios/tls_test.go b/testing/scenarios/tls_test.go index 8204157e1..f60ba490a 100644 --- a/testing/scenarios/tls_test.go +++ b/testing/scenarios/tls_test.go @@ -16,7 +16,7 @@ import ( "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/inbound" "v2ray.com/core/proxy/vmess/outbound" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" tlsgen "v2ray.com/core/testing/tls" "v2ray.com/core/transport/internet" @@ -25,13 +25,13 @@ import ( ) func TestSimpleTLSConnection(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -118,34 +118,34 @@ func TestSimpleTLSConnection(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*2, len(payload)) - assert.Bytes(response).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) CloseAllServers(servers) } func TestTLSOverKCP(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -234,34 +234,34 @@ func TestTLSOverKCP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*2, len(payload)) - assert.Bytes(response).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) CloseAllServers(servers) } func TestTLSOverWebSocket(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -356,23 +356,23 @@ func TestTLSOverWebSocket(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 10240*1024) rand.Read(payload) nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, len(payload)) - assert.Bytes(response).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) CloseAllServers(servers) } diff --git a/testing/scenarios/transport_test.go b/testing/scenarios/transport_test.go index e2052e01d..e5ae32699 100644 --- a/testing/scenarios/transport_test.go +++ b/testing/scenarios/transport_test.go @@ -15,7 +15,7 @@ import ( "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/inbound" "v2ray.com/core/proxy/vmess/outbound" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/headers/http" @@ -23,13 +23,13 @@ import ( ) func TestHttpConnectionHeader(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -120,22 +120,22 @@ func TestHttpConnectionHeader(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*2, len(payload)) - assert.Bytes(response).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) CloseAllServers(servers) } diff --git a/testing/scenarios/vmess_test.go b/testing/scenarios/vmess_test.go index 80a53fbf6..d8c5edcfe 100644 --- a/testing/scenarios/vmess_test.go +++ b/testing/scenarios/vmess_test.go @@ -18,20 +18,20 @@ import ( "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/inbound" "v2ray.com/core/proxy/vmess/outbound" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/udp" "v2ray.com/core/transport/internet" ) func TestVMessDynamicPort(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -135,38 +135,38 @@ func TestVMessDynamicPort(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) for i := 0; i < 10; i++ { conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) - assert.Error(err).IsNil() - assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(response[:nBytes], Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) } CloseAllServers(servers) } func TestVMessGCM(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -252,7 +252,7 @@ func TestVMessGCM(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) var wg sync.WaitGroup wg.Add(10) @@ -262,18 +262,18 @@ func TestVMessGCM(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 10240*1024) rand.Read(payload) nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 10240*1024) - assert.Bytes(response).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) wg.Done() }() } @@ -283,13 +283,13 @@ func TestVMessGCM(t *testing.T) { } func TestVMessGCMUDP(t *testing.T) { - assert := assert.On(t) + assert := With(t) udpServer := udp.Server{ MsgProcessor: xor, } dest, err := udpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer udpServer.Close() userID := protocol.NewID(uuid.New()) @@ -375,7 +375,7 @@ func TestVMessGCMUDP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) var wg sync.WaitGroup wg.Add(10) @@ -385,28 +385,28 @@ func TestVMessGCMUDP(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 1024) rand.Read(payload) nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) payload1 := make([]byte, 1024) rand.Read(payload1) nBytes, err = conn.Write([]byte(payload1)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload1)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload1)) response := readFrom(conn, time.Second*5, 1024) - assert.Bytes(response).Equals(xor([]byte(payload))) + assert(response, Equals, xor([]byte(payload))) response = readFrom(conn, time.Second*5, 1024) - assert.Bytes(response).Equals(xor([]byte(payload1))) + assert(response, Equals, xor([]byte(payload1))) - assert.Error(conn.Close()).IsNil() + assert(conn.Close(), IsNil) wg.Done() }() } @@ -416,13 +416,13 @@ func TestVMessGCMUDP(t *testing.T) { } func TestVMessChacha20(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -508,7 +508,7 @@ func TestVMessChacha20(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) var wg sync.WaitGroup wg.Add(10) @@ -518,18 +518,18 @@ func TestVMessChacha20(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 10240*1024) rand.Read(payload) nBytes, err := conn.Write([]byte(payload)) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 10240*1024) - assert.Bytes(response).Equals(xor([]byte(payload))) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor([]byte(payload))) + assert(conn.Close(), IsNil) wg.Done() }() } @@ -539,13 +539,13 @@ func TestVMessChacha20(t *testing.T) { } func TestVMessNone(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -631,7 +631,7 @@ func TestVMessNone(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) var wg sync.WaitGroup wg.Add(10) @@ -641,18 +641,18 @@ func TestVMessNone(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 1024*1024) rand.Read(payload) nBytes, err := conn.Write(payload) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 1024*1024) - assert.Bytes(response).Equals(xor(payload)) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor(payload)) + assert(conn.Close(), IsNil) wg.Done() }() } @@ -662,13 +662,13 @@ func TestVMessNone(t *testing.T) { } func TestVMessKCP(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -762,7 +762,7 @@ func TestVMessKCP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) var wg sync.WaitGroup for i := 0; i < 10; i++ { @@ -772,18 +772,18 @@ func TestVMessKCP(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 10240*1024) rand.Read(payload) nBytes, err := conn.Write(payload) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Minute, 10240*1024) - assert.Bytes(response).Equals(xor(payload)) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor(payload)) + assert(conn.Close(), IsNil) wg.Done() }() } @@ -794,14 +794,14 @@ func TestVMessKCP(t *testing.T) { func TestVMessIPv6(t *testing.T) { t.SkipNow() // No IPv6 on travis-ci. - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, Listen: net.LocalHostIPv6, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -887,36 +887,36 @@ func TestVMessIPv6(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: net.LocalHostIPv6.IP(), Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 1024) rand.Read(payload) nBytes, err := conn.Write(payload) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 1024) - assert.Bytes(response).Equals(xor(payload)) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xor(payload)) + assert(conn.Close(), IsNil) CloseAllServers(servers) } func TestVMessGCMMux(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() userID := protocol.NewID(uuid.New()) @@ -1008,7 +1008,7 @@ func TestVMessGCMMux(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) for range "abcd" { var wg sync.WaitGroup @@ -1020,7 +1020,7 @@ func TestVMessGCMMux(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 10240) rand.Read(payload) @@ -1028,12 +1028,12 @@ func TestVMessGCMMux(t *testing.T) { xorpayload := xor(payload) nBytes, err := conn.Write(payload) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 10240) - assert.Bytes(response).Equals(xorpayload) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xorpayload) + assert(conn.Close(), IsNil) wg.Done() }() } @@ -1045,20 +1045,20 @@ func TestVMessGCMMux(t *testing.T) { } func TestVMessGCMMuxUDP(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() udpServer := udp.Server{ MsgProcessor: xor, } udpDest, err := udpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer udpServer.Close() userID := protocol.NewID(uuid.New()) @@ -1164,7 +1164,7 @@ func TestVMessGCMMuxUDP(t *testing.T) { } servers, err := InitializeServerConfigs(serverConfig, clientConfig) - assert.Error(err).IsNil() + assert(err, IsNil) for range "abcd" { var wg sync.WaitGroup @@ -1176,7 +1176,7 @@ func TestVMessGCMMuxUDP(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) payload := make([]byte, 10240) rand.Read(payload) @@ -1184,12 +1184,12 @@ func TestVMessGCMMuxUDP(t *testing.T) { xorpayload := xor(payload) nBytes, err := conn.Write(payload) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) response := readFrom(conn, time.Second*20, 10240) - assert.Bytes(response).Equals(xorpayload) - assert.Error(conn.Close()).IsNil() + assert(response, Equals, xorpayload) + assert(conn.Close(), IsNil) wg.Done() }() } @@ -1199,7 +1199,7 @@ func TestVMessGCMMuxUDP(t *testing.T) { IP: []byte{127, 0, 0, 1}, Port: int(clientUDPPort), }) - assert.Error(err).IsNil() + assert(err, IsNil) conn.SetDeadline(time.Now().Add(time.Second * 10)) @@ -1210,20 +1210,20 @@ func TestVMessGCMMuxUDP(t *testing.T) { for j := 0; j < 2; j++ { nBytes, _, err := conn.WriteMsgUDP(payload, nil, nil) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(len(payload)) + assert(err, IsNil) + assert(nBytes, Equals, len(payload)) } response := make([]byte, 1024) oob := make([]byte, 16) for j := 0; j < 2; j++ { nBytes, _, _, _, err := conn.ReadMsgUDP(response, oob) - assert.Error(err).IsNil() - assert.Int(nBytes).Equals(1024) - assert.Bytes(response).Equals(xorpayload) + assert(err, IsNil) + assert(nBytes, Equals, 1024) + assert(response, Equals, xorpayload) } - assert.Error(conn.Close()).IsNil() + assert(conn.Close(), IsNil) wg.Done() }() } diff --git a/transport/internet/dialer_test.go b/transport/internet/dialer_test.go index deabfbbdc..07ce06096 100644 --- a/transport/internet/dialer_test.go +++ b/transport/internet/dialer_test.go @@ -5,21 +5,21 @@ import ( "testing" "v2ray.com/core/common/net" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" . "v2ray.com/core/transport/internet" ) func TestDialWithLocalAddr(t *testing.T) { - assert := assert.On(t) + assert := With(t) server := &tcp.Server{} dest, err := server.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer server.Close() conn, err := DialSystem(context.Background(), net.LocalHostIP, net.TCPDestination(net.LocalHostIP, dest.Port)) - assert.Error(err).IsNil() - assert.String(conn.RemoteAddr().String()).Equals("127.0.0.1:" + dest.Port.String()) + assert(err, IsNil) + assert(conn.RemoteAddr().String(), Equals, "127.0.0.1:" + dest.Port.String()) conn.Close() } diff --git a/transport/internet/header_test.go b/transport/internet/header_test.go index d4a13e4e2..4cfc72ee2 100644 --- a/transport/internet/header_test.go +++ b/transport/internet/header_test.go @@ -3,7 +3,7 @@ package internet_test import ( "testing" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" . "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/headers/noop" "v2ray.com/core/transport/internet/headers/srtp" @@ -11,17 +11,17 @@ import ( ) func TestAllHeadersLoadable(t *testing.T) { - assert := assert.On(t) + assert := With(t) noopAuth, err := CreatePacketHeader((*noop.Config)(nil)) - assert.Error(err).IsNil() - assert.Int(noopAuth.Size()).Equals(0) + assert(err, IsNil) + assert(noopAuth.Size(), Equals, 0) srtp, err := CreatePacketHeader((*srtp.Config)(nil)) - assert.Error(err).IsNil() - assert.Int(srtp.Size()).Equals(4) + assert(err, IsNil) + assert(srtp.Size(), Equals, 4) utp, err := CreatePacketHeader((*utp.Config)(nil)) - assert.Error(err).IsNil() - assert.Int(utp.Size()).Equals(4) + assert(err, IsNil) + assert(utp.Size(), Equals, 4) } diff --git a/transport/internet/headers/http/http_test.go b/transport/internet/headers/http/http_test.go index 6b805e12a..37a990d28 100644 --- a/transport/internet/headers/http/http_test.go +++ b/transport/internet/headers/http/http_test.go @@ -8,31 +8,31 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/net" "v2ray.com/core/common/serial" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" . "v2ray.com/core/transport/internet/headers/http" ) func TestReaderWriter(t *testing.T) { - assert := assert.On(t) + assert := With(t) cache := buf.New() b := buf.NewLocal(256) b.AppendSupplier(serial.WriteString("abcd" + ENDING)) writer := NewHeaderWriter(b) err := writer.Write(cache) - assert.Error(err).IsNil() - assert.Int(cache.Len()).Equals(8) + assert(err, IsNil) + assert(cache.Len(), Equals, 8) _, err = cache.Write([]byte{'e', 'f', 'g'}) - assert.Error(err).IsNil() + assert(err, IsNil) reader := &HeaderReader{} buffer, err := reader.Read(cache) - assert.Error(err).IsNil() - assert.Bytes(buffer.Bytes()).Equals([]byte{'e', 'f', 'g'}) + assert(err, IsNil) + assert(buffer.Bytes(), Equals, []byte{'e', 'f', 'g'}) } func TestRequestHeader(t *testing.T) { - assert := assert.On(t) + assert := With(t) auth, err := NewHttpAuthenticator(context.Background(), &Config{ Request: &RequestConfig{ @@ -45,39 +45,39 @@ func TestRequestHeader(t *testing.T) { }, }, }) - assert.Error(err).IsNil() + assert(err, IsNil) cache := buf.New() err = auth.GetClientWriter().Write(cache) - assert.Error(err).IsNil() + assert(err, IsNil) - assert.String(cache.String()).Equals("GET / HTTP/1.1\r\nTest: Value\r\n\r\n") + assert(cache.String(), Equals, "GET / HTTP/1.1\r\nTest: Value\r\n\r\n") } func TestConnection(t *testing.T) { - assert := assert.On(t) + assert := With(t) auth, err := NewHttpAuthenticator(context.Background(), new(Config)) - assert.Error(err).IsNil() + assert(err, IsNil) listener, err := net.Listen("tcp", "127.0.0.1:0") - assert.Error(err).IsNil() + assert(err, IsNil) go func() { conn, err := listener.Accept() - assert.Error(err).IsNil() + assert(err, IsNil) authConn := auth.Server(conn) b := make([]byte, 256) for { n, err := authConn.Read(b) - assert.Error(err).IsNil() + assert(err, IsNil) _, err = authConn.Write(b[:n]) - assert.Error(err).IsNil() + assert(err, IsNil) } }() conn, err := net.DialTCP("tcp", nil, listener.Addr().(*net.TCPAddr)) - assert.Error(err).IsNil() + assert(err, IsNil) authConn := auth.Client(conn) authConn.Write([]byte("Test payload")) @@ -89,12 +89,12 @@ func TestConnection(t *testing.T) { totalBytes := 0 for { n, err := authConn.Read(actualResponse[totalBytes:]) - assert.Error(err).IsNil() + assert(err, IsNil) totalBytes += n if totalBytes >= len(expectedResponse) || time.Now().After(deadline) { break } } - assert.String(string(actualResponse[:totalBytes])).Equals(expectedResponse) + assert(string(actualResponse[:totalBytes]), Equals, expectedResponse) } diff --git a/transport/internet/headers/srtp/srtp_test.go b/transport/internet/headers/srtp/srtp_test.go index f0e0be2ba..4417e1c7e 100644 --- a/transport/internet/headers/srtp/srtp_test.go +++ b/transport/internet/headers/srtp/srtp_test.go @@ -4,12 +4,12 @@ import ( "testing" "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" . "v2ray.com/core/transport/internet/headers/srtp" ) func TestSRTPWrite(t *testing.T) { - assert := assert.On(t) + assert := With(t) content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} srtp := SRTP{} @@ -18,5 +18,5 @@ func TestSRTPWrite(t *testing.T) { payload.AppendSupplier(srtp.Write) payload.Append(content) - assert.Int(payload.Len()).Equals(len(content) + srtp.Size()) + assert(payload.Len(), Equals, len(content) + srtp.Size()) } diff --git a/transport/internet/headers/utp/utp_test.go b/transport/internet/headers/utp/utp_test.go index 3faa3bbcd..06760db3d 100644 --- a/transport/internet/headers/utp/utp_test.go +++ b/transport/internet/headers/utp/utp_test.go @@ -4,12 +4,12 @@ import ( "testing" "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" . "v2ray.com/core/transport/internet/headers/utp" ) func TestUTPWrite(t *testing.T) { - assert := assert.On(t) + assert := With(t) content := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g'} utp := UTP{} @@ -18,5 +18,5 @@ func TestUTPWrite(t *testing.T) { payload.AppendSupplier(utp.Write) payload.Append(content) - assert.Int(payload.Len()).Equals(len(content) + utp.Size()) + assert(payload.Len(), Equals, len(content) + utp.Size()) } diff --git a/transport/internet/headers/wechat/wechat_test.go b/transport/internet/headers/wechat/wechat_test.go index 5bdaa04fc..e3e840eaf 100644 --- a/transport/internet/headers/wechat/wechat_test.go +++ b/transport/internet/headers/wechat/wechat_test.go @@ -4,17 +4,17 @@ import ( "testing" "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" . "v2ray.com/core/transport/internet/headers/wechat" ) func TestUTPWrite(t *testing.T) { - assert := assert.On(t) + assert := With(t) video := VideoChat{} payload := buf.NewLocal(2048) payload.AppendSupplier(video.Write) - assert.Int(payload.Len()).Equals(video.Size()) + assert(payload.Len(), Equals, video.Size()) } diff --git a/transport/internet/kcp/connection_test.go b/transport/internet/kcp/connection_test.go index 231f6e325..786c320c2 100644 --- a/transport/internet/kcp/connection_test.go +++ b/transport/internet/kcp/connection_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" . "v2ray.com/core/transport/internet/kcp" ) @@ -51,15 +51,15 @@ func (o *NoOpConn) SetWriteDeadline(time.Time) error { func (o *NoOpConn) Reset(input func([]Segment)) {} func TestConnectionReadTimeout(t *testing.T) { - assert := assert.On(t) + assert := With(t) conn := NewConnection(1, &NoOpConn{}, &Config{}) conn.SetReadDeadline(time.Now().Add(time.Second)) b := make([]byte, 1024) nBytes, err := conn.Read(b) - assert.Int(nBytes).Equals(0) - assert.Error(err).IsNotNil() + assert(nBytes, Equals, 0) + assert(err, IsNotNil) conn.Terminate() } diff --git a/transport/internet/kcp/crypt_test.go b/transport/internet/kcp/crypt_test.go index 47ddc7b52..524170766 100644 --- a/transport/internet/kcp/crypt_test.go +++ b/transport/internet/kcp/crypt_test.go @@ -3,12 +3,12 @@ package kcp_test import ( "testing" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" . "v2ray.com/core/transport/internet/kcp" ) func TestSimpleAuthenticator(t *testing.T) { - assert := assert.On(t) + assert := With(t) cache := make([]byte, 512) @@ -17,12 +17,12 @@ func TestSimpleAuthenticator(t *testing.T) { auth := NewSimpleAuthenticator() b := auth.Seal(cache[:0], nil, payload, nil) c, err := auth.Open(cache[:0], nil, b, nil) - assert.Error(err).IsNil() - assert.Bytes(c).Equals(payload) + assert(err, IsNil) + assert(c, Equals, payload) } func TestSimpleAuthenticator2(t *testing.T) { - assert := assert.On(t) + assert := With(t) cache := make([]byte, 512) @@ -31,6 +31,6 @@ func TestSimpleAuthenticator2(t *testing.T) { auth := NewSimpleAuthenticator() b := auth.Seal(cache[:0], nil, payload, nil) c, err := auth.Open(cache[:0], nil, b, nil) - assert.Error(err).IsNil() - assert.Bytes(c).Equals(payload) + assert(err, IsNil) + assert(c, Equals, payload) } diff --git a/transport/internet/kcp/kcp_test.go b/transport/internet/kcp/kcp_test.go index 6658d2606..1d6dadd78 100644 --- a/transport/internet/kcp/kcp_test.go +++ b/transport/internet/kcp/kcp_test.go @@ -9,13 +9,13 @@ import ( "time" "v2ray.com/core/common/net" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/transport/internet" . "v2ray.com/core/transport/internet/kcp" ) func TestDialAndListen(t *testing.T) { - assert := assert.On(t) + assert := With(t) listerner, err := NewListener(internet.ContextWithTransportSettings(context.Background(), &Config{}), net.LocalHostIP, net.Port(0), func(ctx context.Context, conn internet.Connection) bool { go func(c internet.Connection) { @@ -34,14 +34,14 @@ func TestDialAndListen(t *testing.T) { }(conn) return true }) - assert.Error(err).IsNil() + assert(err, IsNil) port := net.Port(listerner.Addr().(*net.UDPAddr).Port) ctx := internet.ContextWithTransportSettings(context.Background(), &Config{}) wg := new(sync.WaitGroup) for i := 0; i < 10; i++ { clientConn, err := DialKCP(ctx, net.UDPDestination(net.LocalHostIP, port)) - assert.Error(err).IsNil() + assert(err, IsNil) wg.Add(1) go func() { @@ -51,14 +51,14 @@ func TestDialAndListen(t *testing.T) { clientReceived := make([]byte, 1024*1024) nBytes, _ := io.ReadFull(clientConn, clientReceived) - assert.Int(nBytes).Equals(len(clientReceived)) + assert(nBytes, Equals, len(clientReceived)) clientConn.Close() clientExpected := make([]byte, 1024*1024) for idx, b := range clientSend { clientExpected[idx] = b ^ 'c' } - assert.Bytes(clientReceived).Equals(clientExpected) + assert(clientReceived, Equals, clientExpected) wg.Done() }() @@ -68,7 +68,7 @@ func TestDialAndListen(t *testing.T) { for i := 0; i < 60 && listerner.ActiveConnections() > 0; i++ { time.Sleep(500 * time.Millisecond) } - assert.Int(listerner.ActiveConnections()).Equals(0) + assert(listerner.ActiveConnections(), Equals, 0) listerner.Close() } diff --git a/transport/internet/kcp/receiving_test.go b/transport/internet/kcp/receiving_test.go index ca12143b2..21cf2c8f0 100644 --- a/transport/internet/kcp/receiving_test.go +++ b/transport/internet/kcp/receiving_test.go @@ -3,12 +3,12 @@ package kcp_test import ( "testing" - "v2ray.com/core/testing/assert" . "v2ray.com/core/transport/internet/kcp" + . "v2ray.com/ext/assert" ) func TestRecivingWindow(t *testing.T) { - assert := assert.On(t) + assert := With(t) window := NewReceivingWindow(3) @@ -17,20 +17,18 @@ func TestRecivingWindow(t *testing.T) { seg2 := &DataSegment{} seg3 := &DataSegment{} - assert.Bool(window.Set(0, seg0)).IsTrue() - assert.Pointer(window.RemoveFirst()).Equals(seg0) + assert(window.Set(0, seg0), IsTrue) + assert(window.RemoveFirst(), Equals, seg0) e := window.RemoveFirst() - if e != nil { - assert.Fail("Expecting nil.") - } + assert(e, IsNil) - assert.Bool(window.Set(1, seg1)).IsTrue() - assert.Bool(window.Set(2, seg2)).IsTrue() + assert(window.Set(1, seg1), IsTrue) + assert(window.Set(2, seg2), IsTrue) window.Advance() - assert.Bool(window.Set(2, seg3)).IsTrue() + assert(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) + assert(window.RemoveFirst(), Equals, seg1) + assert(window.Remove(1), Equals, seg2) + assert(window.Remove(2), Equals, seg3) } diff --git a/transport/internet/kcp/segment_test.go b/transport/internet/kcp/segment_test.go index c573c1596..13247692b 100644 --- a/transport/internet/kcp/segment_test.go +++ b/transport/internet/kcp/segment_test.go @@ -4,20 +4,20 @@ import ( "testing" "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" . "v2ray.com/core/transport/internet/kcp" ) func TestBadSegment(t *testing.T) { - assert := assert.On(t) + assert := With(t) seg, buf := ReadSegment(nil) - assert.Pointer(seg).IsNil() - assert.Int(len(buf)).Equals(0) + assert(seg, IsNil) + assert(len(buf), Equals, 0) } func TestDataSegment(t *testing.T) { - assert := assert.On(t) + assert := With(t) b := buf.NewLocal(512) b.Append([]byte{'a', 'b', 'c', 'd'}) @@ -33,19 +33,19 @@ func TestDataSegment(t *testing.T) { bytes := make([]byte, nBytes) seg.Bytes()(bytes) - assert.Int(len(bytes)).Equals(nBytes) + assert(len(bytes), Equals, nBytes) iseg, _ := ReadSegment(bytes) seg2 := iseg.(*DataSegment) - assert.Uint16(seg2.Conv).Equals(seg.Conv) - assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp) - assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext) - assert.Uint32(seg2.Number).Equals(seg.Number) - assert.Bytes(seg2.Data.Bytes()).Equals(seg.Data.Bytes()) + assert(seg2.Conv, Equals, seg.Conv) + assert(seg2.Timestamp, Equals, seg.Timestamp) + assert(seg2.SendingNext, Equals, seg.SendingNext) + assert(seg2.Number, Equals, seg.Number) + assert(seg2.Data.Bytes(), Equals, seg.Data.Bytes()) } func Test1ByteDataSegment(t *testing.T) { - assert := assert.On(t) + assert := With(t) b := buf.NewLocal(512) b.AppendBytes('a') @@ -61,19 +61,19 @@ func Test1ByteDataSegment(t *testing.T) { bytes := make([]byte, nBytes) seg.Bytes()(bytes) - assert.Int(len(bytes)).Equals(nBytes) + assert(len(bytes), Equals, nBytes) iseg, _ := ReadSegment(bytes) seg2 := iseg.(*DataSegment) - assert.Uint16(seg2.Conv).Equals(seg.Conv) - assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp) - assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext) - assert.Uint32(seg2.Number).Equals(seg.Number) - assert.Bytes(seg2.Data.Bytes()).Equals(seg.Data.Bytes()) + assert(seg2.Conv, Equals, seg.Conv) + assert(seg2.Timestamp, Equals, seg.Timestamp) + assert(seg2.SendingNext, Equals, seg.SendingNext) + assert(seg2.Number, Equals, seg.Number) + assert(seg2.Data.Bytes(), Equals, seg.Data.Bytes()) } func TestACKSegment(t *testing.T) { - assert := assert.On(t) + assert := With(t) seg := &AckSegment{ Conv: 1, @@ -87,22 +87,22 @@ func TestACKSegment(t *testing.T) { bytes := make([]byte, nBytes) seg.Bytes()(bytes) - assert.Int(len(bytes)).Equals(nBytes) + assert(len(bytes), Equals, nBytes) iseg, _ := ReadSegment(bytes) seg2 := iseg.(*AckSegment) - assert.Uint16(seg2.Conv).Equals(seg.Conv) - assert.Uint32(seg2.ReceivingWindow).Equals(seg.ReceivingWindow) - assert.Uint32(seg2.ReceivingNext).Equals(seg.ReceivingNext) - assert.Int(len(seg2.NumberList)).Equals(len(seg.NumberList)) - assert.Uint32(seg2.Timestamp).Equals(seg.Timestamp) + assert(seg2.Conv, Equals, seg.Conv) + assert(seg2.ReceivingWindow, Equals, seg.ReceivingWindow) + assert(seg2.ReceivingNext, Equals, seg.ReceivingNext) + assert(len(seg2.NumberList), Equals, len(seg.NumberList)) + assert(seg2.Timestamp, Equals, seg.Timestamp) for i, number := range seg2.NumberList { - assert.Uint32(number).Equals(seg.NumberList[i]) + assert(number, Equals, seg.NumberList[i]) } } func TestCmdSegment(t *testing.T) { - assert := assert.On(t) + assert := With(t) seg := &CmdOnlySegment{ Conv: 1, @@ -117,14 +117,14 @@ func TestCmdSegment(t *testing.T) { bytes := make([]byte, nBytes) seg.Bytes()(bytes) - assert.Int(len(bytes)).Equals(nBytes) + assert(len(bytes), Equals, nBytes) iseg, _ := ReadSegment(bytes) seg2 := iseg.(*CmdOnlySegment) - assert.Uint16(seg2.Conv).Equals(seg.Conv) - assert.Byte(byte(seg2.Command())).Equals(byte(seg.Command())) - assert.Byte(byte(seg2.Option)).Equals(byte(seg.Option)) - assert.Uint32(seg2.SendingNext).Equals(seg.SendingNext) - assert.Uint32(seg2.ReceivinNext).Equals(seg.ReceivinNext) - assert.Uint32(seg2.PeerRTO).Equals(seg.PeerRTO) + assert(seg2.Conv, Equals, seg.Conv) + assert(byte(seg2.Command()), Equals, byte(seg.Command())) + assert(byte(seg2.Option), Equals, byte(seg.Option)) + assert(seg2.SendingNext, Equals, seg.SendingNext) + assert(seg2.ReceivinNext, Equals, seg.ReceivinNext) + assert(seg2.PeerRTO, Equals, seg.PeerRTO) } diff --git a/transport/internet/kcp/sending_test.go b/transport/internet/kcp/sending_test.go index dfdb99693..d8f689980 100644 --- a/transport/internet/kcp/sending_test.go +++ b/transport/internet/kcp/sending_test.go @@ -3,40 +3,40 @@ package kcp_test import ( "testing" - "v2ray.com/core/testing/assert" . "v2ray.com/core/transport/internet/kcp" + . "v2ray.com/ext/assert" ) func TestSendingWindow(t *testing.T) { - assert := assert.On(t) + assert := With(t) window := NewSendingWindow(5, nil, nil) window.Push(0, []byte{}) window.Push(1, []byte{}) window.Push(2, []byte{}) - assert.Int(window.Len()).Equals(3) + assert(window.Len(), Equals, 3) window.Remove(1) - assert.Int(window.Len()).Equals(3) - assert.Uint32(window.FirstNumber()).Equals(0) + assert(window.Len(), Equals, 3) + assert(window.FirstNumber(), Equals, uint32(0)) window.Remove(0) - assert.Int(window.Len()).Equals(1) - assert.Uint32(window.FirstNumber()).Equals(2) + assert(window.Len(), Equals, 1) + assert(window.FirstNumber(), Equals, uint32(2)) window.Remove(0) - assert.Int(window.Len()).Equals(0) + assert(window.Len(), Equals, 0) window.Push(4, []byte{}) - assert.Int(window.Len()).Equals(1) - assert.Uint32(window.FirstNumber()).Equals(4) + assert(window.Len(), Equals, 1) + assert(window.FirstNumber(), Equals, uint32(4)) window.Push(5, []byte{}) - assert.Int(window.Len()).Equals(2) + assert(window.Len(), Equals, 2) window.Remove(1) - assert.Int(window.Len()).Equals(2) + assert(window.Len(), Equals, 2) window.Remove(0) - assert.Int(window.Len()).Equals(0) + assert(window.Len(), Equals, 0) } diff --git a/transport/internet/tcp/sockopt_linux_test.go b/transport/internet/tcp/sockopt_linux_test.go index 34e430d8d..bd748824f 100644 --- a/transport/internet/tcp/sockopt_linux_test.go +++ b/transport/internet/tcp/sockopt_linux_test.go @@ -7,23 +7,23 @@ import ( "strings" "testing" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" "v2ray.com/core/testing/servers/tcp" . "v2ray.com/core/transport/internet/tcp" ) func TestGetOriginalDestination(t *testing.T) { - assert := assert.On(t) + assert := With(t) tcpServer := tcp.Server{} dest, err := tcpServer.Start() - assert.Error(err).IsNil() + assert(err, IsNil) defer tcpServer.Close() conn, err := Dial(context.Background(), dest) - assert.Error(err).IsNil() + assert(err, IsNil) defer conn.Close() originalDest, err := GetOriginalDestination(conn) - assert.Bool(dest == originalDest || strings.Contains(err.Error(), "failed to call getsockopt")) + assert(dest == originalDest || strings.Contains(err.Error(), "failed to call getsockopt")) } diff --git a/transport/internet/udp/dispatcher_test.go b/transport/internet/udp/dispatcher_test.go index 5f44b53e7..b780ab16a 100644 --- a/transport/internet/udp/dispatcher_test.go +++ b/transport/internet/udp/dispatcher_test.go @@ -8,9 +8,9 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/net" - "v2ray.com/core/testing/assert" . "v2ray.com/core/transport/internet/udp" "v2ray.com/core/transport/ray" + . "v2ray.com/ext/assert" ) type TestDispatcher struct { @@ -22,7 +22,7 @@ func (d *TestDispatcher) Dispatch(ctx context.Context, dest net.Destination) (ra } func TestSameDestinationDispatching(t *testing.T) { - assert := assert.On(t) + assert := With(t) ctx, cancel := context.WithCancel(context.Background()) link := ray.NewRay(ctx) @@ -33,7 +33,7 @@ func TestSameDestinationDispatching(t *testing.T) { break } err = link.OutboundOutput().Write(data) - assert.Error(err).IsNil() + assert(err, IsNil) } }() @@ -60,6 +60,6 @@ func TestSameDestinationDispatching(t *testing.T) { time.Sleep(time.Second) cancel() - assert.Uint32(count).Equals(1) - assert.Uint32(msgCount).Equals(6) + assert(count, Equals, uint32(1)) + assert(msgCount, Equals, uint32(6)) } diff --git a/transport/internet/websocket/ws_test.go b/transport/internet/websocket/ws_test.go index c90161c1c..ac97fbf89 100644 --- a/transport/internet/websocket/ws_test.go +++ b/transport/internet/websocket/ws_test.go @@ -7,15 +7,15 @@ import ( "time" "v2ray.com/core/common/net" - "v2ray.com/core/testing/assert" tlsgen "v2ray.com/core/testing/tls" "v2ray.com/core/transport/internet" v2tls "v2ray.com/core/transport/internet/tls" . "v2ray.com/core/transport/internet/websocket" + . "v2ray.com/ext/assert" ) func Test_listenWSAndDial(t *testing.T) { - assert := assert.On(t) + assert := With(t) listen, err := ListenWS(internet.ContextWithTransportSettings(context.Background(), &Config{ Path: "ws", }), net.DomainAddress("localhost"), 13146, func(ctx context.Context, conn internet.Connection) bool { @@ -24,60 +24,58 @@ func Test_listenWSAndDial(t *testing.T) { var b [1024]byte n, err := c.Read(b[:]) - //assert.Error(err).IsNil() + //assert(err, IsNil) if err != nil { return } - assert.Bool(bytes.HasPrefix(b[:n], []byte("Test connection"))).IsTrue() + assert(bytes.HasPrefix(b[:n], []byte("Test connection")), IsTrue) _, err = c.Write([]byte("Response")) - assert.Error(err).IsNil() + assert(err, IsNil) }(conn) return true }) - assert.Error(err).IsNil() + assert(err, IsNil) ctx := internet.ContextWithTransportSettings(context.Background(), &Config{Path: "ws"}) conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146)) - assert.Error(err).IsNil() + assert(err, IsNil) _, err = conn.Write([]byte("Test connection 1")) - assert.Error(err).IsNil() + assert(err, IsNil) var b [1024]byte n, err := conn.Read(b[:]) - assert.Error(err).IsNil() - assert.String(string(b[:n])).Equals("Response") + assert(err, IsNil) + assert(string(b[:n]), Equals, "Response") - assert.Error(conn.Close()).IsNil() + assert(conn.Close(), IsNil) <-time.After(time.Second * 5) conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146)) - assert.Error(err).IsNil() + assert(err, IsNil) _, err = conn.Write([]byte("Test connection 2")) - assert.Error(err).IsNil() + assert(err, IsNil) n, err = conn.Read(b[:]) - assert.Error(err).IsNil() - assert.String(string(b[:n])).Equals("Response") - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(string(b[:n]), Equals, "Response") + assert(conn.Close(), IsNil) <-time.After(time.Second * 15) conn, err = Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146)) - assert.Error(err).IsNil() + assert(err, IsNil) _, err = conn.Write([]byte("Test connection 3")) - assert.Error(err).IsNil() + assert(err, IsNil) n, err = conn.Read(b[:]) - assert.Error(err).IsNil() - assert.String(string(b[:n])).Equals("Response") - assert.Error(conn.Close()).IsNil() + assert(err, IsNil) + assert(string(b[:n]), Equals, "Response") + assert(conn.Close(), IsNil) - assert.Error(listen.Close()).IsNil() + assert(listen.Close(), IsNil) } func Test_listenWSAndDial_TLS(t *testing.T) { - assert := assert.On(t) - go func() { - <-time.After(time.Second * 5) - assert.Fail("Too slow") - }() + assert := With(t) + + start := time.Now() ctx := internet.ContextWithTransportSettings(context.Background(), &Config{ Path: "wss", @@ -92,10 +90,13 @@ func Test_listenWSAndDial_TLS(t *testing.T) { }() return true }) - assert.Error(err).IsNil() + assert(err, IsNil) defer listen.Close() conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13143)) - assert.Error(err).IsNil() + assert(err, IsNil) _ = conn.Close() + + end := time.Now() + assert(end.Before(start.Add(time.Second*5)), IsTrue) } diff --git a/transport/ray/direct_test.go b/transport/ray/direct_test.go index 20d0d443f..6a16296c1 100644 --- a/transport/ray/direct_test.go +++ b/transport/ray/direct_test.go @@ -6,44 +6,44 @@ import ( "testing" "v2ray.com/core/common/buf" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" . "v2ray.com/core/transport/ray" ) func TestStreamIO(t *testing.T) { - assert := assert.On(t) + assert := With(t) stream := NewStream(context.Background()) b1 := buf.New() b1.AppendBytes('a') - assert.Error(stream.Write(buf.NewMultiBufferValue(b1))).IsNil() + assert(stream.Write(buf.NewMultiBufferValue(b1)), IsNil) _, err := stream.Read() - assert.Error(err).IsNil() + assert(err, IsNil) stream.Close() _, err = stream.Read() - assert.Error(err).Equals(io.EOF) + assert(err, Equals, io.EOF) b2 := buf.New() b2.AppendBytes('b') err = stream.Write(buf.NewMultiBufferValue(b2)) - assert.Error(err).Equals(io.ErrClosedPipe) + assert(err, Equals, io.ErrClosedPipe) } func TestStreamClose(t *testing.T) { - assert := assert.On(t) + assert := With(t) stream := NewStream(context.Background()) b1 := buf.New() b1.AppendBytes('a') - assert.Error(stream.Write(buf.NewMultiBufferValue(b1))).IsNil() + assert(stream.Write(buf.NewMultiBufferValue(b1)), IsNil) stream.Close() _, err := stream.Read() - assert.Error(err).IsNil() + assert(err, IsNil) _, err = stream.Read() - assert.Error(err).Equals(io.EOF) + assert(err, Equals, io.EOF) } diff --git a/v2ray_test.go b/v2ray_test.go index a78bd6d53..4374340c3 100644 --- a/v2ray_test.go +++ b/v2ray_test.go @@ -14,11 +14,11 @@ import ( "v2ray.com/core/proxy/dokodemo" "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/outbound" - "v2ray.com/core/testing/assert" + . "v2ray.com/ext/assert" ) func TestV2RayClose(t *testing.T) { - assert := assert.On(t) + assert := With(t) port := net.Port(dice.RollUint16()) config := &Config{ @@ -59,7 +59,7 @@ func TestV2RayClose(t *testing.T) { } server, err := New(config) - assert.Error(err).IsNil() + assert(err, IsNil) server.Close() }