161 lines
4.8 KiB
Python
161 lines
4.8 KiB
Python
|
|
"""
|
||
|
|
Unit tests for utility functions.
|
||
|
|
"""
|
||
|
|
|
||
|
|
import unittest
|
||
|
|
import os
|
||
|
|
import sys
|
||
|
|
from unittest.mock import patch
|
||
|
|
|
||
|
|
# Add the access module to the Python path
|
||
|
|
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'access'))
|
||
|
|
|
||
|
|
from utils import (
|
||
|
|
validate_mac_address,
|
||
|
|
normalize_mac_address,
|
||
|
|
hash_password,
|
||
|
|
verify_password,
|
||
|
|
format_duration,
|
||
|
|
get_client_info
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class TestUtilityFunctions(unittest.TestCase):
|
||
|
|
"""Test utility functions."""
|
||
|
|
|
||
|
|
def test_validate_mac_address(self):
|
||
|
|
"""Test MAC address validation."""
|
||
|
|
# Valid MAC addresses
|
||
|
|
valid_macs = [
|
||
|
|
'00:11:22:33:44:55',
|
||
|
|
'00-11-22-33-44-55',
|
||
|
|
'001122334455',
|
||
|
|
'aa:bb:cc:dd:ee:ff',
|
||
|
|
'AA:BB:CC:DD:EE:FF'
|
||
|
|
]
|
||
|
|
|
||
|
|
for mac in valid_macs:
|
||
|
|
with self.subTest(mac=mac):
|
||
|
|
self.assertTrue(validate_mac_address(mac))
|
||
|
|
|
||
|
|
# Invalid MAC addresses
|
||
|
|
invalid_macs = [
|
||
|
|
'',
|
||
|
|
None,
|
||
|
|
'00:11:22:33:44', # Too short
|
||
|
|
'00:11:22:33:44:55:66', # Too long
|
||
|
|
'00:11:22:33:44:gg', # Invalid hex
|
||
|
|
'00:11:22:33:44:5', # Incomplete
|
||
|
|
'not-a-mac-address'
|
||
|
|
]
|
||
|
|
|
||
|
|
for mac in invalid_macs:
|
||
|
|
with self.subTest(mac=mac):
|
||
|
|
self.assertFalse(validate_mac_address(mac))
|
||
|
|
|
||
|
|
def test_normalize_mac_address(self):
|
||
|
|
"""Test MAC address normalization."""
|
||
|
|
test_cases = [
|
||
|
|
('00:11:22:33:44:55', '00:11:22:33:44:55'),
|
||
|
|
('00-11-22-33-44-55', '00:11:22:33:44:55'),
|
||
|
|
('001122334455', '00:11:22:33:44:55'),
|
||
|
|
('AA:BB:CC:DD:EE:FF', 'aa:bb:cc:dd:ee:ff'),
|
||
|
|
('invalid-mac', None),
|
||
|
|
('', None),
|
||
|
|
(None, None)
|
||
|
|
]
|
||
|
|
|
||
|
|
for input_mac, expected in test_cases:
|
||
|
|
with self.subTest(input_mac=input_mac):
|
||
|
|
result = normalize_mac_address(input_mac)
|
||
|
|
self.assertEqual(result, expected)
|
||
|
|
|
||
|
|
def test_hash_password(self):
|
||
|
|
"""Test password hashing."""
|
||
|
|
password = "test_password"
|
||
|
|
hashed, salt = hash_password(password)
|
||
|
|
|
||
|
|
# Hash should be different from password
|
||
|
|
self.assertNotEqual(hashed, password)
|
||
|
|
|
||
|
|
# Salt should be generated
|
||
|
|
self.assertIsNotNone(salt)
|
||
|
|
self.assertTrue(len(salt) > 0)
|
||
|
|
|
||
|
|
# Same password with same salt should produce same hash
|
||
|
|
hashed2, _ = hash_password(password, salt)
|
||
|
|
self.assertEqual(hashed, hashed2)
|
||
|
|
|
||
|
|
def test_verify_password(self):
|
||
|
|
"""Test password verification."""
|
||
|
|
password = "test_password"
|
||
|
|
wrong_password = "wrong_password"
|
||
|
|
|
||
|
|
hashed, salt = hash_password(password)
|
||
|
|
|
||
|
|
# Correct password should verify
|
||
|
|
self.assertTrue(verify_password(password, hashed, salt))
|
||
|
|
|
||
|
|
# Wrong password should not verify
|
||
|
|
self.assertFalse(verify_password(wrong_password, hashed, salt))
|
||
|
|
|
||
|
|
def test_format_duration(self):
|
||
|
|
"""Test duration formatting."""
|
||
|
|
test_cases = [
|
||
|
|
(0, "0s"),
|
||
|
|
(30, "30s"),
|
||
|
|
(60, "1m"),
|
||
|
|
(90, "1m 30s"),
|
||
|
|
(3600, "1h"),
|
||
|
|
(3661, "1h 1m 1s"),
|
||
|
|
(7322, "2h 2m 2s"),
|
||
|
|
(-10, "0s") # Negative should return 0s
|
||
|
|
]
|
||
|
|
|
||
|
|
for seconds, expected in test_cases:
|
||
|
|
with self.subTest(seconds=seconds):
|
||
|
|
result = format_duration(seconds)
|
||
|
|
self.assertEqual(result, expected)
|
||
|
|
|
||
|
|
def test_get_client_info(self):
|
||
|
|
"""Test client info extraction."""
|
||
|
|
# Test with valid environment variables
|
||
|
|
env_vars = {
|
||
|
|
'username': 'testuser',
|
||
|
|
'password': 'testpass',
|
||
|
|
'common_name': 'testclient',
|
||
|
|
'trusted_ip': '192.168.1.1',
|
||
|
|
'untrusted_ip': '10.0.0.1',
|
||
|
|
'CLIENT_MAC': '00:11:22:33:44:55'
|
||
|
|
}
|
||
|
|
|
||
|
|
client_info = get_client_info(env_vars)
|
||
|
|
|
||
|
|
self.assertEqual(client_info['username'], 'testuser')
|
||
|
|
self.assertEqual(client_info['password'], 'testpass')
|
||
|
|
self.assertEqual(client_info['mac_address'], '00:11:22:33:44:55')
|
||
|
|
self.assertTrue(client_info['is_valid'])
|
||
|
|
|
||
|
|
# Test with missing required fields
|
||
|
|
incomplete_env_vars = {
|
||
|
|
'username': 'testuser',
|
||
|
|
# Missing password and MAC
|
||
|
|
}
|
||
|
|
|
||
|
|
client_info = get_client_info(incomplete_env_vars)
|
||
|
|
self.assertFalse(client_info['is_valid'])
|
||
|
|
|
||
|
|
# Test with invalid MAC
|
||
|
|
invalid_mac_env_vars = {
|
||
|
|
'username': 'testuser',
|
||
|
|
'password': 'testpass',
|
||
|
|
'CLIENT_MAC': 'invalid-mac'
|
||
|
|
}
|
||
|
|
|
||
|
|
client_info = get_client_info(invalid_mac_env_vars)
|
||
|
|
self.assertFalse(client_info['is_valid'])
|
||
|
|
self.assertIsNone(client_info['mac_address'])
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
unittest.main()
|