| 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 1) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the WriteFile function. |
| 10 | ** This test will attempt to write to a NULL handle and a |
| 11 | ** read-only file |
| 12 | ** |
| 13 | ** |
| 14 | **===================================================================*/ |
| 15 | |
| 16 | #include <palsuite.h> |
| 17 | |
| 18 | |
| 19 | const char* szStringTest = "The quick fox jumped over the lazy dog's back." ; |
| 20 | const char* szReadOnlyFile = "ReadOnly.txt" ; |
| 21 | void do_cleanup() |
| 22 | { |
| 23 | BOOL bRc = FALSE; |
| 24 | bRc = DeleteFileA(szReadOnlyFile); |
| 25 | if (bRc != TRUE) |
| 26 | { |
| 27 | Fail ("DeleteFileA: ERROR[%ld]: During Cleanup: Couldn't delete WriteFile's" |
| 28 | " \"ReadOnly.txt\"\n" , GetLastError()); |
| 29 | } |
| 30 | |
| 31 | } |
| 32 | |
| 33 | int __cdecl main(int argc, char *argv[]) |
| 34 | { |
| 35 | HANDLE hFile = NULL; |
| 36 | DWORD dwBytesWritten; |
| 37 | BOOL bRc = FALSE; |
| 38 | DWORD last_error; |
| 39 | |
| 40 | if (0 != PAL_Initialize(argc,argv)) |
| 41 | { |
| 42 | return FAIL; |
| 43 | } |
| 44 | |
| 45 | // |
| 46 | // Write to a NULL handle |
| 47 | // |
| 48 | |
| 49 | bRc = WriteFile(hFile, szStringTest, 20, &dwBytesWritten, NULL); |
| 50 | |
| 51 | if (bRc == TRUE) |
| 52 | { |
| 53 | last_error = GetLastError(); |
| 54 | Fail("WriteFile: ERROR[%ld] -> Able to write to a NULL handle\n" , last_error); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | // |
| 59 | // Write to a file with read-only permissions |
| 60 | // |
| 61 | |
| 62 | // create a file without write permissions |
| 63 | hFile = CreateFile(szReadOnlyFile, |
| 64 | GENERIC_READ, |
| 65 | FILE_SHARE_READ, |
| 66 | NULL, |
| 67 | OPEN_ALWAYS, |
| 68 | FILE_ATTRIBUTE_NORMAL, |
| 69 | NULL); |
| 70 | |
| 71 | if(hFile == INVALID_HANDLE_VALUE) |
| 72 | { |
| 73 | last_error = GetLastError(); |
| 74 | Fail("WriteFile: ERROR[%ld] -> Unable to create file \"%s\".\n" , |
| 75 | last_error, szReadOnlyFile); |
| 76 | } |
| 77 | |
| 78 | if (!SetFileAttributes(szReadOnlyFile, FILE_ATTRIBUTE_READONLY)) |
| 79 | { |
| 80 | last_error = GetLastError(); |
| 81 | Trace("WriteFile: ERROR[%ld] -> Unable to make the file read-only.\n" , last_error); |
| 82 | do_cleanup(); |
| 83 | Fail("WriteFile: ERROR[%ld] -> Unable to make the file read-only.\n" , last_error); |
| 84 | } |
| 85 | |
| 86 | bRc = WriteFile(hFile, szStringTest, 20, &dwBytesWritten, NULL); |
| 87 | if (bRc == TRUE) |
| 88 | { last_error = GetLastError(); |
| 89 | Trace("WriteFile: ERROR[%ld] -> Able to write to a read-only file.\n" , last_error); |
| 90 | do_cleanup(); |
| 91 | Fail("WriteFile: ERROR[%ld] -> Able to write to a read-only file.\n" , last_error); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | bRc = CloseHandle(hFile); |
| 96 | if (bRc != TRUE) |
| 97 | { last_error = GetLastError(); |
| 98 | Trace("WriteFile: ERROR[%ld] -> Unable to close file \"%s\".\n" , last_error, szReadOnlyFile); |
| 99 | do_cleanup(); |
| 100 | Fail("WriteFile: ERROR -> Unable to close file \"%s\".\n" , |
| 101 | szReadOnlyFile); |
| 102 | } |
| 103 | |
| 104 | //To delete file need to make it normal |
| 105 | if(!SetFileAttributesA(szReadOnlyFile,FILE_ATTRIBUTE_NORMAL)) |
| 106 | { |
| 107 | last_error = GetLastError(); |
| 108 | Fail("WriteFile: ERROR[%ld] -> Unable to make the file attribute NORMAL.\n" , last_error); |
| 109 | |
| 110 | } |
| 111 | do_cleanup(); |
| 112 | PAL_Terminate(); |
| 113 | return PASS; |
| 114 | } |
| 115 | |