From 2febe632c5cf3a9127c0e71dab9bbe46e4a474a7 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Mon, 26 Dec 2022 18:58:44 +0300 Subject: [PATCH] filesystem: add caseinsensitive emulation test --- filesystem/tests/caseinsensitive.c | 122 +++++++++++++++++++++++++++++ filesystem/wscript | 22 +++++- 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 filesystem/tests/caseinsensitive.c diff --git a/filesystem/tests/caseinsensitive.c b/filesystem/tests/caseinsensitive.c new file mode 100644 index 00000000..f0478630 --- /dev/null +++ b/filesystem/tests/caseinsensitive.c @@ -0,0 +1,122 @@ +#include "port.h" +#include "build.h" +#include +#include +#include +#include "filesystem.h" +#if XASH_POSIX +#include +#define LoadLibrary( x ) dlopen( x, RTLD_NOW ) +#define GetProcAddress( x, y ) dlsym( x, y ) +#define FreeLibrary( x ) dlclose( x ) +#elif XASH_WIN32 +#include +#endif + +void *g_hModule; +FSAPI g_pfnGetFSAPI; +fs_api_t g_fs; +fs_globals_t *g_nullglobals; + + +static qboolean LoadFilesystem( void ) +{ + g_hModule = LoadLibrary( "filesystem_stdio." OS_LIB_EXT ); + if( !g_hModule ) + return false; + + g_pfnGetFSAPI = (void*)GetProcAddress( g_hModule, GET_FS_API ); + if( !g_pfnGetFSAPI ) + return false; + + if( !g_pfnGetFSAPI( FS_API_VERSION, &g_fs, &g_nullglobals, NULL )) + return false; + + return true; +} + +static qboolean CheckFileContents( const char *path, const void *buf, fs_offset_t size ) +{ + fs_offset_t len; + byte *data; + + data = g_fs.LoadFile( path, &len, true ); + if( !data ) + { + printf( "LoadFile fail\n" ); + return false; + } + + if( len != size ) + { + printf( "LoadFile sizeof fail\n" ); + free( data ); + return false; + } + + if( memcmp( data, buf, size )) + { + printf( "LoadFile magic fail\n" ); + free( data ); + return false; + } + + free( data ); + return true; +} + +static qboolean TestCaseinsensitive( void ) +{ + file_t *f1; + FILE *f2; + int magic = rand(); + + // create game dir for us + g_fs.AddGameDirectory( "./", FS_GAMEDIR_PATH ); + + // create some files first and write data + f1 = g_fs.Open( "FOO/Bar.bin", "wb", true ); + g_fs.Write( f1, &magic, sizeof( magic )); + g_fs.Close( f1 ); + + // try to search it with different file name + if( !g_fs.FileExists( "fOO/baR.bin", true )) + { + printf( "FileExists fail\n" ); + return false; + } + + // create a file directly, to check if cache can re-read + f2 = fopen( "FOO/Baz.bin", "wb" ); + fwrite( &magic, sizeof( magic ), 1, f2 ); + fclose( f2 ); + + // try to open first file back but with different file name case + if( !CheckFileContents( "foo/bar.BIN", &magic, sizeof( magic ))) + return false; + + // try to open second file that we created directly + if( !CheckFileContents( "Foo/BaZ.Bin", &magic, sizeof( magic ))) + return false; + + g_fs.Delete( "foo/Baz.biN" ); + g_fs.Delete( "foo/bar.bin" ); + g_fs.Delete( "Foo" ); + + return true; +} + +int main( void ) +{ + if( !LoadFilesystem() ) + return EXIT_FAILURE; + + srand( time( NULL )); + + if( !TestCaseinsensitive()) + return EXIT_FAILURE; + + printf( "success\n" ); + + return EXIT_SUCCESS; +} diff --git a/filesystem/wscript b/filesystem/wscript index 2b8d8a8b..362892b1 100644 --- a/filesystem/wscript +++ b/filesystem/wscript @@ -1,7 +1,12 @@ #!/usr/bin/env python +from waflib.Tools import waf_unit_test + def options(opt): - pass + grp = opt.add_option_group('filesystem_stdio options') + + grp.add_option('--enable-fs-tests', action='store_true', dest = 'FS_TESTS', default = False, + help = 'enable filesystem_stdio tests') def configure(conf): nortti = { @@ -10,6 +15,8 @@ def configure(conf): } conf.env.append_unique('CXXFLAGS', conf.get_flags_by_compiler(nortti, conf.env.COMPILER_CC)) + conf.env.FS_TESTS = conf.options.FS_TESTS + if conf.env.DEST_OS != 'android': if conf.env.cxxshlib_PATTERN.startswith('lib'): conf.env.cxxshlib_PATTERN = conf.env.cxxshlib_PATTERN[3:] @@ -22,3 +29,16 @@ def build(bld): use = 'filesystem_includes public', install_path = bld.env.LIBDIR, subsystem = bld.env.MSVC_SUBSYSTEM) + + if bld.env.FS_TESTS: + # build in same module, so dynamic linking will work + # for now (until we turn libpublic to shared module lol) + bld.program(features = 'test', + source = 'tests/caseinsensitive.c', + target = 'test_caseinsensitive', + use = 'filesystem_includes public DL', + rpath = '$ORIGIN', + subsystem = bld.env.CONSOLE_SUBSYSTEM, + install_path = None) + bld.add_post_fun(waf_unit_test.summary) + bld.add_post_fun(waf_unit_test.set_exit_code)