1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

fix test cases

This commit is contained in:
Darien Raymond 2017-01-29 09:02:19 +01:00
parent 6629b6dd28
commit ebf15aa6bb
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 16 additions and 57 deletions

View File

@ -3,12 +3,17 @@ package blackhole
import ( import (
"context" "context"
"errors"
"time" "time"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/transport/ray" "v2ray.com/core/transport/ray"
) )
var (
errConnectionBlocked = errors.New("Blackhole: connection blocked.")
)
// Handler is an outbound connection that sliently swallow the entire payload. // Handler is an outbound connection that sliently swallow the entire payload.
type Handler struct { type Handler struct {
response ResponseConfig response ResponseConfig
@ -28,12 +33,9 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
// Dispatch implements OutboundHandler.Dispatch(). // Dispatch implements OutboundHandler.Dispatch().
func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay) error { func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay) error {
v.response.WriteTo(outboundRay.OutboundOutput()) v.response.WriteTo(outboundRay.OutboundOutput())
// CloseError() will immediately close the connection.
// Sleep a little here to make sure the response is sent to client. // Sleep a little here to make sure the response is sent to client.
time.Sleep(time.Millisecond * 500) time.Sleep(time.Second)
outboundRay.OutboundInput().CloseError() return errConnectionBlocked
outboundRay.OutboundOutput().CloseError()
return nil
} }
func init() { func init() {

View File

@ -1,6 +1,7 @@
package scenarios package scenarios
import ( import (
"io"
"net" "net"
"time" "time"
@ -29,20 +30,10 @@ func xor(b []byte) []byte {
func readFrom(conn net.Conn, timeout time.Duration, length int) []byte { func readFrom(conn net.Conn, timeout time.Duration, length int) []byte {
b := make([]byte, 2048) b := make([]byte, 2048)
totalBytes := 0
deadline := time.Now().Add(timeout) deadline := time.Now().Add(timeout)
conn.SetReadDeadline(deadline) conn.SetReadDeadline(deadline)
for totalBytes < length { n, _ := io.ReadFull(conn, b[:length])
if time.Now().After(deadline) { return b[:n]
break
}
n, err := conn.Read(b[totalBytes:])
if err != nil {
break
}
totalBytes += n
}
return b[:totalBytes]
} }
func InitializeServerConfig(config *core.Config) error { func InitializeServerConfig(config *core.Config) error {

View File

@ -1,11 +1,10 @@
package scenarios package scenarios
import ( import (
"fmt"
"net" "net"
"testing" "testing"
"time"
"v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/tcp"
@ -42,24 +41,8 @@ func TestDynamicVMess(t *testing.T) {
assert.Int(nBytes).Equals(len(payload)) assert.Int(nBytes).Equals(len(payload))
expectedResponse := "Processed: " + payload expectedResponse := "Processed: " + payload
finished := false response := readFrom(conn, time.Second, len(expectedResponse))
response := buf.New() assert.String(string(response)).Equals(expectedResponse)
for {
err := response.AppendSupplier(buf.ReadFrom(conn))
assert.Error(err).IsNil()
if err != nil {
break
}
if response.String() == expectedResponse {
finished = true
break
}
if response.Len() > len(expectedResponse) {
fmt.Printf("Unexpected response: %v\n", response.Bytes())
break
}
}
assert.Bool(finished).IsTrue()
conn.Close() conn.Close()
} }

View File

@ -1,11 +1,10 @@
package scenarios package scenarios
import ( import (
"fmt"
"net" "net"
"testing" "testing"
"time"
"v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp" "v2ray.com/core/testing/servers/tcp"
@ -41,25 +40,9 @@ func TestShadowsocksTCP(t *testing.T) {
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Int(nBytes).Equals(len(payload)) assert.Int(nBytes).Equals(len(payload))
response := buf.New()
finished := false
expectedResponse := "Processed: " + payload expectedResponse := "Processed: " + payload
for { response := readFrom(conn, time.Second, len(expectedResponse))
err := response.AppendSupplier(buf.ReadFrom(conn)) assert.String(string(response)).Equals(expectedResponse)
assert.Error(err).IsNil()
if err != nil {
break
}
if response.String() == expectedResponse {
finished = true
break
}
if response.Len() > len(expectedResponse) {
fmt.Printf("Unexpected response: %v\n", response.Bytes())
break
}
}
assert.Bool(finished).IsTrue()
conn.Close() conn.Close()
} }