Beispiel #1
0
func abstractConnBufferedSendTest(t *testing.T, protocol string) {
	conn2, done := connectionPair(protocol, func(conn1 Conn) {
		pkt, err := conn1.Receive()
		assert.Equal(t, pkt.Type(), packet.CONNECT)
		assert.NoError(t, err)

		err = conn1.BufferedSend(packet.NewConnackPacket())
		assert.NoError(t, err)

		pkt, err = conn1.Receive()
		assert.Nil(t, pkt)
		assert.Equal(t, ConnectionClose, toError(err).Code())
	})

	err := conn2.BufferedSend(packet.NewConnectPacket())
	assert.NoError(t, err)

	pkt, err := conn2.Receive()
	assert.Equal(t, pkt.Type(), packet.CONNACK)
	assert.NoError(t, err)

	err = conn2.Close()
	assert.NoError(t, err)

	<-done
}
Beispiel #2
0
func abstractConnCountersTest(t *testing.T, protocol string) {
	conn2, done := connectionPair(protocol, func(conn1 Conn) {
		pkt, err := conn1.Receive()
		assert.NoError(t, err)
		assert.Equal(t, int64(pkt.Len()), conn1.BytesRead())

		pkt2 := packet.NewConnackPacket()
		conn1.Send(pkt2)
		assert.Equal(t, int64(pkt2.Len()), conn1.BytesWritten())

		err = conn1.Close()
		assert.NoError(t, err)
	})

	pkt := packet.NewConnectPacket()
	conn2.Send(pkt)
	assert.Equal(t, int64(pkt.Len()), conn2.BytesWritten())

	pkt2, err := conn2.Receive()
	assert.NoError(t, err)
	assert.Equal(t, int64(pkt2.Len()), conn2.BytesRead())

	err = conn2.Close()
	assert.NoError(t, err)

	<-done
}
Beispiel #3
0
func TestClientInvalidPackets(t *testing.T) {
	c := New()

	// state not connecting
	err := c.processConnack(packet.NewConnackPacket())
	assert.NoError(t, err)

	c.state.set(clientConnecting)

	// missing future
	err = c.processSuback(packet.NewSubackPacket())
	assert.NoError(t, err)

	// missing future
	err = c.processUnsuback(packet.NewUnsubackPacket())
	assert.NoError(t, err)

	// missing future
	err = c.processPubrel(0)
	assert.NoError(t, err)

	// missing future
	err = c.processPubackAndPubcomp(0)
	assert.NoError(t, err)
}
Beispiel #4
0
func abstractConnEncodeErrorTest(t *testing.T, protocol string) {
	conn2, done := connectionPair(protocol, func(conn1 Conn) {
		pkt := packet.NewConnackPacket()
		pkt.ReturnCode = 11 // <- invalid return code

		err := conn1.Send(pkt)
		assert.Equal(t, EncodeError, toError(err).Code())
	})

	pkt, err := conn2.Receive()
	assert.Nil(t, pkt)
	assert.Equal(t, ConnectionClose, toError(err).Code())

	<-done
}
Beispiel #5
0
func abstractServerTest(t *testing.T, protocol string) {
	port := tools.NewPort()

	server, err := testLauncher.Launch(port.URL(protocol))
	require.NoError(t, err)

	go func() {
		conn1, err := server.Accept()
		require.NoError(t, err)

		pkt, err := conn1.Receive()
		assert.Equal(t, pkt.Type(), packet.CONNECT)
		assert.NoError(t, err)

		err = conn1.Send(packet.NewConnackPacket())
		assert.NoError(t, err)

		pkt, err = conn1.Receive()
		assert.Nil(t, pkt)
		assert.Equal(t, ConnectionClose, toError(err).Code())
	}()

	conn2, err := testDialer.Dial(port.URL(protocol))
	require.NoError(t, err)

	err = conn2.Send(packet.NewConnectPacket())
	assert.NoError(t, err)

	pkt, err := conn2.Receive()
	assert.Equal(t, pkt.Type(), packet.CONNACK)
	assert.NoError(t, err)

	err = conn2.Close()
	assert.NoError(t, err)

	err = server.Close()
	assert.NoError(t, err)
}
Beispiel #6
0
func connackPacket() *packet.ConnackPacket {
	pkt := packet.NewConnackPacket()
	pkt.ReturnCode = packet.ConnectionAccepted
	pkt.SessionPresent = false
	return pkt
}
Beispiel #7
0
func Example() {
	// launch server
	server, err := Launch("tcp://localhost:1337")
	if err != nil {
		panic(err)
	}

	go func() {
		// accept next incoming connection
		conn, err := server.Accept()
		if err != nil {
			panic(err)
		}

		// receive next packet
		pkt, err := conn.Receive()
		if err != nil {
			panic(err)
		}

		// check packet type
		if _, ok := pkt.(*packet.ConnectPacket); ok {
			// send a connack packet
			err = conn.Send(packet.NewConnackPacket())
			if err != nil {
				panic(err)
			}
		} else {
			panic("unexpected packet")
		}
	}()

	// dial to server
	conn, err := Dial("tcp://localhost:1337")
	if err != nil {
		panic(err)
	}

	// send connect packet
	err = conn.Send(packet.NewConnectPacket())
	if err != nil {
		panic(err)
	}

	// receive next packet
	pkt, err := conn.Receive()
	if err != nil {
		panic(err)
	}

	// check packet type
	if connackPacket, ok := pkt.(*packet.ConnackPacket); ok {
		fmt.Println(connackPacket)

		// close connection
		err = conn.Close()
		if err != nil {
			panic(err)
		}
	} else {
		panic("unexpected packet")
	}

	// close server
	err = server.Close()
	if err != nil {
		panic(err)
	}

	// Output:
	// <ConnackPacket SessionPresent=false ReturnCode=0>
}