2
0

fuzz.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //+build gofuzz
  2. package netlink
  3. import "github.com/google/go-cmp/cmp"
  4. func fuzz(b1 []byte) int {
  5. // 1. unmarshal, marshal, unmarshal again to check m1 and m2 for equality
  6. // after a round trip. checkMessage is also used because there is a fair
  7. // amount of tricky logic around testing for presence of error headers and
  8. // extended acknowledgement attributes.
  9. var m1 Message
  10. if err := m1.UnmarshalBinary(b1); err != nil {
  11. return 0
  12. }
  13. if err := checkMessage(m1); err != nil {
  14. return 0
  15. }
  16. b2, err := m1.MarshalBinary()
  17. if err != nil {
  18. panicf("failed to marshal m1: %v", err)
  19. }
  20. var m2 Message
  21. if err := m2.UnmarshalBinary(b2); err != nil {
  22. panicf("failed to unmarshal m2: %v", err)
  23. }
  24. if err := checkMessage(m2); err != nil {
  25. panicf("failed to check m2: %v", err)
  26. }
  27. if diff := cmp.Diff(m1, m2); diff != "" {
  28. panicf("unexpected Message (-want +got):\n%s", diff)
  29. }
  30. // 2. marshal again and compare b2 and b3 (b1 may have reserved bytes set
  31. // which we ignore and fill with zeros when marshaling) for equality.
  32. b3, err := m2.MarshalBinary()
  33. if err != nil {
  34. panicf("failed to marshal m2: %v", err)
  35. }
  36. if diff := cmp.Diff(b2, b3); diff != "" {
  37. panicf("unexpected message bytes (-want +got):\n%s", diff)
  38. }
  39. // 3. unmarshal any possible attributes from m1's data and marshal them
  40. // again for comparison.
  41. a1, err := UnmarshalAttributes(m1.Data)
  42. if err != nil {
  43. return 0
  44. }
  45. ab1, err := MarshalAttributes(a1)
  46. if err != nil {
  47. panicf("failed to marshal a1: %v", err)
  48. }
  49. a2, err := UnmarshalAttributes(ab1)
  50. if err != nil {
  51. panicf("failed to unmarshal a2: %v", err)
  52. }
  53. if diff := cmp.Diff(a1, a2); diff != "" {
  54. panicf("unexpected Attributes (-want +got):\n%s", diff)
  55. }
  56. ab2, err := MarshalAttributes(a2)
  57. if err != nil {
  58. panicf("failed to marshal a2: %v", err)
  59. }
  60. if diff := cmp.Diff(ab1, ab2); diff != "" {
  61. panicf("unexpected attribute bytes (-want +got):\n%s", diff)
  62. }
  63. return 1
  64. }