From e3beab35ef8c01bafc36e982dbca234cff44814e Mon Sep 17 00:00:00 2001
From: Dan Goodliffe <dan@randomdan.homeip.net>
Date: Mon, 16 Feb 2015 20:51:59 +0000
Subject: Program defined config files and sandbox

---
 netfs/daemon/daemon.cpp      |  8 +++++++-
 netfs/daemon/daemon.h        |  3 +++
 netfs/fuse/fuseApp.cpp       |  8 +++++++-
 netfs/fuse/fuseApp.h         |  3 +++
 netfs/unittests/Jamfile.jam  |  2 --
 netfs/unittests/client.xml   | 14 -------------
 netfs/unittests/daemon.xml   | 19 -----------------
 netfs/unittests/testCore.cpp | 49 +++++++++++++++++++++++++++++++++++++-------
 8 files changed, 62 insertions(+), 44 deletions(-)
 delete mode 100644 netfs/unittests/client.xml
 delete mode 100644 netfs/unittests/daemon.xml

diff --git a/netfs/daemon/daemon.cpp b/netfs/daemon/daemon.cpp
index 0b7b64f..cf312bb 100644
--- a/netfs/daemon/daemon.cpp
+++ b/netfs/daemon/daemon.cpp
@@ -38,11 +38,17 @@ NetFSDaemon::start(const std::string & name, const Ice::CommunicatorPtr &, const
 	adapter->activate();
 }
 
+NetFS::Daemon::ConfigurationPtr
+NetFSDaemon::ReadConfiguration(const boost::filesystem::path & path) const
+{
+	return Slicer::Deserialize<Slicer::XmlFileDeserializer, NetFS::Daemon::Configuration>(path);
+}
+
 void
 NetFSDaemon::LoadConfiguration(const boost::filesystem::path & path)
 {
 	dc = new NetFS::Daemon::RuntimeConfiguration();
-	dc->CurrentConfiguration = Slicer::Deserialize<Slicer::XmlFileDeserializer, NetFS::Daemon::Configuration>(path);
+	dc->CurrentConfiguration = ReadConfiguration(path);
 	auto selfItr = dc->CurrentConfiguration->Hosts.find(hostname());
 	if (selfItr == dc->CurrentConfiguration->Hosts.end()) {
 		throw std::runtime_error("This host is not defined in the configuration.");
diff --git a/netfs/daemon/daemon.h b/netfs/daemon/daemon.h
index a43d7cd..8cf4d4a 100644
--- a/netfs/daemon/daemon.h
+++ b/netfs/daemon/daemon.h
@@ -14,6 +14,9 @@ class NetFSDaemon : public IceBox::Service {
 		virtual void start(const std::string&, const Ice::CommunicatorPtr&, const Ice::StringSeq&) override;
 		virtual void stop() override;
 
+	protected:
+		virtual NetFS::Daemon::ConfigurationPtr ReadConfiguration(const boost::filesystem::path & path) const;
+
 	private:
 		void LoadConfiguration(const boost::filesystem::path & path);
 
diff --git a/netfs/fuse/fuseApp.cpp b/netfs/fuse/fuseApp.cpp
index 2f3b288..d579271 100644
--- a/netfs/fuse/fuseApp.cpp
+++ b/netfs/fuse/fuseApp.cpp
@@ -34,11 +34,17 @@ NetFS::FuseApp::~FuseApp()
 	}
 }
 
+NetFS::Client::ConfigurationPtr
+NetFS::FuseApp::ReadConfiguration(const std::string & path) const
+{
+	return Slicer::Deserialize<Slicer::XmlFileDeserializer, NetFS::Client::Configuration>(path);
+}
+
 void *
 NetFS::FuseApp::init(struct fuse_conn_info *)
 {
 	ic = Ice::initialize(_argc, _argv);
-	fc = Slicer::Deserialize<Slicer::XmlFileDeserializer, NetFS::Client::Configuration>(configPath);
+	fc = ReadConfiguration(configPath);
 	return NULL;
 }
 
diff --git a/netfs/fuse/fuseApp.h b/netfs/fuse/fuseApp.h
index 4786591..e514b55 100644
--- a/netfs/fuse/fuseApp.h
+++ b/netfs/fuse/fuseApp.h
@@ -80,6 +80,9 @@ namespace NetFS {
 
 			virtual struct fuse_context * fuse_get_context() = 0;
 
+		protected:
+			virtual NetFS::Client::ConfigurationPtr ReadConfiguration(const std::string &) const;
+
 		private:
 			void setProxy(OpenFilePtr, uint64_t & fh);
 			OpenFilePtr getFileProxy(uint64_t localID) const;
diff --git a/netfs/unittests/Jamfile.jam b/netfs/unittests/Jamfile.jam
index 1c16bc7..af02411 100644
--- a/netfs/unittests/Jamfile.jam
+++ b/netfs/unittests/Jamfile.jam
@@ -11,8 +11,6 @@ path-constant me : . ;
 run
 	testCore.cpp
 	: :
-	client.xml
-	daemon.xml
 	:
 	<define>BOOST_TEST_DYN_LINK
 	<library>IceUtil
diff --git a/netfs/unittests/client.xml b/netfs/unittests/client.xml
deleted file mode 100644
index c159a24..0000000
--- a/netfs/unittests/client.xml
+++ /dev/null
@@ -1,14 +0,0 @@
-<?xml version="1.0"?>
-<config>
-  <resources>
-    <resource>
-      <name>testvol</name>
-      <resource>
-        <export>unittest</export>
-				<endpoints>
-					<endpoint>tcp -h localhost -p 4000</endpoint>
-				</endpoints>
-      </resource>
-    </resource>
-  </resources>
-</config>
diff --git a/netfs/unittests/daemon.xml b/netfs/unittests/daemon.xml
deleted file mode 100644
index 8bf58a9..0000000
--- a/netfs/unittests/daemon.xml
+++ /dev/null
@@ -1,19 +0,0 @@
-<?xml version="1.0"?>
-<config>
-  <hosts>
-    <host>
-      <hostname>unittest</hostname>
-      <host>
-        <endpoint>tcp -p 4000:udp -p 10000</endpoint>
-      </host>
-    </host>
-  </hosts>
-  <exports>
-    <export>
-      <name>unittest</name>
-      <export>
-        <root>/usr/portage</root>
-      </export>
-    </export>
-  </exports>
-</config>
diff --git a/netfs/unittests/testCore.cpp b/netfs/unittests/testCore.cpp
index b9a1836..2262444 100644
--- a/netfs/unittests/testCore.cpp
+++ b/netfs/unittests/testCore.cpp
@@ -4,7 +4,6 @@
 #include <Ice/ObjectAdapter.h>
 #include <Ice/Service.h>
 #include <daemon.h>
-#include <service.h>
 #include <fuseApp.h>
 #include <boost/filesystem/path.hpp>
 
@@ -15,6 +14,26 @@
 #define XSTR(s) STR(s)
 #define STR(s) #s
 const boost::filesystem::path RootDir(XSTR(ROOT));
+const boost::filesystem::path TestExportRoot(RootDir / "testExport");
+
+std::string testEndpoint("tcp -h localhost -p 12012");
+
+class MockDaemon : public NetFSDaemon {
+	public:
+		MockDaemon(const Ice::CommunicatorPtr & ic) : NetFSDaemon(ic) { }
+
+	protected:
+		virtual NetFS::Daemon::ConfigurationPtr ReadConfiguration(const boost::filesystem::path &) const override
+		{
+			return new NetFS::Daemon::Configuration(
+				{
+					{ "testvol", new NetFS::Daemon::Export(TestExportRoot.string()) }
+				},
+				{
+					{ "unittest", new NetFS::Daemon::Host(testEndpoint) }
+				});
+		}
+};
 
 class FuseMock : public NetFS::FuseApp {
 	public:
@@ -36,12 +55,22 @@ class FuseMock : public NetFS::FuseApp {
 			}
 			return 0;
 		}
+
+	protected:
+		virtual NetFS::Client::ConfigurationPtr ReadConfiguration(const std::string &) const override
+		{
+			return new NetFS::Client::Configuration(
+				{
+					{ "testvol", new NetFS::Client::Resource("testvol", "Service", { testEndpoint }) }
+				});
+		}
+
 	private:
 		fuse_context context;
 };
 
 char * argv[] = {
-	strdup((RootDir / "client.xml:testvol").string().c_str()),
+	strdup((RootDir / ":testvol").string().c_str()),
 	strdup((RootDir / "test").string().c_str())
 };
 
@@ -50,13 +79,12 @@ class Core {
 		Core() :
 			params({}),
 			ic(Ice::initialize(params)),
-			daemon(ic),
+			daemon(new MockDaemon(ic)),
 			argc(2),
 			fuse(new FuseMock(argc, argv))
 		{
-			ic->getProperties()->setProperty("NetFSD.ConfigPath", (RootDir / "daemon.xml").string());
 			ic->getProperties()->setProperty("NetFSD.HostNameOverride", "unittest");
-			daemon.start("NetFSDaemonAdapter", ic, {});
+			daemon->start("NetFSDaemonAdapter", ic, {});
 			char ** a = argv;
 			fuse->runint(argc, a);
 			fuse->init(NULL);
@@ -65,19 +93,26 @@ class Core {
 		~Core()
 		{
 			delete fuse;
+			delete daemon;
 			ic->destroy();
 		}
 
 	protected:
 		Ice::StringSeq params;
 		Ice::CommunicatorPtr ic;
-		NetFSDaemon daemon;
+		NetFSDaemon * daemon;
 		int argc;
 		FuseAppBase * fuse;
 };
 
 BOOST_FIXTURE_TEST_SUITE( NetfsCore, Core )
 
+BOOST_AUTO_TEST_CASE ( initializeSandbox )
+{
+	boost::filesystem::remove_all(TestExportRoot);
+	boost::filesystem::create_directories(TestExportRoot);
+}
+
 BOOST_AUTO_TEST_CASE ( daemonInitialised )
 {
 	auto service = NetFS::ServicePrx::checkedCast(ic->stringToProxy("Service"));
@@ -88,7 +123,7 @@ BOOST_AUTO_TEST_CASE ( daemonInitialised )
 BOOST_AUTO_TEST_CASE ( clientInitialised )
 {
 	struct statvfs s;
-	fuse->statfs("/",  &s);
+	BOOST_REQUIRE_EQUAL(0, fuse->statfs("/",  &s));
 }
 
 BOOST_AUTO_TEST_SUITE_END();
-- 
cgit v1.2.3