File: util_test.go

package info (click to toggle)
golang-github-pion-dtls-v3 3.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,124 kB
  • sloc: makefile: 4
file content (64 lines) | stat: -rw-r--r-- 1,758 bytes parent folder | download
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package util

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"golang.org/x/crypto/cryptobyte"
)

func TestAddUint48(t *testing.T) {
	cases := map[string]struct {
		reason  string
		builder *cryptobyte.Builder
		postAdd func(*cryptobyte.Builder)
		in      uint64
		want    []byte
	}{
		"OnlyUint48": {
			reason:  "Adding only a 48-bit unsigned integer should yield expected result.",
			builder: &cryptobyte.Builder{},
			in:      0xfefcff3cfdfc,
			want:    []byte{254, 252, 255, 60, 253, 252},
		},
		"ExistingAddUint48": {
			reason: "Adding a 48-bit unsigned integer to a builder with existing bytes should yield expected result.",
			builder: func() *cryptobyte.Builder {
				var b cryptobyte.Builder
				b.AddUint64(0xffffffffffffffff)

				return &b
			}(),
			in:   0xfefcff3cfdfc,
			want: []byte{255, 255, 255, 255, 255, 255, 255, 255, 254, 252, 255, 60, 253, 252},
		},
		"ExistingAddUint48AndMore": {
			//nolint:lll
			reason: "Adding a 48-bit unsigned integer to a builder with existing bytes, then adding more bytes, should yield expected result.",
			builder: func() *cryptobyte.Builder {
				var b cryptobyte.Builder
				b.AddUint64(0xffffffffffffffff)

				return &b
			}(),
			postAdd: func(b *cryptobyte.Builder) {
				b.AddUint32(0xffffffff)
			},
			in:   0xfefcff3cfdfc,
			want: []byte{255, 255, 255, 255, 255, 255, 255, 255, 254, 252, 255, 60, 253, 252, 255, 255, 255, 255},
		},
	}
	for name, tc := range cases {
		t.Run(name, func(t *testing.T) {
			AddUint48(tc.builder, tc.in)
			if tc.postAdd != nil {
				tc.postAdd(tc.builder)
			}
			got := tc.builder.BytesOrPanic()
			assert.Equal(t, tc.want, got)
		})
	}
}