コード例 #1
0
    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()))
コード例 #2
0
    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()))
コード例 #3
0
    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())
コード例 #4
0
    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")
コード例 #5
0
    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())
コード例 #6
0
    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")