summaryrefslogtreecommitdiff
path: root/netfs/unittests/testCore.cpp
blob: 3be4ed02ca5a22793d15b7abefae8c588acd5a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#define BOOST_TEST_MODULE TestNetFSCore
#include <boost/test/unit_test.hpp>
#include <boost/filesystem/operations.hpp>
#include <Ice/ObjectAdapter.h>
#include <Ice/Service.h>
#include <daemon.h>
#include <fuseApp.h>
#include <boost/filesystem/path.hpp>

#ifndef ROOT
#error "ROOT needs to be defined at compilation time"
#endif

#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:
		FuseMock(int & argc, char ** argv) :
			NetFS::FuseApp(argc, argv)
		{
			::memset(&context, 0, sizeof(fuse_context));
		}

		struct fuse_context * fuse_get_context() override
		{
			return &context;
		}

		int fuse_opt_parse(struct fuse_args * args, void * data, const struct fuse_opt [], fuse_opt_proc_t proc) override
		{
			for (int n = 0; n < args->argc; n += 1) {
				proc(data, args->argv[n], n, args);
			}
			return 0;
		}

		int main(int &, char **, const struct fuse_operations * o) override
		{
			o->init(NULL);
			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 / ":testvol").string().c_str()),
	strdup((RootDir / "test").string().c_str())
};

class Core {
	public:
		Core() :
			params({}),
			ic(Ice::initialize(params)),
			daemon(new MockDaemon(ic)),
			argc(2),
			fuse(new FuseMock(argc, argv))
		{
			ic->getProperties()->setProperty("NetFSD.HostNameOverride", "unittest");
			daemon->start("NetFSDaemonAdapter", ic, {});
			char ** a = argv;
			FuseAppBase::run(argc, a, fuse);
		}

		~Core()
		{
			delete fuse;
			delete daemon;
			ic->destroy();
		}

	protected:
		Ice::StringSeq params;
		Ice::CommunicatorPtr ic;
		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"));
	BOOST_REQUIRE(service);
	service->ice_ping();
}

BOOST_AUTO_TEST_CASE ( clientInitialised )
{
	struct statvfs s;
	BOOST_REQUIRE_EQUAL(0, fuse->statfs("/",  &s));
}

BOOST_AUTO_TEST_SUITE_END();