def test_key_not_found(self):
     mock_header = '-' * sagecipher.HEADER_SIZE
     mock_fingerprint = 'AA:' * 15 + 'AA'
     self.assertRaises(sagecipher.AgentKeyError,
                       lambda: sagecipher.Cipher(mock_header))
     self.assertRaises(
         sagecipher.AgentKeyError,
         lambda: sagecipher.Cipher(hex_fingerprint=mock_fingerprint))
 def test_fingerprint_mismatch(self):
     self.cipher1 = sagecipher.Cipher()
     p = mock.patch('sagecipher.cipher.sign_via_agent')
     dummy_hex_fingerprint = binascii.unhexlify('12' * 16)
     p.return_value = {
         'signature': '',
         'key_type': '',
         'key_fingerprint': sagecipher.to_hex(dummy_hex_fingerprint)
     }
     p.start()
     self.assertRaises(Exception,
                       lambda: sagecipher.Cipher(self.cipher1.header()))
     p.stop()
    def test_passlib(self):
        # force import of passlib pbkdf2 function, and compare headers/keys
        # generated from both hashlib and passlib
        self.cipher1 = sagecipher.Cipher()
        try:
            import builtins
        except ImportError:
            import __builtin__ as builtins
        realimport = builtins.__import__

        def myimport(*args, **kwargs):
            if args[0] == 'hashlib' and args[
                    3] is not None and 'pbkdf2_hmac' in args[3]:
                raise ImportError
            return realimport(*args, **kwargs)

        builtins.__import__ = myimport
        del (sagecipher.cipher.pbkdf2_hashlib)
        importlib.reload(sagecipher.cipher)
        importlib.reload(sagecipher)
        self.assertIn('pbkdf2_passlib', dir(sagecipher.cipher))
        self.assertNotIn('pbkdf2_hashlib', dir(sagecipher.cipher))
        self.cipher2 = sagecipher.Cipher(self.cipher1.header())
        self.assertEqual(self.cipher1.header(), self.cipher2.header())
 def test_no_keys(self):
     auth_sock = os.environ['SSH_AUTH_SOCK']
     os.environ['SSH_AUTH_SOCK'] = ''
     self.assertRaises(sagecipher.AgentKeyError,
                       lambda: sagecipher.Cipher())
     os.environ['SSH_AUTH_SOCK'] = auth_sock
 def _loadCiphers(self):
     self.cipher1 = sagecipher.Cipher()
     self.cipher2 = sagecipher.Cipher(self.cipher1.header())