Пример #1
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.")
}
Пример #2
0
func TestSubscribeMessageFields(t *testing.T) {
	msg := NewSubscribeMessage()

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

	msg.AddTopic([]byte("/a/b/#/c"), 1)
	assert.Equal(t, true, 1, len(msg.Topics()), "Error adding topic.")

	assert.False(t, true, msg.TopicExists([]byte("a/b")), "Topic should not exist.")

	msg.RemoveTopic([]byte("/a/b/#/c"))
	assert.False(t, true, msg.TopicExists([]byte("/a/b/#/c")), "Topic should not exist.")
}
Пример #3
0
func TestConnackMessageFields(t *testing.T) {
	msg := NewConnackMessage()

	msg.SetSessionPresent(true)
	assert.True(t, true, msg.SessionPresent(), "Error setting session present flag.")

	msg.SetSessionPresent(false)
	assert.False(t, true, msg.SessionPresent(), "Error setting session present flag.")

	msg.SetReturnCode(ConnectionAccepted)
	assert.Equal(t, true, ConnectionAccepted, msg.ReturnCode(), "Error setting return code.")
}
Пример #4
0
func TestConnackMessageDecode(t *testing.T) {
	msgBytes := []byte{
		byte(CONNACK << 4),
		2,
		0, // session not present
		0, // connection accepted
	}

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

	n, err := msg.Decode(src)
	assert.NoError(t, true, err, "Error decoding message.")

	assert.Equal(t, true, len(msgBytes), n, "Error decoding message.")

	assert.False(t, true, msg.SessionPresent(), "Error decoding session present flag.")

	assert.Equal(t, true, ConnectionAccepted, msg.ReturnCode(), "Error decoding return code.")
}
Пример #5
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.")
}