Skip to content

Commit

Permalink
Using test names as sandbox dirs is now optional
Browse files Browse the repository at this point in the history
Given that test names can be long _and_ some platforms insist upon a
short maximum path length (eg, Windows), we may not want to
indiscriminately use path names as the sandbox directory name.

However, this has high utility in some situations, so allow it as an
opt-in. By default, we'll just use an 8 hexadigit "random" string.
(We'll use the rand(3) mechanism, which is not be particularly random,
but since the sandbox directories are sequestered in a unique tempdir,
a clar process need only attempt to avoid collisions between itself,
not with other processes.)
  • Loading branch information
ethomson committed Jan 23, 2025
1 parent 13931e5 commit 18c6e26
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion clar/sandbox.h
Original file line number Diff line number Diff line change
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

0 comments on commit 18c6e26

Please sign in to comment.