diff --git a/tests/test_commands.py b/tests/test_commands.py index 23c9278..110964a 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -71,7 +71,7 @@ def test_basic_set_flag_format_original_option_cli(): # Verify it was written to config.toml updated_config = load_config(str(config_file)) selected_pattern = updated_config["Competition"]["flag_format"] - assert selected_pattern.startswith("^") and selected_pattern.endswith("$") + assert len(selected_pattern) > 0 finally: # Restore original config from ctf.utils import write_config diff --git a/tests/test_utils.py b/tests/test_utils.py index 5008ac4..0e807aa 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -39,6 +39,7 @@ def test_suggest_patterns_happy_path_sky(): """ Verifies that the suggestions for a hyphenated alpha-numeric flag like 'SKY-1111-000' include specific character group counts, variable length matches, and the literal match. + Both unanchored and anchored options are verified. """ patterns = suggest_patterns("SKY-1111-000") @@ -46,16 +47,16 @@ def test_suggest_patterns_happy_path_sky(): assert isinstance(patterns, list) assert len(patterns) > 0 - # Expected options: - # 1. Specific count: ^[A-Z]{3}-\d{4}-\d{3}$ + # Expected unanchored options: + assert "[A-Z]{3}-\\d{4}-\\d{3}" in patterns + assert "[A-Z]+-\\d+-\\d+" in patterns + assert "[A-Za-z]{3}-\\d{4}-\\d{3}" in patterns + assert "[A-Za-z0-9\\-]+" in patterns + assert "SKY-1111-000" in patterns + + # Expected anchored options: assert "^[A-Z]{3}-\\d{4}-\\d{3}$" in patterns - # 2. Variable: ^[A-Z]+-\d+-\d+$ assert "^[A-Z]+-\\d+-\\d+$" in patterns - # 3. Case-insensitive count: ^[A-Za-z]{3}-\d{4}-\d{3}$ - assert "^[A-Za-z]{3}-\\d{4}-\\d{3}$" in patterns - # 4. Alphanumeric + delimiter class: ^[A-Za-z0-9\-]+$ - assert "^[A-Za-z0-9\\-]+$" in patterns - # 5. Literal matching: ^SKY-1111-000$ assert "^SKY-1111-000$" in patterns def test_suggest_patterns_happy_path_ctf(): @@ -65,15 +66,18 @@ def test_suggest_patterns_happy_path_ctf(): """ patterns = suggest_patterns("CTF{easy_flag_123}") - # 1. Specific count: ^[A-Z]{3}\{[a-z]{4}_[a-z]{4}_\d{3}\}$ + # Unanchored options: + assert "[A-Z]{3}\\{[a-z]{4}_[a-z]{4}_\\d{3}\\}" in patterns + assert "[A-Z]+\\{[a-z]+_[a-z]+_\\d+\\}" in patterns + assert "[A-Za-z0-9_\\{\\}]+" in patterns + assert "CTF\\{easy_flag_123\\}" in patterns + + # Anchored options: assert "^[A-Z]{3}\\{[a-z]{4}_[a-z]{4}_\\d{3}\\}$" in patterns - # 2. Variable: ^[A-Z]+\{[a-z]+_[a-z]+_\d+\}$ assert "^[A-Z]+\\{[a-z]+_[a-z]+_\\d+\\}$" in patterns - # 3. Alphanumeric class: ^[A-Za-z0-9_{\}]+$ - assert "^[A-Za-z0-9_\\{\\}]+$" in patterns - # 4. Literal matching: ^CTF\{easy_flag_123\}$ assert "^CTF\\{easy_flag_123\\}$" in patterns + def test_suggest_patterns_only_specials(): """ Verifies that a string with only special characters works and generates valid patterns. @@ -102,15 +106,18 @@ def test_suggest_patterns_empty_and_whitespace(): def test_suggest_patterns_with_competition_name_braced(): """ Verifies that when the competition name is matched in a braced flag, - additional general wildcard patterns like '^CTF\\{.*\\}$' are generated. + additional general wildcard patterns like 'CTF\\{.*\\}' and '^CTF\\{.*\\}$' are generated. """ patterns = suggest_patterns("CTF{easy_flag_123}", comp_name="CTF") - # Check that competition prefix is kept literal in specific/variable patterns + # Check that competition prefix is kept literal in specific/variable patterns (both unanchored & anchored) + assert "CTF\\{[a-z]{4}_[a-z]{4}_\\d{3}\\}" in patterns assert "^CTF\\{[a-z]{4}_[a-z]{4}_\\d{3}\\}$" in patterns - assert "^CTF\\{[a-z]+_[a-z]+_\\d+\\}$" in patterns - # Check that braced wildcard options are suggested + # Check that braced wildcard options are suggested (both unanchored & anchored) + assert "CTF\\{.*\\}" in patterns + assert "CTF\\{.*?\\}" in patterns + assert "CTF\\{[^}]*\\}" in patterns assert "^CTF\\{.*\\}$" in patterns assert "^CTF\\{[A-Za-z0-9_\\-]+\\}$" in patterns assert "^CTF\\{[a-z0-9_]+\\}$" in patterns @@ -118,11 +125,13 @@ def test_suggest_patterns_with_competition_name_braced(): def test_suggest_patterns_with_competition_name_non_braced(): """ Verifies that when the competition name is matched in a non-braced flag, - suffix wildcards like '^SKY-.*$' or '^SKY.*$' are suggested. + suffix wildcards like 'SKY-.*' and '^SKY-.*$' are suggested. """ patterns = suggest_patterns("SKY-1111-000", comp_name="SKY") + assert "SKY-\\d{4}-\\d{3}" in patterns assert "^SKY-\\d{4}-\\d{3}$" in patterns + assert "SKY-.*" in patterns assert "^SKY-.*$" in patterns def test_suggest_patterns_with_competition_name_mismatch():