| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*===================================================================== |
| 6 | ** |
| 7 | ** Source: WriteFile.c (test 3) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the WriteFile function. |
| 10 | ** Performs multiple writes to a file and verifies the results. |
| 11 | ** |
| 12 | ** |
| 13 | **===================================================================*/ |
| 14 | |
| 15 | |
| 16 | #include <palsuite.h> |
| 17 | |
| 18 | |
| 19 | const char* szStringTest = "The quick fox jumped over the lazy dog's back.\0" ; |
| 20 | const char* szWritableFile = "writeable.txt" ; |
| 21 | |
| 22 | |
| 23 | BOOL validateResults(const char* szString) |
| 24 | { |
| 25 | FILE *pFile = NULL; |
| 26 | char szReadString[100]; |
| 27 | DWORD dwBytesRead; |
| 28 | DWORD dwStringLength = strlen(szString); |
| 29 | |
| 30 | |
| 31 | |
| 32 | memset(szReadString, 0, 100); |
| 33 | |
| 34 | /* open the file */ |
| 35 | pFile = fopen(szWritableFile, "r" ); |
| 36 | if (pFile == NULL) |
| 37 | { |
| 38 | Trace("couldn't open test file\n" ); |
| 39 | return FALSE; |
| 40 | } |
| 41 | |
| 42 | dwBytesRead = fread(szReadString, sizeof(char), dwStringLength, pFile); |
| 43 | fclose(pFile); |
| 44 | |
| 45 | if(dwBytesRead != dwStringLength) |
| 46 | { |
| 47 | Trace("dwbyteread != string length\n" ); |
| 48 | return FALSE; |
| 49 | } |
| 50 | |
| 51 | if (strcmp(szReadString, szString)) |
| 52 | { |
| 53 | Trace("read = %s string = %s" , szReadString, szString); |
| 54 | return FALSE; |
| 55 | } |
| 56 | return TRUE; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | BOOL writeTest(const char* szString) |
| 63 | { |
| 64 | HANDLE hFile = NULL; |
| 65 | DWORD dwBytesWritten; |
| 66 | BOOL bRc = FALSE; |
| 67 | BOOL bAllPassed = TRUE; |
| 68 | int nStringLength = 0; |
| 69 | char* szPtr = NULL; |
| 70 | int i = 0; |
| 71 | |
| 72 | // create the test file |
| 73 | hFile = CreateFile(szWritableFile, |
| 74 | GENERIC_WRITE, |
| 75 | FILE_SHARE_WRITE, |
| 76 | NULL, |
| 77 | CREATE_ALWAYS, |
| 78 | FILE_ATTRIBUTE_NORMAL, |
| 79 | NULL); |
| 80 | |
| 81 | if(hFile == INVALID_HANDLE_VALUE) |
| 82 | { |
| 83 | Trace("WriteFile: ERROR -> Unable to create file \"%s\".\n" , |
| 84 | szWritableFile); |
| 85 | return FALSE; |
| 86 | } |
| 87 | |
| 88 | nStringLength = strlen(szString); |
| 89 | szPtr = (char*) szString; |
| 90 | |
| 91 | for (i = 0; i < nStringLength; i++) |
| 92 | { |
| 93 | bRc = WriteFile(hFile, szPtr++, 1, &dwBytesWritten, NULL); |
| 94 | if ((bRc == FALSE) || (dwBytesWritten != 1)) |
| 95 | { |
| 96 | bAllPassed = FALSE; |
| 97 | } |
| 98 | } |
| 99 | CloseHandle(hFile); |
| 100 | |
| 101 | if (bAllPassed == FALSE) |
| 102 | { |
| 103 | Trace ("WriteFile: ERROR: Failed to write data.\n" ); |
| 104 | return FALSE; |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | return (validateResults(szString)); |
| 109 | } |
| 110 | |
| 111 | return TRUE; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | int __cdecl main(int argc, char *argv[]) |
| 118 | { |
| 119 | const char *pString = szStringTest; |
| 120 | BOOL bRc = FALSE; |
| 121 | |
| 122 | if (0 != PAL_Initialize(argc,argv)) |
| 123 | { |
| 124 | return FAIL; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | bRc = writeTest(pString); |
| 129 | if (bRc != TRUE) |
| 130 | { |
| 131 | Fail("WriteFile: ERROR -> Failed\n" ); |
| 132 | } |
| 133 | |
| 134 | PAL_Terminate(); |
| 135 | return PASS; |
| 136 | } |
| 137 | |