Ejemplo n.º 1
0
    def test_overwrite(self):
        fixture = self.fixture
        pibImpl = PibMemory()
        identity1 = PibIdentityImpl(fixture.id1, pibImpl, True)

        identity1.addKey(fixture.id1Key1.toBytes(), fixture.id1Key1Name)
        self.assertTrue(identity1.getKey(fixture.id1Key1Name).getPublicKey()
          .equals(fixture.id1Key1))

        # Overwriting the key should work.
        identity1.addKey(fixture.id1Key2.toBytes(), fixture.id1Key1Name)
        self.assertTrue(identity1.getKey(fixture.id1Key1Name).getPublicKey()
          .equals(fixture.id1Key2))
Ejemplo n.º 2
0
    def add(self, identityName):
        """
        Add an identity with name identityName into the container. Create the
        identity if it does not exist.

        :param Name identityName: The name of the identity, which is copied.
        :return: The PibIdentity object.
        :rtype: PibIdentity
        """
        if not identityName in self._identityNames:
            identityNameCopy = Name(identityName)
            self._identityNames.add(identityNameCopy)
            self._identities[identityNameCopy] = PibIdentityImpl(
                identityName, self._pibImpl, True)

        return self.get(identityName)
Ejemplo n.º 3
0
    def get(self, identityName):
        """
        Get the identity with name identityName from the container.

        :param Name identityName: The name of the identity.
        :return: The PibIdentity object.
        :rtype: PibIdentity
        :raises Pib.Error: If the identity does not exist.
        """
        try:
            pibIdentityImpl = self._identities[identityName]
        except KeyError:
            pibIdentityImpl = None

        if pibIdentityImpl == None:
            pibIdentityImpl = PibIdentityImpl(identityName, self._pibImpl,
                                              False)
            # Copy the Name.
            self._identities[Name(identityName)] = pibIdentityImpl

        return PibIdentity(pibIdentityImpl)
