1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00

replaces death tests by popen

This commit is contained in:
Falk Werner 2020-04-05 20:11:07 +02:00
parent ac855a73c8
commit 7cea9828cb
7 changed files with 418 additions and 100 deletions

View File

@ -3,6 +3,10 @@ if(NOT WITHOUT_TESTS AND NOT WITHOUT_ADAPTER AND NOT WITHOUT_PROVIDER)
set(MEMORYCHECK_COMMAND_OPTIONS "--leak-check=full --error-exitcode=1") set(MEMORYCHECK_COMMAND_OPTIONS "--leak-check=full --error-exitcode=1")
include (CTest) include (CTest)
add_executable(fs_check
test/webfuse/tests/integration/fs_check.c
)
pkg_check_modules(GTEST gtest_main) pkg_check_modules(GTEST gtest_main)
include(GoogleTest) include(GoogleTest)
pkg_check_modules(GMOCK gmock) pkg_check_modules(GMOCK gmock)
@ -21,7 +25,6 @@ add_executable(alltests
test/webfuse/tests/core/timer/test_timer.cc test/webfuse/tests/core/timer/test_timer.cc
test/webfuse/utils/tempdir.cc test/webfuse/utils/tempdir.cc
test/webfuse/utils/file_utils.cc test/webfuse/utils/file_utils.cc
test/webfuse/utils/die_if.cc
test/webfuse/utils/timeout_watcher.cc test/webfuse/utils/timeout_watcher.cc
test/webfuse/utils/path.c test/webfuse/utils/path.c
test/webfuse/utils/static_filesystem.c test/webfuse/utils/static_filesystem.c
@ -66,6 +69,7 @@ add_executable(alltests
test/webfuse/tests/provider/operation/test_readdir.cc test/webfuse/tests/provider/operation/test_readdir.cc
test/webfuse/tests/integration/test_lowlevel.cc test/webfuse/tests/integration/test_lowlevel.cc
test/webfuse/tests/integration/test_integration.cc test/webfuse/tests/integration/test_integration.cc
test/webfuse/tests/integration/file.cc
test/webfuse/tests/integration/server.cc test/webfuse/tests/integration/server.cc
test/webfuse/tests/integration/provider.cc test/webfuse/tests/integration/provider.cc
) )

View File

@ -0,0 +1,85 @@
#include "webfuse/tests/integration/file.hpp"
#include <cstdio>
#include <sstream>
namespace
{
bool invoke(std::string const & command)
{
int exit_code = -1;
FILE * file = ::popen(command.c_str(), "r");
if (nullptr != file)
{
exit_code = ::pclose(file);
}
return (0 == exit_code);
}
}
namespace webfuse_test
{
File::File(std::string const& path)
: path_(path)
{
}
File::~File()
{
}
bool File::isFile()
{
std::stringstream command;
command << "./fs_check -c is_file -f " << path_;
return invoke(command.str());
}
bool File::isDirectory()
{
std::stringstream command;
command << "./fs_check -c is_dir -f " << path_;
return invoke(command.str());
}
bool File::hasAccessRights(int accessRights)
{
std::stringstream command;
command << "./fs_check -c has_mode -f " << path_ << " -a " << accessRights;
return invoke(command.str());
}
bool File::hasSize(size_t size)
{
std::stringstream command;
command << "./fs_check -c has_size -f " << path_ << " -a " << size;
return invoke(command.str());
}
bool File::hasSubdirectory(std::string const & subdir)
{
std::stringstream command;
command << "./fs_check -c has_subdir -f " << path_ << " -a " << subdir;
return invoke(command.str());
}
bool File::hasContents(std::string const & contents)
{
std::stringstream command;
command << "./fs_check -c has_contents -f " << path_ << " -a " << contents;
return invoke(command.str());
}
}

View File

@ -0,0 +1,26 @@
#ifndef WF_TEST_INTEGRATION_FILE_HPP
#define WF_TEST_INTEGRATION_FILE_HPP
#include <string>
namespace webfuse_test
{
class File final
{
public:
explicit File(std::string const& path);
~File();
bool isFile();
bool isDirectory();
bool hasAccessRights(int accessRights);
bool hasSize(size_t size);
bool hasSubdirectory(std::string const & subdir);
bool hasContents(std::string const & contents);
private:
std::string path_;
};
}
#endif

View File

