unit_ping_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. package mqtt
  15. import (
  16. "bytes"
  17. "testing"
  18. "github.com/eclipse/paho.mqtt.golang/packets"
  19. )
  20. func Test_NewPingReqMessage(t *testing.T) {
  21. pr := packets.NewControlPacket(packets.Pingreq).(*packets.PingreqPacket)
  22. if pr.MessageType != packets.Pingreq {
  23. t.Errorf("NewPingReqMessage bad msg type: %v", pr.MessageType)
  24. }
  25. if pr.RemainingLength != 0 {
  26. t.Errorf("NewPingReqMessage bad remlen, expected 0, got %d", pr.RemainingLength)
  27. }
  28. exp := []byte{
  29. 0xC0,
  30. 0x00,
  31. }
  32. var buf bytes.Buffer
  33. pr.Write(&buf)
  34. bs := buf.Bytes()
  35. if len(bs) != 2 {
  36. t.Errorf("NewPingReqMessage.Bytes() wrong length: %d", len(bs))
  37. }
  38. if exp[0] != bs[0] || exp[1] != bs[1] {
  39. t.Errorf("NewPingMessage.Bytes() wrong")
  40. }
  41. }
  42. func Test_DecodeMessage_pingresp(t *testing.T) {
  43. bs := bytes.NewBuffer([]byte{
  44. 0xD0,
  45. 0x00,
  46. })
  47. presp, _ := packets.ReadPacket(bs)
  48. if presp.(*packets.PingrespPacket).MessageType != packets.Pingresp {
  49. t.Errorf("DecodeMessage ping response wrong msg type: %v", presp.(*packets.PingrespPacket).MessageType)
  50. }
  51. if presp.(*packets.PingrespPacket).RemainingLength != 0 {
  52. t.Errorf("DecodeMessage ping response wrong rem len: %d", presp.(*packets.PingrespPacket).RemainingLength)
  53. }
  54. }