storage/mutable: raise a specific error upon seeing bad magic, instead of using assert

This commit is contained in:
Brian Warner 2009-03-08 19:02:01 -07:00
parent 7e7167f2e5
commit 6d7319c588
3 changed files with 24 additions and 3 deletions

View File

@ -4,6 +4,8 @@ from allmydata.util import base32
class DataTooLargeError(Exception):
pass
class UnknownMutableContainerVersionError(Exception):
pass
def si_b2a(storageindex):

View File

@ -4,7 +4,8 @@ from allmydata.interfaces import BadWriteEnablerError
from allmydata.util import idlib, log
from allmydata.util.assertutil import precondition
from allmydata.storage.lease import LeaseInfo
from allmydata.storage.common import DataTooLargeError
from allmydata.storage.common import UnknownMutableContainerVersionError, \
DataTooLargeError
# the MutableShareFile is like the ShareFile, but used for mutable data. It
# has a different layout. See docs/mutable.txt for more details.
@ -56,7 +57,10 @@ class MutableShareFile:
write_enabler_nodeid, write_enabler,
data_length, extra_least_offset) = \
struct.unpack(">32s20s32sQQ", data)
assert magic == self.MAGIC
if magic != self.MAGIC:
msg = "sharefile %s had magic '%r' but we wanted '%r'" % \
(filename, magic, self.MAGIC)
raise UnknownMutableContainerVersionError(msg)
self.parent = parent # for logging
def log(self, *args, **kwargs):

View File

@ -12,7 +12,8 @@ from allmydata.util import fileutil, hashutil, base32, pollmixin
from allmydata.storage.server import StorageServer
from allmydata.storage.mutable import MutableShareFile
from allmydata.storage.immutable import BucketWriter, BucketReader
from allmydata.storage.common import DataTooLargeError, storage_index_to_dir
from allmydata.storage.common import DataTooLargeError, storage_index_to_dir, \
UnknownMutableContainerVersionError
from allmydata.storage.lease import LeaseInfo
from allmydata.storage.crawler import BucketCountingCrawler
from allmydata.storage.expirer import LeaseCheckingCrawler
@ -723,6 +724,20 @@ class MutableServer(unittest.TestCase):
self.failUnless(isinstance(readv_data, dict))
self.failUnlessEqual(len(readv_data), 0)
def test_bad_magic(self):
ss = self.create("test_bad_magic")
self.allocate(ss, "si1", "we1", self._lease_secret.next(), set([0]), 10)
fn = os.path.join(ss.sharedir, storage_index_to_dir("si1"), "0")
f = open(fn, "rb+")
f.seek(0)
f.write("BAD MAGIC")
f.close()
read = ss.remote_slot_readv
e = self.failUnlessRaises(UnknownMutableContainerVersionError,
read, "si1", [0], [(0,10)])
self.failUnless(" had magic " in str(e), e)
self.failUnless(" but we wanted " in str(e), e)
def test_container_size(self):
ss = self.create("test_container_size")
self.allocate(ss, "si1", "we1", self._lease_secret.next(),