| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <winpr/crt.h> |
| 5 |
#include <winpr/file.h> |
| 6 |
#include <winpr/path.h> |
| 7 |
#include <winpr/windows.h> |
| 8 |
|
| 9 |
#if !defined(_WIN32) |
| 10 |
#include <sys/stat.h> |
| 11 |
#endif |
| 12 |
|
| 13 |
static int secure_mkstemp(char* tmpname) |
| 14 |
{ |
| 15 |
#if !defined(_WIN32) |
| 16 |
const mode_t mask = umask(S_IRWXU); |
| 17 |
#endif |
| 18 |
int fd = mkstemp(tmpname); |
| 19 |
#if !defined(_WIN32) |
| 20 |
(void)umask(mask); |
| 21 |
#endif |
| 22 |
return fd; |
| 23 |
} |
| 24 |
|
| 25 |
int TestFileDeleteFile(int argc, char* argv[]) |
| 26 |
{ |
| 27 |
BOOL rc = FALSE; |
| 28 |
int fd = 0; |
| 29 |
char validA[] = "/tmp/valid-test-file-XXXXXX"; |
| 30 |
char validW[] = "/tmp/valid-test-file-XXXXXX"; |
| 31 |
WCHAR* validWW = nullptr; |
| 32 |
const char invalidA[] = "/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
| 33 |
WCHAR invalidW[sizeof(invalidA)] = WINPR_C_ARRAY_INIT; |
| 34 |
|
| 35 |
(void)ConvertUtf8NToWChar(invalidA, ARRAYSIZE(invalidA), invalidW, ARRAYSIZE(invalidW)); |
| 36 |
|
| 37 |
WINPR_UNUSED(argc); |
| 38 |
WINPR_UNUSED(argv); |
| 39 |
|
| 40 |
rc = winpr_DeleteFile(invalidA); |
| 41 |
if (rc) |
| 42 |
return -1; |
| 43 |
|
| 44 |
rc = DeleteFileW(invalidW); |
| 45 |
if (rc) |
| 46 |
return -1; |
| 47 |
|
| 48 |
fd = secure_mkstemp(validA); |
| 49 |
if (fd < 0) |
| 50 |
return -1; |
| 51 |
|
| 52 |
rc = winpr_DeleteFile(validA); |
| 53 |
if (!rc) |
| 54 |
return -1; |
| 55 |
|
| 56 |
fd = secure_mkstemp(validW); |
| 57 |
if (fd < 0) |
| 58 |
return -1; |
| 59 |
|
| 60 |
validWW = ConvertUtf8NToWCharAlloc(validW, ARRAYSIZE(validW), nullptr); |
| 61 |
if (validWW) |
| 62 |
rc = DeleteFileW(validWW); |
| 63 |
free(validWW); |
| 64 |
if (!rc) |
| 65 |
return -1; |
| 66 |
return 0; |
| 67 |
} |