Test that secrets module provides high-quality randomness.
()
| 125 | |
| 126 | |
| 127 | def test_secrets_module_randomness_quality(): |
| 128 | """Test that secrets module provides high-quality randomness.""" |
| 129 | import secrets |
| 130 | |
| 131 | # Generate a large set of random numbers |
| 132 | random_numbers = [secrets.randbelow(100) for _ in range(1000)] |
| 133 | |
| 134 | # Basic statistical test: should have good distribution |
| 135 | unique_values = len(set(random_numbers)) |
| 136 | |
| 137 | # With 1000 random numbers from 0-99, we should see most values at least once |
| 138 | # This is a very basic test - real cryptographic random should pass this easily |
| 139 | assert unique_values >= 60 # Should see at least 60 different values out of 100 |
| 140 | |
| 141 | # Test secrets.choice for string generation |
| 142 | chars = "abcdefghijklmnopqrstuvwxyz0123456789" |
| 143 | random_chars = [secrets.choice(chars) for _ in range(1000)] |
| 144 | |
| 145 | # Should have good character distribution |
| 146 | unique_chars = len(set(random_chars)) |
| 147 | assert unique_chars >= 20 # Should see at least 20 different characters |
| 148 | |
| 149 | |
| 150 | if __name__ == "__main__": |