| 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 5) | 
|---|
| 8 | ** | 
|---|
| 9 | ** Purpose: Tests the PAL implementation of the WriteFile function. | 
|---|
| 10 | **          Performs writing a huge file. | 
|---|
| 11 | ** | 
|---|
| 12 | ** dependency: | 
|---|
| 13 | **          CreateFile. | 
|---|
| 14 | **          GetFileSize. | 
|---|
| 15 | **          FlushFileBuffers | 
|---|
| 16 | **          CloseHandle | 
|---|
| 17 | **          DeleteFile | 
|---|
| 18 | ** | 
|---|
| 19 | ** | 
|---|
| 20 | **===================================================================*/ | 
|---|
| 21 |  | 
|---|
| 22 |  | 
|---|
| 23 | #include <palsuite.h> | 
|---|
| 24 |  | 
|---|
| 25 | BOOL CleanUp(HANDLE hFile, const char * fileName) | 
|---|
| 26 | { | 
|---|
| 27 | BOOL bRc = TRUE; | 
|---|
| 28 | if (CloseHandle(hFile) != TRUE) | 
|---|
| 29 | { | 
|---|
| 30 | bRc = FALSE; | 
|---|
| 31 | Trace( "WriteFile: ERROR -> Unable to close file \"%s\"," | 
|---|
| 32 | " error: %ld.\n", fileName, GetLastError()); | 
|---|
| 33 | } | 
|---|
| 34 | if (!DeleteFileA(fileName)) | 
|---|
| 35 | { | 
|---|
| 36 | bRc = FALSE; | 
|---|
| 37 | Trace( "WriteFile: ERROR -> Unable to delete file \"%s\"," | 
|---|
| 38 | " error: %ld.\n", fileName, GetLastError()); | 
|---|
| 39 | } | 
|---|
| 40 | return bRc; | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | int __cdecl main(int argc, char *argv[]) | 
|---|
| 45 | { | 
|---|
| 46 |  | 
|---|
| 47 | HANDLE hFile = NULL; | 
|---|
| 48 | DWORD dwBytesWritten; | 
|---|
| 49 | const char* hugeStringTest = | 
|---|
| 50 | "1234567890123456789012345678901234567890"; | 
|---|
| 51 | const char* szWritableFile = "writeable.txt"; | 
|---|
| 52 | int i =0; | 
|---|
| 53 | if (0 != PAL_Initialize(argc,argv)) | 
|---|
| 54 | { | 
|---|
| 55 | return FAIL; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | /* create the test file         */ | 
|---|
| 59 | hFile = CreateFile(szWritableFile, | 
|---|
| 60 | GENERIC_WRITE, | 
|---|
| 61 | FILE_SHARE_WRITE, | 
|---|
| 62 | NULL, | 
|---|
| 63 | CREATE_ALWAYS, | 
|---|
| 64 | FILE_ATTRIBUTE_NORMAL, | 
|---|
| 65 | NULL); | 
|---|
| 66 |  | 
|---|
| 67 | if(hFile == INVALID_HANDLE_VALUE) | 
|---|
| 68 | { | 
|---|
| 69 | Fail( "WriteFile: ERROR -> Unable to create file \"%s\".\n", | 
|---|
| 70 | szWritableFile); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | /* write 4000 000 chars to the file.*/ | 
|---|
| 74 | for (i=0; i<100000;i++) | 
|---|
| 75 | { | 
|---|
| 76 | if( WriteFile(hFile,        /* HANDLE handle to file    */ | 
|---|
| 77 | hugeStringTest,         /* data buffer              */ | 
|---|
| 78 | strlen(hugeStringTest), /* number of bytes to write */ | 
|---|
| 79 | &dwBytesWritten,        /* number of bytes written  */ | 
|---|
| 80 | NULL)                   /* overlapped buffer        */ | 
|---|
| 81 | ==0) | 
|---|
| 82 | { | 
|---|
| 83 | Trace( "WriteFile: ERROR -> Unable to write to file error: %ld \n", | 
|---|
| 84 | GetLastError()); | 
|---|
| 85 | CleanUp(hFile,szWritableFile); | 
|---|
| 86 | Fail( ""); | 
|---|
| 87 |  | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | if(!FlushFileBuffers(hFile)) | 
|---|
| 92 | { | 
|---|
| 93 | Trace( "WriteFile: ERROR -> Call to FlushFileBuffers failed" | 
|---|
| 94 | "error %ld \n",GetLastError()); | 
|---|
| 95 | CleanUp(hFile,szWritableFile); | 
|---|
| 96 | Fail( ""); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | /* test if the size changed properly. */ | 
|---|
| 100 | if(GetFileSize(hFile,NULL) != 4000000) | 
|---|
| 101 | { | 
|---|
| 102 | Trace( "WriteFile: ERROR -> file size did not change properly" | 
|---|
| 103 | " after writing 4000 000 chars to it ( size= %u )\n", | 
|---|
| 104 | GetFileSize(hFile,NULL)); | 
|---|
| 105 | CleanUp(hFile,szWritableFile); | 
|---|
| 106 | Fail( ""); | 
|---|
| 107 |  | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | if (!CleanUp(hFile,szWritableFile)) | 
|---|
| 111 | { | 
|---|
| 112 | Fail( ""); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | PAL_Terminate(); | 
|---|
| 116 | return PASS; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|