fuzz.go 1.9 KB

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