blob: 2c3ca401a00de5398ed586bf28218f1ec48b01bb [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package conn
import (
"bytes"
"crypto/rand"
"io"
"testing"
"v.io/v23"
"v.io/v23/context"
"v.io/v23/flow"
_ "v.io/x/ref/runtime/factories/fake"
"v.io/x/ref/test"
)
var randData []byte
func init() {
test.Init()
randData = make([]byte, 2*defaultBufferSize)
if _, err := rand.Read(randData); err != nil {
panic("Could not read random data.")
}
}
func testWrite(t *testing.T, ctx *context.T, want []byte, df flow.Flow, flows <-chan flow.Flow) {
finished := make(chan struct{})
go func(x []byte) {
mid := len(x) / 2
wrote, err := df.WriteMsgAndClose(x[:mid], x[mid:])
if err != nil {
t.Fatalf("Unexpected error for write: %v", err)
}
if wrote != len(x) {
t.Errorf("got %d want %d", wrote, len(x))
}
close(finished)
}(want)
af := <-flows
for len(want) > 0 {
got, err := af.ReadMsg()
if err != nil && err != io.EOF {
t.Fatalf("Unexpected error: %v", err)
}
if !bytes.Equal(got, want[:len(got)]) {
t.Fatalf("Got: %s want %s", got, want)
}
want = want[len(got):]
}
if len(want) != 0 {
t.Errorf("got %d leftover bytes, expected 0.", len(want))
}
<-finished
<-df.Closed()
<-af.Closed()
}
func TestDial(t *testing.T) {
ctx, shutdown := v23.Init()
defer shutdown()
for _, dialerDials := range []bool{true, false} {
df, flows := setupFlow(t, ctx, dialerDials)
testWrite(t, ctx, []byte("hello world"), df, flows)
}
}
func TestLargeWrite(t *testing.T) {
ctx, shutdown := v23.Init()
defer shutdown()
df, flows := setupFlow(t, ctx, true)
testWrite(t, ctx, randData, df, flows)
}