message_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package icmp_test
  5. import (
  6. "net"
  7. "reflect"
  8. "testing"
  9. "code.google.com/p/go.net/internal/iana"
  10. "code.google.com/p/go.net/internal/icmp"
  11. "code.google.com/p/go.net/ipv4"
  12. "code.google.com/p/go.net/ipv6"
  13. )
  14. var marshalAndParseMessageForIPv4Tests = []icmp.Message{
  15. {
  16. Type: ipv4.ICMPTypeEcho, Code: 0,
  17. Body: &icmp.Echo{
  18. ID: 1, Seq: 2,
  19. Data: []byte("HELLO-R-U-THERE"),
  20. },
  21. },
  22. {
  23. Type: ipv4.ICMPTypePhoturis,
  24. Body: &icmp.DefaultMessageBody{
  25. Data: []byte{0x80, 0x40, 0x20, 0x10},
  26. },
  27. },
  28. }
  29. func TestMarshalAndParseMessageForIPv4(t *testing.T) {
  30. for _, tt := range marshalAndParseMessageForIPv4Tests {
  31. b, err := tt.Marshal(nil)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. if m.Type != tt.Type || m.Code != tt.Code {
  40. t.Errorf("got %v; want %v", m, &tt)
  41. }
  42. if !reflect.DeepEqual(m.Body, tt.Body) {
  43. t.Errorf("got %v; want %v", m.Body, tt.Body)
  44. }
  45. }
  46. }
  47. var marshalAndParseMessageForIPv6Tests = []icmp.Message{
  48. {
  49. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  50. Body: &icmp.Echo{
  51. ID: 1, Seq: 2,
  52. Data: []byte("HELLO-R-U-THERE"),
  53. },
  54. },
  55. {
  56. Type: ipv6.ICMPTypeDuplicateAddressConfirmation,
  57. Body: &icmp.DefaultMessageBody{
  58. Data: []byte{0x80, 0x40, 0x20, 0x10},
  59. },
  60. },
  61. }
  62. func TestMarshalAndParseMessageForIPv6(t *testing.T) {
  63. pshicmp := icmp.IPv6PseudoHeader(net.ParseIP("ff02::1"), net.ParseIP("fe80::1"))
  64. for _, tt := range marshalAndParseMessageForIPv6Tests {
  65. for _, psh := range [][]byte{pshicmp, nil} {
  66. b, err := tt.Marshal(psh)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, b)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if m.Type != tt.Type || m.Code != tt.Code {
  75. t.Errorf("got %v; want %v", m, &tt)
  76. }
  77. if !reflect.DeepEqual(m.Body, tt.Body) {
  78. t.Errorf("got %v; want %v", m.Body, tt.Body)
  79. }
  80. }
  81. }
  82. }