|
| 1 | +// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package util |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "golang.org/x/crypto/cryptobyte" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAddUint48(t *testing.T) { |
| 14 | + cases := map[string]struct { |
| 15 | + reason string |
| 16 | + builder *cryptobyte.Builder |
| 17 | + postAdd func(*cryptobyte.Builder) |
| 18 | + in uint64 |
| 19 | + want []byte |
| 20 | + }{ |
| 21 | + "OnlyUint48": { |
| 22 | + reason: "Adding only a 48-bit unsigned integer should yield expected result.", |
| 23 | + builder: &cryptobyte.Builder{}, |
| 24 | + in: 0xfefcff3cfdfc, |
| 25 | + want: []byte{254, 252, 255, 60, 253, 252}, |
| 26 | + }, |
| 27 | + "ExistingAddUint48": { |
| 28 | + reason: "Adding a 48-bit unsigned integer to a builder with existing bytes should yield expected result.", |
| 29 | + builder: func() *cryptobyte.Builder { |
| 30 | + var b cryptobyte.Builder |
| 31 | + b.AddUint64(0xffffffffffffffff) |
| 32 | + return &b |
| 33 | + }(), |
| 34 | + in: 0xfefcff3cfdfc, |
| 35 | + want: []byte{255, 255, 255, 255, 255, 255, 255, 255, 254, 252, 255, 60, 253, 252}, |
| 36 | + }, |
| 37 | + "ExistingAddUint48AndMore": { |
| 38 | + reason: "Adding a 48-bit unsigned integer to a builder with existing bytes, then adding more bytes, should yield expected result.", |
| 39 | + builder: func() *cryptobyte.Builder { |
| 40 | + var b cryptobyte.Builder |
| 41 | + b.AddUint64(0xffffffffffffffff) |
| 42 | + return &b |
| 43 | + }(), |
| 44 | + postAdd: func(b *cryptobyte.Builder) { |
| 45 | + b.AddUint32(0xffffffff) |
| 46 | + }, |
| 47 | + in: 0xfefcff3cfdfc, |
| 48 | + want: []byte{255, 255, 255, 255, 255, 255, 255, 255, 254, 252, 255, 60, 253, 252, 255, 255, 255, 255}, |
| 49 | + }, |
| 50 | + } |
| 51 | + for name, tc := range cases { |
| 52 | + t.Run(name, func(t *testing.T) { |
| 53 | + AddUint48(tc.builder, tc.in) |
| 54 | + if tc.postAdd != nil { |
| 55 | + tc.postAdd(tc.builder) |
| 56 | + } |
| 57 | + got := tc.builder.BytesOrPanic() |
| 58 | + if !bytes.Equal(got, tc.want) { |
| 59 | + t.Errorf("Bytes() = %v, want %v", got, tc.want) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
0 commit comments