Beispiel #1
0
    def testIterate(self):
        """Test writing several records and then reading them back"""
        self.make_backup_dirs()
        rp1 = self.ea_test1_rpath.append('e1')
        rp2 = self.ea_test1_rpath.append('e2')
        rp3 = self.ea_test1_rpath.append('e3')

        # Now write records corresponding to above rps into file
        Globals.rbdir = tempdir
        man = metadata.PatchDiffMan()
        writer = man.get_ea_writer('snapshot', 10000)
        for rp in [self.ea_test1_rpath, rp1, rp2, rp3]:
            ea = ExtendedAttributes(rp.index)
            ea.read_from_rp(rp)
            writer.write_object(ea)
        writer.close()

        # Read back records and compare
        ea_iter = man.get_eas_at_time(10000, None)
        assert ea_iter, "No extended_attributes.<time> file found"
        sample_ea_reread = next(ea_iter)
        # we ignore SELinux extended attributes for comparaison
        if sample_ea_reread.attr_dict:
            sample_ea_reread.attr_dict.pop(b'security.selinux', None)
        assert sample_ea_reread == self.sample_ea, "Re-read EAs %s are different from %s" % \
            (sample_ea_reread.attr_dict, self.sample_ea.attr_dict)
        ea1_reread = next(ea_iter)
        if ea1_reread.attr_dict:
            ea1_reread.attr_dict.pop(b'security.selinux', None)
        assert ea1_reread == self.ea1, "Re-read EAs %s are different from %s" % \
            (ea1_reread.attr_dict, self.ea1.attr_dict)
        ea2_reread = next(ea_iter)
        if ea2_reread.attr_dict:
            ea2_reread.attr_dict.pop(b'security.selinux', None)
        assert ea2_reread == self.ea2, "Re-read EAs %s are different from %s" % \
            (ea2_reread.attr_dict, self.ea2.attr_dict)
        ea3_reread = next(ea_iter)
        if ea3_reread.attr_dict:
            ea3_reread.attr_dict.pop(b'security.selinux', None)
        assert ea3_reread == self.ea3, "Re-read EAs %s are different from %s" % \
            (ea3_reread.attr_dict, self.ea3.attr_dict)
        try:
            next(ea_iter)
        except StopIteration:
            pass
        else:
            assert 0, "Expected end to iterator"
    def testIterate(self):
        """Test writing several records and then reading them back"""
        self.make_backup_dirs()
        rp1 = self.ea_test1_rpath.append('e1')
        rp2 = self.ea_test1_rpath.append('e2')
        rp3 = self.ea_test1_rpath.append('e3')

        # Now write records corresponding to above rps into file
        Globals.rbdir = tempdir
        man = metadata.PatchDiffMan()
        writer = man._get_ea_writer('snapshot', 10000)
        for rp in [self.ea_test1_rpath, rp1, rp2, rp3]:
            ea = ExtendedAttributes(rp.index)
            ea.read_from_rp(rp)
            writer.write_object(ea)
        writer.close()

        # Read back records and compare
        ea_iter = man._get_eas_at_time(10000, None)
        self.assertTrue(ea_iter, "No extended_attributes.<time> file found")
        sample_ea_reread = next(ea_iter)
        # we ignore SELinux extended attributes for comparison
        if sample_ea_reread.attr_dict:
            sample_ea_reread.attr_dict.pop(b'security.selinux', None)
        # Check if re-read EAs are different from sample ones
        self.assertEqual(sample_ea_reread, self.sample_ea)
        ea1_reread = next(ea_iter)
        if ea1_reread.attr_dict:
            ea1_reread.attr_dict.pop(b'security.selinux', None)
        self.assertEqual(ea1_reread, self.ea1)
        ea2_reread = next(ea_iter)
        if ea2_reread.attr_dict:
            ea2_reread.attr_dict.pop(b'security.selinux', None)
        self.assertEqual(ea2_reread, self.ea2)
        ea3_reread = next(ea_iter)
        if ea3_reread.attr_dict:
            ea3_reread.attr_dict.pop(b'security.selinux', None)
        self.assertEqual(ea3_reread, self.ea3)
        with self.assertRaises(StopIteration,
                               msg="Too many elements in iterator"):
            next(ea_iter)
