Skip to content

Commit

Permalink
r2
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiSubira committed May 12, 2022
1 parent 29953d2 commit f2f0f5d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
18 changes: 8 additions & 10 deletions pkg/slayers/extn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down
30 changes: 17 additions & 13 deletions pkg/slayers/extn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit f2f0f5d

Please sign in to comment.