Ejemplo n.º 1
0
    def test_property_filepath(self, mock_tree, mock_challenges):
        """Test filepath property."""

        # filepath is not a str
        with pytest.raises(ValueError):
            ShardManager(1, 1)

        # filepath does not exist
        with pytest.raises(ValueError):
            ShardManager('/dev/nowhere', 1)

        # file path is a directory
        tmpdir = tempfile.mkdtemp()
        try:
            with pytest.raises(ValueError):
                ShardManager(tmpdir, 1)
        finally:
            shutil.rmtree(tmpdir)

        # file path is a text file
        content = '1234567890'
        self._assert_shard_manager(content, 'w+t', mock_tree, mock_challenges)

        content = b'1234567890'
        self._assert_shard_manager(content, 'w+b', mock_tree, mock_challenges)
Ejemplo n.º 2
0
    def test_get_optimal_shard_number(self):
        """Test ShardManager.get_optimal_shard_number()."""
        shard_manager = ShardManager(__file__)

        for file_size, expected_shard_size, expected_shard_count in [
                # (file size, shard size, shard count)
            (43 * self.GB, 4 * self.GB, 11)
        ]:
            shard_manager._filesize = file_size
            shard_count = \
                shard_manager.get_optimal_shard_number()

            assert expected_shard_count == shard_count
Ejemplo n.º 3
0
    def _assert_shard_manager(self, content, mode, mock_tree, mock_challenges):
        tmpfile = tempfile.NamedTemporaryFile(mode, delete=False)
        try:
            tmpfile.write(content)
            tmpfile.close()

            size = 10
            nchallenges = 20
            sm = ShardManager(tmpfile.name, size, nchallenges)

            assert sm.filepath == tmpfile.name
            assert sm.shard_size == size
            assert len(sm.shards) > 0
            assert len(sm.shards) == sm.index

        finally:
            tmpfile.close()
            os.remove(tmpfile.name)

        mock_challenges.assert_called_once_with(nchallenges)
        if isinstance(content, six.binary_type):
            mock_tree.assert_called_once_with(mock_challenges.return_value,
                                              content)
        else:
            mock_tree.assert_called_once_with(mock_challenges.return_value,
                                              bytes(content.encode('utf-8')))

        mock_tree.reset_mock()
        mock_challenges.reset_mock()
Ejemplo n.º 4
0
    def test_sha256_text(self, mock_hashlib):
        """Test ShardManager._sha256"""
        test_data = 'ab'

        output = ShardManager._ripemd160(test_data)

        mock_hashlib.new.assert_called_with('ripemd160', test_data)
        mock_hashlib.new.return_value.digest.assert_called_once_with()
Ejemplo n.º 5
0
    def test_sha256_text(self, mock_hashlib):
        """Test ShardManager._sha256"""
        test_data = 'ab'

        output = ShardManager._ripemd160(test_data)

        mock_hashlib.new.assert_called_with('ripemd160', test_data)
        mock_hashlib.new.return_value.digest.assert_called_once_with()
Ejemplo n.º 6
0
    def test_ripemd160_binary(self, mock_hashlib):
        """Test ShardManager._ripemd160"""
        test_data = b'ab'

        output = ShardManager._ripemd160(test_data)

        assert output is not None

        mock_hashlib.new.assert_called_with('ripemd160', test_data)
        mock_hashlib.new.return_value.digest.assert_called_once_with()
Ejemplo n.º 7
0
    def test_ripemd160_binary(self, mock_hashlib):
        """Test ShardManager._ripemd160"""
        test_data = b'ab'

        output = ShardManager._ripemd160(test_data)

        assert output is not None

        mock_hashlib.new.assert_called_with('ripemd160', test_data)
        mock_hashlib.new.return_value.digest.assert_called_once_with()
Ejemplo n.º 8
0
    def _assert_shard_manager(self, content, mode, mock_tree, mock_challenges):
        with tempfile.NamedTemporaryFile(mode, delete=False) as tmp_file:
            tmp_file.write(content)
            tmp_file.flush()

            nchallenges = 2
            sm = ShardManager(tmp_file.name, nchallenges=nchallenges)

            assert sm.filepath == tmp_file.name
            assert len(sm.shards) > 0

        mock_challenges.assert_called_once_with(nchallenges)
        if isinstance(content, six.binary_type):
            mock_tree.assert_called_once_with(mock_challenges.return_value,
                                              content)
        else:
            mock_tree.assert_called_once_with(mock_challenges.return_value,
                                              bytes(content.encode('utf-8')))

        mock_tree.reset_mock()
        mock_challenges.reset_mock()
Ejemplo n.º 9
0
    def test_hash(self, mock_binascii, mock_bytes, mock_ripemd160,
                  mock_sha256):
        """Test ShardManager.hash()"""

        hash_output = mock.MagicMock()
        test_data = mock.MagicMock()
        test_data.encode.return_value = test_data

        mock_ripemd160.return_value = hash_output
        mock_sha256.return_value = hash_output

        mock_bytes.return_value = test_data
        mock_binascii.hexlify.return_value = test_data

        output = ShardManager.hash(test_data)

        assert output is not None

        test_data.encode.assert_called_with('utf-8')
        mock_bytes.assert_called_with(test_data)
        mock_sha256.assert_called_with(test_data)
        mock_ripemd160.assert_called_with(hash_output)
        mock_binascii.hexlify.assert_called_with(hash_output)
        test_data.decode.assert_called_with('utf-8')
Ejemplo n.º 10
0
    def test_hash(self,
                  mock_binascii, mock_bytes, mock_ripemd160, mock_sha256):
        """Test ShardManager.hash()"""

        hash_output = mock.MagicMock()
        test_data = mock.MagicMock()
        test_data.encode.return_value = test_data

        mock_ripemd160.return_value = hash_output
        mock_sha256.return_value = hash_output

        mock_bytes.return_value = test_data
        mock_binascii.hexlify.return_value = test_data

        output = ShardManager.hash(test_data)

        assert output is not None

        test_data.encode.assert_called_with('utf-8')
        mock_bytes.assert_called_with(test_data)
        mock_sha256.assert_called_with(test_data)
        mock_ripemd160.assert_called_with(hash_output)
        mock_binascii.hexlify.assert_called_with(hash_output)
        test_data.decode.assert_called_with('utf-8')
Ejemplo n.º 11
0
    def _assert_init(self, args, kwargs, file_size):

        shard_manager = ShardManager(*args, **kwargs)

        assert shard_manager.filepath == args[0]

        if 'tmp_path' in kwargs:
            assert shard_manager.tmp_path == kwargs['tmp_path']

        elif platform == 'linux' or platform == 'linux2':
            assert shard_manager.tmp_path == '/tmp'

        elif platform == 'darwin':
            assert shard_manager.tmp_path == '/tmp'

        elif platform == 'win32':
            assert shard_manager.tmp_path == 'C://Windows/temp'

        if 'suffix' in kwargs:
            assert shard_manager.suffix == kwargs['suffix']
        else:
            assert shard_manager.suffix == ''

        assert shard_manager.filesize == file_size