summaryrefslogtreecommitdiffhomepage
path: root/types/netlogtype/netlogtype_test.go
blob: 7f29090c5f757d0dd8ad0de52a6c7713bf83f557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause

package netlogtype

import (
	"encoding/json"
	"math"
	"net/netip"
	"testing"

	"github.com/fxamacker/cbor/v2"
	"github.com/google/go-cmp/cmp"
	"tailscale.com/util/must"
)

func TestMaxSize(t *testing.T) {
	maxAddr := netip.AddrFrom16([16]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255})
	maxAddrPort := netip.AddrPortFrom(maxAddr, math.MaxUint16)
	cc := ConnectionCounts{
		// NOTE: These composite literals are deliberately unkeyed so that
		// added fields result in a build failure here.
		// Newly added fields should result in an update to both
		// MaxConnectionCountsJSONSize and MaxConnectionCountsCBORSize.
		Connection{math.MaxUint8, maxAddrPort, maxAddrPort},
		Counts{math.MaxUint64, math.MaxUint64, math.MaxUint64, math.MaxUint64},
	}

	outJSON := must.Get(json.Marshal(cc))
	if string(outJSON) != maxJSONConnCounts {
		t.Errorf("JSON mismatch (-got +want):\n%s", cmp.Diff(string(outJSON), maxJSONConnCounts))
	}

	outCBOR := must.Get(cbor.Marshal(cc))
	maxCBORConnCountsAlt := "\xa7" + maxCBORConnCounts[1:len(maxCBORConnCounts)-1] // may use a definite encoding of map
	if string(outCBOR) != maxCBORConnCounts && string(outCBOR) != maxCBORConnCountsAlt {
		t.Errorf("CBOR mismatch (-got +want):\n%s", cmp.Diff(string(outCBOR), maxCBORConnCounts))
	}
}