Skip to content

Commit

Permalink
first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavWaza committed Jul 2, 2024
1 parent d338926 commit 2952bbe
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements.txt
- name: Run tests
run: |
python -m unittest discover -s tests/
6 changes: 3 additions & 3 deletions password_entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
NUM_GENERATIONS = 1
MIN_CHARS = 6
MAX_CHARS = 10
TIERS = ["Pathetic", "Low", "Medium", "High", "Extreme"]

def tier(ttd: timedelta):
tiers = ["Pathetic", "Low", "Medium", "High", "Extreme"]
for i, tier in enumerate(tiers[:-1]):
for i, tier in enumerate(TIERS[:-1]):
threshold = timedelta(days=365*pow(10, i))

if ttd < threshold:
return tier

return tiers[-1]
return TIERS[-1]

def calculate_password_entropy(predefined_password):
assert len(predefined_password) >= MIN_CHARS, "Too short password"
Expand Down
35 changes: 35 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from datetime import timedelta
from password_entropy import tier, calculate_password_entropy, TIERS
import json

# Функция для загрузки паролей и их тиров из JSON файла
def load_passwords(filename):
with open(filename, 'r', encoding='utf-8') as file:
data = json.load(file)
return data

# Функция для тестирования паролей
def test_passwords(filename):
password_data = load_passwords(filename)

# Количество совпадений по тирам
correct_predictions = {tier_name : 0 for tier_name in TIERS}

for expected_tier, passwords in password_data.items():
print(f"Testing {expected_tier} passwords:")
for password in passwords:
entropy = calculate_password_entropy(password)
ttd = timedelta(seconds=10e-9 * pow(2, entropy))
predicted_tier = tier(ttd)

print(f"Password: {password}, Predicted Tier: {predicted_tier}, Entropy: {entropy}")

if expected_tier == predicted_tier:
correct_predictions[predicted_tier] += 1

print("\nResults:")
for tier_name, correct_count in correct_predictions.items():
print(f"Correct predictions for {tier_name} tier: {correct_count / len(password_data[tier_name])}")

if __name__ == "__main__":
test_passwords('test_passwords.json')
23 changes: 23 additions & 0 deletions tests/test_passwords.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Pathetic": [
"password",
"123456"
"12345678"
],
"Low": [
"Passw0rd",
"securepwd"
],
"Medium": [
"SecureP@ssword",
"complexPW123!"
],
"High": [
"VeryC0mpl3xP@ss",
"NewPWD$ecure!"
],
"Extreme": [
"Ultr@C0mplexPa$$w0rd",
"Th!sIsAV3ryL0ng&P@ss#0rd"
]
}

0 comments on commit 2952bbe

Please sign in to comment.