FreeRDP
Loading...
Searching...
No Matches
TestPathMakePath.c
1#include <stdlib.h>
2#include <string.h>
3#include <time.h>
4
5#include <winpr/crt.h>
6#include <winpr/crypto.h>
7#include <winpr/file.h>
8#include <winpr/path.h>
9
10static UINT32 prand(UINT32 max)
11{
12 UINT32 tmp = 0;
13 if (max <= 1)
14 return 1;
15 if (winpr_RAND(&tmp, sizeof(tmp)) < 0)
16 {
17 // NOLINTNEXTLINE(concurrency-mt-unsafe)
18 exit(-1);
19 }
20 return tmp % (max - 1) + 1;
21}
22
23int TestPathMakePath(int argc, char* argv[])
24{
25 size_t baseLen = 0;
26 BOOL success = 0;
27 char tmp[64] = WINPR_C_ARRAY_INIT;
28 char* path = nullptr;
29 char* cur = nullptr;
30 char delim = PathGetSeparatorA(0);
31 char* base = GetKnownPath(KNOWN_PATH_TEMP);
32
33 WINPR_UNUSED(argc);
34 WINPR_UNUSED(argv);
35
36 if (!base)
37 {
38 (void)fprintf(stderr, "Failed to get temporary directory!\n");
39 return -1;
40 }
41
42 baseLen = strlen(base);
43
44 for (int x = 0; x < 5; x++)
45 {
46 (void)sprintf_s(tmp, ARRAYSIZE(tmp), "%08" PRIX32, prand(UINT32_MAX));
47 path = GetCombinedPath(base, tmp);
48 free(base);
49
50 if (!path)
51 {
52 (void)fprintf(stderr, "GetCombinedPath failed!\n");
53 return -1;
54 }
55
56 base = path;
57 }
58
59 printf("Creating path %s\n", path);
60 success = winpr_PathMakePath(path, nullptr);
61
62 if (!success)
63 {
64 (void)fprintf(stderr, "MakePath failed!\n");
65 free(path);
66 return -1;
67 }
68
69 success = winpr_PathFileExists(path);
70
71 if (!success)
72 {
73 (void)fprintf(stderr, "MakePath lied about success!\n");
74 free(path);
75 return -1;
76 }
77
78 while (strlen(path) > baseLen)
79 {
80 if (!winpr_RemoveDirectory(path))
81 {
82 (void)fprintf(stderr, "winpr_RemoveDirectory %s failed!\n", path);
83 free(path);
84 return -1;
85 }
86
87 cur = strrchr(path, delim);
88
89 if (cur)
90 *cur = '\0';
91 }
92
93 free(path);
94 printf("%s success!\n", __func__);
95 return 0;
96}