Ticket #9: aes-exception.diff

File aes-exception.diff, 2.0 KB (added by warner, at 2009-02-09T21:20:46Z)

stop using symbol comparison in AES.init exception catching

  • pycryptopp/cipher/aesmodule.cpp

    old new  
    116116    } catch (CryptoPP::InvalidKeyLength le) {
    117117        PyErr_Format(aes_error, "Precondition violation: you are required to pass a valid key size.  Crypto++ gave this exception: %s", le.what());
    118118        return -1;
     119    } catch (const std::exception& e) {
     120        PyErr_Format(aes_error, "Crypto++ exception: %s", e.what());
     121        return -1;
    119122    }
    120123    if (!reinterpret_cast<AES*>(self)->e) {
    121124        PyErr_NoMemory();
  • pycryptopp/test/test_aes.py

    old new  
    5454        ct = cryptor.process("\x00"*16)
    5555        self.failUnlessEqual(self.enc0, b2a_hex(ct))
    5656
     57    def failUnlessRaises2(self, excClass, callableObj, *args, **kwargs):
     58        # like TestCase.failUnlessRaises, but return the exception object
     59        try:
     60            callableObj(*args, **kwargs)
     61        except excClass, e:
     62            return e
     63        else:
     64            if hasattr(excClass,'__name__'): excName = excClass.__name__
     65            else: excName = str(excClass)
     66            raise self.failureException, "%s not raised" % excName
     67
    5768    def test_init_type_check(self):
    58         self.failUnlessRaises(TypeError, aes.AES, None)
    59         self.failUnlessRaises(aes.Error, aes.AES, "a") # too short
     69        e = self.failUnlessRaises2(TypeError, aes.AES, None)
     70        self.failUnless("AES.__init__() argument 1 must be string or read-only character buffer, not None" in str(e), str(e))
     71        e = self.failUnlessRaises2(aes.Error, aes.AES, "a") # too short
     72        self.failUnless("Crypto++ exception: AES/CTR: 1 is not a valid key length" in str(e), str(e))
    6073
    6174    def test_encrypt_zeroes_in_two_parts(self):
    6275        cryptor = aes.AES(key="\x00"*16)