def test_save_split(self): p = Path("./test_dir") test_object = libweakrandom.Weakrandom(40, p, self.pass_prng) fail_size_str1 = "01" fail_size_str2 = "01010" fail_bin_str = "020002" fail_value_str = "0asdösadfkj" pass_str = "000111000111" with self.assertRaises(ValueError): test_object.save_split(fail_size_str1) with self.assertRaises(ValueError): test_object.save_split(fail_size_str2) with self.assertRaises(ValueError): test_object.save_split(fail_bin_str) with self.assertRaises(ValueError): test_object.save_split(fail_value_str) self.assertEqual(test_object.save_split(pass_str), False) files_p = [str(x.name) for x in p.iterdir()] self.assertTrue("train_inputs_test_prng.bin" in files_p) self.assertTrue("train_inputs_test_prng.bin" in files_p)
def javarand(self, num_of_mbytes: int, data: Path) -> None: import subprocess form = wr.Weakrandom(num_of_mbytes, data, "javarand") bits = str( subprocess.check_output( ['java', 'RandomInteger', str(form.get_full_size())]))[2:-1] form.save_split(bits)
def gauss(self, num_of_mbytes: int, data: Path) -> None: """ Good PRNG with gaussian distribution """ form = wr.Weakrandom(num_of_mbytes, data, "gauss") g = intwraprand(random.gauss) while (not form.save_split( self._pyrand(g.randint, form.get_round_size()))): pass
def sysrand(self, num_of_mbytes: int, data: Path) -> None: """ Linux this function use urand (very good random numbers) """ form = wr.Weakrandom(num_of_mbytes, data, "sysrand") s = random.SystemRandom(0) while (not form.save_split( self._pyrand(s.randint, form.get_round_size()))): pass
def crand(self, num_of_mbytes: int, data: Path) -> None: """" c stdlib rand() with srand(0) """ import subprocess form = wr.Weakrandom(num_of_mbytes, data, "crand") bits = str( subprocess.check_output(['./crand', str(form.get_full_size())]))[2:-3] form.save_split(bits)
def mersen(self, num_of_mbytes: int, data: Path) -> None: """ Generates random bits with function randint() Python uses the Mersenne Twister it is not a cryptographical PRNG. """ form = wr.Weakrandom(num_of_mbytes, data, "mersen") r = random while (not form.save_split( self._pyrand(r.randint, form.get_round_size()))): pass
def test_init(self): with self.assertRaises(ValueError): libweakrandom.Weakrandom(1,self.pass_path, self.pass_prng) with self.assertRaises(ValueError): libweakrandom.Weakrandom(-1,self.pass_path, self.pass_prng) with self.assertRaises(ValueError): libweakrandom.Weakrandom(1,self.fail_path, self.pass_prng) with self.assertRaises(AttributeError): libweakrandom.Weakrandom(40,'fail', self.pass_prng) with self.assertRaises(TypeError): libweakrandom.Weakrandom('fail', self.pass_path, self.pass_prng) with self.assertRaises(PermissionError): libweakrandom.Weakrandom(40,self.fail_path, self.pass_prng) p = Path("./test_dir") test_object = libweakrandom.Weakrandom(40,p, self.pass_prng) self.assertTrue(test_object.data_dir != None) self.assertTrue(test_object._rounds != 0) self.assertEqual(test_object._round_size % (test_object._targ_bytes + test_object._src_bytes),0) self.assertTrue(p.is_dir())
def rc4rand(self, num_of_mbytes: int, data: Path) -> None: form = wr.Weakrandom(num_of_mbytes, data, "rc4rand") a = rc4.arc4random() while (not form.save_split(a.randbin(form.get_round_size()))): pass
def betavariate(self, num_of_mbytes: int, data: Path) -> None: form = wr.Weakrandom(num_of_mbytes, data, "betavariate") g = intwraprand(random.betavariate) while (not form.save_split( self._pyrand(g.randint, form.get_round_size()))): pass
def triangular(self, num_of_mbytes: int, data: Path) -> None: form = wr.Weakrandom(num_of_mbytes, data, "triangular") g = intwraprand(random.triangular) while (not form.save_split( self._pyrand(g.randint, form.get_round_size()))): pass
def test_get_full_size(self): p = Path("./test_dir") test_object = libweakrandom.Weakrandom(40, p, self.pass_prng) self.assertTrue(test_object.get_full_size() >= (3*(test_object.train_number_of_lines + test_object.eval_number_of_lines + test_object.test_number_of_lines)))
def test_get_round_size(self): p = Path("./test_dir") test_object = libweakrandom.Weakrandom(40, p, self.pass_prng) self.assertTrue(test_object.get_round_size() > 2)