|
9 | 9 | """ |
10 | 10 |
|
11 | 11 | import unittest |
12 | | -import time |
13 | 12 | from wifite.model.target import Target |
14 | 13 | from wifite.util.wpa3 import WPA3Detector |
15 | 14 |
|
@@ -57,104 +56,105 @@ def setUp(self): |
57 | 56 | '' |
58 | 57 | ] |
59 | 58 |
|
60 | | - def test_cache_performance_improvement(self): |
61 | | - """Benchmark cache performance improvement.""" |
62 | | - target = Target(self.wpa3_transition_fields) |
63 | | - iterations = 1000 |
64 | | - |
65 | | - # Measure time without cache (fresh detection each time) |
66 | | - start_time = time.time() |
67 | | - for _ in range(iterations): |
68 | | - WPA3Detector.detect_wpa3_capability(target, use_cache=False) |
69 | | - no_cache_time = time.time() - start_time |
70 | | - |
71 | | - # Set cache once |
72 | | - wpa3_info_dict = WPA3Detector.detect_wpa3_capability(target, use_cache=False) |
| 59 | + def test_cache_short_circuits_detection(self): |
| 60 | + """Cached detection returns the stored result without recomputing. |
| 61 | +
|
| 62 | + Behavioural check (deterministic, not timing-based): when a target |
| 63 | + already carries a ``wpa3_info`` cache, ``use_cache=True`` must return |
| 64 | + that cached value verbatim, while ``use_cache=False`` must ignore the |
| 65 | + cache and recompute from the target's encryption fields. We prove this |
| 66 | + by seeding the cache with a sentinel that deliberately disagrees with |
| 67 | + what the fields would produce. |
| 68 | + """ |
73 | 69 | from wifite.util.wpa3 import WPA3Info |
74 | | - target.wpa3_info = WPA3Info.from_dict(wpa3_info_dict) |
75 | | - |
76 | | - # Measure time with cache |
77 | | - start_time = time.time() |
78 | | - for _ in range(iterations): |
79 | | - WPA3Detector.detect_wpa3_capability(target, use_cache=True) |
80 | | - cache_time = time.time() - start_time |
81 | | - |
82 | | - # Cache should be significantly faster |
83 | | - speedup = no_cache_time / cache_time if cache_time > 0 else float('inf') |
84 | | - |
85 | | - print(f"\nCache Performance Benchmark ({iterations} iterations):") |
86 | | - print(f" Without cache: {no_cache_time:.4f}s") |
87 | | - print(f" With cache: {cache_time:.4f}s") |
88 | | - print(f" Speedup: {speedup:.2f}x") |
89 | | - |
90 | | - # Cache should be at least 2x faster |
91 | | - self.assertGreater(speedup, 2.0, |
92 | | - f"Cache speedup {speedup:.2f}x is less than expected 2x") |
93 | | - |
94 | | - def test_early_return_performance(self): |
95 | | - """Benchmark early return optimization for WPA2-only targets.""" |
96 | | - wpa2_target = Target(self.wpa2_only_fields) |
97 | | - wpa3_target = Target(self.wpa3_transition_fields) |
98 | | - iterations = 1000 |
99 | | - |
100 | | - # Measure WPA2-only detection (should be faster with early return) |
101 | | - start_time = time.time() |
102 | | - for _ in range(iterations): |
103 | | - WPA3Detector.detect_wpa3_capability(wpa2_target, use_cache=False) |
104 | | - wpa2_time = time.time() - start_time |
105 | | - |
106 | | - # Measure WPA3 detection (full processing) |
107 | | - start_time = time.time() |
108 | | - for _ in range(iterations): |
109 | | - WPA3Detector.detect_wpa3_capability(wpa3_target, use_cache=False) |
110 | | - wpa3_time = time.time() - start_time |
111 | | - |
112 | | - print(f"\nEarly Return Benchmark ({iterations} iterations):") |
113 | | - print(f" WPA2-only (early return): {wpa2_time:.4f}s") |
114 | | - print(f" WPA3 (full processing): {wpa3_time:.4f}s") |
115 | | - print(f" Ratio: {wpa3_time/wpa2_time:.2f}x") |
116 | | - |
117 | | - # WPA2-only should be faster or similar (early return optimization) |
118 | | - # Allow some variance due to system load |
119 | | - self.assertLessEqual(wpa2_time, wpa3_time * 1.5, |
120 | | - "WPA2-only detection should benefit from early return") |
121 | 70 |
|
122 | | - def test_helper_method_cache_usage(self): |
123 | | - """Benchmark helper methods using cache vs fresh detection.""" |
124 | 71 | target = Target(self.wpa3_transition_fields) |
125 | | - iterations = 1000 |
126 | | - |
127 | | - # Without cache - helper methods trigger full detection |
128 | | - start_time = time.time() |
129 | | - for _ in range(iterations): |
130 | | - target.wpa3_info = None # Clear cache |
131 | | - WPA3Detector.identify_transition_mode(target) |
132 | | - WPA3Detector.check_pmf_status(target) |
133 | | - WPA3Detector.get_supported_sae_groups(target) |
134 | | - no_cache_time = time.time() - start_time |
135 | | - |
136 | | - # With cache - helper methods use cached data |
137 | | - wpa3_info_dict = WPA3Detector.detect_wpa3_capability(target, use_cache=False) |
| 72 | + |
| 73 | + # What a fresh (uncached) detection derives from the fields. |
| 74 | + fresh = WPA3Detector.detect_wpa3_capability(target, use_cache=False) |
| 75 | + self.assertTrue(fresh['has_wpa3'], |
| 76 | + 'Fixture should detect WPA3 from the encryption fields') |
| 77 | + |
| 78 | + # Seed the cache with a sentinel that disagrees with the fields, so a |
| 79 | + # cache hit is distinguishable from a recompute. |
| 80 | + sentinel = WPA3Info.from_dict({ |
| 81 | + 'has_wpa3': False, |
| 82 | + 'has_wpa2': True, |
| 83 | + 'is_transition': False, |
| 84 | + 'pmf_status': WPA3Detector.PMF_DISABLED, |
| 85 | + 'sae_groups': [], |
| 86 | + 'dragonblood_vulnerable': False, |
| 87 | + }) |
| 88 | + target.wpa3_info = sentinel |
| 89 | + |
| 90 | + # Cache hit: must return the sentinel, proving detection was skipped. |
| 91 | + cached = WPA3Detector.detect_wpa3_capability(target, use_cache=True) |
| 92 | + self.assertEqual(cached, sentinel.to_dict(), |
| 93 | + 'Cached path must return the stored wpa3_info verbatim') |
| 94 | + self.assertFalse(cached['has_wpa3'], |
| 95 | + 'Cached path must not recompute from the fields') |
| 96 | + |
| 97 | + # Cache bypass: must recompute and match the fresh detection. |
| 98 | + recomputed = WPA3Detector.detect_wpa3_capability(target, use_cache=False) |
| 99 | + self.assertEqual(recomputed, fresh, |
| 100 | + 'use_cache=False must ignore the cache and recompute') |
| 101 | + self.assertTrue(recomputed['has_wpa3']) |
| 102 | + |
| 103 | + def test_early_return_for_wpa2_only(self): |
| 104 | + """WPA2-only targets take the early-return branch (no WPA3 work). |
| 105 | +
|
| 106 | + Behavioural check (deterministic): a target whose fields advertise no |
| 107 | + WPA3/SAE must short-circuit to the WPA2-only result shape, while a |
| 108 | + WPA3 transition target must go through full detection. This exercises |
| 109 | + the same early-return optimisation the old benchmark targeted, without |
| 110 | + asserting on wall-clock time. |
| 111 | + """ |
| 112 | + wpa2 = WPA3Detector.detect_wpa3_capability( |
| 113 | + Target(self.wpa2_only_fields), use_cache=False) |
| 114 | + wpa3 = WPA3Detector.detect_wpa3_capability( |
| 115 | + Target(self.wpa3_transition_fields), use_cache=False) |
| 116 | + |
| 117 | + # Early-return branch: WPA2-only, not transition, no SAE groups. |
| 118 | + self.assertFalse(wpa2['has_wpa3']) |
| 119 | + self.assertFalse(wpa2['is_transition']) |
| 120 | + self.assertEqual(wpa2['sae_groups'], []) |
| 121 | + self.assertFalse(wpa2['dragonblood_vulnerable']) |
| 122 | + |
| 123 | + # Full-detection branch still flags WPA3. |
| 124 | + self.assertTrue(wpa3['has_wpa3']) |
| 125 | + |
| 126 | + def test_helper_methods_use_cache(self): |
| 127 | + """Helper methods read cached wpa3_info instead of recomputing. |
| 128 | +
|
| 129 | + Deterministic check: seed the cache with a sentinel that disagrees |
| 130 | + with the target's fields, then confirm each helper returns the cached |
| 131 | + value. Clearing the cache makes them recompute from the fields. |
| 132 | + """ |
138 | 133 | from wifite.util.wpa3 import WPA3Info |
139 | | - target.wpa3_info = WPA3Info.from_dict(wpa3_info_dict) |
140 | | - |
141 | | - start_time = time.time() |
142 | | - for _ in range(iterations): |
143 | | - WPA3Detector.identify_transition_mode(target) |
144 | | - WPA3Detector.check_pmf_status(target) |
145 | | - WPA3Detector.get_supported_sae_groups(target) |
146 | | - cache_time = time.time() - start_time |
147 | | - |
148 | | - speedup = no_cache_time / cache_time if cache_time > 0 else float('inf') |
149 | | - |
150 | | - print(f"\nHelper Method Cache Benchmark ({iterations} iterations):") |
151 | | - print(f" Without cache: {no_cache_time:.4f}s") |
152 | | - print(f" With cache: {cache_time:.4f}s") |
153 | | - print(f" Speedup: {speedup:.2f}x") |
154 | | - |
155 | | - # Cache should provide significant speedup |
156 | | - self.assertGreater(speedup, 2.0, |
157 | | - f"Helper method cache speedup {speedup:.2f}x is less than expected") |
| 134 | + |
| 135 | + target = Target(self.wpa3_transition_fields) |
| 136 | + |
| 137 | + sentinel = WPA3Info.from_dict({ |
| 138 | + 'has_wpa3': True, |
| 139 | + 'has_wpa2': True, |
| 140 | + 'is_transition': False, # disagrees with the fields |
| 141 | + 'pmf_status': WPA3Detector.PMF_REQUIRED, |
| 142 | + 'sae_groups': [21], # not the default [19] |
| 143 | + 'dragonblood_vulnerable': False, |
| 144 | + }) |
| 145 | + target.wpa3_info = sentinel |
| 146 | + |
| 147 | + # Cache hit: helpers return the sentinel's values. |
| 148 | + self.assertEqual(WPA3Detector.identify_transition_mode(target), |
| 149 | + sentinel.is_transition) |
| 150 | + self.assertEqual(WPA3Detector.check_pmf_status(target), |
| 151 | + sentinel.pmf_status) |
| 152 | + self.assertEqual(WPA3Detector.get_supported_sae_groups(target), |
| 153 | + sentinel.sae_groups) |
| 154 | + |
| 155 | + # Cache cleared: helpers recompute from the fields (transition mode). |
| 156 | + target.wpa3_info = None |
| 157 | + self.assertTrue(WPA3Detector.identify_transition_mode(target)) |
158 | 158 |
|
159 | 159 |
|
160 | 160 | if __name__ == '__main__': |
|
0 commit comments