1/*
2 * Copyright (c) 2013 IBM Corp.
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 *    Seth Hoenig
11 *    Allan Stockdill-Mander
12 *    Mike Robertson
13 */
14
15package mqtt
16
17import (
18	"bytes"
19	"testing"
20
21	"github.com/eclipse/paho.mqtt.golang/packets"
22)
23
24func Test_NewPingReqMessage(t *testing.T) {
25	pr := packets.NewControlPacket(packets.Pingreq).(*packets.PingreqPacket)
26	if pr.MessageType != packets.Pingreq {
27		t.Errorf("NewPingReqMessage bad msg type: %v", pr.MessageType)
28	}
29	if pr.RemainingLength != 0 {
30		t.Errorf("NewPingReqMessage bad remlen, expected 0, got %d", pr.RemainingLength)
31	}
32
33	exp := []byte{
34		0xC0,
35		0x00,
36	}
37
38	var buf bytes.Buffer
39	pr.Write(&buf)
40	bs := buf.Bytes()
41
42	if len(bs) != 2 {
43		t.Errorf("NewPingReqMessage.Bytes() wrong length: %d", len(bs))
44	}
45
46	if exp[0] != bs[0] || exp[1] != bs[1] {
47		t.Errorf("NewPingMessage.Bytes() wrong")
48	}
49}
50
51func Test_DecodeMessage_pingresp(t *testing.T) {
52	bs := bytes.NewBuffer([]byte{
53		0xD0,
54		0x00,
55	})
56	presp, _ := packets.ReadPacket(bs)
57	if presp.(*packets.PingrespPacket).MessageType != packets.Pingresp {
58		t.Errorf("DecodeMessage ping response wrong msg type: %v", presp.(*packets.PingrespPacket).MessageType)
59	}
60	if presp.(*packets.PingrespPacket).RemainingLength != 0 {
61		t.Errorf("DecodeMessage ping response wrong rem len: %d", presp.(*packets.PingrespPacket).RemainingLength)
62	}
63}
64