mirror of https://github.com/cbeuw/Cloak
Improve tests
This commit is contained in:
parent
e202d8d03b
commit
360f84c5d5
|
|
@ -4,7 +4,7 @@ go:
|
||||||
- "1.14"
|
- "1.14"
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- go test -race -coverprofile=coverage.txt -covermode=atomic ./...
|
- go test -race -coverprofile=coverage.txt -coverpkg=./... -covermode=atomic ./...
|
||||||
|
|
||||||
after_success:
|
after_success:
|
||||||
- bash <(curl -s https://codecov.io/bash)
|
- bash <(curl -s https://codecov.io/bash)
|
||||||
|
|
|
||||||
|
|
@ -218,9 +218,10 @@ func TestStream_Close(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStream_Read(t *testing.T) {
|
func TestStream_Read(t *testing.T) {
|
||||||
var sessionKey [32]byte
|
seshes := map[string]bool{
|
||||||
rand.Read(sessionKey[:])
|
"ordered": false,
|
||||||
sesh := setupSesh(false, sessionKey, E_METHOD_PLAIN)
|
"unordered": true,
|
||||||
|
}
|
||||||
testPayload := []byte{42, 42, 42}
|
testPayload := []byte{42, 42, 42}
|
||||||
const smallPayloadLen = 3
|
const smallPayloadLen = 3
|
||||||
|
|
||||||
|
|
@ -231,211 +232,107 @@ func TestStream_Read(t *testing.T) {
|
||||||
testPayload,
|
testPayload,
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, writingEnd := connutil.AsyncPipe()
|
|
||||||
sesh.AddConnection(conn)
|
|
||||||
|
|
||||||
var streamID uint32
|
var streamID uint32
|
||||||
buf := make([]byte, 10)
|
buf := make([]byte, 10)
|
||||||
|
|
||||||
obfsBuf := make([]byte, 512)
|
obfsBuf := make([]byte, 512)
|
||||||
t.Run("Plain read", func(t *testing.T) {
|
|
||||||
f.StreamID = streamID
|
|
||||||
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
|
||||||
streamID++
|
|
||||||
writingEnd.Write(obfsBuf[:i])
|
|
||||||
time.Sleep(100 * time.Microsecond)
|
|
||||||
stream, err := sesh.Accept()
|
|
||||||
if err != nil {
|
|
||||||
t.Error("failed to accept stream", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
i, err = stream.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("failed to read", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if i != smallPayloadLen {
|
|
||||||
t.Errorf("expected read %v, got %v", smallPayloadLen, i)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf[:i], testPayload) {
|
|
||||||
t.Error("expected", testPayload,
|
|
||||||
"got", buf[:i])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
t.Run("Nil buf", func(t *testing.T) {
|
|
||||||
f.StreamID = streamID
|
|
||||||
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
|
||||||
streamID++
|
|
||||||
writingEnd.Write(obfsBuf[:i])
|
|
||||||
time.Sleep(100 * time.Microsecond)
|
|
||||||
stream, _ := sesh.Accept()
|
|
||||||
i, err := stream.Read(nil)
|
|
||||||
if i != 0 || err != nil {
|
|
||||||
t.Error("expecting", 0, nil,
|
|
||||||
"got", i, err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
t.Run("Read after stream close", func(t *testing.T) {
|
|
||||||
f.StreamID = streamID
|
|
||||||
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
|
||||||
streamID++
|
|
||||||
writingEnd.Write(obfsBuf[:i])
|
|
||||||
time.Sleep(100 * time.Microsecond)
|
|
||||||
stream, _ := sesh.Accept()
|
|
||||||
stream.Close()
|
|
||||||
i, err := stream.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("failed to read", err)
|
|
||||||
}
|
|
||||||
if i != smallPayloadLen {
|
|
||||||
t.Errorf("expected read %v, got %v", smallPayloadLen, i)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf[:i], testPayload) {
|
|
||||||
t.Error("expected", testPayload,
|
|
||||||
"got", buf[:i])
|
|
||||||
}
|
|
||||||
_, err = stream.Read(buf)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expecting error", ErrBrokenStream,
|
|
||||||
"got nil error")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
t.Run("Read after session close", func(t *testing.T) {
|
|
||||||
f.StreamID = streamID
|
|
||||||
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
|
||||||
streamID++
|
|
||||||
writingEnd.Write(obfsBuf[:i])
|
|
||||||
time.Sleep(100 * time.Microsecond)
|
|
||||||
stream, _ := sesh.Accept()
|
|
||||||
sesh.Close()
|
|
||||||
i, err := stream.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("failed to read", err)
|
|
||||||
}
|
|
||||||
if i != smallPayloadLen {
|
|
||||||
t.Errorf("expected read %v, got %v", smallPayloadLen, i)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf[:i], testPayload) {
|
|
||||||
t.Error("expected", testPayload,
|
|
||||||
"got", buf[:i])
|
|
||||||
}
|
|
||||||
_, err = stream.Read(buf)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expecting error", ErrBrokenStream,
|
|
||||||
"got nil error")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
for name, unordered := range seshes {
|
||||||
|
sesh := setupSesh(unordered, emptyKey, E_METHOD_PLAIN)
|
||||||
func TestStream_UnorderedRead(t *testing.T) {
|
conn, writingEnd := connutil.AsyncPipe()
|
||||||
var sessionKey [32]byte
|
sesh.AddConnection(conn)
|
||||||
rand.Read(sessionKey[:])
|
t.Run(name, func(t *testing.T) {
|
||||||
sesh := setupSesh(false, sessionKey, E_METHOD_PLAIN)
|
t.Run("Plain read", func(t *testing.T) {
|
||||||
testPayload := []byte{42, 42, 42}
|
f.StreamID = streamID
|
||||||
const smallPayloadLen = 3
|
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
||||||
|
streamID++
|
||||||
f := &Frame{
|
writingEnd.Write(obfsBuf[:i])
|
||||||
1,
|
time.Sleep(100 * time.Microsecond)
|
||||||
0,
|
stream, err := sesh.Accept()
|
||||||
0,
|
if err != nil {
|
||||||
testPayload,
|
t.Error("failed to accept stream", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
i, err = stream.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("failed to read", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if i != smallPayloadLen {
|
||||||
|
t.Errorf("expected read %v, got %v", smallPayloadLen, i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !bytes.Equal(buf[:i], testPayload) {
|
||||||
|
t.Error("expected", testPayload,
|
||||||
|
"got", buf[:i])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("Nil buf", func(t *testing.T) {
|
||||||
|
f.StreamID = streamID
|
||||||
|
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
||||||
|
streamID++
|
||||||
|
writingEnd.Write(obfsBuf[:i])
|
||||||
|
time.Sleep(100 * time.Microsecond)
|
||||||
|
stream, _ := sesh.Accept()
|
||||||
|
i, err := stream.Read(nil)
|
||||||
|
if i != 0 || err != nil {
|
||||||
|
t.Error("expecting", 0, nil,
|
||||||
|
"got", i, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("Read after stream close", func(t *testing.T) {
|
||||||
|
f.StreamID = streamID
|
||||||
|
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
||||||
|
streamID++
|
||||||
|
writingEnd.Write(obfsBuf[:i])
|
||||||
|
time.Sleep(100 * time.Microsecond)
|
||||||
|
stream, _ := sesh.Accept()
|
||||||
|
stream.Close()
|
||||||
|
i, err := stream.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("failed to read", err)
|
||||||
|
}
|
||||||
|
if i != smallPayloadLen {
|
||||||
|
t.Errorf("expected read %v, got %v", smallPayloadLen, i)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(buf[:i], testPayload) {
|
||||||
|
t.Error("expected", testPayload,
|
||||||
|
"got", buf[:i])
|
||||||
|
}
|
||||||
|
_, err = stream.Read(buf)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expecting error", ErrBrokenStream,
|
||||||
|
"got nil error")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("Read after session close", func(t *testing.T) {
|
||||||
|
f.StreamID = streamID
|
||||||
|
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
||||||
|
streamID++
|
||||||
|
writingEnd.Write(obfsBuf[:i])
|
||||||
|
time.Sleep(100 * time.Microsecond)
|
||||||
|
stream, _ := sesh.Accept()
|
||||||
|
sesh.Close()
|
||||||
|
i, err := stream.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("failed to read", err)
|
||||||
|
}
|
||||||
|
if i != smallPayloadLen {
|
||||||
|
t.Errorf("expected read %v, got %v", smallPayloadLen, i)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(buf[:i], testPayload) {
|
||||||
|
t.Error("expected", testPayload,
|
||||||
|
"got", buf[:i])
|
||||||
|
}
|
||||||
|
_, err = stream.Read(buf)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expecting error", ErrBrokenStream,
|
||||||
|
"got nil error")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, writingEnd := connutil.AsyncPipe()
|
|
||||||
sesh.AddConnection(conn)
|
|
||||||
|
|
||||||
var streamID uint32
|
|
||||||
buf := make([]byte, 10)
|
|
||||||
|
|
||||||
obfsBuf := make([]byte, 512)
|
|
||||||
t.Run("Plain read", func(t *testing.T) {
|
|
||||||
f.StreamID = streamID
|
|
||||||
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
|
||||||
streamID++
|
|
||||||
writingEnd.Write(obfsBuf[:i])
|
|
||||||
time.Sleep(100 * time.Microsecond)
|
|
||||||
stream, err := sesh.Accept()
|
|
||||||
if err != nil {
|
|
||||||
t.Error("failed to accept stream", err)
|
|
||||||
}
|
|
||||||
i, err = stream.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("failed to read", err)
|
|
||||||
}
|
|
||||||
if i != smallPayloadLen {
|
|
||||||
t.Errorf("expected read %v, got %v", smallPayloadLen, i)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf[:i], testPayload) {
|
|
||||||
t.Error("expected", testPayload,
|
|
||||||
"got", buf[:i])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
t.Run("Nil buf", func(t *testing.T) {
|
|
||||||
f.StreamID = streamID
|
|
||||||
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
|
||||||
streamID++
|
|
||||||
writingEnd.Write(obfsBuf[:i])
|
|
||||||
time.Sleep(100 * time.Microsecond)
|
|
||||||
stream, _ := sesh.Accept()
|
|
||||||
i, err := stream.Read(nil)
|
|
||||||
if i != 0 || err != nil {
|
|
||||||
t.Error("expecting", 0, nil,
|
|
||||||
"got", i, err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
t.Run("Read after stream close", func(t *testing.T) {
|
|
||||||
f.StreamID = streamID
|
|
||||||
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
|
||||||
streamID++
|
|
||||||
writingEnd.Write(obfsBuf[:i])
|
|
||||||
time.Sleep(100 * time.Microsecond)
|
|
||||||
stream, _ := sesh.Accept()
|
|
||||||
stream.Close()
|
|
||||||
i, err := stream.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("failed to read", err)
|
|
||||||
}
|
|
||||||
if i != smallPayloadLen {
|
|
||||||
t.Errorf("expected read %v, got %v", smallPayloadLen, i)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf[:i], testPayload) {
|
|
||||||
t.Error("expected", testPayload,
|
|
||||||
"got", buf[:i])
|
|
||||||
}
|
|
||||||
_, err = stream.Read(buf)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expecting error", ErrBrokenStream,
|
|
||||||
"got nil error")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
t.Run("Read after session close", func(t *testing.T) {
|
|
||||||
f.StreamID = streamID
|
|
||||||
i, _ := sesh.Obfs(f, obfsBuf, 0)
|
|
||||||
streamID++
|
|
||||||
writingEnd.Write(obfsBuf[:i])
|
|
||||||
time.Sleep(100 * time.Microsecond)
|
|
||||||
stream, _ := sesh.Accept()
|
|
||||||
sesh.Close()
|
|
||||||
i, err := stream.Read(buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("failed to read", err)
|
|
||||||
}
|
|
||||||
if i != smallPayloadLen {
|
|
||||||
t.Errorf("expected read %v, got %v", smallPayloadLen, i)
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf[:i], testPayload) {
|
|
||||||
t.Error("expected", testPayload,
|
|
||||||
"got", buf[:i])
|
|
||||||
}
|
|
||||||
_, err = stream.Read(buf)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expecting error", ErrBrokenStream,
|
|
||||||
"got nil error")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStream_SetWriteToTimeout(t *testing.T) {
|
func TestStream_SetWriteToTimeout(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -316,7 +316,7 @@ func TestClosingStreamsFromProxy(t *testing.T) {
|
||||||
serverConn, _ := pxyServerL.Accept()
|
serverConn, _ := pxyServerL.Accept()
|
||||||
serverConn.Close()
|
serverConn.Close()
|
||||||
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(300 * time.Millisecond)
|
||||||
if _, err := clientConn.Read(make([]byte, 16)); err == nil {
|
if _, err := clientConn.Read(make([]byte, 16)); err == nil {
|
||||||
t.Errorf("closing stream on server side is not reflected to the client: %v", err)
|
t.Errorf("closing stream on server side is not reflected to the client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -329,7 +329,7 @@ func TestClosingStreamsFromProxy(t *testing.T) {
|
||||||
serverConn, _ := pxyServerL.Accept()
|
serverConn, _ := pxyServerL.Accept()
|
||||||
clientConn.Close()
|
clientConn.Close()
|
||||||
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(300 * time.Millisecond)
|
||||||
if _, err := serverConn.Read(make([]byte, 16)); err == nil {
|
if _, err := serverConn.Read(make([]byte, 16)); err == nil {
|
||||||
t.Errorf("closing stream on client side is not reflected to the server: %v", err)
|
t.Errorf("closing stream on client side is not reflected to the server: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue