face: Make default transport configurable
This commit also removes support for the ndnd-tlv-0.7
protocol's default socket location from UnixTransport.
Test HOME environment changing has also been centralized
into a single fixture (util/test-home-environment-fixture.hpp)
refs #2189
Change-Id: Iac1a6c88d7a2d9fcfb91a20356ac21b75daf7d17
diff --git a/tests/unit-tests/security/identity-fixture.cpp b/tests/unit-tests/security/identity-fixture.cpp
index 04589bc..38c85a8 100644
--- a/tests/unit-tests/security/identity-fixture.cpp
+++ b/tests/unit-tests/security/identity-fixture.cpp
@@ -20,20 +20,18 @@
*/
#include "security/key-chain.hpp"
-
+#include "../util/test-home-environment-fixture.hpp"
#include "boost-test.hpp"
namespace ndn {
namespace security {
-class IdentityFixture
+class IdentityFixture : public util::TestHomeEnvironmentFixture
{
public:
IdentityFixture()
{
// initialize KeyChain from TEST_HOME
- if (std::getenv("TEST_HOME"))
- m_HOME = std::getenv("TEST_HOME");
setenv("TEST_HOME", "tests/unit-tests/security/config-file-home", 1);
KeyChain keyChain("sqlite3", "file");
@@ -71,11 +69,6 @@
// XXX This has no effect if oldDefaultIdentity doesn't exist.
// newIdentity would be kept as default.
keyChain.deleteIdentity(m_newIdentity);
-
- if (!m_HOME.empty())
- setenv("TEST_HOME", m_HOME.c_str(), 1);
- else
- unsetenv("TEST_HOME");
}
private:
diff --git a/tests/unit-tests/security/test-keychain.cpp b/tests/unit-tests/security/test-keychain.cpp
index dad7e75..bb3af0c 100644
--- a/tests/unit-tests/security/test-keychain.cpp
+++ b/tests/unit-tests/security/test-keychain.cpp
@@ -20,6 +20,7 @@
*/
#include "security/key-chain.hpp"
+#include "../util/test-home-environment-fixture.hpp"
#include <boost/filesystem.hpp>
#include "boost-test.hpp"
@@ -29,28 +30,7 @@
using std::vector;
-class KeychainConfigFileFixture
-{
-public:
- KeychainConfigFileFixture()
- {
- if (std::getenv("TEST_HOME"))
- m_HOME = std::getenv("TEST_HOME");
- }
-
- ~KeychainConfigFileFixture()
- {
- if (!m_HOME.empty())
- setenv("TEST_HOME", m_HOME.c_str(), 1);
- else
- unsetenv("TEST_HOME");
- }
-
-protected:
- std::string m_HOME;
-};
-
-BOOST_FIXTURE_TEST_SUITE(SecurityTestKeyChain, KeychainConfigFileFixture)
+BOOST_FIXTURE_TEST_SUITE(SecurityTestKeyChain, util::TestHomeEnvironmentFixture)
BOOST_AUTO_TEST_CASE(ConstructorNormalConfig)
{
diff --git a/tests/unit-tests/transport/test-homes/missing-unix-socket-with-ndnd-protocol/.ndn/client.conf b/tests/unit-tests/transport/test-homes/missing-unix-socket-with-ndnd-protocol/.ndn/client.conf
deleted file mode 100644
index 6708aaa..0000000
--- a/tests/unit-tests/transport/test-homes/missing-unix-socket-with-ndnd-protocol/.ndn/client.conf
+++ /dev/null
@@ -1 +0,0 @@
-protocol=ndnd-tlv-0.7
\ No newline at end of file
diff --git a/tests/unit-tests/transport/test-homes/ok/.ndn/client.conf b/tests/unit-tests/transport/test-homes/ok/.ndn/client.conf
deleted file mode 100644
index e2d3556..0000000
--- a/tests/unit-tests/transport/test-homes/ok/.ndn/client.conf
+++ /dev/null
@@ -1,2 +0,0 @@
-
-unix_socket=/tmp/test/nfd.sock
diff --git a/tests/unit-tests/transport/test-homes/tcp-transport/bad-malformed-uri/.ndn/client.conf b/tests/unit-tests/transport/test-homes/tcp-transport/bad-malformed-uri/.ndn/client.conf
new file mode 100644
index 0000000..872a17a
--- /dev/null
+++ b/tests/unit-tests/transport/test-homes/tcp-transport/bad-malformed-uri/.ndn/client.conf
@@ -0,0 +1 @@
+transport=tcp
\ No newline at end of file
diff --git a/tests/unit-tests/transport/test-homes/tcp-transport/bad-missing-host/.ndn/client.conf b/tests/unit-tests/transport/test-homes/tcp-transport/bad-missing-host/.ndn/client.conf
new file mode 100644
index 0000000..c09f144
--- /dev/null
+++ b/tests/unit-tests/transport/test-homes/tcp-transport/bad-missing-host/.ndn/client.conf
@@ -0,0 +1 @@
+transport=tcp://:6000
\ No newline at end of file
diff --git a/tests/unit-tests/transport/test-homes/tcp-transport/bad-wrong-transport/.ndn/client.conf b/tests/unit-tests/transport/test-homes/tcp-transport/bad-wrong-transport/.ndn/client.conf
new file mode 100644
index 0000000..1ee2a2f
--- /dev/null
+++ b/tests/unit-tests/transport/test-homes/tcp-transport/bad-wrong-transport/.ndn/client.conf
@@ -0,0 +1 @@
+transport=unix://
\ No newline at end of file
diff --git a/tests/unit-tests/transport/test-homes/tcp-transport/ok-omitted-host-omitted-port/.ndn/client.conf b/tests/unit-tests/transport/test-homes/tcp-transport/ok-omitted-host-omitted-port/.ndn/client.conf
new file mode 100644
index 0000000..d09dff3
--- /dev/null
+++ b/tests/unit-tests/transport/test-homes/tcp-transport/ok-omitted-host-omitted-port/.ndn/client.conf
@@ -0,0 +1 @@
+transport=tcp://
\ No newline at end of file
diff --git a/tests/unit-tests/transport/test-homes/tcp-transport/ok-omitted-port/.ndn/client.conf b/tests/unit-tests/transport/test-homes/tcp-transport/ok-omitted-port/.ndn/client.conf
new file mode 100644
index 0000000..8ce375b
--- /dev/null
+++ b/tests/unit-tests/transport/test-homes/tcp-transport/ok-omitted-port/.ndn/client.conf
@@ -0,0 +1 @@
+transport=tcp://127.0.0.1
\ No newline at end of file
diff --git a/tests/unit-tests/transport/test-homes/tcp-transport/ok/.ndn/client.conf b/tests/unit-tests/transport/test-homes/tcp-transport/ok/.ndn/client.conf
new file mode 100644
index 0000000..2dbad7a
--- /dev/null
+++ b/tests/unit-tests/transport/test-homes/tcp-transport/ok/.ndn/client.conf
@@ -0,0 +1 @@
+transport=tcp://127.0.0.1:6000
diff --git a/tests/unit-tests/transport/test-homes/unix-transport/bad-malformed-uri/.ndn/client.conf b/tests/unit-tests/transport/test-homes/unix-transport/bad-malformed-uri/.ndn/client.conf
new file mode 100644
index 0000000..9d981af
--- /dev/null
+++ b/tests/unit-tests/transport/test-homes/unix-transport/bad-malformed-uri/.ndn/client.conf
@@ -0,0 +1 @@
+transport=unix
\ No newline at end of file
diff --git a/tests/unit-tests/transport/test-homes/unix-transport/bad-wrong-transport/.ndn/client.conf b/tests/unit-tests/transport/test-homes/unix-transport/bad-wrong-transport/.ndn/client.conf
new file mode 100644
index 0000000..d09dff3
--- /dev/null
+++ b/tests/unit-tests/transport/test-homes/unix-transport/bad-wrong-transport/.ndn/client.conf
@@ -0,0 +1 @@
+transport=tcp://
\ No newline at end of file
diff --git a/tests/unit-tests/transport/test-homes/missing-unix-socket-missing-protocol/.ndn/client.conf b/tests/unit-tests/transport/test-homes/unix-transport/ok-omitted-unix-socket-omitted-protocol/.ndn/client.conf
similarity index 100%
rename from tests/unit-tests/transport/test-homes/missing-unix-socket-missing-protocol/.ndn/client.conf
rename to tests/unit-tests/transport/test-homes/unix-transport/ok-omitted-unix-socket-omitted-protocol/.ndn/client.conf
diff --git a/tests/unit-tests/transport/test-homes/missing-unix-socket-with-protocol/.ndn/client.conf b/tests/unit-tests/transport/test-homes/unix-transport/ok-omitted-unix-socket-with-protocol/.ndn/client.conf
similarity index 100%
rename from tests/unit-tests/transport/test-homes/missing-unix-socket-with-protocol/.ndn/client.conf
rename to tests/unit-tests/transport/test-homes/unix-transport/ok-omitted-unix-socket-with-protocol/.ndn/client.conf
diff --git a/tests/unit-tests/transport/test-homes/unix-transport/ok/.ndn/client.conf b/tests/unit-tests/transport/test-homes/unix-transport/ok/.ndn/client.conf
new file mode 100644
index 0000000..cb3fae3
--- /dev/null
+++ b/tests/unit-tests/transport/test-homes/unix-transport/ok/.ndn/client.conf
@@ -0,0 +1,2 @@
+
+transport=unix:///tmp/test/nfd.sock
diff --git a/tests/unit-tests/transport/test-tcp-transport.cpp b/tests/unit-tests/transport/test-tcp-transport.cpp
new file mode 100644
index 0000000..7c646d8
--- /dev/null
+++ b/tests/unit-tests/transport/test-tcp-transport.cpp
@@ -0,0 +1,104 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "transport/tcp-transport.hpp"
+#include "transport-fixture.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+
+
+
+BOOST_FIXTURE_TEST_SUITE(TransportTestTcpTransport, TransportFixture)
+
+BOOST_AUTO_TEST_CASE(GetDefaultSocketNameOk)
+{
+ initializeConfig("tests/unit-tests/transport/test-homes/tcp-transport/ok");
+
+ const auto got = TcpTransport::getDefaultSocketHostAndPort(*m_config);
+
+ BOOST_CHECK_EQUAL(got.first, "127.0.0.1");
+ BOOST_CHECK_EQUAL(got.second, "6000");
+}
+
+BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortBadMissingHost)
+{
+ initializeConfig("tests/unit-tests/transport/test-homes/tcp-transport/"
+ "bad-missing-host");
+
+ BOOST_CHECK_EXCEPTION(TcpTransport::getDefaultSocketHostAndPort(*m_config),
+ ConfigFile::Error,
+ [] (const ConfigFile::Error& error) {
+ return error.what() == std::string("Malformed URI: tcp://:6000");
+ });
+}
+
+BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortOkOmittedPort)
+{
+ initializeConfig("tests/unit-tests/transport/test-homes/tcp-transport/"
+ "ok-omitted-port");
+
+ const auto got = TcpTransport::getDefaultSocketHostAndPort(*m_config);
+
+ BOOST_CHECK_EQUAL(got.first, "127.0.0.1");
+ BOOST_CHECK_EQUAL(got.second, "6363");
+}
+
+BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortNameOkOmittedHostOmittedPort)
+{
+ initializeConfig("tests/unit-tests/transport/test-homes/tcp-transport/"
+ "ok-omitted-host-omitted-port");
+
+ const auto got = TcpTransport::getDefaultSocketHostAndPort(*m_config);
+
+ BOOST_CHECK_EQUAL(got.first, "localhost");
+ BOOST_CHECK_EQUAL(got.second, "6363");
+}
+
+BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortBadWrongTransport)
+{
+ initializeConfig("tests/unit-tests/transport/test-homes/tcp-transport/"
+ "bad-wrong-transport");
+
+ BOOST_CHECK_EXCEPTION(TcpTransport::getDefaultSocketHostAndPort(*m_config),
+ Transport::Error,
+ [] (const Transport::Error& error) {
+ return error.what() == std::string("Cannot create TcpTransport "
+ "from \"unix\" URI");
+ });
+}
+
+BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortBadMalformedUri)
+{
+ initializeConfig("tests/unit-tests/transport/test-homes/tcp-transport/"
+ "bad-malformed-uri");
+
+ BOOST_CHECK_EXCEPTION(TcpTransport::getDefaultSocketHostAndPort(*m_config),
+ ConfigFile::Error,
+ [] (const ConfigFile::Error& error) {
+ return error.what() == std::string("Malformed URI: tcp");
+ });
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn
diff --git a/tests/unit-tests/transport/test-unix-transport.cpp b/tests/unit-tests/transport/test-unix-transport.cpp
index 7812664..8d5548c 100644
--- a/tests/unit-tests/transport/test-unix-transport.cpp
+++ b/tests/unit-tests/transport/test-unix-transport.cpp
@@ -20,62 +20,62 @@
*/
#include "transport/unix-transport.hpp"
-#include "util/config-file.hpp"
+#include "transport-fixture.hpp"
#include "boost-test.hpp"
namespace ndn {
-class UnixTransportFixture
+
+
+BOOST_FIXTURE_TEST_SUITE(TransportTestUnixTransport, TransportFixture)
+
+BOOST_AUTO_TEST_CASE(GetDefaultSocketNameOk)
{
-public:
- UnixTransportFixture()
- {
- m_HOME = std::getenv("TEST_HOME");
- }
+ initializeConfig("tests/unit-tests/transport/test-homes/unix-transport/ok");
- ~UnixTransportFixture()
- {
- setenv("TEST_HOME", m_HOME.c_str(), 1);
- // std::cerr << "restoring home = " << m_HOME << std::endl;
- }
-
-protected:
- std::string m_HOME;
-};
-
-BOOST_FIXTURE_TEST_SUITE(TransportTestUnixTransport, UnixTransportFixture)
-
-BOOST_AUTO_TEST_CASE(TestGetDefaultSocketNameOk)
-{
- setenv("TEST_HOME", "tests/unit-tests/transport/test-homes/ok", 1);
-
- ConfigFile config;
- BOOST_REQUIRE_EQUAL(UnixTransport::getDefaultSocketName(config), "/tmp/test/nfd.sock");
+ BOOST_CHECK_EQUAL(UnixTransport::getDefaultSocketName(*m_config), "/tmp/test/nfd.sock");
}
-BOOST_AUTO_TEST_CASE(TestGetDefaultSocketNameMissingSocketMissingProtocol)
+BOOST_AUTO_TEST_CASE(GetDefaultSocketNameOkOmittedSocketOmittedProtocol)
{
- setenv("TEST_HOME",
- "tests/unit-tests/transport/test-homes/missing-unix-socket-missing-protocol", 1);
- ConfigFile config;
- BOOST_REQUIRE_EQUAL(UnixTransport::getDefaultSocketName(config), "/var/run/nfd.sock");
+ initializeConfig("tests/unit-tests/transport/test-homes/unix-transport/"
+ "ok-omitted-unix-socket-omitted-protocol");
+
+ BOOST_CHECK_EQUAL(UnixTransport::getDefaultSocketName(*m_config), "/var/run/nfd.sock");
}
-BOOST_AUTO_TEST_CASE(TestGetDefaultSocketNameMissingSocketNdndProtocol)
+BOOST_AUTO_TEST_CASE(GetDefaultSocketNameOkOmittedSocketWithProtocol)
{
- setenv("TEST_HOME",
- "tests/unit-tests/transport/test-homes/missing-unix-socket-with-ndnd-protocol", 1);
- ConfigFile config;
- BOOST_REQUIRE_EQUAL(UnixTransport::getDefaultSocketName(config), "/tmp/.ndnd.sock");
+ initializeConfig("tests/unit-tests/transport/test-homes/unix-transport/"
+ "ok-omitted-unix-socket-with-protocol");
+
+ BOOST_CHECK_EQUAL(UnixTransport::getDefaultSocketName(*m_config), "/var/run/nfd.sock");
}
-BOOST_AUTO_TEST_CASE(TestGetDefaultSocketNameMissingSocketWithProtocol)
+BOOST_AUTO_TEST_CASE(GetDefaultSocketNameBadWrongTransport)
{
- setenv("TEST_HOME",
- "tests/unit-tests/transport/test-homes/missing-unix-socket-with-protocol", 1);
- ConfigFile config;
- BOOST_REQUIRE_EQUAL(UnixTransport::getDefaultSocketName(config), "/var/run/nfd.sock");
+ initializeConfig("tests/unit-tests/transport/test-homes/unix-transport/"
+ "bad-wrong-transport");
+
+ BOOST_CHECK_EXCEPTION(UnixTransport::getDefaultSocketName(*m_config),
+ Transport::Error,
+ [] (const Transport::Error& error) {
+ return error.what() == std::string("Cannot create UnixTransport "
+ "from \"tcp\" URI");
+ });
+}
+
+BOOST_AUTO_TEST_CASE(GetDefaultSocketNameBadMalformedUri)
+{
+ initializeConfig("tests/unit-tests/transport/test-homes/unix-transport/"
+ "bad-malformed-uri");
+
+ BOOST_CHECK_EXCEPTION(UnixTransport::getDefaultSocketName(*m_config),
+ ConfigFile::Error,
+ [] (const ConfigFile::Error& error) {
+ return error.what() == std::string("Malformed URI: unix");
+ });
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/unit-tests/transport/transport-fixture.hpp b/tests/unit-tests/transport/transport-fixture.hpp
new file mode 100644
index 0000000..71970c5
--- /dev/null
+++ b/tests/unit-tests/transport/transport-fixture.hpp
@@ -0,0 +1,48 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "util/config-file.hpp"
+#include "../util/test-home-environment-fixture.hpp"
+
+#ifndef NDN_TESTS_UNIT_TESTS_TRANSPORT_FIXTURE_HPP
+#define NDN_TESTS_UNIT_TESTS_TRANSPORT_FIXTURE_HPP
+
+namespace ndn {
+
+class TransportFixture : public util::TestHomeEnvironmentFixture
+{
+public:
+
+ void
+ initializeConfig(const char* path)
+ {
+ setenv("TEST_HOME", path, 1);
+ m_config.reset(new ConfigFile);
+ }
+
+protected:
+ std::string m_HOME;
+ unique_ptr<ConfigFile> m_config;
+};
+
+} // namespace ndn
+
+#endif // NDN_TESTS_UNIT_TESTS_TRANSPORT_FIXTURE_HPP
diff --git a/tests/unit-tests/util/test-config-file.cpp b/tests/unit-tests/util/test-config-file.cpp
index 06316bb..2f6650e 100644
--- a/tests/unit-tests/util/test-config-file.cpp
+++ b/tests/unit-tests/util/test-config-file.cpp
@@ -20,6 +20,7 @@
*/
#include "util/config-file.hpp"
+#include "../util/test-home-environment-fixture.hpp"
#include <cstdlib>
@@ -28,25 +29,7 @@
namespace ndn {
namespace tests {
-class ConfigFileFixture
-{
-public:
- ConfigFileFixture()
- {
- m_HOME = std::getenv("TEST_HOME");
- }
-
- ~ConfigFileFixture()
- {
- setenv("TEST_HOME", m_HOME.c_str(), 1);
- // std::cerr << "restoring home = " << m_HOME << std::endl;
- }
-
-protected:
- std::string m_HOME;
-};
-
-BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, ConfigFileFixture)
+BOOST_FIXTURE_TEST_SUITE(UtilTestConfigFile, util::TestHomeEnvironmentFixture)
BOOST_AUTO_TEST_CASE(TestParse)
{
diff --git a/tests/unit-tests/util/test-home-environment-fixture.hpp b/tests/unit-tests/util/test-home-environment-fixture.hpp
new file mode 100644
index 0000000..4fd5dca
--- /dev/null
+++ b/tests/unit-tests/util/test-home-environment-fixture.hpp
@@ -0,0 +1,53 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_TESTS_UNIT_TESTS_UTIL_HOME_ENVIRONMENT_FIXTURE_HPP
+#define NDN_TESTS_UNIT_TESTS_UTIL_HOME_ENVIRONMENT_FIXTURE_HPP
+
+namespace ndn {
+namespace util {
+
+class TestHomeEnvironmentFixture
+{
+public:
+ TestHomeEnvironmentFixture()
+ {
+ if (std::getenv("TEST_HOME"))
+ m_HOME = std::getenv("TEST_HOME");
+ }
+
+ virtual
+ ~TestHomeEnvironmentFixture()
+ {
+ if (!m_HOME.empty())
+ setenv("TEST_HOME", m_HOME.c_str(), 1);
+ else
+ unsetenv("TEST_HOME");
+ }
+
+protected:
+ std::string m_HOME;
+};
+
+} // namespace tests
+} // namespace ndn
+
+#endif // NDN_TESTS_UNIT_TESTS_UTIL_HOME_ENVIRONMENT_FIXTURE_HPP