| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <winpr/crt.h> |
| 4 |
#include <winpr/file.h> |
| 5 |
#include <winpr/path.h> |
| 6 |
#include <winpr/windows.h> |
| 7 |
|
| 8 |
static const char* get_dir(char* filename, size_t len) |
| 9 |
{ |
| 10 |
#if defined(WIN32) |
| 11 |
if ((len == 0) || (strnlen_s(filename, len) == len)) |
| 12 |
return nullptr; |
| 13 |
char* ptr = strrchr(filename, '\\'); |
| 14 |
#else |
| 15 |
if ((len == 0) || (strnlen(filename, len) == len)) |
| 16 |
return nullptr; |
| 17 |
char* ptr = strrchr(filename, '/'); |
| 18 |
#endif |
| 19 |
if (!ptr) |
| 20 |
return nullptr; |
| 21 |
*ptr = '\0'; |
| 22 |
return filename; |
| 23 |
} |
| 24 |
|
| 25 |
static BOOL get_tmp(char* path, size_t len) |
| 26 |
{ |
| 27 |
#if defined(WIN32) |
| 28 |
const char template[] = "tmpdir.XXXXXX"; |
| 29 |
strncmp(path, template, strnlen_s(template, len) + 1); |
| 30 |
if (!mktemp_s(path)) |
| 31 |
return FALSE; |
| 32 |
return winpr_str_append("testfile", path, len, "\\"); |
| 33 |
#else |
| 34 |
const char template[] = "/tmp/tmpdir.XXXXXX"; |
| 35 |
if (!strncpy(path, template, strnlen(template, len) + 1)) |
| 36 |
return FALSE; |
| 37 |
if (!mkdtemp(path)) |
| 38 |
return FALSE; |
| 39 |
return winpr_str_append("testfile", path, len, "/"); |
| 40 |
#endif |
| 41 |
} |
| 42 |
|
| 43 |
static BOOL test_write(const char* filename, const char* data, size_t datalen) |
| 44 |
{ |
| 45 |
BOOL rc = FALSE; |
| 46 |
|
| 47 |
WINPR_ASSERT(filename); |
| 48 |
WINPR_ASSERT(data); |
| 49 |
WINPR_ASSERT(datalen > 0); |
| 50 |
|
| 51 |
HANDLE hdl = winpr_CreateFile(filename, GENERIC_WRITE, 0, nullptr, CREATE_NEW, |
| 52 |
FILE_ATTRIBUTE_NORMAL, nullptr); |
| 53 |
if (!hdl || (hdl == INVALID_HANDLE_VALUE)) |
| 54 |
goto fail; |
| 55 |
|
| 56 |
DWORD written = 0; |
| 57 |
if (!WriteFile(hdl, data, datalen, &written, nullptr)) |
| 58 |
goto fail; |
| 59 |
if (written != datalen) |
| 60 |
goto fail; |
| 61 |
|
| 62 |
if (!FlushFileBuffers(hdl)) |
| 63 |
goto fail; |
| 64 |
|
| 65 |
rc = TRUE; |
| 66 |
fail: |
| 67 |
CloseHandle(hdl); |
| 68 |
return rc; |
| 69 |
} |
| 70 |
|
| 71 |
static BOOL test_read(const char* filename, const char* data, size_t datalen) |
| 72 |
{ |
| 73 |
BOOL rc = FALSE; |
| 74 |
|
| 75 |
WINPR_ASSERT(filename); |
| 76 |
WINPR_ASSERT(data); |
| 77 |
WINPR_ASSERT(datalen > 0); |
| 78 |
|
| 79 |
char* cmp = calloc(datalen + 1, sizeof(char)); |
| 80 |
HANDLE hdl = winpr_CreateFile(filename, GENERIC_READ, 0, nullptr, OPEN_EXISTING, |
| 81 |
FILE_ATTRIBUTE_NORMAL, nullptr); |
| 82 |
if (!hdl || (hdl == INVALID_HANDLE_VALUE) || !cmp) |
| 83 |
goto fail; |
| 84 |
|
| 85 |
DWORD read = 0; |
| 86 |
if (!ReadFile(hdl, cmp, datalen, &read, nullptr)) |
| 87 |
goto fail; |
| 88 |
if (read != datalen) |
| 89 |
goto fail; |
| 90 |
if (memcmp(data, cmp, datalen) != 0) |
| 91 |
goto fail; |
| 92 |
if (FlushFileBuffers(hdl)) |
| 93 |
goto fail; |
| 94 |
|
| 95 |
rc = TRUE; |
| 96 |
fail: |
| 97 |
free(cmp); |
| 98 |
CloseHandle(hdl); |
| 99 |
return rc; |
| 100 |
} |
| 101 |
|
| 102 |
int TestFileWriteFile(int argc, char* argv[]) |
| 103 |
{ |
| 104 |
const char data[] = "sometesttext\nanother line\r\ngogogo\r\tfoo\t\r\n\r"; |
| 105 |
char filename[MAX_PATH] = WINPR_C_ARRAY_INIT; |
| 106 |
|
| 107 |
int rc = -1; |
| 108 |
if (!get_tmp(filename, sizeof(filename))) |
| 109 |
goto fail; |
| 110 |
|
| 111 |
if (!test_write(filename, data, sizeof(data))) |
| 112 |
goto fail; |
| 113 |
|
| 114 |
if (!test_read(filename, data, sizeof(data))) |
| 115 |
goto fail; |
| 116 |
|
| 117 |
rc = 0; |
| 118 |
fail: |
| 119 |
if (!winpr_DeleteFile(filename)) |
| 120 |
rc = -2; |
| 121 |
|
| 122 |
const char* d = get_dir(filename, sizeof(filename)); |
| 123 |
if (d) |
| 124 |
{ |
| 125 |
if (!winpr_RemoveDirectory(d)) |
| 126 |
rc = -3; |
| 127 |
} |
| 128 |
return rc; |
| 129 |
} |