add tests for peap-extensions
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
@ -2,6 +2,7 @@ package peap
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,13 +21,18 @@ type ExtensionAVP struct {
|
|||||||
Value []byte
|
Value []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrorReservedBitSet = errors.New("PEAP-Extension: Reserved bit is not 0")
|
||||||
|
)
|
||||||
|
|
||||||
func (eavp *ExtensionAVP) Decode(raw []byte) error {
|
func (eavp *ExtensionAVP) Decode(raw []byte) error {
|
||||||
typ := binary.BigEndian.Uint16(raw[:2])
|
typ := binary.BigEndian.Uint16(raw[:2])
|
||||||
// TODO fix this
|
if typ>>15 == 1 {
|
||||||
if typ&0b1000000000000000 == 0 {
|
|
||||||
eavp.Mandatory = true
|
eavp.Mandatory = true
|
||||||
}
|
}
|
||||||
// TODO: Check reserved bit
|
if typ>>14&1 != 0 {
|
||||||
|
return ErrorReservedBitSet
|
||||||
|
}
|
||||||
eavp.Type = AVPType(typ & 0b0011111111111111)
|
eavp.Type = AVPType(typ & 0b0011111111111111)
|
||||||
eavp.Length = binary.BigEndian.Uint16(raw[2:4])
|
eavp.Length = binary.BigEndian.Uint16(raw[2:4])
|
||||||
val := raw[4:]
|
val := raw[4:]
|
||||||
|
|||||||
@ -0,0 +1,36 @@
|
|||||||
|
package peap_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"goauthentik.io/internal/outpost/radius/eap/protocol/peap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEncode(t *testing.T) {
|
||||||
|
eavp := peap.ExtensionAVP{
|
||||||
|
Mandatory: true,
|
||||||
|
Type: peap.AVPType(3),
|
||||||
|
}
|
||||||
|
assert.Equal(t, []byte{0x80, 0x3, 0x0, 0x0}, eavp.Encode())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecode(t *testing.T) {
|
||||||
|
eavp := peap.ExtensionAVP{}
|
||||||
|
err := eavp.Decode([]byte{0x80, 0x3, 0x0, 0x0})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, eavp.Mandatory)
|
||||||
|
assert.Equal(t, peap.AVPType(3), eavp.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecode_Invalid_ReservedBitSet(t *testing.T) {
|
||||||
|
eavp := peap.ExtensionAVP{}
|
||||||
|
err := eavp.Decode([]byte{0xc0, 0x3, 0x0, 0x0})
|
||||||
|
assert.ErrorIs(t, err, peap.ErrorReservedBitSet)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecode_Invalid_Length(t *testing.T) {
|
||||||
|
eavp := peap.ExtensionAVP{}
|
||||||
|
err := eavp.Decode([]byte{0x80, 0x3, 0x0, 0x0, 0x0})
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user