encoding_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package discover
  2. import (
  3. "bytes"
  4. "reflect"
  5. "testing"
  6. )
  7. var testdata = []struct {
  8. data []byte
  9. packet *Packet
  10. err error
  11. }{
  12. {
  13. []byte{0x20, 0x12, 0x10, 0x25,
  14. 0x12, 0x34, 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x05,
  16. 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x00,
  17. 0x00, 0x00, 0x00, 0x00},
  18. &Packet{
  19. Magic: 0x20121025,
  20. Port: 0x1234,
  21. ID: "hello",
  22. },
  23. nil,
  24. },
  25. {
  26. []byte{0x20, 0x12, 0x10, 0x25,
  27. 0x34, 0x56, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x08,
  29. 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x21, 0x21,
  30. 0x00, 0x00, 0x00, 0x04,
  31. 0x01, 0x02, 0x03, 0x04},
  32. &Packet{
  33. Magic: 0x20121025,
  34. Port: 0x3456,
  35. ID: "hello!!!",
  36. IP: []byte{1, 2, 3, 4},
  37. },
  38. nil,
  39. },
  40. {
  41. []byte{0x19, 0x76, 0x03, 0x09,
  42. 0x00, 0x00, 0x00, 0x06,
  43. 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00},
  44. &Packet{
  45. Magic: 0x19760309,
  46. ID: "hello!",
  47. },
  48. nil,
  49. },
  50. {
  51. []byte{0x20, 0x12, 0x10, 0x25,
  52. 0x12, 0x34, 0x12, 0x34, // reserved bits not set to zero
  53. 0x00, 0x00, 0x00, 0x06,
  54. 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00},
  56. nil,
  57. errFormat,
  58. },
  59. {
  60. []byte{0x20, 0x12, 0x10, 0x25,
  61. 0x12, 0x34, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x06,
  63. 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, // missing padding
  64. 0x00, 0x00, 0x00, 0x00},
  65. nil,
  66. errFormat,
  67. },
  68. {
  69. []byte{0x19, 0x77, 0x03, 0x09, // incorrect Magic
  70. 0x00, 0x00, 0x00, 0x06,
  71. 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00},
  72. nil,
  73. errBadMagic,
  74. },
  75. {
  76. []byte{0x19, 0x76, 0x03, 0x09,
  77. 0x6c, 0x6c, 0x6c, 0x6c, // length exceeds packet size
  78. 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00},
  79. nil,
  80. errFormat,
  81. },
  82. {
  83. []byte{0x19, 0x76, 0x03, 0x09,
  84. 0x00, 0x00, 0x00, 0x06,
  85. 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x00, 0x00,
  86. 0x23}, // extra data at the end
  87. nil,
  88. errFormat,
  89. },
  90. }
  91. func TestDecodePacket(t *testing.T) {
  92. for i, test := range testdata {
  93. p, err := DecodePacket(test.data)
  94. if err != test.err {
  95. t.Errorf("%d: unexpected error %v", i, err)
  96. } else {
  97. if !reflect.DeepEqual(p, test.packet) {
  98. t.Errorf("%d: incorrect packet\n%v\n%v", i, test.packet, p)
  99. }
  100. }
  101. }
  102. }
  103. func TestEncodePacket(t *testing.T) {
  104. for i, test := range testdata {
  105. if test.err != nil {
  106. continue
  107. }
  108. buf := EncodePacket(*test.packet)
  109. if bytes.Compare(buf, test.data) != 0 {
  110. t.Errorf("%d: incorrect encoded packet\n% x\n% 0x", i, test.data, buf)
  111. }
  112. }
  113. }
  114. var ipstrTests = []struct {
  115. d []byte
  116. s string
  117. }{
  118. {[]byte{192, 168, 34}, ""},
  119. {[]byte{192, 168, 0, 34}, "192.168.0.34"},
  120. {[]byte{0x20, 0x01, 0x12, 0x34,
  121. 0x34, 0x56, 0x56, 0x78,
  122. 0x78, 0x00, 0x00, 0xdc,
  123. 0x00, 0x00, 0x43, 0x54}, "2001:1234:3456:5678:7800:00dc:0000:4354"},
  124. }
  125. func TestIPStr(t *testing.T) {
  126. for _, tc := range ipstrTests {
  127. s1 := ipStr(tc.d)
  128. if s1 != tc.s {
  129. t.Errorf("Incorrect ipstr %q != %q", tc.s, s1)
  130. }
  131. }
  132. }