Skip to content

Commit

Permalink
Added more test cases for path resolving.
Browse files Browse the repository at this point in the history
Fixed branching in resolve function.
  • Loading branch information
nmoinvaz committed Nov 4, 2019
1 parent e22948c commit d82ea2c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
23 changes: 10 additions & 13 deletions mz_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
{
check += 1;

/* Remove current directory . if at end of string */
if ((*check == 0) && (source != path))
/* Remove . if at end of string and not at the beginning */
if ((*check == 0) && (source != path && target != output))
{
/* Copy last slash */
*target = *source;
Expand All @@ -180,20 +180,17 @@ int32_t mz_path_resolve(const char *path, char *output, int32_t max_output)
source += (check - source);
continue;
}

/* Remove current directory . if not at end of string */
if ((*check == 0) || (*check == '\\' || *check == '/'))
/* Remove . if not at end of string */
else if ((*check == '\\') || (*check == '/'))
{
/* Only proceed if .\ is not entire string */
if (check[1] != 0 || (path != source))
{
source += (check - source);
continue;
}
source += (check - source);
/* Skip slash if at beginning of string */
if (target == output && *source != 0)
source += 1;
continue;
}

/* Go to parent directory .. */
if ((*check != 0) || (*check == '.'))
else if (*check == '.')
{
check += 1;
if ((*check == 0) || (*check == '\\' || *check == '/'))
Expand Down
7 changes: 6 additions & 1 deletion test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ void test_path_resolve_int(char *path, char *expected_path)
memset(output, 'z', sizeof(output));
mz_path_resolve(path, output, sizeof(output));
ok = (strcmp(output, expected_path) == 0);
printf("path resolve - %s -> %s (%" PRId32 ")\n", path, expected_path, ok);
printf("path resolve - %s -> %s = %s (%" PRId32 ")\n", path, expected_path, output, ok);
}

void test_path_resolve(void)
{
test_path_resolve_int("c:\\test\\.", "c:\\test\\");
test_path_resolve_int("c:\\test\\.\\", "c:\\test\\");
test_path_resolve_int("c:\\test\\.\\.", "c:\\test\\");
test_path_resolve_int("c:\\test\\..", "c:\\");
test_path_resolve_int("c:\\test\\..\\", "c:\\");
test_path_resolve_int("c:\\test\\.\\..", "c:\\");
Expand All @@ -60,11 +61,15 @@ void test_path_resolve(void)
test_path_resolve_int(".\\", "");
test_path_resolve_int("..", "");
test_path_resolve_int("..\\", "");
test_path_resolve_int(".\\test\\123", "test\\123");
test_path_resolve_int(".\\..\\test\\123", "test\\123");
test_path_resolve_int("..\\..\\test\\123", "test\\123");
test_path_resolve_int("test\\.abc.txt", "test\\.abc.txt");
test_path_resolve_int("c:\\test\\123\\.\\abc.txt", "c:\\test\\123\\abc.txt");
test_path_resolve_int("c:\\test\\123\\..\\abc.txt", "c:\\test\\abc.txt");
test_path_resolve_int("c:\\test\\123\\..\\..\\abc.txt", "c:\\abc.txt");
test_path_resolve_int("c:\\test\\123\\..\\..\\..\\abc.txt", "abc.txt");
test_path_resolve_int("c:\\test\\123\\..\\.\\..\\abc.txt", "c:\\abc.txt");
}

void test_encrypt(char *method, mz_stream_create_cb crypt_create, char *password)
Expand Down

0 comments on commit d82ea2c

Please sign in to comment.