Example #1
0
func TestChannelAbstraction(t *testing.T) {
	client := func(t *testing.T, sender libchan.Sender, s *Transport) {
		recv, send := libchan.Pipe()

		m1 := &AbstractionMessage{
			Message: "irrelevant content",
			Channel: recv,
		}

		sendErr := sender.Send(m1)
		if sendErr != nil {
			t.Fatalf("Error sending channel: %s", sendErr)
		}

		closeErr := send.Close()
		if closeErr != nil {
			t.Fatalf("Error closing sender: %s", closeErr)
		}
	}
	server := func(t *testing.T, receiver libchan.Receiver, s *Transport) {
		m1 := &AbstractionMessage{}
		recvErr := receiver.Receive(m1)
		if recvErr != nil {
			t.Fatalf("Error receiving message: %s", recvErr)
		}

		if expected := "irrelevant content"; m1.Message != expected {
			t.Fatalf("Unexpected message value:\n\tExpected: %s\n\tActual: %s", expected, m1.Message)
		}
	}
	SpawnClientServerTest(t, "localhost:12943", ClientSendWrapper(client), ServerReceiveWrapper(server))
}
Example #2
0
func TestChannelProxy(t *testing.T) {
	messages := []string{
		"Proxied messages",
		"Another proxied message",
		"Far less interesting message",
		"This was ALSO sent over the proxy",
	}
	client := func(t *testing.T, sender libchan.Sender) {
		for i, m := range messages {
			nestedReceiver, remoteSender := libchan.Pipe()

			message := &ProxiedMessage{
				Message: m,
				Ret:     remoteSender,
			}

			err := sender.Send(message)
			if err != nil {
				t.Fatalf("Error sending message: %s", err)
			}

			ack := &ProxyAckMessage{}
			err = nestedReceiver.Receive(ack)
			if err != nil {
				t.Fatalf("Error receiving ack: %s", err)
			}

			if ack.N != i {
				t.Fatalf("Unexpected ack value\n\tExpected: %d\n\tActual: %d", i, ack.N)
			}

			if ack.MessageLen != len(m) {
				t.Fatalf("Unexpected ack value\n\tExpected: %d\n\tActual: %d", len(m), ack.MessageLen)
			}
		}

	}
	server := func(t *testing.T, receiver libchan.Receiver) {
		for i, m := range messages {
			message := &ProxiedMessage{}
			err := receiver.Receive(message)
			if err != nil {
				t.Fatalf("Error receiving message: %s", err)
			}

			if message.Message != m {
				t.Fatalf("Unexpected message:\n\tExpected: %s\n\tActual: %s", m, message.Message)
			}

			ack := &ProxyAckMessage{N: i, MessageLen: len(message.Message)}
			err = message.Ret.Send(ack)
			if err != nil {
				t.Fatalf("Error sending ack: %s", err)
			}
		}
	}
	SpawnProxyTest(t, client, server, 4)
}
Example #3
0
func main() {
	if len(os.Args) < 2 {
		log.Fatal("usage: <command> [<arg> ]")
	}

	var client net.Conn
	var err error
	if os.Getenv("USE_TLS") != "" {
		client, err = tls.Dial("tcp", "127.0.0.1:9323", &tls.Config{InsecureSkipVerify: true})
	} else {
		client, err = net.Dial("tcp", "127.0.0.1:9323")
	}
	if err != nil {
		log.Fatal(err)
	}

	transport, err := spdy.NewClientTransport(client)
	if err != nil {
		log.Fatal(err)
	}
	sender, err := transport.NewSendChannel()
	if err != nil {
		log.Fatal(err)
	}

	receiver, remoteSender := libchan.Pipe()

	command := &RemoteCommand{
		Cmd:        os.Args[1],
		Args:       os.Args[2:],
		Stdin:      os.Stdin,
		Stdout:     os.Stdout,
		Stderr:     os.Stderr,
		StatusChan: remoteSender,
	}

	err = sender.Send(command)
	if err != nil {
		log.Fatal(err)
	}

	response := &CommandResponse{}
	err = receiver.Receive(response)
	if err != nil {
		log.Fatal(err)
	}

	os.Exit(response.Status)
}
Example #4
0
func TestSenderSubChannel(t *testing.T) {
	client := func(t *testing.T, sender libchan.Sender, s *Transport) {
		recv, remote1 := libchan.Pipe()
		m1 := &SenderMessage{
			Message: "WithSender",
			Sender:  remote1,
		}
		if sendErr := sender.Send(m1); sendErr != nil {
			t.Fatalf("Error sending SenderMessage: %s", sendErr)
		}

		m2 := &SenderMessage{}
		if receiveErr := recv.Receive(m2); receiveErr != nil {
			t.Fatalf("Error receiving ReceiverMessage: %s", receiveErr)
		}
		if expected := "Nested"; m2.Message != expected {
			t.Fatalf("Unexpected message value:\n\tExpected: %s\n\tActual: %s", expected, m2.Message)
		}

		if m2.Sender == nil {
			t.Fatalf("Receiver is nil")
		}

		m3 := &SimpleMessage{"This is a simple message"}
		if sendErr := m2.Sender.Send(m3); sendErr != nil {
			t.Fatalf("Error sending simple message: %s", sendErr)
		}

		if closeErr := m2.Sender.Close(); closeErr != nil {
			t.Fatalf("Error closing send2: %s", closeErr)
		}
	}
	server := func(t *testing.T, receiver libchan.Receiver, s *Transport) {
		m1 := &SenderMessage{}
		if receiveErr := receiver.Receive(m1); receiveErr != nil {
			t.Fatalf("Error receiving SenderMessage: %s", receiveErr)
		}

		if expected := "WithSender"; m1.Message != expected {
			t.Fatalf("Unexpected message\n\tExpected: %s\n\tActual: %s", expected, m1.Message)
		}

		if m1.Sender == nil {
			t.Fatalf("Receiver is nil")
		}

		recv, remote := libchan.Pipe()
		m2 := &SenderMessage{
			Message: "Nested",
			Sender:  remote,
		}
		if sendErr := m1.Sender.Send(m2); sendErr != nil {
			t.Fatalf("Error sending SenderMessage: %s", sendErr)
		}

		m3 := &SimpleMessage{}
		if receiverErr := recv.Receive(m3); receiverErr != nil {
			t.Fatalf("Error receiving SimpleMessage: %s", receiverErr)
		}
		if expected := "This is a simple message"; m3.Message != expected {
			t.Fatalf("Unexpected message value:\n\tExpected: %s\n\tActual: %s", expected, m3.Message)
		}

		if closeErr := m1.Sender.Close(); closeErr != nil {
			t.Fatalf("Error closing send1: %s", closeErr)
		}

	}
	SpawnClientServerTest(t, "localhost:12845", ClientSendWrapper(client), ServerReceiveWrapper(server))
}
Example #5
0
func TestChannelEncoding(t *testing.T) {
	client := func(t *testing.T, sender libchan.Sender, s *Transport) {
		recv, s1 := libchan.Pipe()
		r1, send := libchan.Pipe()

		m1 := &InOutMessage{
			Message: "WithInOut",
			Recv:    r1,
			Send:    s1,
		}
		sendErr := sender.Send(m1)
		if sendErr != nil {
			t.Fatalf("Error sending InOutMessage: %s", sendErr)
		}

		m2 := &SimpleMessage{"This is a simple message"}
		sendErr = send.Send(m2)
		if sendErr != nil {
			t.Fatalf("Error sending simple message: %s", sendErr)
		}

		m3 := &SimpleMessage{}
		recvErr := recv.Receive(m3)
		if recvErr != nil {
			t.Fatalf("Error receiving simple message: %s", recvErr)
		}
		if expected := "This is a responding simple message"; m3.Message != expected {
			t.Fatalf("Unexpected message value\n\tExpected: %s\n\tActual: %s", expected, m3.Message)
		}

		closeErr := send.Close()
		if closeErr != nil {
			t.Fatalf("Error closing s1: %s", closeErr)
		}
	}
	server := func(t *testing.T, receiver libchan.Receiver, s *Transport) {
		m1 := &InOutMessage{}
		receiveErr := receiver.Receive(m1)
		if receiveErr != nil {
			t.Fatalf("Error receiving InOutMessage: %s", receiveErr)
		}

		if expected := "WithInOut"; m1.Message != expected {
			t.Fatalf("Unexpected message\n\tExpected: %s\n\tActual: %s", expected, m1.Message)
		}

		if m1.Recv == nil {
			t.Fatalf("Recv is nil")
		}

		if m1.Send == nil {
			t.Fatalf("Send is nil")
		}

		m2 := &SimpleMessage{}
		receiveErr = m1.Recv.Receive(m2)
		if receiveErr != nil {
			t.Fatalf("Error receiving SimpleMessage: %s", receiveErr)
		}
		if expected := "This is a simple message"; m2.Message != expected {
			t.Fatalf("Unexpected message value:\n\tExpected: %s\n\tActual: %s", expected, m2.Message)
		}

		m3 := &SimpleMessage{"This is a responding simple message"}
		sendErr := m1.Send.Send(m3)
		if sendErr != nil {
			t.Fatalf("Error sending SimpleMessage: %s", sendErr)
		}

		closeErr := m1.Send.Close()
		if closeErr != nil {
			t.Fatalf("Error closing send connection: %s", closeErr)
		}
	}
	SpawnClientServerTest(t, "localhost:12843", ClientSendWrapper(client), ServerReceiveWrapper(server))
}
Example #6
0
func TestSendFirstPipe(t *testing.T) {
	message1 := "Pipe messages"
	message2 := "Must more simple message"
	message3 := "This was sent over a byte stream"
	message4 := "This was ALSO sent over a byte stream"
	client := func(t *testing.T, sender libchan.Sender) {
		bs, bsRemote := net.Pipe()

		nestedReceiver, remoteSender := libchan.Pipe()

		m1 := &PipeMessage{
			Message: message1,
			Stream:  bsRemote,
			Send:    remoteSender,
		}
		sendErr := sender.Send(m1)
		if sendErr != nil {
			t.Fatalf("Error sending message: %s", sendErr)
		}

		m2 := &SimpleMessage{}
		recvErr := nestedReceiver.Receive(m2)
		if recvErr != nil {
			t.Fatalf("Error receiving from nested receiver: %s", recvErr)
		}

		if m2.Message != message2 {
			t.Fatalf("Unexpected message\n\tExpected: %s\n\tActual: %s", message2, m2.Message)
		}

		_, writeErr := bs.Write([]byte(message3))
		if writeErr != nil {
			t.Fatalf("Error writing on byte stream: %s", writeErr)
		}

		readData := make([]byte, len([]byte(message4)))
		_, readErr := bs.Read(readData)
		if readErr != nil {
			t.Fatalf("Error reading on byte stream: %s", readErr)
		}
		if string(readData) != message4 {
			t.Fatalf("Unexpected message\n\tExpected: %s\n\tActual: %s", message4, string(readData))
		}
	}
	server := func(t *testing.T, receiver libchan.Receiver) {
		m1 := &PipeMessage{}
		recvErr := receiver.Receive(m1)
		if recvErr != nil {
			t.Fatalf("Error receiving message: %s", recvErr)
		}

		if m1.Message != message1 {
			t.Fatalf("Unexpected message\n\tExpected: %s\n\tActual: %s", message1, m1.Message)
		}

		sendErr := m1.Send.Send(&SimpleMessage{message2})
		if sendErr != nil {
			t.Fatalf("Error creating sender: %s", sendErr)
		}

		readData := make([]byte, len([]byte(message3)))
		_, readErr := m1.Stream.Read(readData)
		if readErr != nil {
			t.Fatalf("Error reading on byte stream: %s", readErr)
		}
		if string(readData) != message3 {
			t.Fatalf("Unexpected message\n\tExpected: %s\n\tActual: %s", message3, string(readData))
		}

		_, writeErr := m1.Stream.Write([]byte(message4))
		if writeErr != nil {
			t.Fatalf("Error writing on byte stream: %s", writeErr)
		}

		closeErr := m1.Send.Close()
		if closeErr != nil {
			t.Fatalf("Error closing received sender: %s", closeErr)
		}

	}
	SpawnPipeTest(t, client, server)
}