@ -0,0 +1,288 @@
/* Why this tool is used:
*
* In order to test webfuse as a fuse filesystem, file system operations should be made.
* To check for memory leaks, valgind (memcheck) is used.
*
* Early tests discovered some ugly behavior of valgrind:
* - valgrind intercepts syscalls like open, read and write
* - valgrind does not expect that syscalls are handled within the process to be checked
*
* There is a more or less (un-) documented switch, which changes valgrind's bevahior, but
* this caused other problems.
*
* The second approach used GTests's death tests. Death tests were used quite a while,
* until we discovered a configuration bug when running CTest:
* - memory leaks did not lead to test error
*
* After fixing CTest configuration, memory leaks within the death tests were shown.
* Which is correct, since death tests pematurely exits the program an therefore no
* cleanup is done.
*
* Finally, it was decided to use good old popen together with this tool.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <getopt.h>
struct command;
typedef bool
command_invoke_fn(
struct command * command);
struct command
{
command_invoke_fn * invoke;
char * file;
char * arg;
bool success;
bool has_arg;
};
static bool print_usage(
struct command * command)
{
printf(
"fs_check, (c) 2020 by Webfuse authors (https://github.com/falk-werner/webfuse)\n"
"Checks file information\n"
"\n"
"Usage:\n"
"\t./fs_check --command <command> --file <file> [--arg <arg>]\n"
"\n"
"Options:\n"
"\t-c, --command - Check to perform (see below)\n"
"\t-f, --file - Path to file to check\n"
"\t-a, --arg - Argument, depending on command\n"
"\n"
"Commands:\n"
"\tis_file - checks, if <file> is a regular file; no arg\n"
"\tis_dir - checks, if <file> is a directory; no arg\n"
"\thas_mode - checks, if <file> has the mode given in <arg>\n"
"\thas_size - checks, if <file> has the size given in <arg>\n"
"\thas_subdir - checks, if <file> contains the sub directory given in <arg>\n"
"\thas_contents - checks, if <file> has the contents given in <arg>\n"
);
return command->success;
}
static bool is_file(
struct command * command)
{
struct stat buffer;
int rc = stat(command->file, &buffer);
return ((0 == rc) && (S_ISREG(buffer.st_mode)));
}
static bool is_dir(
struct command * command)
{
struct stat buffer;
int rc = stat(command->file, &buffer);
return ((0 == rc) && (S_ISDIR(buffer.st_mode)));
}
static bool has_mode(
struct command * command)
{
mode_t mode = (mode_t) atoi(command->arg);
struct stat buffer;
int rc = stat(command->file, &buffer);
return ((0 == rc) && (mode == (buffer.st_mode & 0777)));
}
static bool has_size(
struct command * command)
{
int size = atoi(command->arg);
struct stat buffer;
int rc = stat(command->file, &buffer);
return ((0 == rc) && (size == (buffer.st_size)));
}
static bool has_subdir(
struct command * command)
{
bool result = false;
char const * subdir = command->arg;
DIR * dir = opendir(command->file);
if (NULL != dir)
{
struct dirent * entry = readdir(dir);
while (NULL != entry)
{
if (0 == strcmp(subdir, entry->d_name))
{
result = true;
break;
}
entry = readdir(dir);
}
closedir(dir);
}
return result;
}
static bool has_contents(
struct command * command)
{
bool result = false;
char const * contents = command->arg;
size_t length = strlen(contents);
char * buffer = malloc(length);
FILE * file = fopen(command->file, "rb");
{
ssize_t count = fread(buffer, 1, length, file);
fclose(file);
result = (count == (ssize_t) length) && (0 == strncmp(buffer, contents, length));
}
free(buffer);
return result;
}
static bool get_command(
struct command * command,
char const * name)
{
static struct {
char const * name;
command_invoke_fn * invoke;
bool has_arg;
} commands[] =
{
{"is_file" , &is_file , false},
{"is_dir" , &is_dir , false},
{"has_mode" , &has_mode , true},
{"has_size" , &has_size , true},
{"has_subdir" , &has_subdir , true},
{"has_contents", &has_contents, true},
{NULL, NULL, false}
};
for (int i = 0; NULL != commands[i].name; i++)
{
if (0 == strcmp(commands[i].name, name))
{
command->invoke = commands[i].invoke;
command->has_arg = commands[i].has_arg;
return true;
}
}
return false;
}
static void command_init(
struct command * command,
int argc,
char * argv[])
{
static struct option const options[] =
{
{"file" , required_argument, NULL, 'f'},
{"command", required_argument, NULL, 'c'},
{"arg" , required_argument, NULL, 'a'},
{"help" , no_argument , NULL, 'h'},
{NULL, 0, NULL, 0}
};
command->invoke = &print_usage;
command->file = NULL;
command->arg = NULL;
command->success = true;
command->has_arg = false;
optind = 0;
bool is_finished = false;
while (!is_finished)
{
int option_index = 0;
int const c = getopt_long(argc, argv, "f:c:a:h", options, &option_index);
switch(c)
{
case -1:
is_finished = true;
break;
case 'c':
if (!get_command(command, optarg))
{
fprintf(stderr, "error: unknown command\n");
command->invoke = &print_usage;
command->success = false;
is_finished = true;
}
break;
case 'f':
free(command->file);
command->file = strdup(optarg);
break;
case 'a':
free(command->arg);
command->arg = strdup(optarg);
break;
case 'h':
command->invoke = &print_usage;
break;
default:
fprintf(stderr, "error: unknown argument\n");
command->invoke = &print_usage;
command->success = false;
is_finished = true;
break;
}
}
if (command->success)
{
if (NULL == command->file)
{
fprintf(stderr, "error: missing required arg: -f\n");
command->invoke = &print_usage;
command->success = false;
}
else if ((command->has_arg) && (NULL == command->arg))
{
fprintf(stderr, "error: missing required arg: -a\n");
command->invoke = &print_usage;
command->success = false;
}
}
}
static void command_cleanup(
struct command * command)
{
free(command->file);
free(command->arg);
}
int main(int argc, char* argv[])
{
struct command command;
command_init(&command, argc, argv);
bool success = command.invoke(&command);
command_cleanup(&command);
return success ? EXIT_SUCCESS : EXIT_FAILURE;
}

View File

@ -1,6 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "webfuse/tests/integration/server.hpp" #include "webfuse/tests/integration/server.hpp"
#include "webfuse/tests/integration/provider.hpp" #include "webfuse/tests/integration/provider.hpp"
#include "webfuse/tests/integration/file.hpp"
#include <cstdio> #include <cstdio>
#include <csignal> #include <csignal>
@ -15,11 +16,10 @@
#include <jansson.h> #include <jansson.h>
#include "webfuse/core/lws_log.h" #include "webfuse/core/lws_log.h"
#include "webfuse/utils/die_if.hpp"
using webfuse_test::Server; using webfuse_test::Server;
using webfuse_test::Provider; using webfuse_test::Provider;
using webfuse_test::die_if; using webfuse_test::File;
namespace namespace
{ {
@ -70,87 +70,28 @@ TEST_F(IntegrationTest, ProvidesTextFile)
{ {
std::string file_name = std::string(GetBaseDir()) + "/cprovider/hello.txt"; std::string file_name = std::string(GetBaseDir()) + "/cprovider/hello.txt";
ASSERT_EXIT({ File file(file_name);
struct stat buffer; ASSERT_TRUE(file.isFile());
int rc = stat(file_name.c_str(), &buffer); ASSERT_TRUE(file.hasAccessRights(0444));
ASSERT_TRUE(file.hasSize(12));
die_if(0 != rc);
die_if(!S_ISREG(buffer.st_mode));
die_if(0444 != (buffer.st_mode & 0777));
die_if(12 != buffer.st_size);
exit(0);
}, ::testing::ExitedWithCode(0), ".*");
} }
TEST_F(IntegrationTest, ReadTextFile) TEST_F(IntegrationTest, ReadTextFile)
{ {
std::string file_name = std::string(GetBaseDir()) + "/cprovider/hello.txt"; std::string file_name = std::string(GetBaseDir()) + "/cprovider/hello.txt";
ASSERT_EXIT({ File file(file_name);
FILE * file = fopen(file_name.c_str(), "rb"); ASSERT_TRUE(file.hasContents("Hello, World"));
die_if(nullptr == file);
char buffer[13];
ssize_t count = fread(buffer, 1, 12, file);
int rc = fclose(file);
die_if(12 != count);
die_if(0 != strncmp("Hello, World", buffer, 12));
die_if(0 != rc);
exit(0);
}, ::testing::ExitedWithCode(0), ".*");
} }
TEST_F(IntegrationTest, ReadDir) TEST_F(IntegrationTest, ReadDir)
{ {
std::string dir_name = std::string(GetBaseDir()) + "/cprovider"; std::string dir_name = std::string(GetBaseDir()) + "/cprovider";
ASSERT_EXIT({ File dir(dir_name);
ASSERT_TRUE(dir.isDirectory());
DIR * dir = opendir(dir_name.c_str()); ASSERT_TRUE(dir.hasSubdirectory("."));
die_if(nullptr == dir); ASSERT_TRUE(dir.hasSubdirectory(".."));
ASSERT_TRUE(dir.hasSubdirectory("hello.txt"));
bool found_self = false; ASSERT_FALSE(dir.hasSubdirectory("other"));
bool found_parent = false;
bool found_hello_txt = false;
bool found_other = false;
dirent * entry = readdir(dir);
while (NULL != entry)
{
if (0 == strcmp(".", entry->d_name))
{
found_self = true;
}
else if (0 == strcmp("..", entry->d_name))
{
found_parent = true;
}
else if (0 == strcmp("hello.txt", entry->d_name))
{
found_hello_txt = true;
}
else
{
found_other = true;
}
entry = readdir(dir);
}
closedir(dir);
die_if(!found_self);
die_if(!found_parent);
die_if(!found_hello_txt);
die_if(found_other);
exit(0);
}, ::testing::ExitedWithCode(0), ".*");
} }

View File

@ -1,15 +0,0 @@
#include "webfuse/utils/die_if.hpp"
#include <cstdlib>
namespace webfuse_test
{
void die_if(bool expression)
{
if (expression)
{
exit(EXIT_FAILURE);
}
}
}

View File

@ -1,11 +0,0 @@
#ifndef WF_TEST_DIE_IF_HPP
#define WF_TEST_DIE_IF_HPP
namespace webfuse_test
{
extern void die_if(bool expression);
}
#endif