Exemple #1
0
func TestConnectMessageDecode4(t *testing.T) {
	// missing client Id, clean session == 0
	msgBytes := []byte{
		byte(CONNECT << 4),
		60,
		0, // Length MSB (0)
		4, // Length LSB (4)
		'M', 'Q', 'T', 'T',
		4,   // Protocol level 4
		204, // connect flags 11001110, will QoS = 01
		0,   // Keep Alive MSB (0)
		10,  // Keep Alive LSB (10)
		0,   // Client ID MSB (0)
		0,   // Client ID LSB (0)
		0,   // Will Topic MSB (0)
		4,   // Will Topic LSB (4)
		'w', 'i', 'l', 'l',
		0,  // Will Message MSB (0)
		12, // Will Message LSB (12)
		's', 'e', 'n', 'd', ' ', 'm', 'e', ' ', 'h', 'o', 'm', 'e',
		0, // Username ID MSB (0)
		7, // Username ID LSB (7)
		's', 'u', 'r', 'g', 'e', 'm', 'q',
		0,  // Password ID MSB (0)
		10, // Password ID LSB (10)
		'v', 'e', 'r', 'y', 's', 'e', 'c', 'r', 'e', 't',
	}

	src := bytes.NewBuffer(msgBytes)
	msg := NewConnectMessage()

	_, err := msg.Decode(src)
	assert.Error(t, true, err)
}
Exemple #2
0
func TestPublishMessageFixedHeaderFields(t *testing.T) {
	msg := NewPublishMessage()
	msg.flags = 11

	assert.True(t, true, msg.Dup(), "Incorrect DUP flag.")

	assert.True(t, true, msg.Retain(), "Incorrect RETAIN flag.")

	assert.Equal(t, true, 1, msg.QoS(), "Incorrect QoS.")

	msg.SetDup(false)
	assert.False(t, true, msg.Dup(), "Incorrect DUP flag.")

	msg.SetRetain(false)
	assert.False(t, true, msg.Retain(), "Incorrect RETAIN flag.")

	err := msg.SetQoS(2)
	assert.NoError(t, true, err, "Error setting QoS.")

	assert.Equal(t, true, 2, msg.QoS(), "Incorrect QoS.")

	err = msg.SetQoS(3)
	assert.Error(t, true, err)

	err = msg.SetQoS(0)
	assert.NoError(t, true, err, "Error setting QoS.")

	assert.Equal(t, true, 0, msg.QoS(), "Incorrect QoS.")

	msg.SetDup(true)
	assert.True(t, true, msg.Dup(), "Incorrect DUP flag.")

	msg.SetRetain(true)
	assert.True(t, true, msg.Retain(), "Incorrect RETAIN flag.")
}
Exemple #3
0
// test empty topic name
func TestPublishMessageEncode2(t *testing.T) {
	msg := NewPublishMessage()
	msg.SetTopic([]byte(""))
	msg.SetPacketId(7)
	msg.SetPayload([]byte{'s', 'e', 'n', 'd', ' ', 'm', 'e', ' ', 'h', 'o', 'm', 'e'})

	_, _, err := msg.Encode()
	assert.Error(t, true, err)
}
Exemple #4
0
func TestSubackMessageFields(t *testing.T) {
	msg := NewSubackMessage()

	msg.SetPacketId(100)
	assert.Equal(t, true, 100, msg.PacketId(), "Error setting packet ID.")

	msg.AddReturnCode(1)
	assert.Equal(t, true, 1, len(msg.ReturnCodes()), "Error adding return code.")

	err := msg.AddReturnCode(0x90)
	assert.Error(t, true, err)
}
Exemple #5
0
// test insufficient bytes
func TestPubrelMessageDecode2(t *testing.T) {
	msgBytes := []byte{
		byte(PUBREL<<4) | 2,
		2,
		7, // packet ID LSB (7)
	}

	src := bytes.NewBuffer(msgBytes)
	msg := NewPubrelMessage()

	_, err := msg.Decode(src)
	assert.Error(t, true, err)
}
Exemple #6
0
// testing wrong message size
func TestConnackMessageDecode3(t *testing.T) {
	msgBytes := []byte{
		byte(CONNACK << 4),
		2,
		0, // session not present
	}

	src := bytes.NewBuffer(msgBytes)
	msg := NewConnackMessage()

	_, err := msg.Decode(src)
	assert.Error(t, true, err, "Error decoding message.")
}
Exemple #7
0
// test insufficient bytes
func TestUnsubackMessageDecode2(t *testing.T) {
	msgBytes := []byte{
		byte(UNSUBACK << 4),
		2,
		7, // packet ID LSB (7)
	}

	src := bytes.NewBuffer(msgBytes)
	msg := NewUnsubackMessage()

	_, err := msg.Decode(src)
	assert.Error(t, true, err)
}
Exemple #8
0
// testing wrong reserve bits
func TestConnackMessageDecode4(t *testing.T) {
	msgBytes := []byte{
		byte(CONNACK << 4),
		2,
		64, // <- wrong size
		0,  // connection accepted
	}

	src := bytes.NewBuffer(msgBytes)
	msg := NewConnackMessage()

	_, err := msg.Decode(src)
	assert.Error(t, true, err, "Error decoding message.")
}
// test empty topic list
func TestSubscribeMessageDecode2(t *testing.T) {
	msgBytes := []byte{
		byte(SUBSCRIBE<<4) | 2,
		2,
		0, // packet ID MSB (0)
		7, // packet ID LSB (7)
	}

	src := bytes.NewBuffer(msgBytes)
	msg := NewSubscribeMessage()

	_, err := msg.Decode(src)
	assert.Error(t, true, err)
}
Exemple #10
0
func TestPublishMessageFields(t *testing.T) {
	msg := NewPublishMessage()

	msg.SetTopic([]byte("coolstuff"))
	assert.Equal(t, true, "coolstuff", string(msg.Topic()), "Error setting message topic.")

	err := msg.SetTopic([]byte("coolstuff/#"))
	assert.Error(t, true, err)

	msg.SetPacketId(100)
	assert.Equal(t, true, 100, msg.PacketId(), "Error setting acket ID.")

	msg.SetPayload([]byte("this is a payload to be sent"))
	assert.Equal(t, true, []byte("this is a payload to be sent"), msg.Payload(), "Error setting payload.")
}
Exemple #11
0
func TestMessageHeaderDecode4(t *testing.T) {
	headerBytes := []byte{0x62, 0xff, 0xff, 0xff, 0x7f}
	buf := bytes.NewBuffer(headerBytes)
	header := &fixedHeader{
		mtype: 6,
		flags: 2,
	}

	n, err := header.Decode(buf)
	assert.Equal(t, true, 5, n, "Incorrect bytes decoded")

	assert.Equal(t, true, maxRemainingLength, header.RemainingLength(), "Incorrect remaining length")

	assert.Error(t, true, err)
}
Exemple #12
0
// test with wrong return code
func TestSubackMessageDecode2(t *testing.T) {
	msgBytes := []byte{
		byte(SUBACK << 4),
		6,
		0,    // packet ID MSB (0)
		7,    // packet ID LSB (7)
		0,    // return code 1
		1,    // return code 2
		2,    // return code 3
		0x81, // return code 4
	}

	src := bytes.NewBuffer(msgBytes)
	msg := NewSubackMessage()

	_, err := msg.Decode(src)
	assert.Error(t, true, err)
}
Exemple #13
0
// test insufficient bytes
func TestPublishMessageDecode2(t *testing.T) {
	msgBytes := []byte{
		byte(PUBLISH<<4) | 2,
		26,
		0, // topic name MSB (0)
		7, // topic name LSB (7)
		's', 'u', 'r', 'g', 'e', 'm', 'q',
		0, // packet ID MSB (0)
		7, // packet ID LSB (7)
		's', 'e', 'n', 'd', ' ', 'm', 'e', ' ', 'h', 'o', 'm', 'e',
	}

	src := bytes.NewBuffer(msgBytes)
	msg := NewPublishMessage()

	_, err := msg.Decode(src)
	assert.Error(t, true, err)
}
Exemple #14
0
func TestConnectMessageFields(t *testing.T) {
	msg := NewConnectMessage()

	err := msg.SetVersion(0x3)
	assert.NoError(t, false, err, "Error setting message version.")

	assert.Equal(t, false, 0x3, msg.Version(), "Incorrect version number")

	err = msg.SetVersion(0x5)
	assert.Error(t, false, err)

	msg.SetCleanSession(true)
	assert.True(t, false, msg.CleanSession(), "Error setting clean session flag.")

	msg.SetCleanSession(false)
	assert.False(t, false, msg.CleanSession(), "Error setting clean session flag.")

	msg.SetWillFlag(true)
	assert.True(t, false, msg.WillFlag(), "Error setting will flag.")

	msg.SetWillFlag(false)
	assert.False(t, false, msg.WillFlag(), "Error setting will flag.")

	msg.SetWillRetain(true)
	assert.True(t, false, msg.WillRetain(), "Error setting will retain.")

	msg.SetWillRetain(false)
	assert.False(t, false, msg.WillRetain(), "Error setting will retain.")

	msg.SetPasswordFlag(true)
	assert.True(t, false, msg.PasswordFlag(), "Error setting password flag.")

	msg.SetPasswordFlag(false)
	assert.False(t, false, msg.PasswordFlag(), "Error setting password flag.")

	msg.SetUsernameFlag(true)
	assert.True(t, false, msg.UsernameFlag(), "Error setting username flag.")

	msg.SetUsernameFlag(false)
	assert.False(t, false, msg.UsernameFlag(), "Error setting username flag.")

	msg.SetWillQos(1)
	assert.Equal(t, false, 1, msg.WillQos(), "Error setting will QoS.")

	err = msg.SetWillQos(4)
	assert.Error(t, false, err)

	err = msg.SetClientId([]byte("j0j0jfajf02j0asdjf"))
	assert.NoError(t, false, err, "Error setting client ID")

	assert.Equal(t, false, "j0j0jfajf02j0asdjf", string(msg.ClientId()), "Error setting client ID.")

	err = msg.SetClientId([]byte("this is no good"))
	assert.Error(t, false, err)

	msg.SetWillTopic([]byte("willtopic"))
	assert.Equal(t, false, "willtopic", string(msg.WillTopic()), "Error setting will topic.")

	assert.True(t, false, msg.WillFlag(), "Error setting will flag.")

	msg.SetWillTopic([]byte(""))
	assert.Equal(t, false, "", string(msg.WillTopic()), "Error setting will topic.")

	assert.False(t, false, msg.WillFlag(), "Error setting will flag.")

	msg.SetWillMessage([]byte("this is a will message"))
	assert.Equal(t, false, "this is a will message", string(msg.WillMessage()), "Error setting will message.")

	assert.True(t, false, msg.WillFlag(), "Error setting will flag.")

	msg.SetWillMessage([]byte(""))
	assert.Equal(t, false, "", string(msg.WillMessage()), "Error setting will topic.")

	assert.False(t, false, msg.WillFlag(), "Error setting will flag.")

	msg.SetWillTopic([]byte("willtopic"))
	msg.SetWillMessage([]byte("this is a will message"))
	msg.SetWillTopic([]byte(""))
	assert.True(t, false, msg.WillFlag(), "Error setting will topic.")

	msg.SetUsername([]byte("myname"))
	assert.Equal(t, false, "myname", string(msg.Username()), "Error setting will message.")

	assert.True(t, false, msg.UsernameFlag(), "Error setting will flag.")

	msg.SetUsername([]byte(""))
	assert.Equal(t, false, "", string(msg.Username()), "Error setting will message.")

	assert.False(t, false, msg.UsernameFlag(), "Error setting will flag.")

	msg.SetPassword([]byte("myname"))
	assert.Equal(t, false, "myname", string(msg.Password()), "Error setting will message.")

	assert.True(t, false, msg.PasswordFlag(), "Error setting will flag.")

	msg.SetPassword([]byte(""))
	assert.Equal(t, false, "", string(msg.Password()), "Error setting will message.")

	assert.False(t, false, msg.PasswordFlag(), "Error setting will flag.")
}