Commit ee2e061e authored by Julian Andres Klode's avatar Julian Andres Klode

Add more extensive test cases for cache remapping

This ensures cache remapping works better.
parent cf80a8da
......@@ -8,6 +8,8 @@
# notice and this notice are preserved.
"""Unit tests for verifying the correctness of check_dep, etc in apt_pkg."""
from __future__ import print_function
import glob
import logging
import os
......@@ -283,6 +285,81 @@ class TestAptCache(testcommon.TestCase):
new_pkg, old_ver)
new_depcache.set_candidate_ver(new_pkg, new_ver)
@staticmethod
def write_status_file(packages):
with open(apt_pkg.config["Dir::State::Status"], "w") as fobj:
for package in packages:
print("Package:", package, file=fobj)
print("Status: install ok installed", file=fobj)
print("Priority: optional", file=fobj)
print("Section: admin", file=fobj)
print("Installed-Size: 1", file=fobj)
print("Maintainer: X <x@x.invalid>", file=fobj)
print("Architecture: amd64", file=fobj)
print("Version: 1", file=fobj)
print("Description: blah", file=fobj)
print("", file=fobj)
def test_apt_cache_reopen_is_safe_out_of_bounds(self):
"""Check that out of bounds access is remapped correctly."""
with tempfile.NamedTemporaryFile() as status:
apt_pkg.config["Dir::Etc::SourceList"] = "/dev/null"
apt_pkg.config["Dir::Etc::SourceParts"] = "/dev/null"
apt_pkg.config["Dir::State::Status"] = status.name
apt_pkg.init_system()
self.write_status_file("abcdefghijklmnopqrstuvwxyz")
c = apt.Cache()
p = c["z"]
p_id = p.id
self.write_status_file("az")
apt_pkg.init_system()
c.open()
self.assertNotEqual(p.id, p_id)
self.assertLess(p.id, 2)
p.mark_delete()
self.assertEqual([p], c.get_changes())
def test_apt_cache_reopen_is_safe_out_of_bounds_no_match(self):
"""Check that installing gone package raises correct exception"""
with tempfile.NamedTemporaryFile() as status:
apt_pkg.config["Dir::Etc::SourceList"] = "/dev/null"
apt_pkg.config["Dir::Etc::SourceParts"] = "/dev/null"
apt_pkg.config["Dir::State::Status"] = status.name
apt_pkg.init_system()
self.write_status_file("abcdefghijklmnopqrstuvwxyz")
c = apt.Cache()
p = c["z"]
p_id = p.id
self.write_status_file("a")
apt_pkg.init_system()
c.open()
self.assertEqual(p.id, p_id) # Could not be remapped
self.assertRaises(apt_pkg.CacheMismatchError, p.mark_delete)
def test_apt_cache_reopen_is_safe_swap(self):
"""Check that swapping a and b does not mark the wrong package."""
with tempfile.NamedTemporaryFile() as status:
apt_pkg.config["Dir::Etc::SourceList"] = "/dev/null"
apt_pkg.config["Dir::Etc::SourceParts"] = "/dev/null"
apt_pkg.config["Dir::State::Status"] = status.name
apt_pkg.init_system()
self.write_status_file("abcdefghijklmnopqrstuvwxyz")
c = apt.Cache()
p = c["a"]
a_id = p.id
self.write_status_file("baz")
apt_pkg.init_system()
c.open()
# b now has the same id as a in the old cache
self.assertEqual(c["b"].id, a_id)
self.assertNotEqual(p.id, a_id)
# Marking a should still mark a, not b.
p.mark_delete()
self.assertEqual([p], c.get_changes())
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment