Changeset d8ba8c2 in trunk


Ignore:
Timestamp:
2009-07-15T06:45:10Z (16 years ago)
Author:
Brian Warner <warner@…>
Branches:
master
Children:
7950fc0
Parents:
760bab7
Message:

Allow tests to pass with -OO by turning some AssertionErrors? (the ones that
we actually exercise during tests) into more specific exceptions, so they
don't get optimized away. The best rule to follow is probably this: if an
exception is worth testing, then it's part of the API, and AssertionError?
should never be part of the API. Closes #749.

Location:
src/allmydata
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/client.py

    r760bab7 rd8ba8c2  
    2828from allmydata.history import History
    2929from allmydata.interfaces import IURI, INewDirectoryURI, IStatsProducer, \
    30      IReadonlyNewDirectoryURI, IFileURI, IMutableFileURI, RIStubClient
     30     IReadonlyNewDirectoryURI, IFileURI, IMutableFileURI, RIStubClient, \
     31     UnhandledCapTypeError
    3132
    3233KiB=1024
     
    430431                else:
    431432                    node = FileNode(u, self, self.download_cache_dirman) # CHK
     433            elif IMutableFileURI.providedBy(u):
     434                node = MutableFileNode(self).init_from_uri(u)
    432435            else:
    433                 assert IMutableFileURI.providedBy(u), u
    434                 node = MutableFileNode(self).init_from_uri(u)
     436                raise UnhandledCapTypeError("cap is recognized, but has no Node")
    435437            self._node_cache[u_s] = node  # note: WeakValueDictionary
    436438        return self._node_cache[u_s]
  • TabularUnified src/allmydata/interfaces.py

    r760bab7 rd8ba8c2  
    482482    """UnknownNodes (using filecaps from the future that we don't understand)
    483483    cannot yet be copied safely, so I refuse to copy them."""
     484
     485class UnhandledCapTypeError(Exception):
     486    """I recognize the cap/URI, but I cannot create an IFilesystemNode for
     487    it."""
    484488
    485489class IFilesystemNode(Interface):
  • TabularUnified src/allmydata/mutable/common.py

    r760bab7 rd8ba8c2  
    5454                                                               self.reason)
    5555
    56 
     56class UnknownVersionError(Exception):
     57    """The share we received was of a version we don't recognize."""
    5758
    5859class ResponseCache:
  • TabularUnified src/allmydata/mutable/layout.py

    r760bab7 rd8ba8c2  
    11
    22import struct
    3 from common import NeedMoreDataError
     3from common import NeedMoreDataError, UnknownVersionError
    44
    55PREFIX = ">BQ32s16s" # each version has a different prefix
     
    3535     o) = unpack_header(data)
    3636
    37     assert version == 0
     37    if version != 0:
     38        raise UnknownVersionError("got mutable share version %d, but I only understand version 0" % version)
     39
    3840    if len(data) < o['share_hash_chain']:
    3941        raise NeedMoreDataError(o['share_hash_chain'],
     
    6163     o['EOF']) = struct.unpack(HEADER, data[:HEADER_LENGTH])
    6264
    63     assert version == 0
     65    if version != 0:
     66        raise UnknownVersionError("got mutable share version %d, but I only understand version 0" % version)
     67
    6468    if len(data) < o['EOF']:
    6569        raise NeedMoreDataError(o['EOF'],
     
    133137    cs_len = struct.calcsize(PREFIX)
    134138    version, seqnum, root_hash, IV = struct.unpack(PREFIX, checkstring[:cs_len])
    135     assert version == 0 # TODO: just ignore the share
     139    if version != 0: # TODO: just ignore the share
     140        raise UnknownVersionError("got mutable share version %d, but I only understand version 0" % version)
    136141    return (seqnum, root_hash, IV)
    137142
  • TabularUnified src/allmydata/test/test_mutable.py

    r760bab7 rd8ba8c2  
    10311031                if substring:
    10321032                    allproblems = [str(f) for f in servermap.problems]
    1033                     self.failUnless(substring in "".join(allproblems))
     1033                    self.failUnlessIn(substring, "".join(allproblems))
    10341034                return servermap
    10351035            if should_succeed:
     
    10501050
    10511051    def test_corrupt_all_verbyte(self):
    1052         # when the version byte is not 0, we hit an assertion error in
    1053         # unpack_share().
    1054         d = self._test_corrupt_all(0, "AssertionError")
     1052        # when the version byte is not 0, we hit an UnknownVersionError error
     1053        # in unpack_share().
     1054        d = self._test_corrupt_all(0, "UnknownVersionError")
    10551055        def _check_servermap(servermap):
    10561056            # and the dump should mention the problems
  • TabularUnified src/allmydata/test/test_uri.py

    r760bab7 rd8ba8c2  
    187187       good="http://127.0.0.1:3456/uri/URI%3ADIR2%3Agh3l5rbvnv2333mrfvalmjfr4i%3Alz6l7u3z3b7g37s4zkdmfpx5ly4ib4m6thrpbusi6ys62qtc6mma/"
    188188       uri.NewDirectoryURI.init_from_human_encoding(good)
    189        self.failUnlessRaises(AssertionError, uri.NewDirectoryURI.init_from_string, good)
     189       self.failUnlessRaises(uri.BadURIError, uri.NewDirectoryURI.init_from_string, good)
    190190       bad = good + '==='
    191        self.failUnlessRaises(AssertionError, uri.NewDirectoryURI.init_from_human_encoding, bad)
    192        self.failUnlessRaises(AssertionError, uri.NewDirectoryURI.init_from_string, bad)
     191       self.failUnlessRaises(uri.BadURIError, uri.NewDirectoryURI.init_from_human_encoding, bad)
     192       self.failUnlessRaises(uri.BadURIError, uri.NewDirectoryURI.init_from_string, bad)
    193193       fileURI = 'URI:CHK:gh3l5rbvnv2333mrfvalmjfr4i:lz6l7u3z3b7g37s4zkdmfpx5ly4ib4m6thrpbusi6ys62qtc6mma:3:10:345834'
    194194       uri.CHKFileURI.init_from_string(fileURI)
  • TabularUnified src/allmydata/test/test_web.py

    r760bab7 rd8ba8c2  
    2020     FakeMutableFileNode, create_chk_filenode, WebErrorMixin, ShouldFailMixin
    2121from allmydata.interfaces import IURI, INewDirectoryURI, \
    22      IReadonlyNewDirectoryURI, IFileURI, IMutableFileURI, IMutableFileNode
     22     IReadonlyNewDirectoryURI, IFileURI, IMutableFileURI, IMutableFileNode, \
     23     UnhandledCapTypeError
    2324from allmydata.mutable import servermap, publish, retrieve
    2425import common_util as testutil
     
    7576        if IFileURI.providedBy(u):
    7677            return FakeCHKFileNode(u, self)
    77         assert IMutableFileURI.providedBy(u), u
    78         return FakeMutableFileNode(self).init_from_uri(u)
     78        if IMutableFileURI.providedBy(u):
     79            return FakeMutableFileNode(self).init_from_uri(u)
     80        raise UnhandledCapTypeError("cap '%s' is recognized, but has no Node" % auri)
    7981
    8082    def create_empty_dirnode(self):
  • TabularUnified src/allmydata/uri.py

    r760bab7 rd8ba8c2  
    77from allmydata.interfaces import IURI, IDirnodeURI, IFileURI, IImmutableFileURI, \
    88    IVerifierURI, IMutableFileURI, INewDirectoryURI, IReadonlyNewDirectoryURI
     9
     10class BadURIError(Exception):
     11    pass
    912
    1013# the URI shall be an ascii representation of the file. It shall contain
     
    6164        self.size = size
    6265        self.storage_index = hashutil.storage_index_hash(self.key)
    63         assert len(self.storage_index) == 16
    64         self.storage_index = hashutil.storage_index_hash(key)
    65         assert len(self.storage_index) == 16 # sha256 hash truncated to 128
     66        if not len(self.storage_index) == 16: # sha256 hash truncated to 128
     67            raise BadURIError("storage index must be 16 bytes long")
    6668
    6769    @classmethod
    6870    def init_from_human_encoding(cls, uri):
    6971        mo = cls.HUMAN_RE.search(uri)
    70         assert mo, uri
     72        if not mo:
     73            raise BadURIError("%s doesn't look like a cap" % (uri,))
    7174        return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)),
    7275                   int(mo.group(3)), int(mo.group(4)), int(mo.group(5)))
     
    7578    def init_from_string(cls, uri):
    7679        mo = cls.STRING_RE.search(uri)
    77         assert mo, uri
     80        if not mo:
     81            raise BadURIError("%s doesn't look like a cap" % (uri,))
    7882        return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)),
    7983                   int(mo.group(3)), int(mo.group(4)), int(mo.group(5)))
     
    213217    def init_from_human_encoding(cls, uri):
    214218        mo = cls.HUMAN_RE.search(uri)
    215         assert mo, uri
     219        if not mo:
     220            raise BadURIError("'%s' doesn't look like a cap" % (uri,))
    216221        return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
    217222
     
    219224    def init_from_string(cls, uri):
    220225        mo = cls.STRING_RE.search(uri)
    221         assert mo, (uri, cls)
     226        if not mo:
     227            raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
    222228        return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
    223229
     
    261267    def init_from_human_encoding(cls, uri):
    262268        mo = cls.HUMAN_RE.search(uri)
    263         assert mo, uri
     269        if not mo:
     270            raise BadURIError("'%s' doesn't look like a cap" % (uri,))
    264271        return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
    265272
     
    267274    def init_from_string(cls, uri):
    268275        mo = cls.STRING_RE.search(uri)
    269         assert mo, uri
     276        if not mo:
     277            raise BadURIError("'%s' doesn't look like a cap" % (uri,))
    270278        return cls(base32.a2b(mo.group(1)), base32.a2b(mo.group(2)))
    271279
     
    334342    def init_from_string(cls, uri):
    335343        mo = cls.BASE_STRING_RE.search(uri)
    336         assert mo, (uri, cls)
     344        if not mo:
     345            raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
    337346        bits = uri[mo.end():]
    338347        fn = cls.INNER_URI_CLASS.init_from_string(
     
    343352    def init_from_human_encoding(cls, uri):
    344353        mo = cls.BASE_HUMAN_RE.search(uri)
    345         assert mo, (uri, cls)
     354        if not mo:
     355            raise BadURIError("'%s' doesn't look like a %s cap" % (uri, cls))
    346356        bits = uri[mo.end():]
    347357        while bits and bits[-1] == '/':
  • TabularUnified src/allmydata/web/root.py

    r760bab7 rd8ba8c2  
    1313from allmydata import provisioning
    1414from allmydata.util import idlib, log
    15 from allmydata.interfaces import IFileNode
     15from allmydata.interfaces import IFileNode, UnhandledCapTypeError
    1616from allmydata.web import filenode, directory, unlinked, status, operations
    1717from allmydata.web import reliability, storage
     
    8080            node = self.client.create_node_from_uri(name)
    8181            return directory.make_handler_for(node, self.client)
    82         except (TypeError, AssertionError):
     82        except (TypeError, UnhandledCapTypeError, AssertionError):
    8383            raise WebError("'%s' is not a valid file- or directory- cap"
    8484                           % name)
     
    9999        try:
    100100            node = self.client.create_node_from_uri(name)
    101         except (TypeError, AssertionError):
     101        except (TypeError, UnhandledCapTypeError, AssertionError):
    102102            raise WebError("'%s' is not a valid file- or directory- cap"
    103103                           % name)
Note: See TracChangeset for help on using the changeset viewer.