Beispiel #3
0
    def testBasic(self):
        """Test basic writing and reading of extended attributes"""
        self.make_temp_out_dirs()
        new_ea = ExtendedAttributes(())
        new_ea.read_from_rp(tempdir)
        # we ignore SELinux extended attributes for comparaison
        if new_ea.attr_dict:
            new_ea.attr_dict.pop(b'security.selinux', None)
        assert not new_ea.attr_dict, "The attributes of %s should have been empty: %s" % (
            tempdir, new_ea.attr_dict)
        assert not new_ea == self.sample_ea
        assert new_ea != self.sample_ea
        assert new_ea == self.empty_ea

        self.sample_ea.write_to_rp(tempdir)
        new_ea.read_from_rp(tempdir)
        if new_ea.attr_dict:
            new_ea.attr_dict.pop(b'security.selinux', None)
        assert new_ea.attr_dict == self.sample_ea.attr_dict, \
            (new_ea.attr_dict, self.sample_ea.attr_dict)
        assert new_ea == self.sample_ea
    def testBasic(self):
        """Test basic writing and reading of extended attributes"""
        self.make_temp_out_dirs()
        new_ea = ExtendedAttributes(())
        new_ea.read_from_rp(tempdir)
        # we ignore SELinux extended attributes for comparison
        if new_ea.attr_dict:
            new_ea.attr_dict.pop(b'security.selinux', None)
        self.assertFalse(
            new_ea.attr_dict,
            "The attributes of {dir} should have been empty: {attr}".format(
                dir=tempdir, attr=new_ea.attr_dict))
        self.assertNotEqual(new_ea, self.sample_ea)
        self.assertEqual(new_ea, self.empty_ea)

        self.sample_ea.write_to_rp(tempdir)
        new_ea.read_from_rp(tempdir)
        if new_ea.attr_dict:
            new_ea.attr_dict.pop(b'security.selinux', None)
        self.assertEqual(new_ea.attr_dict, self.sample_ea.attr_dict)
        self.assertEqual(new_ea, self.sample_ea)
