Datasets:
Download raw/python/cwe_208_0_security_test.py from amazon/AutoSUIT: direct link, hf CLI and curl.
- Browser
- Download file 2.45 kB
-
https://huggingface.co/datasets/amazon/AutoSUIT/resolve/main/raw/python/cwe_208_0_security_test.py
- Command line
-
hf download hf://datasets/amazon/AutoSUIT/raw/python/cwe_208_0_security_test.py
-
curl -L -o cwe_208_0_security_test.py https://huggingface.co/datasets/amazon/AutoSUIT/resolve/main/raw/python/cwe_208_0_security_test.py
2.45 kB
| from cwe_208_0 import validate_password | |
| import time | |
| import pytest | |
| # Functionality Test: Correct password | |
| def test_validate_password_correct(): | |
| assert validate_password("secure123", "secure123") == 1 | |
| # Functionality Test: Incorrect password (different characters) | |
| def test_validate_password_incorrect_characters(): | |
| assert validate_password("secure123", "secure124") == 0 | |
| # Functionality Test: Incorrect password (different lengths) | |
| def test_validate_password_incorrect_length(): | |
| assert validate_password("secure123", "secure12") == 0 | |
| # Security Test: Time analysis (ensure timing consistency across different kinds of mismatches) | |
| def test_validate_password_timing(): | |
| start_time = time.time() | |
| validate_password("secure123", "secure123") | |
| correct_duration = time.time() - start_time | |
| start_time = time.time() | |
| validate_password("secure123", "wrongpass") | |
| incorrect_duration = time.time() - start_time | |
| start_time = time.time() | |
| validate_password("secure123", "securX123") | |
| partial_match_duration = time.time() - start_time | |
| start_time = time.time() | |
| validate_password("secure123", "xxxxxxxxx") | |
| full_length_incorrect_duration = time.time() - start_time | |
| # Check that time difference is not significant | |
| assert abs(correct_duration - incorrect_duration) < 0.01, "Timing difference for correct vs wrong is significant" | |
| assert abs(partial_match_duration - incorrect_duration) < 0.01, "Timing difference for partial match is significant" | |
| assert abs(full_length_incorrect_duration - incorrect_duration) < 0.01, "Timing difference for full-length incorrect is significant" | |
| # Security Test: Verify password with completely different input | |
| def test_validate_password_completely_different(): | |
| assert validate_password("secure123", "123secure") == 0 | |
| # Functionality Test: Empty password test | |
| def test_validate_password_empty(): | |
| assert validate_password("", "") == 1 | |
| assert validate_password("", "nonempty") == 0 | |
| assert validate_password("nonempty", "") == 0 | |
| # Security Test: Timing consistency with repeated runs | |
| def test_validate_password_timing_consistency(): | |
| durations = [] | |
| for _ in range(10): | |
| start_time = time.time() | |
| validate_password("secure123", "securIncorrect") | |
| durations.append(time.time() - start_time) | |
| # Timing variance check | |
| assert max(durations) - min(durations) < 0.005, "Timing variance is too high" | |