Пример #1
0
    def test_errors(self):
        fixture = self.fixture
        pibImpl = PibMemory()

        container = PibKeyContainer(fixture.id1, pibImpl)

        try:
            container.add(fixture.id2Key1.toBytes(), fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        try:
            container.remove(fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")

        try:
            container.get(fixture.id2Key1Name)
            self.fail("Did not throw the expected exception")
        except ValueError:
            pass
        else:
            self.fail("Did not throw the expected exception")
Пример #2
0
    def __init__(self, identityName, pibImpl, needInit):
        self._defaultKey = None

        # Copy the Name.
        self._identityName = Name(identityName)
        self._keys = PibKeyContainer(identityName, pibImpl)
        self._pibImpl = pibImpl

        if pibImpl == None:
            raise ValueError("The pibImpl is None")

        if needInit:
            self._pibImpl.addIdentity(self._identityName)
        else:
            if not self._pibImpl.hasIdentity(self._identityName):
                raise Pib.Error("Identity " + self._identityName.toUri() +
                                " does not exist")
Пример #3
0
    def __init__(self, identityName, pibImpl, needInit):
        self._defaultKey = None

        # Copy the Name.
        self._identityName = Name(identityName)
        self._keys = PibKeyContainer(identityName, pibImpl)
        self._pibImpl = pibImpl

        if pibImpl == None:
            raise ValueError("The pibImpl is None")

        if needInit:
            self._pibImpl.addIdentity(self._identityName)
        else:
            if not self._pibImpl.hasIdentity(self._identityName):
                raise Pib.Error("Identity " + self._identityName.toUri() +
                  " does not exist")
Пример #4
0
    def test_basic(self):
        fixture = self.fixture
        pibImpl = PibMemory()

        # Start with an empty container.
        container = PibKeyContainer(fixture.id1, pibImpl)
        self.assertEqual(0, container.size())
        self.assertEqual(0, len(container._keys))

        # Add the first key.
        key11 = container.add(fixture.id1Key1.buf(), fixture.id1Key1Name)
        self.assertTrue(fixture.id1Key1Name.equals(key11.getName()))
        self.assertTrue(key11.getPublicKey().equals(fixture.id1Key1))
        self.assertEqual(1, container.size())
        self.assertEqual(1, len(container._keys))
        self.assertTrue(fixture.id1Key1Name in container._keys)

        # Add the same key again.
        key12 = container.add(fixture.id1Key1.buf(), fixture.id1Key1Name)
        self.assertTrue(fixture.id1Key1Name.equals(key12.getName()))
        self.assertTrue(key12.getPublicKey().equals(fixture.id1Key1))
        self.assertEqual(1, container.size())
        self.assertEqual(1, len(container._keys))
        self.assertTrue(fixture.id1Key1Name in container._keys)

        # Add the second key.
        key21 = container.add(fixture.id1Key2.buf(), fixture.id1Key2Name)
        self.assertTrue(fixture.id1Key2Name.equals(key21.getName()))
        self.assertTrue(key21.getPublicKey().equals(fixture.id1Key2))
        self.assertEqual(2, container.size())
        self.assertEqual(2, len(container._keys))
        self.assertTrue(fixture.id1Key1Name in container._keys)
        self.assertTrue(fixture.id1Key2Name in container._keys)

        # Get keys.
        try:
            container.get(fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        try:
            container.get(fixture.id1Key2Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        id1Key3Name = PibKey.constructKeyName(
          fixture.id1, Name.Component("non-existing-id"))
        try:
            container.get(id1Key3Name)
            self.fail("Did not throw the expected exception")
        except Pib.Error:
            pass
        else:
            self.fail("Did not throw the expected exception")

        # Get and check keys.
        key1 = container.get(fixture.id1Key1Name)
        key2 = container.get(fixture.id1Key2Name)
        self.assertTrue(fixture.id1Key1Name.equals(key1.getName()))
        self.assertTrue(key1.getPublicKey().equals(fixture.id1Key1))
        self.assertEqual(fixture.id1Key2Name, key2.getName())
        self.assertTrue(key2.getPublicKey().equals(fixture.id1Key2))

        # Create another container using the same PibImpl. The cache should be empty.
        container2 = PibKeyContainer(fixture.id1, pibImpl)
        self.assertEqual(2, container2.size())
        self.assertEqual(0, len(container2._keys))

        # Get a key. The cache should be filled.
        try:
            container2.get(fixture.id1Key1Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        self.assertEqual(2, container2.size())
        self.assertEqual(1, len(container2._keys))

        try:
            container2.get(fixture.id1Key2Name)
        except Exception as ex:
            self.fail("Unexpected exception: " + str(ex))
        self.assertEqual(2, container2.size())
        self.assertEqual(2, len(container2._keys))

        # Remove a key.
        container2.remove(fixture.id1Key1Name)
        self.assertEqual(1, container2.size())
        self.assertEqual(1, len(container2._keys))
        self.assertTrue(not (fixture.id1Key1Name in container2._keys))
        self.assertTrue(fixture.id1Key2Name in container2._keys)

        # Remove another key.
        container2.remove(fixture.id1Key2Name)
        self.assertEqual(0, container2.size())
        self.assertEqual(0, len(container2._keys))
        self.assertTrue(not (fixture.id1Key2Name in container2._keys))
Пример #5
0
class PibIdentityImpl(object):
    """
    Create a PibIdentityImpl with identityName.

    :param Name identityName: The name of the identity, which is copied.
    :param PibImpl pibImpl: The Pib backend implementation.
    :param bool needInit: If true and the identity does not exist in the pibImpl
      back end, then create it (and If no default identity has been set,
      identityName becomes the default). If false, then throw Pib.Error if the
      identity does not exist in the pibImpl back end.
    :raises Pib.Error: If the identity does not exist in the pibImpl back end
      and needInit is false.
    """
    def __init__(self, identityName, pibImpl, needInit):
        self._defaultKey = None

        # Copy the Name.
        self._identityName = Name(identityName)
        self._keys = PibKeyContainer(identityName, pibImpl)
        self._pibImpl = pibImpl

        if pibImpl == None:
            raise ValueError("The pibImpl is None")

        if needInit:
            self._pibImpl.addIdentity(self._identityName)
        else:
            if not self._pibImpl.hasIdentity(self._identityName):
                raise Pib.Error("Identity " + self._identityName.toUri() +
                                " does not exist")

    def getName(self):
        """
        Get the name of the identity.

        :return: The name of the identity. You must not change the Name object.
          If you need to change it then make a copy.
        :rtype: Name
        """
        return self._identityName

    def addKey(self, key, keyName):
        """
        Add the key. If a key with the same name already exists, overwrite the
        key. If no default key for the identity has been set, then set the added
        key as default for the identity.

        :param key: The public key bits. This copies the buffer.
        :type key: an array which implements the buffer protocol
        :param Name keyName: The name of the key. This copies the name.
        :return: The PibKey object.
        :rtype: PibKey
        """
        # BOOST_ASSERT(keys_.isConsistent())

        return self._keys.add(key, keyName)

    def removeKey(self, keyName):
        """
        Remove the key with keyName and its related certificates. If the key
        does not exist, do nothing.

        :param Name keyName: The name of the key.
        """
        # BOOST_ASSERT(keys_.isConsistent())

        if (self._defaultKey != None
                and self._defaultKey.getName().equals(keyName)):
            self._defaultKey = None

        self._keys.remove(keyName)

    def getKey(self, keyName):
        """
        Get the key with name keyName.

        :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.
        """
        # BOOST_ASSERT(keys_.isConsistent())

        return self._keys.get(keyName)

    def setDefaultKey(self, keyOrKeyName, arg2=None):
        """
        setDefaultKey has two forms:
        setDefaultKey(keyName) - Set the key with name keyName as the default
        key of the identity.
        setDefaultKey(key, keyName) - Add a key with name keyName and set it as
        the default key of the identity.

        :param key: The buffer of encoded key bytes. (This is only used when
          calling setDefaultKey(key, keyName). )
        :type key: an array which implements the buffer protocol
        :param Name keyName: The name of the key. This copies the name.
        :return: The PibKey object of the default key.
        :rtype: PibKey
        :raises ValueError: If the name of the key does not match the identity
          name.
        :raises Pib.Error: If calling setDefaultKey(keyName) and the key does
          not exist, or if calling setDefaultKey(key, keyName) and a key with
          the same name already exists.
        """
        # BOOST_ASSERT(keys_.isConsistent())

        if isinstance(keyOrKeyName, Name):
            keyName = keyOrKeyName

            self._defaultKey = self._keys.get(keyName)
            self._pibImpl.setDefaultKeyOfIdentity(self._identityName, keyName)
            return self._defaultKey
        else:
            key = keyOrKeyName
            keyName = arg2

            self.addKey(key, keyName)
            return self.setDefaultKey(keyName)

    def getDefaultKey(self):
        """
        Get the default key of this Identity.

        :return: The default PibKey.
        :rtype: PibKey
        :raises Pib.Error: If the default key has not been set.
        """
        # BOOST_ASSERT(keys_.isConsistent())

        if self._defaultKey == None:
            self._defaultKey = self._keys.get(
                self._pibImpl.getDefaultKeyOfIdentity(self._identityName))

        # BOOST_ASSERT(pibImpl_->getDefaultKeyOfIdentity(identityName_) == defaultKey_.getName());

        return self._defaultKey
Пример #6
0
class PibIdentityImpl(object):
    """
    Create a PibIdentityImpl with identityName.

    :param Name identityName: The name of the identity, which is copied.
    :param PibImpl pibImpl: The Pib backend implementation.
    :param bool needInit: If true and the identity does not exist in the pibImpl
      back end, then create it (and If no default identity has been set,
      identityName becomes the default). If false, then throw Pib.Error if the
      identity does not exist in the pibImpl back end.
    :raises Pib.Error: If the identity does not exist in the pibImpl back end
      and needInit is false.
    """
    def __init__(self, identityName, pibImpl, needInit):
        self._defaultKey = None

        # Copy the Name.
        self._identityName = Name(identityName)
        self._keys = PibKeyContainer(identityName, pibImpl)
        self._pibImpl = pibImpl

        if pibImpl == None:
            raise ValueError("The pibImpl is None")

        if needInit:
            self._pibImpl.addIdentity(self._identityName)
        else:
            if not self._pibImpl.hasIdentity(self._identityName):
                raise Pib.Error("Identity " + self._identityName.toUri() +
                  " does not exist")

    def getName(self):
        """
        Get the name of the identity.

        :return: The name of the identity. You must not change the Name object.
          If you need to change it then make a copy.
        :rtype: Name
        """
        return self._identityName

    def addKey(self, key, keyName):
        """
        Add the key. If a key with the same name already exists, overwrite the
        key. If no default key for the identity has been set, then set the added
        key as default for the identity.

        :param key: The public key bits. This copies the buffer.
        :type key: an array which implements the buffer protocol
        :param Name keyName: The name of the key. This copies the name.
        :return: The PibKey object.
        :rtype: PibKey
        """
        # BOOST_ASSERT(keys_.isConsistent())

        return self._keys.add(key, keyName)

    def removeKey(self, keyName):
        """
        Remove the key with keyName and its related certificates. If the key
        does not exist, do nothing.

        :param Name keyName: The name of the key.
        """
        # BOOST_ASSERT(keys_.isConsistent())

        if (self._defaultKey != None and
            self._defaultKey.getName().equals(keyName)):
            self._defaultKey = None

        self._keys.remove(keyName)

    def getKey(self, keyName):
        """
        Get the key with name keyName.

        :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.
        """
        # BOOST_ASSERT(keys_.isConsistent())

        return self._keys.get(keyName)

    def setDefaultKey(self, keyOrKeyName, arg2 = None):
        """
        setDefaultKey has two forms:
        setDefaultKey(keyName) - Set the key with name keyName as the default
        key of the identity.
        setDefaultKey(key, keyName) - Add a key with name keyName and set it as
        the default key of the identity.

        :param key: The buffer of encoded key bytes. (This is only used when
          calling setDefaultKey(key, keyName). )
        :type key: an array which implements the buffer protocol
        :param Name keyName: The name of the key. This copies the name.
        :return: The PibKey object of the default key.
        :rtype: PibKey
        :raises ValueError: If the name of the key does not match the identity
          name.
        :raises Pib.Error: If calling setDefaultKey(keyName) and the key does
          not exist, or if calling setDefaultKey(key, keyName) and a key with
          the same name already exists.
        """
        # BOOST_ASSERT(keys_.isConsistent())

        if isinstance(keyOrKeyName, Name):
            keyName = keyOrKeyName

            self._defaultKey = self._keys.get(keyName)
            self._pibImpl.setDefaultKeyOfIdentity(self._identityName, keyName)
            return self._defaultKey
        else:
            key = keyOrKeyName
            keyName = arg2

            self.addKey(key, keyName)
            return self.setDefaultKey(keyName)

    def getDefaultKey(self):
        """
        Get the default key of this Identity.

        :return: The default PibKey.
        :rtype: PibKey
        :raises Pib.Error: If the default key has not been set.
        """
        # BOOST_ASSERT(keys_.isConsistent())

        if self._defaultKey == None:
            self._defaultKey = self._keys.get(
              self._pibImpl.getDefaultKeyOfIdentity(self._identityName))

        # BOOST_ASSERT(pibImpl_->getDefaultKeyOfIdentity(identityName_) == defaultKey_.getName());

        return self._defaultKey