ϪfdZddlmZddlmZddlmZmZddlmZddl m Z GddeZ Gd d Z Gd d e e jZGd de e jZGdde e jZy)z- Test cases for L{twisted.python.randbytes}. ) annotations)Callable)NoReturnProtocol) randbytes)unittestceZdZddZddZy)_SupportsAssertionscyNselfabs =/usr/lib/python3/dist-packages/twisted/test/test_randbytes.py assertEqualz_SupportsAssertions.assertEqual cyr r rs rassertNotEqualz"_SupportsAssertions.assertNotEqualrrN)robjectrrreturnr)__name__ __module__ __qualname__rrr rrr r s   rr ceZdZdZddZy)SecureRandomTestCaseBasez1 Base class for secureRandom test cases. ctddD]Z}||}|jt||||}|jt|||j||\y)a| The given random bytes source should return the number of bytes requested each time it is called and should probably not return the same bytes on two consecutive calls (although this is a perfectly legitimate occurrence and rejecting it may generate a spurious failure -- maybe we'll get lucky and the heat death with come first). N)rangerlenr)rsourcenbytesss2s r_checkzSecureRandomTestCaseBase._checkscBm 'FvA   SVV ,B   SWf -   A &  'rN)rr r$zCallable[[int], bytes]rNone)rrr__doc__r(r rrrrs 'rrceZdZdZddZy)SecureRandomTestsz4 Test secureRandom under normal conditions. cB|jtjy)z L{randbytes.secureRandom} should return a string of the requested length and make some effort to make its result otherwise unpredictable. N)r(r secureRandomrs r test_normalzSecureRandomTests.test_normal4s I**+rNrr))rrrr*r0r rrr,r,/s ,rr,c0eZdZdZddZddZddZddZy) ConditionalSecureRandomTestsz< Test random sources one by one, then remove it to. c6tj|_y)zJ Create a L{randbytes.RandomFactory} to use in the tests. N)r RandomFactoryfactoryr/s rsetUpz"ConditionalSecureRandomTests.setUpCs!..0 rc*tj)zL A factory raising an error when a source is not available. )rSourceNotAvailable)rr%s r errorFactoryz)ConditionalSecureRandomTests.errorFactoryIs**,,rcN|j|jjy)zy L{RandomFactory._osUrandom} should work as a random source whenever L{os.urandom} is available. N)r(r6 _osUrandomr/s rtest_osUrandomz+ConditionalSecureRandomTests.test_osUrandomOs DLL++,rc jj_jtj jj ddfd }jtdt|}jt|dy)zt Remove all secure sources and assert it raises a failure. Then try the fallback parameter. c>jjddS)Nr?T)fallback)r6r.r/srwrapperzBConditionalSecureRandomTests.test_withoutAnything..wrapper`s<<,,R$,? ?rzPurandom unavailable - proceeding with non-cryptographically secure random sourceN)rbytes) r:r6r< assertRaisesrSecureRandomNotAvailabler. assertWarnsRuntimeWarning__file__rr#)rrBr&s` rtest_withoutAnythingz1ConditionalSecureRandomTests.test_withoutAnythingVsy #'"3"3    . . 0I0I2  @     I     Q$rNr1)r%rrr)rrrr*r7r:r=rIr rrr3r3<s1 - -%rr3c eZdZdZddZddZy)RandomBaseTestsz% 'Normal' random test cases. cB|jtjy)z" Test basic case. N)r(rinsecureRandomr/s rr0zRandomBaseTests.test_normalrs I,,-rcptj}d|_|j|jy)zG Test C{insecureRandom} without C{random.getrandbits}. N)rr5 getrandbitsr(rM)rr6s rtest_withoutGetrandbitsz'RandomBaseTests.test_withoutGetrandbitsxs,))+" G**+rNr1)rrrr*r0rPr rrrKrKms. ,rrKN)r* __future__rtypingrrrtwisted.pythonr twisted.trialrr rTestCaser,SynchronousTestCaser3rKr rrrWsq#%$" ( '', ,0(2C2C ,.%h::.%b,.0L0L,r