Beispiel #5
0
class EATest(unittest.TestCase):
    """Test extended attributes"""
    sample_ea = ExtendedAttributes(
        (), {
            b'user.empty':
            b'',
            b'user.not_empty':
            b'foobar',
            b'user.third':
            b'hello',
            b'user.binary':
            bytes((0, 1, 2, 140)) + b'/="',
            b'user.multiline':
            b"""This is a fairly long extended attribute.
                Encoding it will require several lines of
                base64.""" + bytes((177, ) * 300)
        })
    empty_ea = ExtendedAttributes(())
    ea1 = ExtendedAttributes(('e1', ), sample_ea.attr_dict.copy())
    ea1.delete(b'user.not_empty')
    ea2 = ExtendedAttributes(('e2', ), sample_ea.attr_dict.copy())
    ea2.set(b'user.third', b'Another random attribute')
    ea3 = ExtendedAttributes(('e3', ))
    ea4 = ExtendedAttributes(('e4', ),
                             {b'user.deleted': b'File to be deleted'})
    ea_test1_dir = os.path.join(abs_test_dir, b'ea_test1')
    ea_test1_rpath = rpath.RPath(Globals.local_connection, ea_test1_dir)
    ea_test2_dir = os.path.join(abs_test_dir, b'ea_test2')
    ea_test2_rpath = rpath.RPath(Globals.local_connection, ea_test2_dir)
    ea_empty_dir = os.path.join(abs_test_dir, b'ea_empty')
    ea_empty_rpath = rpath.RPath(Globals.local_connection, ea_empty_dir)

    def make_temp_out_dirs(self):
        """Make temp output and restore directories empty"""
        tempdir.setdata()  # in case the file changed in-between
        if tempdir.lstat():
            tempdir.delete()
        tempdir.mkdir()
        restore_dir.setdata()
        if restore_dir.lstat():
            restore_dir.delete()

    def testBasic(self):
        """Test basic writing and reading of extended attributes"""
        self.make_temp_out_dirs()
        new_ea = ExtendedAttributes(())
        new_ea.read_from_rp(tempdir)
        # we ignore SELinux extended attributes for comparaison
        if new_ea.attr_dict:
            new_ea.attr_dict.pop(b'security.selinux', None)
        assert not new_ea.attr_dict, "The attributes of %s should have been empty: %s" % (
            tempdir, new_ea.attr_dict)
        assert not new_ea == self.sample_ea
        assert new_ea != self.sample_ea
        assert new_ea == self.empty_ea

        self.sample_ea.write_to_rp(tempdir)
        new_ea.read_from_rp(tempdir)
        if new_ea.attr_dict:
            new_ea.attr_dict.pop(b'security.selinux', None)
        assert new_ea.attr_dict == self.sample_ea.attr_dict, \
            (new_ea.attr_dict, self.sample_ea.attr_dict)
        assert new_ea == self.sample_ea

    def testRecord(self):
        """Test writing a record and reading it back"""
        record = EA2Record(self.sample_ea)
        new_ea = Record2EA(record)
        if not new_ea == self.sample_ea:
            new_list = list(new_ea.attr_dict.keys())
            sample_list = list(self.sample_ea.attr_dict.keys())
            new_list.sort()
            sample_list.sort()
            assert new_list == sample_list, (new_list, sample_list)
            for name in new_list:
                assert self.sample_ea.get(name) == new_ea.get(name), \
                    (self.sample_ea.get(name), new_ea.get(name))
            assert self.sample_ea.index == new_ea.index, \
                (self.sample_ea.index, new_ea.index)
            assert 0, "We shouldn't have gotten this far"

    def testExtractor(self):
        """Test seeking inside a record list"""
        record_list = """# file: 0foo
user.multiline=0sVGhpcyBpcyBhIGZhaXJseSBsb25nIGV4dGVuZGVkIGF0dHJpYnV0ZS4KCQkJIEVuY29kaW5nIGl0IHdpbGwgcmVxdWlyZSBzZXZlcmFsIGxpbmVzIG9mCg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=0saGVsbG8=
user.binary=0sAAECjC89Ig==
user.empty
# file: 2foo/\\012
user.empty
"""
        extractor = EAExtractor(io.BytesIO(os.fsencode(record_list)))
        ea_iter = extractor.iterate_starting_with(())
        first = next(ea_iter)
        assert first.index == (b'0foo', ), first
        second = next(ea_iter)
        assert second.index == (b'1foo', b'bar', b'baz'), second
        third = next(ea_iter)  # Test quoted filenames
        assert third.index == (b'2foo', b'\n'), third.index
        try:
            next(ea_iter)
        except StopIteration:
            pass
        else:
            assert 0, "Too many elements in iterator"

        extractor = EAExtractor(io.BytesIO(os.fsencode(record_list)))
        ea_iter = extractor.iterate_starting_with((b'1foo', b'bar'))
        assert next(ea_iter).index == (b'1foo', b'bar', b'baz')
        try:
            next(ea_iter)
        except StopIteration:
            pass
        else:
            assert 0, "Too many elements in iterator"

    def make_backup_dirs(self):
        """Create testfiles/ea_test[12] directories

        Goal is to set range of extended attributes, to give good test
        to extended attribute code.

        """
        if self.ea_test1_rpath.lstat():
            self.ea_test1_rpath.delete()
        if self.ea_test2_rpath.lstat():
            self.ea_test2_rpath.delete()
        self.ea_test1_rpath.mkdir()
        rp1_1 = self.ea_test1_rpath.append('e1')
        rp1_2 = self.ea_test1_rpath.append('e2')
        rp1_3 = self.ea_test1_rpath.append('e3')
        rp1_4 = self.ea_test1_rpath.append('e4')
        list(map(rpath.RPath.touch, [rp1_1, rp1_2, rp1_3, rp1_4]))
        self.sample_ea.write_to_rp(self.ea_test1_rpath)
        self.ea1.write_to_rp(rp1_1)
        self.ea2.write_to_rp(rp1_2)
        self.ea4.write_to_rp(rp1_4)

        self.ea_test2_rpath.mkdir()
        rp2_1 = self.ea_test2_rpath.append('e1')
        rp2_2 = self.ea_test2_rpath.append('e2')
        rp2_3 = self.ea_test2_rpath.append('e3')
        list(map(rpath.RPath.touch, [rp2_1, rp2_2, rp2_3]))
        self.ea3.write_to_rp(self.ea_test2_rpath)
        self.sample_ea.write_to_rp(rp2_1)
        self.ea1.write_to_rp(rp2_2)
        self.ea2.write_to_rp(rp2_3)

        # just create an empty dir for tests
        if self.ea_empty_rpath.lstat():
            self.ea_empty_rpath.delete()
        self.ea_empty_rpath.mkdir()

    def testIterate(self):
        """Test writing several records and then reading them back"""
        self.make_backup_dirs()
        rp1 = self.ea_test1_rpath.append('e1')
        rp2 = self.ea_test1_rpath.append('e2')
        rp3 = self.ea_test1_rpath.append('e3')

        # Now write records corresponding to above rps into file
        Globals.rbdir = tempdir
        man = metadata.PatchDiffMan()
        writer = man.get_ea_writer('snapshot', 10000)
        for rp in [self.ea_test1_rpath, rp1, rp2, rp3]:
            ea = ExtendedAttributes(rp.index)
            ea.read_from_rp(rp)
            writer.write_object(ea)
        writer.close()

        # Read back records and compare
        ea_iter = man.get_eas_at_time(10000, None)
        assert ea_iter, "No extended_attributes.<time> file found"
        sample_ea_reread = next(ea_iter)
        # we ignore SELinux extended attributes for comparaison
        if sample_ea_reread.attr_dict:
            sample_ea_reread.attr_dict.pop(b'security.selinux', None)
        assert sample_ea_reread == self.sample_ea, "Re-read EAs %s are different from %s" % \
            (sample_ea_reread.attr_dict, self.sample_ea.attr_dict)
        ea1_reread = next(ea_iter)
        if ea1_reread.attr_dict:
            ea1_reread.attr_dict.pop(b'security.selinux', None)
        assert ea1_reread == self.ea1, "Re-read EAs %s are different from %s" % \
            (ea1_reread.attr_dict, self.ea1.attr_dict)
        ea2_reread = next(ea_iter)
        if ea2_reread.attr_dict:
            ea2_reread.attr_dict.pop(b'security.selinux', None)
        assert ea2_reread == self.ea2, "Re-read EAs %s are different from %s" % \
            (ea2_reread.attr_dict, self.ea2.attr_dict)
        ea3_reread = next(ea_iter)
        if ea3_reread.attr_dict:
            ea3_reread.attr_dict.pop(b'security.selinux', None)
        assert ea3_reread == self.ea3, "Re-read EAs %s are different from %s" % \
            (ea3_reread.attr_dict, self.ea3.attr_dict)
        try:
            next(ea_iter)
        except StopIteration:
            pass
        else:
            assert 0, "Expected end to iterator"

    def testSeriesLocal(self):
        """Test backing up and restoring directories with EAs locally"""
        self.make_backup_dirs()
        dirlist = [
            self.ea_test1_dir, self.ea_empty_dir, self.ea_test2_dir,
            self.ea_test1_dir
        ]
        BackupRestoreSeries(1, 1, dirlist, compare_eas=1)

    def testSeriesRemote(self):
        """Test backing up, restoring directories with EA remotely"""
        self.make_backup_dirs()
        dirlist = [
            self.ea_test1_dir, self.ea_test2_dir, self.ea_empty_dir,
            self.ea_test1_dir
        ]
        BackupRestoreSeries(None, None, dirlist, compare_eas=1)

    def test_final_local(self):
        """Test backing up and restoring using 'rdiff-backup' script"""
        self.make_backup_dirs()
        self.make_temp_out_dirs()
        rdiff_backup(1,
                     1,
                     self.ea_test1_rpath.path,
                     tempdir.path,
                     current_time=10000)
        assert CompareRecursive(self.ea_test1_rpath, tempdir, compare_eas=1)

        rdiff_backup(1,
                     1,
                     self.ea_test2_rpath.path,
                     tempdir.path,
                     current_time=20000)
        assert CompareRecursive(self.ea_test2_rpath, tempdir, compare_eas=1)

        rdiff_backup(1,
                     1,
                     tempdir.path,
                     restore_dir.path,
                     extra_options=b'-r 10000')
        assert CompareRecursive(self.ea_test1_rpath,
                                restore_dir,
                                compare_eas=1)