diff options
Diffstat (limited to 'src/pal/tests/palsuite/file_io/WriteFile')
17 files changed, 802 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/file_io/WriteFile/CMakeLists.txt b/src/pal/tests/palsuite/file_io/WriteFile/CMakeLists.txt new file mode 100644 index 0000000000..8083faf655 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +add_subdirectory(test1) +add_subdirectory(test2) +add_subdirectory(test3) +add_subdirectory(test4) +add_subdirectory(test5) + diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test1/CMakeLists.txt b/src/pal/tests/palsuite/file_io/WriteFile/test1/CMakeLists.txt new file mode 100644 index 0000000000..0c6760a2ce --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test1/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + WriteFile.c +) + +add_executable(paltest_writefile_test1 + ${SOURCES} +) + +add_dependencies(paltest_writefile_test1 coreclrpal) + +target_link_libraries(paltest_writefile_test1 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test1/WriteFile.c b/src/pal/tests/palsuite/file_io/WriteFile/test1/WriteFile.c new file mode 100644 index 0000000000..1ac38ddaf0 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test1/WriteFile.c @@ -0,0 +1,114 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +/*===================================================================== +** +** Source: WriteFile.c (test 1) +** +** Purpose: Tests the PAL implementation of the WriteFile function. +** This test will attempt to write to a NULL handle and a +** read-only file +** +** +**===================================================================*/ + +#include <palsuite.h> + + +const char* szStringTest = "The quick fox jumped over the lazy dog's back."; +const char* szReadOnlyFile = "ReadOnly.txt"; +void do_cleanup() +{ + BOOL bRc = FALSE; + bRc = DeleteFileA(szReadOnlyFile); + if (bRc != TRUE) + { + Fail ("DeleteFileA: ERROR[%ld]: During Cleanup: Couldn't delete WriteFile's" + " \"ReadOnly.txt\"\n", GetLastError()); + } + +} + +int __cdecl main(int argc, char *argv[]) +{ + HANDLE hFile = NULL; + DWORD dwBytesWritten; + BOOL bRc = FALSE; + DWORD last_error; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + // + // Write to a NULL handle + // + + bRc = WriteFile(hFile, szStringTest, 20, &dwBytesWritten, NULL); + + if (bRc == TRUE) + { + last_error = GetLastError(); + Fail("WriteFile: ERROR[%ld] -> Able to write to a NULL handle\n", last_error); + } + + + // + // Write to a file with read-only permissions + // + + // create a file without write permissions + hFile = CreateFile(szReadOnlyFile, + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + last_error = GetLastError(); + Fail("WriteFile: ERROR[%ld] -> Unable to create file \"%s\".\n", + last_error, szReadOnlyFile); + } + + if (!SetFileAttributes(szReadOnlyFile, FILE_ATTRIBUTE_READONLY)) + { + last_error = GetLastError(); + Trace("WriteFile: ERROR[%ld] -> Unable to make the file read-only.\n", last_error); + do_cleanup(); + Fail("WriteFile: ERROR[%ld] -> Unable to make the file read-only.\n", last_error); + } + + bRc = WriteFile(hFile, szStringTest, 20, &dwBytesWritten, NULL); + if (bRc == TRUE) + { last_error = GetLastError(); + Trace("WriteFile: ERROR[%ld] -> Able to write to a read-only file.\n", last_error); + do_cleanup(); + Fail("WriteFile: ERROR[%ld] -> Able to write to a read-only file.\n", last_error); + } + + + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { last_error = GetLastError(); + Trace("WriteFile: ERROR[%ld] -> Unable to close file \"%s\".\n", last_error, szReadOnlyFile); + do_cleanup(); + Fail("WriteFile: ERROR -> Unable to close file \"%s\".\n", + szReadOnlyFile); + } + + //To delete file need to make it normal + if(!SetFileAttributesA(szReadOnlyFile,FILE_ATTRIBUTE_NORMAL)) + { + last_error = GetLastError(); + Fail("WriteFile: ERROR[%ld] -> Unable to make the file attribute NORMAL.\n", last_error); + + } + do_cleanup(); + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test1/testinfo.dat b/src/pal/tests/palsuite/file_io/WriteFile/test1/testinfo.dat new file mode 100644 index 0000000000..148a2678e5 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test1/testinfo.dat @@ -0,0 +1,13 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +Version = 1.0 +Section = file_io +Function = WriteFile +Name = test for WriteFile +Type = DEFAULT +EXE1 = writefile +Description +=Attempt to write to a NULL handle and to a read-only file + diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test2/CMakeLists.txt b/src/pal/tests/palsuite/file_io/WriteFile/test2/CMakeLists.txt new file mode 100644 index 0000000000..a9b51efa3d --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test2/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + WriteFile.c +) + +add_executable(paltest_writefile_test2 + ${SOURCES} +) + +add_dependencies(paltest_writefile_test2 coreclrpal) + +target_link_libraries(paltest_writefile_test2 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test2/Results.txt b/src/pal/tests/palsuite/file_io/WriteFile/test2/Results.txt new file mode 100644 index 0000000000..1c4c37885a --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test2/Results.txt @@ -0,0 +1 @@ +0011100111
\ No newline at end of file diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test2/WriteFile.c b/src/pal/tests/palsuite/file_io/WriteFile/test2/WriteFile.c new file mode 100644 index 0000000000..24c148afb8 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test2/WriteFile.c @@ -0,0 +1,109 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +/*===================================================================== +** +** Source: WriteFile.c (test 2) +** +** Purpose: Tests the PAL implementation of the WriteFile function. +** Creates a number of files and writes different amounts of +** data and verifies the writes. +** +** +**===================================================================*/ + + +#include <palsuite.h> + + +char* writeBuffer; +const char* szWritableFile = "Writeable.txt"; +const char* szResultsFile = "Results.txt"; +#ifdef __sparc__ +const int PAGESIZE = 8192; +#else // __sparc__ +const int PAGESIZE = 4096; +#endif // __sparc__ + +BOOL writeTest(DWORD dwByteCount, DWORD dwBytesWrittenResult, BOOL bResult) +{ + HANDLE hFile = NULL; + DWORD dwBytesWritten; + BOOL bRc = FALSE; + + /* create the test file */ + DeleteFile(szWritableFile); + hFile = CreateFile(szWritableFile, GENERIC_WRITE, FILE_SHARE_WRITE, + NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Trace("WriteFile: ERROR -> Unable to create file \"%s\".\n", + szWritableFile); + return FALSE; + } + + bRc = WriteFile(hFile, writeBuffer, dwByteCount, &dwBytesWritten, NULL); + CloseHandle(hFile); + + if ((bRc != bResult) || (dwBytesWrittenResult != dwBytesWritten)) + { + Trace("WriteFile returned BOOL:%d and dwWritten:%d what we do expect is" + " BOOL:%d and dwWritten:%d\n", bRc, dwBytesWritten, bResult, + dwBytesWrittenResult); + return FALSE; + } + + return TRUE; +} + +int __cdecl main(int argc, char *argv[]) +{ + const char * testString = "The quick fox jumped over the lazy dog's back."; + const int testStringLen = strlen(testString); + + DWORD dwByteCount[4] = {-1, 10, testStringLen, 0}; + DWORD dwByteWritten[4] = {0, 10, testStringLen, 0}; + BOOL bResults[] = {FALSE, TRUE, TRUE, TRUE}; + + const int BUFFER_SIZE = 2 * PAGESIZE; + int j; + BOOL bRc = FALSE; + DWORD oldProt; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* allocate read-write memery for writeBuffer */ + if (!(writeBuffer = (char*) VirtualAlloc(NULL, BUFFER_SIZE, MEM_COMMIT, + PAGE_READWRITE))) + { + Fail("VirtualAlloc failed: GetLastError returns %d\n", GetLastError()); + return FAIL; + } + + memset((void*) writeBuffer, '.', BUFFER_SIZE); + strcpy(writeBuffer, testString); + + /* write protect the second page of writeBuffer */ + if (!VirtualProtect(&writeBuffer[PAGESIZE], PAGESIZE, PAGE_NOACCESS, &oldProt)) + { + Fail("VirtualProtect failed: GetLastError returns %d\n", GetLastError()); + return FAIL; + } + + for (j = 0; j< 4; j++) + { + bRc = writeTest(dwByteCount[j], dwByteWritten[j], bResults[j]); + if (bRc != TRUE) + { + Fail("WriteFile: ERROR -> Failed on test[%d]\n", j); + } + } + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test2/testinfo.dat b/src/pal/tests/palsuite/file_io/WriteFile/test2/testinfo.dat new file mode 100644 index 0000000000..a09df962f3 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test2/testinfo.dat @@ -0,0 +1,13 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +Version = 1.0 +Section = file_io +Function = WriteFile +Name = Positive Test for WriteFile +Type = DEFAULT +EXE1 = writefile +Description +=Multiple tests of writes of varying sizes with verification + diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test3/CMakeLists.txt b/src/pal/tests/palsuite/file_io/WriteFile/test3/CMakeLists.txt new file mode 100644 index 0000000000..e16e8a48f6 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test3/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + WriteFile.c +) + +add_executable(paltest_writefile_test3 + ${SOURCES} +) + +add_dependencies(paltest_writefile_test3 coreclrpal) + +target_link_libraries(paltest_writefile_test3 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test3/WriteFile.c b/src/pal/tests/palsuite/file_io/WriteFile/test3/WriteFile.c new file mode 100644 index 0000000000..751f89ff2c --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test3/WriteFile.c @@ -0,0 +1,136 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +/*===================================================================== +** +** Source: WriteFile.c (test 3) +** +** Purpose: Tests the PAL implementation of the WriteFile function. +** Performs multiple writes to a file and verifies the results. +** +** +**===================================================================*/ + + +#include <palsuite.h> + + +const char* szStringTest = "The quick fox jumped over the lazy dog's back.\0"; +const char* szWritableFile = "writeable.txt"; + + +BOOL validateResults(const char* szString) +{ + FILE *pFile = NULL; + char szReadString[100]; + DWORD dwBytesRead; + DWORD dwStringLength = strlen(szString); + + + + memset(szReadString, 0, 100); + + /* open the file */ + pFile = fopen(szWritableFile, "r"); + if (pFile == NULL) + { + Trace("couldn't open test file\n"); + return FALSE; + } + + dwBytesRead = fread(szReadString, sizeof(char), dwStringLength, pFile); + fclose(pFile); + + if(dwBytesRead != dwStringLength) + { + Trace("dwbyteread != string length\n"); + return FALSE; + } + + if (strcmp(szReadString, szString)) + { + Trace("read = %s string = %s", szReadString, szString); + return FALSE; + } + return TRUE; +} + + + + +BOOL writeTest(const char* szString) +{ + HANDLE hFile = NULL; + DWORD dwBytesWritten; + BOOL bRc = FALSE; + BOOL bAllPassed = TRUE; + int nStringLength = 0; + char* szPtr = NULL; + int i = 0; + + // create the test file + hFile = CreateFile(szWritableFile, + GENERIC_WRITE, + FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Trace("WriteFile: ERROR -> Unable to create file \"%s\".\n", + szWritableFile); + return FALSE; + } + + nStringLength = strlen(szString); + szPtr = (char*) szString; + + for (i = 0; i < nStringLength; i++) + { + bRc = WriteFile(hFile, szPtr++, 1, &dwBytesWritten, NULL); + if ((bRc == FALSE) || (dwBytesWritten != 1)) + { + bAllPassed = FALSE; + } + } + CloseHandle(hFile); + + if (bAllPassed == FALSE) + { + Trace ("WriteFile: ERROR: Failed to write data.\n"); + return FALSE; + } + else + { + return (validateResults(szString)); + } + + return TRUE; +} + + + + +int __cdecl main(int argc, char *argv[]) +{ + const char *pString = szStringTest; + BOOL bRc = FALSE; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + + bRc = writeTest(pString); + if (bRc != TRUE) + { + Fail("WriteFile: ERROR -> Failed\n"); + } + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test3/testinfo.dat b/src/pal/tests/palsuite/file_io/WriteFile/test3/testinfo.dat new file mode 100644 index 0000000000..e88e985b44 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test3/testinfo.dat @@ -0,0 +1,13 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +Version = 1.0 +Section = file_io +Function = WriteFile +Name = Positive Test for WriteFile +Type = DEFAULT +EXE1 = writefile +Description +=Multiple consecutive writes to a file with verification + diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test4/CMakeLists.txt b/src/pal/tests/palsuite/file_io/WriteFile/test4/CMakeLists.txt new file mode 100644 index 0000000000..fff886b430 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test4/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + writefile.c +) + +add_executable(paltest_writefile_test4 + ${SOURCES} +) + +add_dependencies(paltest_writefile_test4 coreclrpal) + +target_link_libraries(paltest_writefile_test4 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test4/testinfo.dat b/src/pal/tests/palsuite/file_io/WriteFile/test4/testinfo.dat new file mode 100644 index 0000000000..87ddf9d857 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test4/testinfo.dat @@ -0,0 +1,13 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +Version = 1.0 +Section = file_io +Function = WriteFile +Name = Positive Test for WriteFile +Type = DEFAULT +EXE1 = writefile +Description += writes to a file at different locations with verification + diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test4/writefile.c b/src/pal/tests/palsuite/file_io/WriteFile/test4/writefile.c new file mode 100644 index 0000000000..47a0066ec9 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test4/writefile.c @@ -0,0 +1,155 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +/*===================================================================== +** +** Source: WriteFile.c (test 4) +** +** Purpose: Tests the PAL implementation of the WriteFile function. +** Performs multiple writes to a file at different locations +** then verifies the results with GetFileSize. +** +** dependency: +** CreateFile. +** GetFileSize. +** FlushFileBuffers +** SetFilePointer. +** CloseHandle. +** DeleteFile. +** +** +** +**===================================================================*/ + + +#include <palsuite.h> + +BOOL CleanUp(HANDLE hFile, const char * fileName) +{ + BOOL bRc = TRUE; + if (CloseHandle(hFile) != TRUE) + { + bRc = FALSE; + Trace("WriteFile: ERROR -> Unable to close file \"%s\"," + " error: %ld.\n", fileName, GetLastError()); + } + if (!DeleteFileA(fileName)) + { + bRc = FALSE; + Trace("WriteFile: ERROR -> Unable to delete file \"%s\"," + " error: %ld.\n", fileName, GetLastError()); + } + return bRc; +} + +int __cdecl main(int argc, char *argv[]) +{ + const char* szStringTest = "1234567890"; + const char* szWritableFile = "writeable.txt"; + HANDLE hFile = NULL; + DWORD dwBytesWritten; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create the test file */ + hFile = CreateFile(szWritableFile, + GENERIC_WRITE, + FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("WriteFile: ERROR -> Unable to create file \"%s\".\n", + szWritableFile); + } + + + /* test wtriting to the file */ + if( WriteFile(hFile, /* HANDLE handle to file */ + szStringTest, /* data buffer */ + strlen(szStringTest), /* number of bytes to write */ + &dwBytesWritten, /* number of bytes written */ + NULL) /* overlapped buffer */ + ==0) + { + Trace("WriteFile: ERROR -> Unable to write to file error: %ld \n", + GetLastError()); + CleanUp(hFile,szWritableFile); + Fail(""); + } + + if(!FlushFileBuffers(hFile)) + { Trace("WriteFile: ERROR -> Call to FlushFile Buffers failed " + "error %ld \n",GetLastError()); + CleanUp(hFile,szWritableFile); + Fail(""); + } + + /* check the file size */ + if(GetFileSize(hFile, NULL)!=strlen(szStringTest)) + { + Trace("WriteFile: ERROR -> writing %u chars to empty file " + "caused its size to become %u\n",strlen(szStringTest), + GetFileSize(hFile, NULL)); + CleanUp(hFile,szWritableFile); + Fail(""); + } + + /* test writing to the file at position 5. */ + SetFilePointer( + hFile, /* handle to file */ + 0x5, /* bytes to move pointer */ + NULL, /* bytes to move pointer */ + FILE_BEGIN /* starting point */ + ); + + + if( WriteFile(hFile, /* HANDLE handle to file */ + szStringTest, /* data buffer */ + strlen(szStringTest), /* number of bytes to write */ + &dwBytesWritten, /* number of bytes written */ + NULL) /* overlapped buffer */ + ==0) + { + Trace("WriteFile: ERROR -> Unable to write to file after " + " moiving the file poiner to 5 error: %ld \n", + GetLastError()); + CleanUp(hFile,szWritableFile); + Fail(""); + } + + + if(!FlushFileBuffers(hFile)) + { + Trace("WriteFile: ERROR -> Call to FlushFile Buffers failed " + "error %ld \n",GetLastError()); + CleanUp(hFile,szWritableFile); + Fail(""); + } + + /* Check the file size */ + if(GetFileSize(hFile, NULL)!=(strlen(szStringTest)+5)) + { + Trace("WriteFile: ERROR -> writing %u chars to the file after " + "sitting the file pointer to 5 resulted in wrong file size; " + "Expected %u resulted %u.",strlen(szStringTest), + (strlen(szStringTest)+5),GetFileSize(hFile, NULL)); + CleanUp(hFile,szWritableFile); + Fail(""); + } + + if (!CleanUp(hFile,szWritableFile)) + { + Fail(""); + } + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test5/CMakeLists.txt b/src/pal/tests/palsuite/file_io/WriteFile/test5/CMakeLists.txt new file mode 100644 index 0000000000..978eb7f1e7 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test5/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + writefile.c +) + +add_executable(paltest_writefile_test5 + ${SOURCES} +) + +add_dependencies(paltest_writefile_test5 coreclrpal) + +target_link_libraries(paltest_writefile_test5 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test5/testinfo.dat b/src/pal/tests/palsuite/file_io/WriteFile/test5/testinfo.dat new file mode 100644 index 0000000000..ffde30ea23 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test5/testinfo.dat @@ -0,0 +1,15 @@ +# Licensed to the .NET Foundation under one or more agreements. +# The .NET Foundation licenses this file to you under the MIT license. +# See the LICENSE file in the project root for more information. + +Version = 1.0 +Section = file_io +Function = WriteFile +Name = Positive Test for WriteFile +Type = DEFAULT +EXE1 = writefile +Description += write lots of data to a file. then check with += GetFileSize += This test is disabled due to its time overhead. + diff --git a/src/pal/tests/palsuite/file_io/WriteFile/test5/writefile.c b/src/pal/tests/palsuite/file_io/WriteFile/test5/writefile.c new file mode 100644 index 0000000000..46920b3335 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/WriteFile/test5/writefile.c @@ -0,0 +1,117 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +/*===================================================================== +** +** Source: WriteFile.c (test 5) +** +** Purpose: Tests the PAL implementation of the WriteFile function. +** Performs writing a huge file. +** +** dependency: +** CreateFile. +** GetFileSize. +** FlushFileBuffers +** CloseHandle +** DeleteFile +** +** +**===================================================================*/ + + +#include <palsuite.h> + +BOOL CleanUp(HANDLE hFile, const char * fileName) +{ + BOOL bRc = TRUE; + if (CloseHandle(hFile) != TRUE) + { + bRc = FALSE; + Trace("WriteFile: ERROR -> Unable to close file \"%s\"," + " error: %ld.\n", fileName, GetLastError()); + } + if (!DeleteFileA(fileName)) + { + bRc = FALSE; + Trace("WriteFile: ERROR -> Unable to delete file \"%s\"," + " error: %ld.\n", fileName, GetLastError()); + } + return bRc; +} + + +int __cdecl main(int argc, char *argv[]) +{ + + HANDLE hFile = NULL; + DWORD dwBytesWritten; + const char* hugeStringTest = + "1234567890123456789012345678901234567890"; + const char* szWritableFile = "writeable.txt"; + int i =0; + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create the test file */ + hFile = CreateFile(szWritableFile, + GENERIC_WRITE, + FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("WriteFile: ERROR -> Unable to create file \"%s\".\n", + szWritableFile); + } + + /* write 4000 000 chars to the file.*/ + for (i=0; i<100000;i++) + { + if( WriteFile(hFile, /* HANDLE handle to file */ + hugeStringTest, /* data buffer */ + strlen(hugeStringTest), /* number of bytes to write */ + &dwBytesWritten, /* number of bytes written */ + NULL) /* overlapped buffer */ + ==0) + { + Trace("WriteFile: ERROR -> Unable to write to file error: %ld \n", + GetLastError()); + CleanUp(hFile,szWritableFile); + Fail(""); + + } + } + + if(!FlushFileBuffers(hFile)) + { + Trace("WriteFile: ERROR -> Call to FlushFileBuffers failed" + "error %ld \n",GetLastError()); + CleanUp(hFile,szWritableFile); + Fail(""); + } + + /* test if the size changed properly. */ + if(GetFileSize(hFile,NULL) != 4000000) + { + Trace("WriteFile: ERROR -> file size did not change properly" + " after writing 4000 000 chars to it ( size= %u )\n", + GetFileSize(hFile,NULL)); + CleanUp(hFile,szWritableFile); + Fail(""); + + } + + if (!CleanUp(hFile,szWritableFile)) + { + Fail(""); + } + + PAL_Terminate(); + return PASS; +} |