Ejemplo n.º 4
0
    def test_key_operation(self):
        fixture = self.fixture
        pibImpl = PibMemory()
        identity1 = PibIdentityImpl(fixture.id1, pibImpl, True)
        try:
            PibIdentityImpl(fixture.id1, pibImpl, False)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))

        # The identity should not have any key.
        self.assertEquals(0, identity1._keys.size())

        # Getting non-existing key should throw Pib.Error.
        try:
            identity1.getKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        # Getting the default key should throw Pib.Error.
        try:
            identity1.getDefaultKey()
            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 key as the default key should throw Pib.Error.
        try:
            identity1.setDefaultKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        # Add a key.
        identity1.addKey(fixture.id1Key1.toBytes(), fixture.id1Key1Name)
        try:
            identity1.getKey(fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))

        # A new key should become the default key when there is no default.
        try:
            identity1.getDefaultKey()
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        defaultKey0 = identity1.getDefaultKey()
        self.assertTrue(fixture.id1Key1Name.equals(defaultKey0.getName()))
        self.assertTrue(defaultKey0.getPublicKey().equals(fixture.id1Key1))

        # Remove a key.
        identity1.removeKey(fixture.id1Key1Name)
        try:
            identity1.setDefaultKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        try:
            identity1.getDefaultKey()
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        # Set the default key directly.
        try:
            identity1.setDefaultKey(fixture.id1Key1.toBytes(),
                                    fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        try:
            identity1.getDefaultKey()
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        try:
            identity1.getKey(fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))

        # Check for a default key.
        defaultKey1 = identity1.getDefaultKey()
        self.assertTrue(fixture.id1Key1Name.equals(defaultKey1.getName()))
        self.assertTrue(defaultKey1.getPublicKey().equals(fixture.id1Key1))

        # Add another key.
        identity1.addKey(fixture.id1Key2.toBytes(), fixture.id1Key2Name)
        self.assertEquals(2, identity1._keys.size())

        # Set the default key using a name.
        try:
            identity1.setDefaultKey(fixture.id1Key2Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        try:
            identity1.getDefaultKey()
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        defaultKey2 = identity1.getDefaultKey()
        self.assertTrue(fixture.id1Key2Name.equals(defaultKey2.getName()))
        self.assertTrue(defaultKey2.getPublicKey().equals(fixture.id1Key2))

        # Remove a key.
        identity1.removeKey(fixture.id1Key1Name)
        try:
            identity1.getKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        self.assertEquals(1, identity1._keys.size())

        # Seting the default key directly again should change the default.
        try:
            identity1.setDefaultKey(fixture.id1Key1.toBytes(),
                                    fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        defaultKey3 = identity1.getDefaultKey()
        self.assertTrue(fixture.id1Key1Name.equals(defaultKey3.getName()))
        self.assertTrue(defaultKey3.getPublicKey().equals(fixture.id1Key1))
        self.assertEquals(2, identity1._keys.size())

        # Remove all keys.
        identity1.removeKey(fixture.id1Key1Name)
        try:
            identity1.getKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        self.assertEquals(1, identity1._keys.size())
        identity1.removeKey(fixture.id1Key2Name)
        try:
            identity1.getKey(fixture.id1Key2Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        self.assertEquals(0, identity1._keys.size())
        try:
            identity1.getDefaultKey()
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")
Ejemplo n.º 5
0
    def test_basic(self):
        fixture = self.fixture
        pibImpl = PibMemory()
        identity1 = PibIdentityImpl(fixture.id1, pibImpl, True)

        self.assertTrue(fixture.id1.equals(identity1.getName()))
Ejemplo n.º 6
0
    def test_errors(self):
        fixture = self.fixture
        pibImpl = PibMemory()

        try:
            PibIdentityImpl(fixture.id1, pibImpl, False)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1 = PibIdentityImpl(fixture.id1, pibImpl, True)

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.addKey(fixture.id2Key1.buf(), fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.removeKey(fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.getKey(fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.setDefaultKey(fixture.id2Key1.buf(), fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.setDefaultKey(fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")
Ejemplo n.º 7
0
    def test_key_operation(self):
        fixture = self.fixture
        pibImpl = PibMemory()
        identity1 = PibIdentityImpl(fixture.id1, pibImpl, True)
        try:
            PibIdentityImpl(fixture.id1, pibImpl, False)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))

        # The identity should not have any key.
        self.assertEqual(0, identity1._keys.size())

        # Getting non-existing key should throw Pib.Error.
        try:
            identity1.getKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        # Getting the default key should throw Pib.Error.
        try:
            identity1.getDefaultKey()
            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 key as the default key should throw Pib.Error.
        try:
            identity1.setDefaultKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        # Add a key.
        identity1.addKey(fixture.id1Key1.toBytes(), fixture.id1Key1Name)
        try:
          identity1.getKey(fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))

        # A new key should become the default key when there is no default.
        try:
            identity1.getDefaultKey()
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        defaultKey0 = identity1.getDefaultKey()
        self.assertTrue(fixture.id1Key1Name.equals(defaultKey0.getName()))
        self.assertTrue(defaultKey0.getPublicKey().equals(fixture.id1Key1))

        # Remove a key.
        identity1.removeKey(fixture.id1Key1Name)
        try:
            identity1.setDefaultKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        try:
            identity1.getDefaultKey()
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        # Set the default key directly.
        try:
            identity1.setDefaultKey(fixture.id1Key1.toBytes(), fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        try:
            identity1.getDefaultKey()
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        try:
          identity1.getKey(fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))

        # Check for a default key.
        defaultKey1 = identity1.getDefaultKey()
        self.assertTrue(fixture.id1Key1Name.equals(defaultKey1.getName()))
        self.assertTrue(defaultKey1.getPublicKey().equals(fixture.id1Key1))

        # Add another key.
        identity1.addKey(fixture.id1Key2.toBytes(), fixture.id1Key2Name)
        self.assertEqual(2, identity1._keys.size())

        # Set the default key using a name.
        try:
            identity1.setDefaultKey(fixture.id1Key2Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        try:
          identity1.getDefaultKey()
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        defaultKey2 = identity1.getDefaultKey()
        self.assertTrue(fixture.id1Key2Name.equals(defaultKey2.getName()))
        self.assertTrue(defaultKey2.getPublicKey().equals(fixture.id1Key2))

        # Remove a key.
        identity1.removeKey(fixture.id1Key1Name)
        try:
            identity1.getKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        self.assertEqual(1, identity1._keys.size())

        # Seting the default key directly again should change the default.
        try:
            identity1.setDefaultKey(fixture.id1Key1.toBytes(), fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        defaultKey3 = identity1.getDefaultKey()
        self.assertTrue(fixture.id1Key1Name.equals(defaultKey3.getName()))
        self.assertTrue(defaultKey3.getPublicKey().equals(fixture.id1Key1))
        self.assertEqual(2, identity1._keys.size())

        # Remove all keys.
        identity1.removeKey(fixture.id1Key1Name)
        try:
            identity1.getKey(fixture.id1Key1Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        self.assertEqual(1, identity1._keys.size())
        identity1.removeKey(fixture.id1Key2Name)
        try:
            identity1.getKey(fixture.id1Key2Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        self.assertEqual(0, identity1._keys.size())
        try:
            identity1.getDefaultKey()
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")
Ejemplo n.º 8
0
    def test_basic(self):
        fixture = self.fixture
        pibImpl = PibMemory()
        identity1 = PibIdentityImpl(fixture.id1, pibImpl, True)

        self.assertTrue(fixture.id1.equals(identity1.getName()))
Ejemplo n.º 9
0
    def test_errors(self):
        fixture = self.fixture
        pibImpl = PibMemory()

        try:
            PibIdentityImpl(fixture.id1, pibImpl, False)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1 = PibIdentityImpl(fixture.id1, pibImpl, True)

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.addKey(fixture.id2Key1.buf(), fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.removeKey(fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.getKey(fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.setDefaultKey(fixture.id2Key1.buf(), fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name)
        try:
            identity1.setDefaultKey(fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")