def testRemoveIssuerCertsFromKeycahin(self): """Test RemoveIssuerCertsFromKeychain.""" self.StubSetup() self.mox.StubOutWithMock(certs, 'DeleteCert') self.mox.StubOutWithMock(certs, 'FindCertificates') c = self.mox.CreateMockAnything() c.osx_fingerprint = 'f' # Remove is successful certs.FindCertificates(issuer_cn='i', keychain='k').AndReturn([c]) certs.logging.debug('Removing cert with fingerprint %s from %s', 'f', 'k').AndReturn(None) certs.DeleteCert('f', password=None, gui=False, keychain='k').AndReturn(None) # Remove fails certs.FindCertificates(issuer_cn='i', keychain='k').AndReturn([c]) certs.logging.debug('Removing cert with fingerprint %s from %s', 'f', 'k').AndReturn(None) certs.DeleteCert('f', password=None, gui=False, keychain='k').AndRaise(certs.CertError('err')) certs.logging.error('Cannot delete old certificate: %s', 'err') self.mox.ReplayAll() # Remove is sucessful certs.RemoveIssuerCertsFromKeychain('i', keychain='k') # Remove fails certs.RemoveIssuerCertsFromKeychain('i', keychain='k') self.mox.VerifyAll()
def testFindCertificates(self): """Test FindCertificates.""" self.StubSetup() self.mox.StubOutWithMock(certs, '_GetCertificates') allcerts = [{ 'subject': 's1', 'issuer': 'i1', 'fingerprint': 'f1' }, { 'subject': 's2', 'issuer': 'i1', 'fingerprint': 'f2' }, { 'subject': 's3', 'issuer': 'i2', 'fingerprint': 'f3' }] certs._GetCertificates(keychain=None).AndReturn(allcerts) certs._GetCertificates(keychain=None).AndReturn(allcerts) certs._GetCertificates(keychain=None).AndReturn(allcerts) certs._GetCertificates(keychain=None).AndReturn(allcerts) certs._GetCertificates(keychain=None).AndReturn(allcerts) self.mox.ReplayAll() self.assertEqual(allcerts, certs.FindCertificates()) self.assertEqual(allcerts[0:1], certs.FindCertificates(subject='s1')) self.assertEqual(allcerts[0:2], certs.FindCertificates(issuer='i1')) self.assertEqual([], certs.FindCertificates(issuer='i3')) self.assertEqual([], certs.FindCertificates(subject='s1', issuer='i2')) self.mox.VerifyAll()
def testCreateIdentityPreference(self): """Test CreateIdentityPreference.""" self.StubSetup() self.mox.StubOutWithMock(certs, 'FindCertificates') c = self.mox.CreateMockAnything() c.osx_fingerprint = 'f' # No matching certs found certs.logging.debug( 'Creating TLS identity preference for %s ' 'in keychain %s', 's', 'k') certs.FindCertificates(issuer_cn='i', keychain='k').AndReturn([]) # Successful ID pref creation certs.logging.debug( 'Creating TLS identity preference for %s ' 'in keychain %s', 's', 'k') certs.FindCertificates(issuer_cn='i', keychain='k').AndReturn([c]) command = [ certs.CMD_SECURITY, 'set-identity-preference', '-Z', 'f', '-s', 's', 'k' ] certs.logging.debug('Command: %s', command) certs.gmacpyutil.RunProcess(command).AndReturn(('out', 'err', 0)) certs.logging.debug('Identity preference creation output: %s', 'out') # Error creating ID pref certs.logging.debug( 'Creating TLS identity preference for %s ' 'in keychain %s', 's', 'k') certs.FindCertificates(issuer_cn='i', keychain='k').AndReturn([c]) command = [ certs.CMD_SECURITY, 'set-identity-preference', '-Z', 'f', '-s', 's', 'k' ] certs.logging.debug('Command: %s', command) certs.gmacpyutil.RunProcess(command).AndReturn(('out', 'err', 1)) certs.logging.debug('Identity preference creation output: %s', 'out') self.mox.ReplayAll() # No matching certs found self.assertRaises(certs.CertError, certs.CreateIdentityPreference, 'i', 's', keychain='k') # Successful ID pref creation self.assertEquals( None, certs.CreateIdentityPreference('i', 's', keychain='k')) # Error creating ID pref self.assertRaises(certs.CertError, certs.CreateIdentityPreference, 'i', 's', keychain='k') self.mox.VerifyAll()