Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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())
Ejemplo n.º 8
0
 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
Ejemplo n.º 9
0
 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
Ejemplo n.º 10
0
 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
Ejemplo n.º 11
0
    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)))
Ejemplo n.º 12
0
    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)