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/client.conf.sample b/client.conf.sample
index 9dc8605..d911623 100644
--- a/client.conf.sample
+++ b/client.conf.sample
@@ -1,5 +1,13 @@
-; "unix_socket" specifies the location of the NFD unix socket
-unix_socket=/var/run/nfd.sock
+; "transport" specifies Face's default transport connection.
+; The value is a unix or tcp4 scheme Face URI.
+;
+; For example:
+;
+;   unix:///var/run/nfd.sock
+;   tcp://192.0.2.1
+;   tcp4://example.com:6363
+
+transport=unix:///var/run/nfd.sock
 
 ; "protocol" determines the protocol for prefix registration
 ; it has a value of:
diff --git a/src/face.cpp b/src/face.cpp
index 1db6371..8535924 100644
--- a/src/face.cpp
+++ b/src/face.cpp
@@ -28,6 +28,7 @@
 #include "security/key-chain.hpp"
 #include "util/time.hpp"
 #include "util/random.hpp"
+#include "util/face-uri.hpp"
 
 namespace ndn {
 
@@ -38,9 +39,7 @@
   , m_isDirectNfdFibManagementRequested(false)
   , m_impl(new Impl(*this))
 {
-  const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
-  construct(make_shared<UnixTransport>(socketName),
-            m_internalKeyChain);
+  construct(m_internalKeyChain);
 }
 
 Face::Face(boost::asio::io_service& ioService)
@@ -49,9 +48,7 @@
   , m_isDirectNfdFibManagementRequested(false)
   , m_impl(new Impl(*this))
 {
-  const std::string socketName = UnixTransport::getDefaultSocketName(m_impl->m_config);
-  construct(make_shared<UnixTransport>(socketName),
-            m_internalKeyChain);
+  construct(m_internalKeyChain);
 }
 
 Face::Face(const std::string& host, const std::string& port/* = "6363"*/)
@@ -99,6 +96,50 @@
 }
 
 void
+Face::construct(KeyChain* keyChain)
+{
+  // transport=unix:///var/run/nfd.sock
+  // transport=tcp://localhost:6363
+
+  const ConfigFile::Parsed& parsed = m_impl->m_config.getParsedConfiguration();
+
+  const auto transportType = parsed.get_optional<std::string>("transport");
+  if (!transportType)
+    {
+      // transport not specified, use default Unix transport.
+      construct(UnixTransport::create(m_impl->m_config), keyChain);
+      return;
+    }
+
+  unique_ptr<util::FaceUri> uri;
+  try
+    {
+      uri.reset(new util::FaceUri(*transportType));
+    }
+  catch (const util::FaceUri::Error& error)
+    {
+      throw ConfigFile::Error(error.what());
+    }
+
+  shared_ptr<Transport> transport;
+  const std::string protocol = uri->getScheme();
+
+  if (protocol == "unix")
+    {
+      construct(UnixTransport::create(m_impl->m_config), keyChain);
+
+    }
+  else if (protocol == "tcp" || protocol == "tcp4" || protocol == "tcp6")
+    {
+      construct(TcpTransport::create(m_impl->m_config), keyChain);
+    }
+  else
+    {
+      throw ConfigFile::Error("Unsupported transport protocol \"" + protocol + "\"");
+    }
+}
+
+void
 Face::construct(shared_ptr<Transport> transport,
                 KeyChain* keyChain)
 {
@@ -171,8 +212,8 @@
                       const OnData& onData, const OnTimeout& onTimeout/* = OnTimeout()*/)
 {
   return expressInterest(Interest(tmpl)
-                           .setName(name)
-                           .setNonce(0),
+                         .setName(name)
+                         .setNonce(0),
                          onData, onTimeout);
 }
 
diff --git a/src/face.hpp b/src/face.hpp
index abc1d74..96fae40 100644
--- a/src/face.hpp
+++ b/src/face.hpp
@@ -535,6 +535,13 @@
   }
 
 private:
