Exemplo n.º 1
0
func TestDirect(t *testing.T) {
	connch, url, closech, err := setupServer()

	indata := []byte("abc123")
	outdata := []byte("123abcd")

	// Handle inbound connections: read then write
	go func() {
		for conn := range connch {
			rdata := make([]byte, len(indata))
			n, err := conn.Read(rdata)
			if err != nil {
				t.Error("failed to read")
				conn.Close()
				continue
			}
			if !vbytes.Equals(rdata[:n], indata) {
				t.Error("data mismatch:", rdata, indata)
			}
			n, err = conn.Write(outdata)
			if err != nil {
				t.Error("failed to send")
			}
			conn.Close()
		}
	}()

	// establish outbound connection
	conn, err := Dial(url)
	if err != nil {
		t.Fatal("Could not direct connect:", err)
	}
	defer conn.Close()

	// write some data
	n, err := conn.Write(indata)
	if err != nil || n != len(indata) {
		t.Error("couldn't write:", err, n)
	}

	// read some data
	rdata := make([]byte, len(outdata))
	n, err = conn.Read(rdata)
	if err != nil {
		t.Fatal("Read failed")
	}
	if !vbytes.Equals(rdata[:n], outdata) {
		t.Error("data mismatch:", rdata[:n], outdata)
	}
	closech <- true
}
Exemplo n.º 2
0
func getClientToken(conn net.Conn) (err error) {
	rdata := make([]byte, len(outbounddirectOK))
	_, err = conn.Read(rdata)
	if err != nil {
		return err
	}
	if !vbytes.Equals(rdata, outbounddirectOK) {
		return errors.New("client: server token exchange failed")
	}
	return nil
}
Exemplo n.º 3
0
func TestStack(t *testing.T) {
	s := stack{}
	s.push(byte(123))
	if len(s) != 1 {
		t.Error("push failed")
	}
	if b, _ := s.pop(); b != byte(123) {
		t.Error("pop failed")
	}
	if len(s) != 0 {
		t.Error("post-pop len failed")
	}

	b := vbytes.RandBytes(1000)
	s.Write(b)
	if len(s) != len(b) {
		t.Error("post-write len failed")
	}
	for _, cb := range b {
		if b, _ := s.pop(); b != cb {
			t.Error("write failed")
		}
	}
	if len(s) != 0 {
		t.Error("post-read len failed")
	}

	s.Write(b)
	rs := make([]byte, len(b))
	s.Read(rs)
	if !vbytes.Equals(rs, b) {
		t.Error("readback failed")
	}
	if len(s) != 0 {
		t.Error("post-readback len failed")
	}

	s.push(byte(42))
	s.pop()
	if len(s) == cap(s) {
		t.Error("memory error")
	}
	s.compact()
	if len(s) != cap(s) {
		t.Error("compact error", len(s), "!=", cap(s))
	}
}
Exemplo n.º 4
0
func TestTape(t *testing.T) {
	t1 := tape{}
	if t1.Len() != 0 || t1.Ahead() != 0 || t1.Behind() != 0 {
		t.Error("initial state error")
	}
	_, err := t1.ReadByte()
	if err != EmptyErr {
		t.Error("failed to get EmptyErr")
	}
	n := 1000
	b := vbytes.RandBytes(n)
	t1.Write(b)
	if t1.Len() != n || t1.Ahead() != n || t1.Behind() != 0 {
		t.Error("post-write state error")
	}
	rb := make([]byte, n)
	t1.Read(rb)
	if !vbytes.Equals(rb, b) {
		t.Error("readback failed")
	}
	if t1.Len() != n || t1.Ahead() != 0 || t1.Behind() != n {
		t.Error("post-readback state error")
	}
	if t1.Advance(-2) != -2 {
		t.Error("advance error")
	}
	if t1.Len() != n || t1.Ahead() != 2 || t1.Behind() != n-2 {
		t.Error("post-advance state error")
	}
	t1.Read(rb[:1])
	if rb[0] != b[len(b)-2] {
		t.Error("post-advance post-readback error")
	}
	abyte, err := t1.ReadByte()
	if err != nil {
		t.Error("ReadByte() gave error:", err)
	}
	if abyte != b[len(b)-1] {
		t.Error("post-ReadByte error")
	}
}
Exemplo n.º 5
0
func (sg *GUID) Equals(sg2 *GUID) bool {
	return vbytes.Equals(sg[:], sg2[:])
}