security: MemoryIdentityStorage: Implemented addIdentity, setDefaultIdentity.
diff --git a/ndn-cpp/security/identity/identity-storage.hpp b/ndn-cpp/security/identity/identity-storage.hpp
index 52ec3b4..daead73 100644
--- a/ndn-cpp/security/identity/identity-storage.hpp
+++ b/ndn-cpp/security/identity/identity-storage.hpp
@@ -137,7 +137,7 @@
/**
* Get the default identity.
- * @param return The name of default identity.
+ * @param return The name of default identity, or an empty name if there is no default.
*/
virtual Name
getDefaultIdentity() = 0;
@@ -170,7 +170,8 @@
getDefaultCertificateNameForKey(const Name& keyName) = 0;
/**
- * Set the default identity.
+ * Set the default identity. If the identityName does not exist, then clear the default identity
+ * so that getDefaultIdentity() returns an empty name.
* @param identityName The default identity name.
*/
virtual void
diff --git a/ndn-cpp/security/identity/memory-identity-storage.cpp b/ndn-cpp/security/identity/memory-identity-storage.cpp
index a7acf84..1e4f0fc 100644
--- a/ndn-cpp/security/identity/memory-identity-storage.cpp
+++ b/ndn-cpp/security/identity/memory-identity-storage.cpp
@@ -6,6 +6,7 @@
*/
#include <stdexcept>
+#include "../security-exception.hpp"
#include "memory-identity-storage.hpp"
using namespace std;
@@ -20,17 +21,18 @@
bool
MemoryIdentityStorage::doesIdentityExist(const Name& identityName)
{
-#if 1
- throw std::runtime_error("MemoryIdentityStorage::doesIdentityExist not implemented");
-#endif
+ string identityUri = identityName.toUri();
+ return find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end();
}
void
MemoryIdentityStorage::addIdentity(const Name& identityName)
{
-#if 1
- throw std::runtime_error("MemoryIdentityStorage::addIdentity not implemented");
-#endif
+ string identityUri = identityName.toUri();
+ if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
+ throw SecurityException("Identity already exists: " + identityUri);
+
+ identityStore_.push_back(identityUri);
}
bool
@@ -129,9 +131,7 @@
Name
MemoryIdentityStorage::getDefaultIdentity()
{
-#if 1
- throw std::runtime_error("MemoryIdentityStorage::getDefaultIdentity not implemented");
-#endif
+ return Name(defaultIdentity_);
}
Name
@@ -153,9 +153,12 @@
void
MemoryIdentityStorage::setDefaultIdentity(const Name& identityName)
{
-#if 1
- throw std::runtime_error("MemoryIdentityStorage::setDefaultIdentity not implemented");
-#endif
+ string identityUri = identityName.toUri();
+ if (find(identityStore_.begin(), identityStore_.end(), identityUri) != identityStore_.end())
+ defaultIdentity_ = identityUri;
+ else
+ // The identity doesn't exist, so clear the default.
+ defaultIdentity_.clear();
}
void
diff --git a/ndn-cpp/security/identity/memory-identity-storage.hpp b/ndn-cpp/security/identity/memory-identity-storage.hpp
index b5b085a..dc8509d 100644
--- a/ndn-cpp/security/identity/memory-identity-storage.hpp
+++ b/ndn-cpp/security/identity/memory-identity-storage.hpp
@@ -8,6 +8,7 @@
#ifndef NDN_MEMORY_IDENTITY_STORAGE_HPP
#define NDN_MEMORY_IDENTITY_STORAGE_HPP
+#include <vector>
#include "identity-storage.hpp"
namespace ndn {
@@ -132,7 +133,7 @@
/**
* Get the default identity.
- * @param return The name of default identity.
+ * @param return The name of default identity, or an empty name if there is no default.
*/
virtual Name
getDefaultIdentity();
@@ -154,7 +155,8 @@
getDefaultCertificateNameForKey(const Name& keyName);
/**
- * Set the default identity.
+ * Set the default identity. If the identityName does not exist, then clear the default identity
+ * so that getDefaultIdentity() returns an empty name.
* @param identityName The default identity name.
*/
virtual void
@@ -175,6 +177,10 @@
*/
virtual void
setDefaultCertificateNameForKey(const Name& keyName, const Name& certificateName);
+
+private:
+ std::vector<std::string> identityStore_; /**< A list of name URI. */
+ std::string defaultIdentity_; /**< The default identity in identityStore_, or "" if not defined. */
};
}
diff --git a/tests/test-encode-decode-data.cpp b/tests/test-encode-decode-data.cpp
index 452afbe..7521221 100644
--- a/tests/test-encode-decode-data.cpp
+++ b/tests/test-encode-decode-data.cpp
@@ -242,9 +242,11 @@
KeyChain keyChain(shared_ptr<IdentityManager>(new IdentityManager(identityStorage, privateKeyStorage)));
// Initialize the storage.
- Name keyName("/testname/DSK-123");
- Name certificateName = keyName;
- certificateName.append(Name("ID-CERT/0"));
+ Name identityName("/testname");
+ Name keyName = Name(identityName).append(Name("DSK-123"));
+ Name certificateName = Name(keyName).append(Name("ID-CERT/0"));
+ identityStorage->addIdentity(identityName);
+ identityStorage->setDefaultIdentity(identityName);
privateKeyStorage->setKeyPairForKeyName
(keyName, DEFAULT_PUBLIC_KEY_DER, sizeof(DEFAULT_PUBLIC_KEY_DER), DEFAULT_PRIVATE_KEY_DER, sizeof(DEFAULT_PRIVATE_KEY_DER));