netlogtype_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package netlogtype
  4. import (
  5. "encoding/json"
  6. "math"
  7. "net/netip"
  8. "testing"
  9. "github.com/fxamacker/cbor/v2"
  10. "github.com/google/go-cmp/cmp"
  11. "tailscale.com/util/must"
  12. )
  13. func TestMaxSize(t *testing.T) {
  14. maxAddr := netip.AddrFrom16([16]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255})
  15. maxAddrPort := netip.AddrPortFrom(maxAddr, math.MaxUint16)
  16. cc := ConnectionCounts{
  17. // NOTE: These composite literals are deliberately unkeyed so that
  18. // added fields result in a build failure here.
  19. // Newly added fields should result in an update to both
  20. // MaxConnectionCountsJSONSize and MaxConnectionCountsCBORSize.
  21. Connection{math.MaxUint8, maxAddrPort, maxAddrPort},
  22. Counts{math.MaxUint64, math.MaxUint64, math.MaxUint64, math.MaxUint64},
  23. }
  24. outJSON := must.Get(json.Marshal(cc))
  25. if string(outJSON) != maxJSONConnCounts {
  26. t.Errorf("JSON mismatch (-got +want):\n%s", cmp.Diff(string(outJSON), maxJSONConnCounts))
  27. }
  28. outCBOR := must.Get(cbor.Marshal(cc))
  29. maxCBORConnCountsAlt := "\xa7" + maxCBORConnCounts[1:len(maxCBORConnCounts)-1] // may use a definite encoding of map
  30. if string(outCBOR) != maxCBORConnCounts && string(outCBOR) != maxCBORConnCountsAlt {
  31. t.Errorf("CBOR mismatch (-got +want):\n%s", cmp.Diff(string(outCBOR), maxCBORConnCounts))
  32. }
  33. }