Ejemplo n.º 1
0
    def test_indexer_can_add_file(self, mocker):
        connection = mocker.Mock()

        c1 = mocker.Mock()
        connection.execute.return_value = c1
        c1.fetchone.return_value = (1,)

        cursor = mocker.MagicMock()
        connection.cursor.return_value = cursor

        hasher = mocker.Mock()
        hasher.get_hashes.return_value = ("hash1", "hash2")

        indexer = Indexer(connection, hasher)
        f = File('~/test.txt', 'test.txt')

        indexer.add_file(f)

        cursor.execute.assert_has_calls([
            mocker.call("INSERT OR IGNORE INTO hashes (sha1_hash, md5_hash) VALUES (?,?)", ("hash1", "hash2")),
            mocker.call("INSERT OR IGNORE INTO files (hash_id, full_path, filename, mimetype, size) VALUES (?,?,?,?,?)",
                        (1, '~/test.txt', 'test.txt', '', 0)),
        ])

        assert connection.commit.called is True
Ejemplo n.º 2
0
    def test_hashes_parts_of_big_files(self, mocker):
        if six.PY3:
            mocked_open = mocker.patch("builtins.open")
        else:
            mocked_open = mocker.patch("__builtin__.open")

        file_mock = mocker.MagicMock()
        mocked_open.return_value = file_mock

        mocked_stat = mocker.patch("os.stat")

        hasher = Hasher()

        hasher.BYTE_COUNT = 6
        mocked_stat.return_value = DummyStatResult(hasher.BYTE_COUNT + 1)

        # mock a file with "abcdefg" as content
        file_mock.__enter__.return_value = file_mock
        file_mock.read.side_effect = [b"abc", b"efg"]

        f = File('~/test.txt', 'test.txt')
        hashes = hasher.get_hashes(f)

        assert hashes == (sha1(b"abcefg").hexdigest(),
                          md5(b"abcefg").hexdigest())
Ejemplo n.º 3
0
    def test_delete_file_fails(self, mocker):
        connection = mocker.MagicMock()
        connection \
            .cursor.return_value \
            .execute.side_effect = sqlite3.Error()

        indexer = Indexer(connection, mocker.Mock())
        assert indexer.delete(File("/full/path.log", "path.log")) is False
Ejemplo n.º 4
0
def test_cleanup_old_files(mocker):
    runner = CliRunner()

    i = mocker.MagicMock()
    i.get_files.return_value = [
        File("./existing.txt", "existing.txt"),
        File("./non-existing.txt", "non-existing.txt")
    ]

    mocked_indexer = mocker.patch('hashdex.cli.Indexer')
    mocked_indexer.return_value = i

    with runner.isolated_filesystem():
        with open('./existing.txt', 'w') as df:
            df.write("a" * 10000)

        result = runner.invoke(cli, ['cleanup', '--index', './index.db'])

    assert 'Deleted ./non-existing.txt' in result.output
Ejemplo n.º 5
0
    def test_fetch_non_existing_file(self, mocker):
        connection = mocker.MagicMock()
        connection.cursor.return_value.execute.return_value.fetchone.return_value = None

        hasher = mocker.Mock()
        hasher.get_hashes.return_value = ("hash1", "hash2")

        f = File("x", "y")
        indexer = Indexer(connection, hasher)

        assert indexer.fetch_indexed_file(f) is None
Ejemplo n.º 6
0
    def test_in_index(self, mocker):
        connection = mocker.Mock()
        connection.execute.return_value.fetchone.return_value = ("x",)

        hasher = mocker.Mock()
        hasher.get_hashes.return_value = ("hash1", "hash2")

        f = File("x", "y")
        indexer = Indexer(connection, hasher)

        assert indexer.in_index(f) is True
Ejemplo n.º 7
0
    def test_get_files(self, mocker):
        connection = mocker.MagicMock()
        connection \
            .cursor.return_value \
            .execute.return_value \
            .fetchmany.side_effect = [
                [("/full/path.log", "path.log"), ("/full/path2.log", "path2.log")],
                None
            ]

        indexer = Indexer(connection, mocker.Mock())
        assert list(indexer.get_files()) == [File("/full/path.log", "path.log"), ("/full/path2.log", "path2.log")]
Ejemplo n.º 8
0
    def test_logging_of_db_exception_on_file_add(self, mocker):
        connection = mocker.Mock()
        connection.execute.return_value = mocker.Mock()

        cursor = mocker.MagicMock()
        cursor.execute.side_effect = sqlite3.Error()
        connection.cursor.return_value = cursor

        hasher = mocker.Mock()
        hasher.get_hashes.return_value = ("hash1", "hash2")

        indexer = Indexer(connection, hasher)
        f = File('~/test.txt', 'test.txt')

        indexer.add_file(f)

        assert connection.rollback.called is True
Ejemplo n.º 9
0
def test_check_without_rm(mocker):
    f = File("./x.txt", 'x.txt')
    i = mocker.MagicMock()
    i.fetch_indexed_file.return_value = f

    mocked_indexer = mocker.patch('hashdex.cli.Indexer')
    mocked_indexer.return_value = i

    runner = CliRunner()

    with runner.isolated_filesystem():
        os.mkdir("output")
        os.mkdir("input")
        with open('./input/x.txt', 'w') as df:
            df.write("a" * 10000)

        result = runner.invoke(cli, ['check', './input', '--index', './index.db'])

        assert os.path.exists('./input/x.txt') is True
        assert f.full_path in result.output
Ejemplo n.º 10
0
    def test_hasher_hashes_file_content(self, mocker):
        if six.PY3:
            mocked_open = mocker.patch("builtins.open")
        else:
            mocked_open = mocker.patch("__builtin__.open")

        file_mock = mocker.MagicMock()
        mocked_open.return_value = file_mock

        mocked_stat = mocker.patch("os.stat")

        mocked_stat.return_value = DummyStatResult(Hasher.BYTE_COUNT - 1)

        file_mock.__enter__.return_value = file_mock
        file_mock.read.return_value = b"content"

        f = File('~/test.txt', 'test.txt')
        hasher = Hasher()
        hashes = hasher.get_hashes(f)

        assert hashes == (sha1(b"content").hexdigest(), md5(b"content").hexdigest())
Ejemplo n.º 11
0
    def test_delete_file_from_index(self, mocker):
        connection = mocker.MagicMock()

        indexer = Indexer(connection, mocker.Mock())
        assert indexer.delete(File("/full/path.log", "path.log")) is True