summaryrefslogtreecommitdiff
path: root/netfs/unittests/mockMount.cpp
blob: 7655296e69e041af99fff241bb89a9b7b7311885 (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
#include "mockMount.h"
#include <boost/test/included/unit_test.hpp>
#include <filesystem>

const std::string testEndpoint("tcp -h localhost -p 12015");
const std::string testUri("tcp://localhost:12015/testvol");

FuseMountPoint::FuseMountPoint() :
	MockDaemonHost(::testEndpoint, {"--NetFSD.ConfigPath=" + (rootDir / "defaultDaemon.xml").string()}),
	NetFS::FuseApp({::testUri}), fargs {}
{
	std::filesystem::remove_all(mntpnt);
	std::filesystem::create_directory(mntpnt);
	::fuse_opt_add_arg(&fargs, selfExe.c_str());
	::fuse_opt_add_arg(&fargs, "-onoauto_cache");
	::fuse_opt_add_arg(&fargs, "-oattr_timeout=0");
	::fuse_opt_add_arg(&fargs, "-oac_attr_timeout=0");
	fs = ::fuse_new(&fargs, &operations, sizeof(fuse_operations), this);
	BOOST_REQUIRE(fs);
}

void
FuseMountPoint::start()
{
	BOOST_REQUIRE(!th);
	BOOST_REQUIRE_EQUAL(0, ::fuse_mount(fs, mntpnt.c_str()));
	th = std::make_unique<std::thread>(::fuse_loop, fs);
	BOOST_REQUIRE(th);
}

FuseMountPoint::~FuseMountPoint()
{
	stop();
	std::filesystem::remove(mntpnt);
	::fuse_destroy(fs);
	::fuse_opt_free_args(&fargs);
}

void
FuseMountPoint::stop()
{
	if (th) {
		::fuse_unmount(fs);
		th->join();
		th.reset();
	}
}

struct fuse_context *
FuseMountPoint::fuse_get_context()
{
	return ::fuse_get_context();
}

char *
FuseMountPoint::vstrdupf(const char * fmt, va_list args)
{
	char * out {};
	BOOST_REQUIRE_GE(vasprintf(&out, fmt, args), 0);
	BOOST_REQUIRE(out);
	return out;
}

void
FuseMountPoint::vlogf(int, const char * fmt, va_list args) const noexcept
{
	std::unique_ptr<char, void (*)(void *)> msg(vstrdupf(fmt, args), std::free);
	static std::mutex btm;
	ScopeLock(btm) {
		BOOST_TEST_MESSAGE(msg.get());
	}
}