From a9bcc81f821d7c66f623779fa5147e728eb3c388 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 03:24:41 +0200 Subject: Adding upstream version 3.3.0+dfsg1. Signed-off-by: Daniel Baumann --- winpr/libwinpr/file/test/TestFileCreateFile.c | 94 +++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 winpr/libwinpr/file/test/TestFileCreateFile.c (limited to 'winpr/libwinpr/file/test/TestFileCreateFile.c') diff --git a/winpr/libwinpr/file/test/TestFileCreateFile.c b/winpr/libwinpr/file/test/TestFileCreateFile.c new file mode 100644 index 0000000..a939416 --- /dev/null +++ b/winpr/libwinpr/file/test/TestFileCreateFile.c @@ -0,0 +1,94 @@ + +#include +#include +#include +#include +#include +#include +#include + +int TestFileCreateFile(int argc, char* argv[]) +{ + HANDLE handle = NULL; + HRESULT hr = 0; + DWORD written = 0; + const char buffer[] = "Some random text\r\njust want it done."; + char cmp[sizeof(buffer)]; + char sname[8192]; + LPSTR name = NULL; + int rc = 0; + SYSTEMTIME systemTime; + WINPR_UNUSED(argc); + WINPR_UNUSED(argv); + GetSystemTime(&systemTime); + sprintf_s(sname, sizeof(sname), + "CreateFile-%04" PRIu16 "%02" PRIu16 "%02" PRIu16 "%02" PRIu16 "%02" PRIu16 + "%02" PRIu16 "%04" PRIu16, + systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, + systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds); + name = GetKnownSubPath(KNOWN_PATH_TEMP, sname); + + if (!name) + return -1; + + /* On windows we would need '\\' or '/' as seperator. + * Single '\' do not work. */ + hr = PathCchConvertStyleA(name, strlen(name), PATH_STYLE_UNIX); + + if (FAILED(hr)) + rc = -1; + + handle = CreateFileA(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, + FILE_ATTRIBUTE_NORMAL, NULL); + + if (!handle) + { + free(name); + return -1; + } + + if (!winpr_PathFileExists(name)) + rc = -1; + + if (!WriteFile(handle, buffer, sizeof(buffer), &written, NULL)) + rc = -1; + + if (written != sizeof(buffer)) + rc = -1; + + written = SetFilePointer(handle, 5, NULL, FILE_BEGIN); + + if (written != 5) + rc = -1; + + written = SetFilePointer(handle, 0, NULL, FILE_CURRENT); + + if (written != 5) + rc = -1; + + written = SetFilePointer(handle, -5, NULL, FILE_CURRENT); + + if (written != 0) + rc = -1; + + if (!ReadFile(handle, cmp, sizeof(cmp), &written, NULL)) + rc = -1; + + if (written != sizeof(cmp)) + rc = -1; + + if (memcmp(buffer, cmp, sizeof(buffer))) + rc = -1; + + if (!CloseHandle(handle)) + rc = -1; + + if (!winpr_DeleteFile(name)) + rc = -1; + + if (winpr_PathFileExists(name)) + rc = -1; + + free(name); + return rc; +} -- cgit v1.2.3