mirror of
https://github.com/gnif/LookingGlass.git
synced 2026-11-13 07:17:28 +00:00
[common/client] platform: make filesystem access portable
This commit is contained in:
@@ -7,6 +7,9 @@ target_link_libraries(common-platform-tests
|
||||
${EXE_FLAGS}
|
||||
lg_common
|
||||
)
|
||||
target_compile_definitions(common-platform-tests PRIVATE
|
||||
LG_TEST_TMP_DIR="${CMAKE_CURRENT_BINARY_DIR}"
|
||||
)
|
||||
add_test(NAME common-platform-tests
|
||||
COMMAND common-platform-tests
|
||||
)
|
||||
@@ -14,6 +17,26 @@ set_tests_properties(common-platform-tests PROPERTIES
|
||||
TIMEOUT 10
|
||||
)
|
||||
|
||||
if(LG_CLIENT_PLATFORM_LINUX)
|
||||
add_executable(config-platform-tests
|
||||
config_platform_test.c
|
||||
../platform/Linux/config.c
|
||||
)
|
||||
target_include_directories(config-platform-tests PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../src"
|
||||
)
|
||||
target_link_libraries(config-platform-tests
|
||||
${EXE_FLAGS}
|
||||
lg_common
|
||||
)
|
||||
add_test(NAME config-platform-tests
|
||||
COMMAND config-platform-tests
|
||||
)
|
||||
set_tests_properties(config-platform-tests PROPERTIES
|
||||
TIMEOUT 10
|
||||
)
|
||||
endif()
|
||||
|
||||
if(LG_CLIENT_PLATFORM_WINDOWS)
|
||||
add_executable(common-windows-tests
|
||||
common_windows_test.c
|
||||
@@ -822,6 +845,7 @@ add_executable(config-message-tests
|
||||
../src/message.c
|
||||
../src/core.c
|
||||
../src/config.c
|
||||
../platform/${LG_CLIENT_PLATFORM}/config.c
|
||||
)
|
||||
target_compile_definitions(config-message-tests PRIVATE
|
||||
CIMGUI_DEFINE_ENUMS_AND_STRUCTS=1
|
||||
|
||||
@@ -21,13 +21,17 @@
|
||||
#include "test.h"
|
||||
|
||||
#include "common/event.h"
|
||||
#include "common/filesystem.h"
|
||||
#include "common/random.h"
|
||||
#include "common/semaphore.h"
|
||||
#include "common/stringutils.h"
|
||||
#include "common/thread.h"
|
||||
#include "common/time.h"
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TEST_WAIT_NS UINT64_C(1000000000)
|
||||
@@ -161,6 +165,42 @@ static void testRandom(void)
|
||||
CHECK(memcmp(first, second, sizeof(first)) != 0);
|
||||
}
|
||||
|
||||
static void testFilesystem(void)
|
||||
{
|
||||
char * directory;
|
||||
char * file;
|
||||
CHECK(alloc_sprintf(&directory, "%s/filesystem-test", LG_TEST_TMP_DIR) > 0);
|
||||
CHECK(alloc_sprintf(&file, "%s/entry.txt", directory) > 0);
|
||||
|
||||
lgDeleteFile(file);
|
||||
lgDeleteDirectory(directory);
|
||||
CHECK(lgPathType(directory) == LG_FILE_TYPE_MISSING);
|
||||
CHECK(lgCreateDirectory(directory));
|
||||
CHECK(lgCreateDirectory(directory));
|
||||
CHECK(lgPathType(directory) == LG_FILE_TYPE_DIRECTORY);
|
||||
|
||||
FILE * stream = lgOpenFile(file, "wb");
|
||||
CHECK(stream);
|
||||
CHECK(fputs("Looking Glass", stream) >= 0);
|
||||
CHECK(fclose(stream) == 0);
|
||||
CHECK(lgPathType(file) == LG_FILE_TYPE_REGULAR);
|
||||
|
||||
StringList entries = lgListDirectory(directory);
|
||||
bool found = false;
|
||||
CHECK(entries);
|
||||
for (unsigned int i = 0; i < stringlist_count(entries); ++i)
|
||||
if (!strcmp(stringlist_at(entries, i), "entry.txt"))
|
||||
found = true;
|
||||
CHECK(found);
|
||||
stringlist_free(&entries);
|
||||
|
||||
CHECK(lgDeleteFile(file));
|
||||
CHECK(lgDeleteDirectory(directory));
|
||||
CHECK(lgPathType(directory) == LG_FILE_TYPE_MISSING);
|
||||
free(file);
|
||||
free(directory);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
lgTimeInit();
|
||||
@@ -169,5 +209,6 @@ int main(void)
|
||||
testTimer();
|
||||
testSemaphore();
|
||||
testRandom();
|
||||
testFilesystem();
|
||||
return 0;
|
||||
}
|
||||
|
||||
92
client/tests/config_platform_test.c
Normal file
92
client/tests/config_platform_test.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* Looking Glass
|
||||
* Copyright © 2017-2026 The Looking Glass Authors
|
||||
* https://looking-glass.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program 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 General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
||||
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "config_platform.h"
|
||||
#include "test.h"
|
||||
|
||||
#include <pwd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAX_PATHS 3U
|
||||
|
||||
struct PathList
|
||||
{
|
||||
char * paths[MAX_PATHS];
|
||||
unsigned int count;
|
||||
};
|
||||
|
||||
const char * lgConfigDir(void)
|
||||
{
|
||||
return "/test/config";
|
||||
}
|
||||
|
||||
static bool collectPath(const char * path, void * opaque)
|
||||
{
|
||||
struct PathList * paths = opaque;
|
||||
CHECK(paths->count < MAX_PATHS);
|
||||
paths->paths[paths->count] = strdup(path);
|
||||
CHECK(paths->paths[paths->count]);
|
||||
++paths->count;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool stopAtFirstPath(const char * path, void * opaque)
|
||||
{
|
||||
(void)path;
|
||||
unsigned int * count = opaque;
|
||||
++*count;
|
||||
return false;
|
||||
}
|
||||
|
||||
static void testCandidates(void)
|
||||
{
|
||||
struct PathList paths = { 0 };
|
||||
CHECK(config_platformVisitDefaultFiles(collectPath, &paths));
|
||||
CHECK(paths.count == MAX_PATHS);
|
||||
CHECK(strcmp(paths.paths[0], "/etc/looking-glass-client.ini") == 0);
|
||||
|
||||
const struct passwd * password = getpwuid(getuid());
|
||||
CHECK(password);
|
||||
|
||||
const size_t homeLength = strlen(password->pw_dir);
|
||||
const char suffix[] = "/.looking-glass-client.ini";
|
||||
char * expected = malloc(homeLength + sizeof(suffix));
|
||||
CHECK(expected);
|
||||
memcpy(expected, password->pw_dir, homeLength);
|
||||
memcpy(expected + homeLength, suffix, sizeof(suffix));
|
||||
CHECK(strcmp(paths.paths[1], expected) == 0);
|
||||
CHECK(strcmp(paths.paths[2], "/test/config/client.ini") == 0);
|
||||
|
||||
free(expected);
|
||||
for (unsigned int i = 0; i < paths.count; ++i)
|
||||
free(paths.paths[i]);
|
||||
|
||||
unsigned int count = 0;
|
||||
CHECK(!config_platformVisitDefaultFiles(stopAtFirstPath, &count));
|
||||
CHECK(count == 1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
testCandidates();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user