Skip to content

Commit 4dd2c6f

Browse files
author
Maxim Kochukov
committed
new: tests
1 parent 109de13 commit 4dd2c6f

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: python
2+
python:
3+
- "3.3"
4+
- "3.4"
5+
- "3.5"
6+
- "3.6"
7+
- "3.7"
8+
install:
9+
- pip install -r requirements.txt
10+
script:
11+
- pytest

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/randcrack.svg)
77
![PyPI - Implementation](https://img.shields.io/pypi/implementation/randcrack.svg)
88

9-
This script is able to predict python's `random` module random generated values. Script was tested against **Python 3.5.2**, **3.6.2.** and **3.7.0.** Should work against other versions of Python as well, since the generator is pretty much the same in **2.7.12**. Enjoy!
9+
This script is able to predict python's `random` module random generated values.
10+
11+
Script was tested against **Python 3.5.2**, **3.6.2.** and **3.7.0.** Should work against other versions of Python as well, since the generator is pretty much the same in **2.7.12**. Enjoy!
1012

1113
## Installation
1214
To install randcrack, simply:

randcrack/randcrack.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def __init__(self):
77

88
def submit(self, num):
99
if self.state:
10-
print("Already got enough bits")
11-
return
10+
raise ValueError("Already got enough bits")
11+
1212
bits = self._to_bitarray(num)
1313

1414
assert (all([x == 0 or x == 1 for x in bits]))
@@ -20,8 +20,8 @@ def submit(self, num):
2020

2121
def _predict_32(self):
2222
if not self.state:
23-
print("Didn't recieve enough bits to predict")
24-
return 0
23+
raise ValueError("Didn't recieve enough bits to predict")
24+
2525
if self.counter >= 624:
2626
self._regen()
2727
self.counter += 1
@@ -30,8 +30,8 @@ def _predict_32(self):
3030

3131
def predict_getrandbits(self, k):
3232
if not self.state:
33-
print("Didn't recieve enough bits to predict")
34-
return 0
33+
raise ValueError("Didn't recieve enough bits to predict")
34+
3535
if k == 0:
3636
return 0
3737
words = (k - 1) // 32 + 1
@@ -208,7 +208,8 @@ def _regen(self):
208208

209209

210210
if __name__ == "__main__":
211-
import random, time
211+
import random
212+
import time
212213

213214
print("Testing random module cracker...")
214215

tests/test_randcrack.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import random
2+
import time
3+
4+
import pytest
5+
6+
from randcrack import RandCrack
7+
8+
9+
def test_submit_not_enough():
10+
random.seed(time.time())
11+
12+
cracker = RandCrack()
13+
14+
for i in range(623):
15+
cracker.submit(random.randint(0, 4294967294))
16+
17+
with pytest.raises(ValueError):
18+
cracker.predict_randint(0, 1)
19+
20+
21+
def test_submit_too_much():
22+
random.seed(time.time())
23+
24+
cracker = RandCrack()
25+
26+
for i in range(624):
27+
cracker.submit(random.randint(0, 4294967294))
28+
29+
with pytest.raises(ValueError):
30+
cracker.submit(random.randint(0, 4294967294))
31+
32+
33+
def test_predict_first_624():
34+
random.seed(time.time())
35+
36+
cracker = RandCrack()
37+
38+
for i in range(624):
39+
cracker.submit(random.randint(0, 4294967294))
40+
41+
assert sum([random.getrandbits(32) == cracker.predict_getrandbits(32) for _ in range(1000)]) >= 620
42+
43+
44+
def test_predict_first_1000_close():
45+
random.seed(time.time())
46+
47+
cracker = RandCrack()
48+
49+
for i in range(624):
50+
cracker.submit(random.randint(0, 4294967294))
51+
52+
assert sum([random.getrandbits(32) == cracker.predict_getrandbits(32) for _ in range(1000)]) >= 980

0 commit comments

Comments
 (0)