Beispiel #1
0
func TestClient_handlePUBLISH_QoS2(t *testing.T) {
	cli := New(&Options{
		ErrorHandler: func(_ error) {},
	})

	err := cli.Connect(&ConnectOptions{
		Network:  "tcp",
		Address:  testAddress,
		ClientID: []byte("clientID"),
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	defer cli.Disconnect()

	p, err := packet.NewPUBLISH(&packet.PUBLISHOptions{
		QoS:      mqtt.QoS2,
		PacketID: 1,
	})
	if err != nil {
		nilErrorExpected(t, err)
		return
	}

	if err = cli.handlePUBLISH(p); err != nil {
		nilErrorExpected(t, err)
	}
}
Beispiel #2
0
func TestClient_Connect_restoreSession(t *testing.T) {
	cli := New(&Options{
		ErrorHandler: func(_ error) {},
	})

	cli.sess = newSession(false, []byte("cliendID"))

	publish, err := packet.NewPUBLISH(&packet.PUBLISHOptions{
		PacketID: 1,
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	cli.sess.sendingPackets[1] = publish

	pubrel, err := packet.NewPUBREL(&packet.PUBRELOptions{
		PacketID: 2,
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	cli.sess.sendingPackets[2] = pubrel

	cli.sess.sendingPackets[3] = packet.NewPINGREQ()

	err = cli.Connect(&ConnectOptions{
		Network: "tcp",
		Address: testAddress,
	})
	if err != nil {
		nilErrorExpected(t, err)
	}
}
Beispiel #3
0
func TestClient_handlePUBREL(t *testing.T) {
	cli := New(&Options{
		ErrorHandler: func(_ error) {},
	})

	err := cli.Connect(&ConnectOptions{
		Network:  "tcp",
		Address:  testAddress,
		ClientID: []byte("clientID"),
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	defer cli.Disconnect()

	p := &packet.PUBREL{
		PacketID: 1,
	}

	publish, err := packet.NewPUBLISH(&packet.PUBLISHOptions{
		PacketID: 1,
	})
	if err != nil {
		nilErrorExpected(t, err)
		return
	}

	cli.sess.receivingPackets[1] = publish

	if err := cli.handlePUBREL(p); err != nil {
		nilErrorExpected(t, err)
	}
}
Beispiel #4
0
func TestClient_validatePacketID(t *testing.T) {
	cli := New(nil)

	p, err := packet.NewPUBLISH(&packet.PUBLISHOptions{
		PacketID: 1,
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	if err := cli.validatePacketID(map[uint16]packet.Packet{1: p}, 1, packet.TypePUBLISH); err != nil {
		nilErrorExpected(t, err)
	}
}
Beispiel #5
0
// newPUBLISHPacket creates and returns a PUBLISH Packet.
func (cli *Client) newPUBLISHPacket(opts *PublishOptions) (packet.Packet, error) {
	// Define a Packet Identifier.
	var packetID uint16

	if opts.QoS != mqtt.QoS0 {
		// Lock for reading and updating the Session.
		cli.muSess.Lock()

		defer cli.muSess.Unlock()

		// Define an error.
		var err error

		// Generate a Packet Identifer.
		if packetID, err = cli.generatePacketID(); err != nil {
			return nil, err
		}
	}

	// Create a PUBLISH Packet.
	p, err := packet.NewPUBLISH(&packet.PUBLISHOptions{
		QoS:       opts.QoS,
		Retain:    opts.Retain,
		TopicName: opts.TopicName,
		PacketID:  packetID,
		Message:   opts.Message,
	})
	if err != nil {
		return nil, err
	}

	if opts.QoS != mqtt.QoS0 {
		// Set the Packet to the Session.
		cli.sess.sendingPackets[packetID] = p
	}

	// Return the Packet.
	return p, nil
}
Beispiel #6
0
func TestClient_handlePUBREC_NewPUBRELErr(t *testing.T) {
	cli := New(&Options{
		ErrorHandler: func(_ error) {},
	})

	err := cli.Connect(&ConnectOptions{
		Network:  "tcp",
		Address:  testAddress,
		ClientID: []byte("clientID"),
	})
	if err != nil {
		nilErrorExpected(t, err)
	}

	defer cli.Disconnect()

	p := &packet.PUBREC{
		PacketID: 0,
	}

	publish, err := packet.NewPUBLISH(&packet.PUBLISHOptions{
		PacketID: 1,
	})
	if err != nil {
		nilErrorExpected(t, err)
		return
	}

	publish.(*packet.PUBLISH).PacketID = 0

	cli.sess.sendingPackets[0] = publish

	if err := cli.handlePUBREC(p); err != packet.ErrInvalidPacketID {
		invalidError(t, err, packet.ErrInvalidPacketID)
	}
}