def test_overwrite(self): fixture = self.fixture pibImpl = PibMemory() try: PibKeyImpl(fixture.id1Key1Name, pibImpl) self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") PibKeyImpl(fixture.id1Key1Name, fixture.id1Key1.buf(), pibImpl) key1 = PibKeyImpl(fixture.id1Key1Name, pibImpl) # Overwriting the key should work. PibKeyImpl(fixture.id1Key1Name, fixture.id1Key2.buf(), pibImpl) key2 = PibKeyImpl(fixture.id1Key1Name, pibImpl) # key1 should have cached the original public key. self.assertTrue(not key1.getPublicKey().equals(key2.getPublicKey())) self.assertTrue(key2.getPublicKey().equals(fixture.id1Key2)) key1.addCertificate(fixture.id1Key1Cert1) # Use the wire encoding to check equivalence. self.assertTrue( key1.getCertificate( fixture.id1Key1Cert1.getName()).wireEncode().equals( fixture.id1Key1Cert1.wireEncode())) otherCert = CertificateV2(fixture.id1Key1Cert1) otherCert.getSignature().getValidityPeriod().setPeriod( Common.getNowMilliseconds(), Common.getNowMilliseconds() + 1000) # Don't bother resigning so we don't have to load a private key. self.assertTrue(fixture.id1Key1Cert1.getName().equals( otherCert.getName())) self.assertTrue(otherCert.getContent().equals( fixture.id1Key1Cert1.getContent())) self.assertFalse(otherCert.wireEncode().equals( fixture.id1Key1Cert1.wireEncode())) key1.addCertificate(otherCert) self.assertTrue( key1.getCertificate( fixture.id1Key1Cert1.getName()).wireEncode().equals( otherCert.wireEncode()))
def get(self, keyName): """ Get the key with name keyName from the container. :param Name keyName: The name of the key. :return: The PibKey object. :rtype: PibKey :raises ValueError: If keyName does not match the identity name. :raises Pib.Error: If the key does not exist. """ if not self._identityName.equals( PibKey.extractIdentityFromKeyName(keyName)): raise ValueError("Key name `" + keyName.toUri() + "` does not match identity `" + self._identityName.toUri() + "`") try: pibKeyImpl = self._keys[keyName] except KeyError: pibKeyImpl = None if pibKeyImpl == None: pibKeyImpl = PibKeyImpl(keyName, self._pibImpl) # Copy the Name. self._keys[Name(keyName)] = pibKeyImpl return PibKey(pibKeyImpl)
def test_basic(self): fixture = self.fixture pibImpl = PibMemory() key11 = PibKeyImpl( fixture.id1Key1Name, fixture.id1Key1.toBytes(), pibImpl) self.assertTrue(fixture.id1Key1Name.equals(key11.getName())) self.assertTrue(fixture.id1.equals(key11.getIdentityName())) self.assertEquals(KeyType.RSA, key11.getKeyType()) self.assertTrue(key11.getPublicKey().equals(fixture.id1Key1)) key11FromBackend = PibKeyImpl(fixture.id1Key1Name, pibImpl) self.assertTrue(fixture.id1Key1Name.equals(key11FromBackend.getName())) self.assertTrue(fixture.id1.equals(key11FromBackend.getIdentityName())) self.assertEquals(KeyType.RSA, key11FromBackend.getKeyType()) self.assertTrue(key11FromBackend.getPublicKey().equals(fixture.id1Key1))
def add(self, key, keyName): """ Add a key with name keyName into the container. If a key with the same name already exists, this replaces it. :param key: The buffer of encoded key bytes. :type key: an array which implements the buffer protocol :param Name keyName: The name of the key, which is copied. :return: The PibKey object. :rtype: PibKey :raises ValueError: If the name of the key does not match the identity name. """ if not self._identityName.equals( PibKey.extractIdentityFromKeyName(keyName)): raise ValueError("The key name `" + keyName.toUri() + "` does not match the identity name `" + self._identityName.toUri() + "`") # Copy the Name. self._keyNames.add(Name(keyName)) self._keys[Name(keyName)] = PibKeyImpl(keyName, key, self._pibImpl) return self.get(keyName)
def test_certificate_operation(self): fixture = self.fixture pibImpl = PibMemory() key11 = PibKeyImpl(fixture.id1Key1Name, fixture.id1Key1.toBytes(), pibImpl) try: PibKeyImpl(fixture.id1Key1Name, pibImpl) except Exception as ex: self.fail("Unexpected exception: " + str(ex)) # The key should not have any certificates. self.assertEqual(0, key11._certificates.size()) # Getting a non-existing certificate should throw Pib.Error. try: key11.getCertificate(fixture.id1Key1Cert1.getName()) self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") # Getting the non-existing default certificate should throw Pib.Error. try: key11.getDefaultCertificate() self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") # Setting a non-existing certificate as the default should throw Pib.Error. try: key11.setDefaultCertificate(fixture.id1Key1Cert1.getName()) self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") # Add a certificate. key11.addCertificate(fixture.id1Key1Cert1) try: key11.getCertificate(fixture.id1Key1Cert1.getName()) except Exception as ex: self.fail("Unexpected exception: " + str(ex)) # The new certificate becomes the default when there was no default. try: key11.getDefaultCertificate() except Exception as ex: self.fail("Unexpected exception: " + str(ex)) defaultCert0 = key11.getDefaultCertificate() self.assertTrue(fixture.id1Key1Cert1.getName().equals( defaultCert0.getName())) # Use the wire encoding to check equivalence. self.assertTrue(fixture.id1Key1Cert1.wireEncode().equals( defaultCert0.wireEncode())) # Remove the certificate. key11.removeCertificate(fixture.id1Key1Cert1.getName()) try: key11.getCertificate(fixture.id1Key1Cert1.getName()) self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") try: key11.getDefaultCertificate() self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") # Set the default certificate directly. try: key11.setDefaultCertificate(fixture.id1Key1Cert1) except Exception as ex: self.fail("Unexpected exception: " + str(ex)) try: key11.getDefaultCertificate() except Exception as ex: self.fail("Unexpected exception: " + str(ex)) try: key11.getCertificate(fixture.id1Key1Cert1.getName()) except Exception as ex: self.fail("Unexpected exception: " + str(ex)) # Check the default cert. defaultCert1 = key11.getDefaultCertificate() self.assertTrue(fixture.id1Key1Cert1.getName().equals( defaultCert1.getName())) self.assertTrue(defaultCert1.wireEncode().equals( fixture.id1Key1Cert1.wireEncode())) # Add another certificate. key11.addCertificate(fixture.id1Key1Cert2) self.assertEqual(2, key11._certificates.size()) # Set the default certificate using a name. try: key11.setDefaultCertificate(fixture.id1Key1Cert2.getName()) except Exception as ex: self.fail("Unexpected exception: " + str(ex)) try: key11.getDefaultCertificate() except Exception as ex: self.fail("Unexpected exception: " + str(ex)) defaultCert2 = key11.getDefaultCertificate() self.assertTrue(fixture.id1Key1Cert2.getName().equals( defaultCert2.getName())) self.assertTrue(defaultCert2.wireEncode().equals( fixture.id1Key1Cert2.wireEncode())) # Remove a certificate. key11.removeCertificate(fixture.id1Key1Cert1.getName()) try: key11.getCertificate(fixture.id1Key1Cert1.getName()) self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") self.assertEqual(1, key11._certificates.size()) # Set the default certificate directly again, which should change the default. try: key11.setDefaultCertificate(fixture.id1Key1Cert1) except Exception as ex: self.fail("Unexpected exception: " + str(ex)) defaultCert3 = key11.getDefaultCertificate() self.assertTrue(fixture.id1Key1Cert1.getName().equals( defaultCert3.getName())) self.assertTrue(defaultCert3.wireEncode().equals( fixture.id1Key1Cert1.wireEncode())) self.assertEqual(2, key11._certificates.size()) # Remove all certificates. key11.removeCertificate(fixture.id1Key1Cert1.getName()) try: key11.getCertificate(fixture.id1Key1Cert1.getName()) self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") self.assertEqual(1, key11._certificates.size()) key11.removeCertificate(fixture.id1Key1Cert2.getName()) try: key11.getCertificate(fixture.id1Key1Cert2.getName()) self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") try: key11.getDefaultCertificate() self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") self.assertEqual(0, key11._certificates.size())
def test_errors(self): fixture = self.fixture pibImpl = PibMemory() try: PibKeyImpl(fixture.id1Key1Name, pibImpl) self.fail("Did not throw the expected exception") except Pib.Error: pass else: self.fail("Did not throw the expected exception") key11 = PibKeyImpl(fixture.id1Key1Name, fixture.id1Key1.buf(), pibImpl) try: PibKeyImpl(Name("/wrong"), pibImpl) self.fail("Did not throw the expected exception") except ValueError: pass else: self.fail("Did not throw the expected exception") try: PibKeyImpl(Name("/wrong"), fixture.id1Key1.buf(), pibImpl) self.fail("Did not throw the expected exception") except ValueError: pass else: self.fail("Did not throw the expected exception") wrongKey = Blob("") try: PibKeyImpl(fixture.id1Key2Name, wrongKey.toBytes(), pibImpl) self.fail("Did not throw the expected exception") except ValueError: pass else: self.fail("Did not throw the expected exception") key11.addCertificate(fixture.id1Key1Cert1) try: key11.addCertificate(fixture.id1Key2Cert1) self.fail("Did not throw the expected exception") except ValueError: pass else: self.fail("Did not throw the expected exception") try: key11.removeCertificate(fixture.id1Key2Cert1.getName()) self.fail("Did not throw the expected exception") except ValueError: pass else: self.fail("Did not throw the expected exception") try: key11.getCertificate(fixture.id1Key2Cert1.getName()) self.fail("Did not throw the expected exception") except ValueError: pass else: self.fail("Did not throw the expected exception") try: key11.setDefaultCertificate(fixture.id1Key2Cert1) self.fail("Did not throw the expected exception") except ValueError: pass else: self.fail("Did not throw the expected exception") try: key11.setDefaultCertificate(fixture.id1Key2Cert1.getName()) self.fail("Did not throw the expected exception") except ValueError: pass else: self.fail("Did not throw the expected exception")