From f2f0f5d23667a36f4ee9a8b9bb829fe047cfcb81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jordi=20Subir=C3=A0=20Nieto?= Date: Thu, 12 May 2022 15:47:55 +0200 Subject: [PATCH] r2 --- pkg/slayers/extn.go | 18 ++++++++---------- pkg/slayers/extn_test.go | 30 +++++++++++++++++------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/pkg/slayers/extn.go b/pkg/slayers/extn.go index c8e50d3943..4a38c8f036 100644 --- a/pkg/slayers/extn.go +++ b/pkg/slayers/extn.go @@ -538,10 +538,10 @@ type PacketAuthenticatorOption struct { // NewPacketAuthenticatorOption creates a new EndToEndOption of // OptTypeAuthenticator, initialized with the given SPAO data. func NewPacketAuthenticatorOption(spi PacketAuthSPI, alg PacketAuthAlg, ts uint32, - sn uint32, auth []byte) (PacketAuthenticatorOption, error) { + sn uint32, auth []byte) PacketAuthenticatorOption { o := PacketAuthenticatorOption{EndToEndOption: new(EndToEndOption)} - err := o.Reset(spi, alg, ts, sn, auth) - return o, err + o.Reset(spi, alg, ts, sn, auth) + return o } // ParsePacketAuthenticatorOption parses o as a packet authenticator option. @@ -564,13 +564,13 @@ func ParsePacketAuthenticatorOption(o *EndToEndOption) (PacketAuthenticatorOptio // Reset reinitializes the underlying EndToEndOption with the SPAO data. // Reuses the OptData buffer if it is of sufficient capacity. func (o PacketAuthenticatorOption) Reset(spi PacketAuthSPI, alg PacketAuthAlg, - ts uint32, sn uint32, auth []byte) error { + ts uint32, sn uint32, auth []byte) { - if ts > (1 << 24) { - return serrors.New("Timestamp value should be smaller than 2^24", "ts", ts) + if ts >= (1 << 24) { + panic("Timestamp value should be smaller than 2^24") } - if sn > (1 << 24) { - return serrors.New("Sequence number should be smaller than 2^24", "sn", sn) + if sn >= (1 << 24) { + panic("Sequence number should be smaller than 2^24") } o.OptType = OptTypeAuthenticator @@ -596,8 +596,6 @@ func (o PacketAuthenticatorOption) Reset(spi PacketAuthSPI, alg PacketAuthAlg, // reset unused/implicit fields o.OptDataLen = 0 o.ActualLength = 0 - - return nil } // SPI returns returns the value set in the homonym field in the extension. diff --git a/pkg/slayers/extn_test.go b/pkg/slayers/extn_test.go index 3eb072c895..475d4ab34b 100644 --- a/pkg/slayers/extn_test.go +++ b/pkg/slayers/extn_test.go @@ -585,45 +585,49 @@ func TestOptAuthenticatorSerialize(t *testing.T) { ts uint32 sn uint32 optAuth []byte - err assert.ErrorAssertionFunc + panics bool }{ { name: "correct", spi: slayers.PacketAuthSPI(spi), - algo: slayers.PacketAuthAlg(algo), + algo: algo, ts: ts, sn: sn, optAuth: optAuthMAC, - err: assert.NoError, + panics: false, }, { name: "bad_ts", spi: slayers.PacketAuthSPI(spi), - algo: slayers.PacketAuthAlg(algo), - ts: binary.LittleEndian.Uint32([]byte{1, 2, 3, 1}), + algo: algo, + ts: binary.LittleEndian.Uint32([]byte{0, 0, 0, 1}), sn: sn, optAuth: optAuthMAC, - err: assert.Error, + panics: true, }, { name: "bad_sn", spi: slayers.PacketAuthSPI(spi), - algo: slayers.PacketAuthAlg(algo), + algo: algo, ts: ts, - sn: binary.LittleEndian.Uint32([]byte{4, 5, 6, 1}), + sn: binary.LittleEndian.Uint32([]byte{0, 0, 0, 1}), optAuth: optAuthMAC, - err: assert.Error, + panics: true, }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { - spao, err := slayers.NewPacketAuthenticatorOption(c.spi, c.algo, c.ts, c.sn, c.optAuth) - c.err(t, err, "NewPacketAuthenticatorOption") - - if err != nil { + if c.panics { + assert.Panics(t, func() { + slayers.NewPacketAuthenticatorOption(c.spi, c.algo, c.ts, c.sn, c.optAuth) + }, + "The code did not panic", + ) return } + spao := slayers.NewPacketAuthenticatorOption(c.spi, c.algo, c.ts, c.sn, c.optAuth) + e2e := slayers.EndToEndExtn{} e2e.NextHdr = slayers.L4UDP e2e.Options = []*slayers.EndToEndOption{spao.EndToEndOption}