Commit 8fbd7e3f authored by Chris Lamb's avatar Chris Lamb 👀
Browse files

Add support for comparing Berkeley DB files. This is currently incomplete...

Add support for comparing Berkeley DB files. This is currently incomplete because the Berkeley DB libraries do not return the same uid/hash reliably (it returns "random" memory contents) so we must strip those from the human-readable output. (Closes: #890528)
parent c1e29ac2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ Build-Depends:
 binutils-multiarch <!nocheck>,
 caca-utils <!nocheck>,
 colord <!nocheck>,
 db-util <!nocheck>,
 debhelper (>= 11~),
 default-jdk-headless <!nocheck> | default-jdk <!nocheck>,
 dh-python (>= 2.20160818~),
+1 −0
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ class ComparatorManager(object):
        ('dtb.DeviceTreeFile',),
        ('ogg.OggFile',),
        ('xsb.XsbFile',),
        ('berkeley_db.BerkeleyDBFile',),
    )

    _singleton = {}
+53 −0
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2018 Chris Lamb <lamby@debian.org>
#
# diffoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffoscope is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.

import re

from diffoscope.tools import tool_required
from diffoscope.difference import Difference

from .utils.file import File
from .utils.command import Command


class DbDump(Command):
    @tool_required('db_dump')
    def cmdline(self):
        return ('db_dump', '-d', 'a', self.path)

    def filter(self, line):
        l = line.decode('utf-8')
        # We must strip some fields as libdb itself does not repeatedly read
        # its own metadata reliably, even on the same file.
        for x in ('h_hash: ', 'bt_compare: ', '\tuid: '):
            if l.startswith(x):
                return b''
        return line


class BerkeleyDBFile(File):
    FILE_TYPE_RE = re.compile(r'^Berkeley DB ')

    def compare_details(self, other, source=None):
        return [Difference.from_command(
            DbDump,
            self.path,
            other.path,
            source="Berkeley DB file",
        )]
+3 −0
Original line number Diff line number Diff line
@@ -24,6 +24,9 @@ EXTERNAL_TOOLS = {
    'apktool': {
        'debian': 'apktool',
    },
    'db_dump': {
        'debian': 'db-util',
    },
    'bsdtar': {
        'debian': 'libarchive-tools',
        'arch': 'libarchive',
+54 −0
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2018 Chris Lamb <lamby@debian.org>
#
# diffoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffoscope is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.

import pytest

from diffoscope.comparators.berkeley_db import BerkeleyDBFile

from ..utils.data import load_fixture, get_data
from ..utils.tools import skip_unless_tools_exist
from ..utils.nonexisting import assert_non_existing

db1 = load_fixture('test1.db')
db2 = load_fixture('test2.db')


def test_identification(db1):
    assert isinstance(db1, BerkeleyDBFile)


@pytest.fixture
def differences(db1, db2):
    return db1.compare(db2).details


def test_no_differences(db1):
    difference = db1.compare(db1)
    assert difference is None


@skip_unless_tools_exist('db_dump')
def test_diff(differences):
    expected_diff = get_data('berkeley_db_expected_diff')
    assert differences[0].unified_diff == expected_diff


@skip_unless_tools_exist('db_dump')
def test_compare_non_existing(monkeypatch, db1):
    assert_non_existing(monkeypatch, db1, has_null_source=False)
Loading