+
+  /**
+   * @throws ConfigFile::Error on parse error and unsupported protocols
+   */
+  void
+  construct(KeyChain* keyChain);
+
   /**
    * @throws Face::Error on unsupported protocol
    * @note shared_ptr is passed by value because ownership is transferred to this function
diff --git a/src/transport/tcp-transport.cpp b/src/transport/tcp-transport.cpp
index 7ffe530..a5b311f 100644
--- a/src/transport/tcp-transport.cpp
+++ b/src/transport/tcp-transport.cpp
@@ -23,6 +23,7 @@
 
 #include "tcp-transport.hpp"
 #include "stream-transport.hpp"
+#include "util/face-uri.hpp"
 
 namespace ndn {
 
@@ -36,6 +37,58 @@
 {
 }
 
+shared_ptr<TcpTransport>
+TcpTransport::create(const ConfigFile& config)
+{
+  const auto hostAndPort(getDefaultSocketHostAndPort(config));
+  return make_shared<TcpTransport>(hostAndPort.first,
+                                   hostAndPort.second);
+}
+
+std::pair<std::string, std::string>
+TcpTransport::getDefaultSocketHostAndPort(const ConfigFile& config)
+{
+  const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
+  std::string host = "localhost";
+  std::string port = "6363";
+
+  try
+    {
+      const util::FaceUri uri(parsed.get<std::string>("transport"));
+
+      const std::string scheme = uri.getScheme();
+      if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6")
+        {
+          throw Transport::Error("Cannot create TcpTransport from \"" +
+                                 scheme + "\" URI");
+        }
+
+      if (!uri.getHost().empty())
+        {
+          host = uri.getHost();
+        }
+
+      if (!uri.getPort().empty())
+        {
+          port = uri.getPort();
+        }
+    }
+  catch (const boost::property_tree::ptree_bad_path& error)
+    {
+      // no transport specified, use default host and port
+    }
+  catch (const boost::property_tree::ptree_bad_data& error)
+    {
+      throw ConfigFile::Error(error.what());
+    }
+  catch (const util::FaceUri::Error& error)
+    {
+      throw ConfigFile::Error(error.what());
+    }
+
+  return std::make_pair(host, port);
+}
+
 void
 TcpTransport::connect(boost::asio::io_service& ioService,
                       const ReceiveCallback& receiveCallback)
diff --git a/src/transport/tcp-transport.hpp b/src/transport/tcp-transport.hpp
index 8afe007..13be254 100644
--- a/src/transport/tcp-transport.hpp
+++ b/src/transport/tcp-transport.hpp
@@ -24,6 +24,8 @@
 
 #include "../common.hpp"
 #include "transport.hpp"
+#include "util/config-file.hpp"
+
 
 // forward declaration
 namespace boost { namespace asio { namespace ip { class tcp; } } }
@@ -60,6 +62,14 @@
   virtual void
   send(const Block& header, const Block& payload);
 
+  static shared_ptr<TcpTransport>
+  create(const ConfigFile& config);
+
+NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+
+  static std::pair<std::string, std::string>
+  getDefaultSocketHostAndPort(const ConfigFile& config);
+
 private:
   std::string m_host;
   std::string m_port;
diff --git a/src/transport/unix-transport.cpp b/src/transport/unix-transport.cpp
index 03ee8b6..df505d7 100644
--- a/src/transport/unix-transport.cpp
+++ b/src/transport/unix-transport.cpp
@@ -25,6 +25,7 @@
 #include "stream-transport.hpp"
 
 #include "../face.hpp"
+#include "util/face-uri.hpp"
 
 namespace ndn {
 
@@ -41,44 +42,45 @@
 UnixTransport::getDefaultSocketName(const ConfigFile& config)
 {
   const ConfigFile::Parsed& parsed = config.getParsedConfiguration();
-  try
-    {
-      return parsed.get<std::string>("unix_socket");
-    }
-  catch (boost::property_tree::ptree_bad_path& error)
-    {
-      // unix_socket not present, continue
-    }
-  catch (boost::property_tree::ptree_bad_data& error)
-    {
-      throw ConfigFile::Error(error.what());
-    }
 
-  // no unix_socket specified so the default socket name
-  // depends on the protocol we're using
   try
     {
-      const std::string protocol = parsed.get<std::string>("protocol");
-      if (protocol == "ndnd-tlv-0.7")
+      const util::FaceUri uri(parsed.get<std::string>("transport"));
+
+      if (uri.getScheme() != "unix")
         {
-          return "/tmp/.ndnd.sock";
+          throw Transport::Error("Cannot create UnixTransport from \"" +
+                                 uri.getScheme() + "\" URI");
+        }
+
+      if (!uri.getPath().empty())
+        {
+          return uri.getPath();
         }
     }
-  catch (boost::property_tree::ptree_bad_path& error)
+  catch (const boost::property_tree::ptree_bad_path& error)
     {
-      return "/var/run/nfd.sock";
+      // no transport specified
     }
-  catch (boost::property_tree::ptree_bad_data& error)
+  catch (const boost::property_tree::ptree_bad_data& error)
+    {
+      throw ConfigFile::Error(error.what());
+    }
+  catch (const util::FaceUri::Error& error)
     {
       throw ConfigFile::Error(error.what());
     }
 
-  // A we made here, then there's no unix_socket specified in the configuration
-  // file. A protocol is present, but it's not ndnd.
   // Assume the default nfd.sock location.
   return "/var/run/nfd.sock";
 }
 
+shared_ptr<UnixTransport>
+UnixTransport::create(const ConfigFile& config)
+{
+  return make_shared<UnixTransport>(getDefaultSocketName(config));
+}
+
 void
 UnixTransport::connect(boost::asio::io_service& ioService,
                        const ReceiveCallback& receiveCallback)
diff --git a/src/transport/unix-transport.hpp b/src/transport/unix-transport.hpp
index 25fa698..1c94225 100644
--- a/src/transport/unix-transport.hpp
+++ b/src/transport/unix-transport.hpp
@@ -19,12 +19,12 @@
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
  */
 
-#ifndef NDN_UDPTRANSPORT_HPP
-#define NDN_UDPTRANSPORT_HPP
+#ifndef NDN_TRANSPORT_UNIX_TRANSPORT_HPP
+#define NDN_TRANSPORT_UNIX_TRANSPORT_HPP
 
 #include "../common.hpp"
 #include "transport.hpp"
-#include "../util/config-file.hpp"
+#include "util/config-file.hpp"
 
 // forward declaration
 namespace boost { namespace asio { namespace local { class stream_protocol; } } }
@@ -69,11 +69,15 @@
   virtual void
   send(const Block& header, const Block& payload);
 
+  static shared_ptr<UnixTransport>
+  create(const ConfigFile& config);
+
+NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   /**
    * Determine the default NFD unix socket
    *
-   * @returns unix_socket value if present in config, else /var/run/nfd.sock
-   * @throws ConfigFile::Error if fail to parse value of a present "unix_socket" field
+   * @returns transport value if present in config, else /var/run/nfd.sock
+   * @throws ConfigFile::Error if fail to parse value of a present "transport" field
    */
   static std::string
   getDefaultSocketName(const ConfigFile& config);
@@ -86,6 +90,6 @@
   shared_ptr< Impl > m_impl;
 };
 
-}
+} // namespace ndn
 
-#endif
+#endif // NDN_TRANSPORT_UNIX_TRANSPORT_HPP
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