Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use random sandbox names (by default) #113

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
include(CheckFunctionExists)

cmake_minimum_required(VERSION 3.16..3.29)

project(clar LANGUAGES C)

option(BUILD_EXAMPLE "Build the example." ON)

check_function_exists(realpath CLAR_HAS_REALPATH)
if(CLAR_HAS_REALPATH)
add_compile_definitions(-DCLAR_HAS_REALPATH)
endif()

add_library(clar INTERFACE)
target_sources(clar INTERFACE
clar.c
Expand Down
41 changes: 39 additions & 2 deletions clar/sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static int canonicalize_tmp_path(char *buffer)
*p = '/';

return 0;
#elif defined(__APPLE__) || defined(HAS_REALPATH)
#elif defined(CLAR_HAS_REALPATH)
char tmp[CLAR_MAX_PATH];

if (realpath(buffer, tmp) == NULL)
Expand Down Expand Up @@ -187,6 +187,12 @@ static void clar_tempdir_init(void)
if (chdir(_clar_tempdir) != 0)
clar_abort("Failed to change into tempdir '%s': %s.\n",
_clar_tempdir, strerror(errno));

#if !defined(CLAR_SANDBOX_TEST_NAMES) && defined(_WIN32)
srand(clock() ^ (unsigned int)time(NULL) ^ GetCurrentProcessId() ^ GetCurrentThreadId());
#elif !defined(CLAR_SANDBOX_TEST_NAMES)
srand(clock() ^ time(NULL) ^ (getpid() << 16));
#endif
}

static void append(char *dst, const char *src)
Expand All @@ -208,9 +214,20 @@ static void append(char *dst, const char *src)

static int clar_sandbox_create(const char *suite_name, const char *test_name)
{
#ifndef CLAR_SANDBOX_TEST_NAMES
char alpha[] = "0123456789abcdef";
int num = rand();
#endif

cl_assert(_clar_sandbox[0] == '\0');

cl_assert(strlen(_clar_tempdir) + strlen(suite_name) + strlen(test_name) + 2 < CLAR_MAX_PATH);
/*
* We may want to use test names as sandbox directory names for
* readability, _however_ on platforms with restrictions for short
* file / folder names (eg, Windows), this may be too long.
*/
#ifdef CLAR_SANDBOX_TEST_NAMES
cl_assert(strlen(_clar_tempdir) + strlen(suite_name) + strlen(test_name) + 3 < CLAR_MAX_PATH);

strcpy(_clar_sandbox, _clar_tempdir);
_clar_sandbox[_clar_tempdir_len] = '/';
Expand All @@ -219,6 +236,26 @@ static int clar_sandbox_create(const char *suite_name, const char *test_name)
append(_clar_sandbox, suite_name);
append(_clar_sandbox, "__");
append(_clar_sandbox, test_name);
#else
((void)suite_name);
((void)test_name);
((void)append);

cl_assert(strlen(_clar_tempdir) + 9 < CLAR_MAX_PATH);

strcpy(_clar_sandbox, _clar_tempdir);
_clar_sandbox[_clar_tempdir_len] = '/';

_clar_sandbox[_clar_tempdir_len + 1] = alpha[(num & 0xf0000000) >> 28];
_clar_sandbox[_clar_tempdir_len + 2] = alpha[(num & 0x0f000000) >> 24];
_clar_sandbox[_clar_tempdir_len + 3] = alpha[(num & 0x00f00000) >> 20];
_clar_sandbox[_clar_tempdir_len + 4] = alpha[(num & 0x000f0000) >> 16];
_clar_sandbox[_clar_tempdir_len + 5] = alpha[(num & 0x0000f000) >> 12];
_clar_sandbox[_clar_tempdir_len + 6] = alpha[(num & 0x00000f00) >> 8];
_clar_sandbox[_clar_tempdir_len + 7] = alpha[(num & 0x000000f0) >> 4];
_clar_sandbox[_clar_tempdir_len + 8] = alpha[(num & 0x0000000f) >> 0];
_clar_sandbox[_clar_tempdir_len + 9] = '\0';
#endif

if (mkdir(_clar_sandbox, 0700) != 0)
return -1;
Expand Down
Loading