1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -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 (
"context"
"errors"
"time"
"v2ray.com/core/common"
"v2ray.com/core/transport/ray"
)
var (
errConnectionBlocked = errors.New("Blackhole: connection blocked.")
)
// Handler is an outbound connection that sliently swallow the entire payload.
type Handler struct {
response ResponseConfig
@ -28,12 +33,9 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
// Dispatch implements OutboundHandler.Dispatch().
func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay) error {
v.response.WriteTo(outboundRay.OutboundOutput())
// CloseError() will immediately close the connection.
// Sleep a little here to make sure the response is sent to client.
time.Sleep(time.Millisecond * 500)
outboundRay.OutboundInput().CloseError()
outboundRay.OutboundOutput().CloseError()
return nil
time.Sleep(time.Second)
return errConnectionBlocked
}
func init() {

View File

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

View File

@ -1,11 +1,10 @@
package scenarios
import (
"fmt"
"net"
"testing"
"time"
"v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp"
@ -42,24 +41,8 @@ func TestDynamicVMess(t *testing.T) {
assert.Int(nBytes).Equals(len(payload))
expectedResponse := "Processed: " + payload
finished := false
response := buf.New()
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()
response := readFrom(conn, time.Second, len(expectedResponse))
assert.String(string(response)).Equals(expectedResponse)
conn.Close()
}

View File

@ -1,11 +1,10 @@
package scenarios
import (
"fmt"
"net"
"testing"
"time"
"v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp"
@ -41,25 +40,9 @@ func TestShadowsocksTCP(t *testing.T) {
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(len(payload))
response := buf.New()
finished := false
expectedResponse := "Processed: " + payload
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()
response := readFrom(conn, time.Second, len(expectedResponse))
assert.String(string(response)).Equals(expectedResponse)
conn.Close()
}