79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
# Copyright 2020 Istvan Ruzman
|
|
# SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
import struct
|
|
|
|
from pyrad3 import utils
|
|
import pytest
|
|
|
|
# @pytest.mark.parametrize("header", [
|
|
# b""])
|
|
# def test_valid_header(header):
|
|
# utils.parse_header(header)
|
|
|
|
SECRET = b"secret"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"header",
|
|
[
|
|
b"\1\0" + struct.pack("!H", 5000) + 4996 * b"\0",
|
|
b"\1\0" + struct.pack("!H", 100),
|
|
b"\0\0" + struct.pack("!H", 20) + 16 * b"\0",
|
|
b"",
|
|
],
|
|
)
|
|
def test_invalid_header(header):
|
|
with pytest.raises(utils.PacketError):
|
|
utils.parse_header(header)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"plaintext, obfuscated, authenticator",
|
|
[
|
|
(
|
|
b"short_password",
|
|
"ed9b49281f9de8edefae1b09b04beb86",
|
|
"7b19486d8372b8c136ccf2444d0a5b2c",
|
|
),
|
|
(
|
|
b"superlongpassword_exeeding_16_bytes",
|
|
"1f123e277869997fdfb93f6df037024463918d29064c9fcd5831c57dccd9308ac6b835e6d8f70995d1498a6c5a2a5b71",
|
|
"12441ce350ce269c04f650f7923058e1",
|
|
),
|
|
],
|
|
)
|
|
def test_password(plaintext, obfuscated, authenticator):
|
|
obfuscated = bytes.fromhex(obfuscated)
|
|
authenticator = bytes.fromhex(authenticator)
|
|
encoded = utils.password_encode(SECRET, authenticator, plaintext)
|
|
assert len(encoded) == len(obfuscated)
|
|
assert encoded == obfuscated
|
|
decoded = utils.password_decode(SECRET, authenticator, encoded)
|
|
plaintext_str = plaintext.decode("utf-8")
|
|
assert len(decoded) == len(plaintext_str)
|
|
assert decoded == plaintext_str
|
|
|
|
assert utils.validate_pap_password(
|
|
SECRET, authenticator, encoded, plaintext
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"plaintext, chap, challenge",
|
|
[
|
|
(
|
|
b"short_password",
|
|
bytes.fromhex("2302a92821f675a52df8e5a3b10e49b0ab"),
|
|
b"1234567890ABCDEF",
|
|
),
|
|
],
|
|
)
|
|
def test_chap_password(plaintext, chap, challenge):
|
|
chapid = chap[:1]
|
|
encoded = utils.create_chap_password(chapid, challenge, plaintext)
|
|
|
|
assert len(encoded) == len(chap)
|
|
assert encoded == chap
|
|
assert utils.validate_chap_password(chapid, challenge, chap, plaintext)
|