FreeRDP/winpr/libwinpr/path/test/TestPathMakePath.c

82 lines
1.3 KiB
C
Raw Normal View History

2015-06-03 12:47:40 +03:00
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <winpr/crt.h>
#include <winpr/file.h>
2015-06-03 12:47:40 +03:00
#include <winpr/path.h>
int TestPathMakePath(int argc, char* argv[])
{
int x;
size_t baseLen;
2015-06-03 12:47:40 +03:00
BOOL success;
char tmp[64];
char* path;
char* cur;
char delim = PathGetSeparatorA(0);
2015-06-03 12:47:40 +03:00
char* base = GetKnownPath(KNOWN_PATH_TEMP);
2018-08-24 11:39:48 +03:00
2015-06-03 12:47:40 +03:00
if (!base)
{
fprintf(stderr, "Failed to get temporary directory!\n");
return -1;
}
baseLen = strlen(base);
2015-06-03 12:47:40 +03:00
srand(time(NULL));
2018-08-24 11:39:48 +03:00
for (x = 0; x < 5; x++)
2015-06-03 12:47:40 +03:00
{
2018-08-24 11:39:48 +03:00
sprintf_s(tmp, ARRAYSIZE(tmp), "%08X", rand());
2015-06-03 12:47:40 +03:00
path = GetCombinedPath(base, tmp);
free(base);
2018-08-24 11:39:48 +03:00
2015-06-03 12:47:40 +03:00
if (!path)
{
fprintf(stderr, "GetCombinedPath failed!\n");
return -1;
}
base = path;
}
printf("Creating path %s\n", path);
success = PathMakePathA(path, NULL);
2018-08-24 11:39:48 +03:00
2015-06-03 12:47:40 +03:00
if (!success)
{
fprintf(stderr, "MakePath failed!\n");
2018-08-24 11:39:48 +03:00
free(path);
2015-06-03 12:47:40 +03:00
return -1;
}
success = PathFileExistsA(path);
2018-08-24 11:39:48 +03:00
2015-06-03 12:47:40 +03:00
if (!success)
{
fprintf(stderr, "MakePath lied about success!\n");
2018-08-24 11:39:48 +03:00
free(path);
2015-06-03 12:47:40 +03:00
return -1;
}
while (strlen(path) > baseLen)
{
if (!RemoveDirectoryA(path))
{
fprintf(stderr, "RemoveDirectoryA %s failed!\n", path);
2018-08-24 11:39:48 +03:00
free(path);
return -1;
}
2018-08-24 11:39:48 +03:00
cur = strrchr(path, delim);
2018-08-24 11:39:48 +03:00
if (cur)
*cur = '\0';
}
2018-08-24 11:39:48 +03:00
free(path);
printf("%s success!\n", __FUNCTION__);
2015-06-03 12:47:40 +03:00
return 0;
}