diff options
Diffstat (limited to 'src/pal/tests/palsuite/file_io/SetFilePointer')
22 files changed, 1916 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/CMakeLists.txt b/src/pal/tests/palsuite/file_io/SetFilePointer/CMakeLists.txt new file mode 100644 index 0000000000..19ee487a6a --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +add_subdirectory(test1) +add_subdirectory(test2) +add_subdirectory(test3) +add_subdirectory(test4) +add_subdirectory(test5) +add_subdirectory(test6) +add_subdirectory(test7) + diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test1/CMakeLists.txt b/src/pal/tests/palsuite/file_io/SetFilePointer/test1/CMakeLists.txt new file mode 100644 index 0000000000..e352449981 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test1/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + SetFilePointer.c +) + +add_executable(paltest_setfilepointer_test1 + ${SOURCES} +) + +add_dependencies(paltest_setfilepointer_test1 coreclrpal) + +target_link_libraries(paltest_setfilepointer_test1 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test1/SetFilePointer.c b/src/pal/tests/palsuite/file_io/SetFilePointer/test1/SetFilePointer.c new file mode 100644 index 0000000000..14b5f85e69 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test1/SetFilePointer.c @@ -0,0 +1,123 @@ +// 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: SetFilePointer.c (test 1) +** +** Purpose: Tests the PAL implementation of the SetFilePointer function. +** Set the file pointer using a NULL handle and other invalid +** options. +** +** +**===================================================================*/ + +#include <palsuite.h> + + +const char* szTextFile = "text.txt"; + + +int __cdecl main(int argc, char *argv[]) +{ + HANDLE hFile = NULL; + DWORD dwByteCount = 0; + DWORD dwOffset = 25; + DWORD dwRc = 0; + BOOL bRc = FALSE; + char buffer[100]; + + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* set the file pointer on a NULL file handle */ + dwRc = SetFilePointer(NULL, dwOffset, NULL, FILE_BEGIN); + if (dwRc != INVALID_SET_FILE_POINTER) + { + Fail("SetFilePointer: ERROR -> Call to SetFilePointer succeeded " + "with a NULL pointer\n"); + } + + + /* create a test file without proper permission */ + hFile = CreateFile(szTextFile, + 0, + 0, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n", + szTextFile); + } + + /* ReadFile fails as expected */ + bRc = ReadFile(hFile, buffer, 1, &dwByteCount, NULL); + if (bRc != FALSE) + { + Trace("SetFilePointer: ERROR -> ReadFile was successful when it was " + "expected to fail\n"); + if (!CloseHandle(hFile)) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* move the file pointer before the beginning of the file */ + dwRc = SetFilePointer(hFile, -1, NULL, FILE_BEGIN); + if (dwRc != INVALID_SET_FILE_POINTER) + { + Trace("SetFilePointer: ERROR -> Was able to move the pointer before " + "the beginning of the file.\n"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + if (!DeleteFileA(szTextFile)) + { + Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test1/testinfo.dat b/src/pal/tests/palsuite/file_io/SetFilePointer/test1/testinfo.dat new file mode 100644 index 0000000000..dfd4b6bd42 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/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 = SetFilePointer +Name = Test for SetFilePointer (test 1) +Type = DEFAULT +EXE1 = setfilepointer +Description +=Set the file pointer on a NULL file handle and other invalid options + diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test2/CMakeLists.txt b/src/pal/tests/palsuite/file_io/SetFilePointer/test2/CMakeLists.txt new file mode 100644 index 0000000000..290a01107f --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test2/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + SetFilePointer.c +) + +add_executable(paltest_setfilepointer_test2 + ${SOURCES} +) + +add_dependencies(paltest_setfilepointer_test2 coreclrpal) + +target_link_libraries(paltest_setfilepointer_test2 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test2/SetFilePointer.c b/src/pal/tests/palsuite/file_io/SetFilePointer/test2/SetFilePointer.c new file mode 100644 index 0000000000..19e99a74b3 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test2/SetFilePointer.c @@ -0,0 +1,356 @@ +// 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: SetFilePointer.c (test 2) +** +** Purpose: Tests the PAL implementation of the SetFilePointer function. +** Test the FILE_BEGIN option +** +** Assumes Successful: +** CreateFile +** ReadFile +** WriteFile +** strlen +** CloseHandle +** strcmp +** GetFileSize +** +** +**===================================================================*/ + +#include <palsuite.h> + +const char * const szText = + "The quick brown fox jumped over the lazy dog's back."; +const char* szTextFile = "text.txt"; + + +int __cdecl main(int argc, char *argv[]) +{ + HANDLE hFile = NULL; + DWORD dwByteCount = 0; + DWORD dwRc = 0; + BOOL bRc = FALSE; + char szBuffer[100]; + const char *szPtr; + + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create a test file */ + hFile = CreateFile(szTextFile, + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n", + szTextFile); + } + + bRc = WriteFile(hFile, szText, (DWORD)strlen(szText), &dwByteCount, NULL); + if (bRc == FALSE) + { + Trace("SetFilePointer: ERROR -> Unable to write to file \"%s\".\n", + szTextFile); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + + /* move -1 from beginning which should fail */ + dwRc = SetFilePointer(hFile, -1, NULL, FILE_BEGIN); + if ((dwRc != INVALID_SET_FILE_POINTER) || + (GetLastError() == ERROR_SUCCESS)) + { + Trace("SetFilePointer: ERROR -> Succeeded to move the pointer " + "before the beginning of the file.\n"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* move the file pointer 0 bytes from the beginning and verify */ + dwRc = SetFilePointer(hFile, 0, NULL, FILE_BEGIN); + if (dwRc != 0) + { + Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the " + "beginning of the file but moved %ld bytes.\n", dwRc); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* move the pointer ahead in the file and verify */ + dwRc = SetFilePointer(hFile, 20, NULL, FILE_BEGIN); + if (dwRc != 20) + { + Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the " + "beginning of the file but moved %ld bytes.\n", dwRc); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify results */ + memset(szBuffer, 0, 100); + bRc = ReadFile(hFile, szBuffer, (DWORD)strlen(szText)-20, &dwByteCount, + NULL); + if ((bRc != TRUE) || (dwByteCount != strlen(szText)-20)) + { + Trace("SetFilePointer: ERROR -> ReadFile failed to read correctly"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + "\"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + "\"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + szPtr = szText + 20; + if (strcmp(szPtr, szBuffer) != 0) + { + Trace("SetFilePointer: ERROR -> Apparently failed to move the " + "pointer properly\n"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + /* move the file pointer back to the beginning and verify */ + dwRc = SetFilePointer(hFile, 0, NULL, FILE_BEGIN); + if (dwRc != 0) + { + Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the " + "beginning of the file but moved %ld bytes.\n", dwRc); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify results */ + memset(szBuffer, 0, 100); + bRc = ReadFile(hFile, szBuffer, (DWORD)strlen(szText), &dwByteCount, + NULL); + if ((bRc != TRUE) || (dwByteCount != strlen(szText))) + { + Trace("SetFilePointer: ERROR -> ReadFile failed to read correctly"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + if (strcmp(szText, szBuffer) != 0) + { + Trace("SetFilePointer: ERROR -> Failed to return the pointer " + "properly to the beginning of the file\n"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + /* return the pointer to the beginning of the file */ + dwRc = SetFilePointer(hFile, 0, NULL, FILE_BEGIN); + if (dwRc != 0) + { + Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the " + "beginning of the file but moved %ld bytes.\n", dwRc); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* set the pointer past the end of the file and verify */ + dwRc = SetFilePointer(hFile, (DWORD)strlen(szText)+20, NULL, FILE_BEGIN); + if ((dwRc == INVALID_SET_FILE_POINTER) && (GetLastError() != ERROR_SUCCESS)) + { + Trace("SetFilePointer: ERROR -> Failed to move pointer past EOF.\n"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify */ + bRc = SetEndOfFile(hFile); + if (bRc != TRUE) + { + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + if (GetFileSize(hFile, NULL) != strlen(szText)+20) + { + Trace("SetFilePointer: ERROR -> Failed to move pointer past" + " EOF.\n"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + + + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + if (!DeleteFileA(szTextFile)) + { + Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test2/testinfo.dat b/src/pal/tests/palsuite/file_io/SetFilePointer/test2/testinfo.dat new file mode 100644 index 0000000000..e3a0a861f4 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/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 = SetFilePointer +Name = Positive Test for SetFilePointer (test 2) +Type = DEFAULT +EXE1 = setfilepointer +Description +=Tests the FILE_BEGIN option + diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test3/CMakeLists.txt b/src/pal/tests/palsuite/file_io/SetFilePointer/test3/CMakeLists.txt new file mode 100644 index 0000000000..daa7553a1a --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test3/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + SetFilePointer.c +) + +add_executable(paltest_setfilepointer_test3 + ${SOURCES} +) + +add_dependencies(paltest_setfilepointer_test3 coreclrpal) + +target_link_libraries(paltest_setfilepointer_test3 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test3/SetFilePointer.c b/src/pal/tests/palsuite/file_io/SetFilePointer/test3/SetFilePointer.c new file mode 100644 index 0000000000..dd53829629 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test3/SetFilePointer.c @@ -0,0 +1,350 @@ +// 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: SetFilePointer.c (test 3) +** +** Purpose: Tests the PAL implementation of the SetFilePointer function. +** Test the FILE_CURRENT option +** +** Assumes Successful: +** CreateFile +** ReadFile +** WriteFile +** strlen +** CloseHandle +** strcmp +** GetFileSize +** +** +**===================================================================*/ + +#include <palsuite.h> + +const char* const szText = + "The quick brown fox jumped over the lazy dog's back."; +const char* szTextFile = "text.txt"; + + +int __cdecl main(int argc, char *argv[]) +{ + HANDLE hFile = NULL; + DWORD dwByteCount = 0; + DWORD dwRc = 0; + BOOL bRc = FALSE; + char szBuffer[100]; + const char* szPtr; + + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create a test file */ + hFile = CreateFile(szTextFile, + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n", + szTextFile); + } + + bRc = WriteFile(hFile, szText, (DWORD)strlen(szText), &dwByteCount, NULL); + if (bRc == FALSE) + { + Trace("SetFilePointer: ERROR -> Unable to write to file \"%s\".\n", + szTextFile); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* reset the pointer to the beginning */ + if (SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) + { + if (GetLastError() != ERROR_SUCCESS) + { + Trace("SetFilePointer: ERROR -> Unable to reset the pointer to the " + "beginning of the file"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + /* move -1 from beginning which should fail */ + dwRc = SetFilePointer(hFile, -1, NULL, FILE_CURRENT); + if ((dwRc != INVALID_SET_FILE_POINTER) || + (GetLastError() == ERROR_SUCCESS)) + { + Trace("SetFilePointer: ERROR -> Succeeded to move the pointer " + "before the beginning of the file.\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + + /* move the file pointer 0 bytes from the beginning and verify */ + dwRc = SetFilePointer(hFile, 0, NULL, FILE_CURRENT); + if (dwRc != 0) + { + Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the " + "beginning of the file but moved %ld bytes.\n", dwRc); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* move the pointer ahead in the file and verify */ + dwRc = SetFilePointer(hFile, 20, NULL, FILE_CURRENT); + if (dwRc != 20) + { + Trace("SetFilePointer: ERROR -> Asked to move 20 bytes from the " + "beginning of the file but moved %ld bytes.\n", dwRc); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify results */ + memset(szBuffer, 0, 100); + bRc = ReadFile(hFile, szBuffer, (DWORD)strlen(szText)-20, &dwByteCount, + NULL); + if ((bRc != TRUE) || (dwByteCount != strlen(szText)-20)) + { + Trace("SetFilePointer: ERROR -> ReadFile failed to read correctly"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + szPtr = szText + 20;; + if (strcmp(szPtr, szBuffer) != 0) + { + Trace("SetFilePointer: ERROR -> Apparently failed to move the" + " pointer properly\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + + /* get the current file pointer position (should be 52) */ + dwRc = SetFilePointer(hFile, 0, NULL, FILE_CURRENT); + if (dwRc != 52) + { + Trace("SetFilePointer: ERROR -> Asked for current position." + " Should be 52 but was %ld.\n", dwRc); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + + /* move the pointer backwards in the file and verify */ + dwRc = SetFilePointer(hFile, -10, NULL, FILE_CURRENT); + if (dwRc != 42) + { + Trace("SetFilePointer: ERROR -> Asked to move back 10 bytes from the" + "end of the file but moved it to position %ld.\n", dwRc); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify results */ + memset(szBuffer, 0, 100); + bRc = ReadFile(hFile, szBuffer, 10, &dwByteCount, NULL); + if ((bRc != TRUE) || (dwByteCount != 10)) + { + Trace("SetFilePointer: ERROR -> ReadFile failed to read correctly"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + szPtr = szText + 42; + if (strcmp(szPtr, szBuffer) != 0) + { + Trace("SetFilePointer: ERROR -> Apparently failed to move the" + " pointer properly\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + /* + * the file pointer is currently at the end of the file so... + * set the pointer past the end of the file and verify + */ + dwRc = SetFilePointer(hFile, 20, NULL, FILE_CURRENT); + if ((dwRc == INVALID_SET_FILE_POINTER) && (GetLastError() != ERROR_SUCCESS)) + { + Trace("SetFilePointer: ERROR -> Failed to move pointer past EOF.\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + if (SetFilePointer(hFile, 0, NULL, FILE_CURRENT) != strlen(szText)+20) + { + Trace("SetFilePointer: ERROR -> Failed to move pointer past" + " EOF.\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + if (!DeleteFileA(szTextFile)) + { + Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test3/testinfo.dat b/src/pal/tests/palsuite/file_io/SetFilePointer/test3/testinfo.dat new file mode 100644 index 0000000000..7c51fbc384 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/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 = SetFilePointer +Name = Positive Test for SetFilePointer (test 3) +Type = DEFAULT +EXE1 = setfilepointer +Description +=Tests the FILE_CURRENT option + diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test4/CMakeLists.txt b/src/pal/tests/palsuite/file_io/SetFilePointer/test4/CMakeLists.txt new file mode 100644 index 0000000000..1117893350 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test4/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + SetFilePointer.c +) + +add_executable(paltest_setfilepointer_test4 + ${SOURCES} +) + +add_dependencies(paltest_setfilepointer_test4 coreclrpal) + +target_link_libraries(paltest_setfilepointer_test4 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test4/SetFilePointer.c b/src/pal/tests/palsuite/file_io/SetFilePointer/test4/SetFilePointer.c new file mode 100644 index 0000000000..2993cfd354 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test4/SetFilePointer.c @@ -0,0 +1,242 @@ +// 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: SetFilePointer.c (test 4) +** +** Purpose: Tests the PAL implementation of the SetFilePointer function. +** Test the FILE_END option +** +** Assumes Successful: +** CreateFile +** ReadFile +** WriteFile +** strlen +** CloseHandle +** strcmp +** GetFileSize +** +** +**===================================================================*/ + +#include <palsuite.h> + +const char* szText = "The quick brown fox jumped over the lazy dog's back."; +const char* szTextFile = "text.txt"; + + +int __cdecl main(int argc, char *argv[]) +{ + HANDLE hFile = NULL; + DWORD dwByteCount = 0; + DWORD dwOffset = 0; + DWORD dwRc = 0; + BOOL bRc = FALSE; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create a test file */ + hFile = CreateFile(szTextFile, + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n", + szTextFile); + } + + bRc = WriteFile(hFile, szText, (DWORD)strlen(szText), &dwByteCount, NULL); + if (bRc == FALSE) + { + Trace("SetFilePointer: ERROR -> Unable to write to file \"%s\".\n", + szTextFile); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + + /* + * move -1 from the end + */ + dwRc = SetFilePointer(hFile, -1, NULL, FILE_END); + if (dwRc == INVALID_SET_FILE_POINTER) + { + if (GetLastError() != ERROR_SUCCESS) + { + Trace("SetFilePointer: ERROR -> Failed to move the pointer " + "back one character from EOF.\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + else + { + /* verify */ + if ((dwRc != strlen(szText)-1)) + { + Trace("SetFilePointer: ERROR -> Failed to move the pointer" + " -1 bytes from EOF\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + /* + * move the file pointer 0 bytes from the end and verify + */ + dwRc = SetFilePointer(hFile, 0, NULL, FILE_END); + if (dwRc != strlen(szText)) + { + Trace("SetFilePointer: ERROR -> Asked to move 0 bytes from the " + "end of the file. Function returned %ld instead of 52.\n", dwRc); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* + * move the pointer past the end of the file and verify + */ + dwRc = SetFilePointer(hFile, 20, NULL, FILE_END); + if (dwRc != strlen(szText)+20) + { + Trace("SetFilePointer: ERROR -> Asked to move 20 bytes past the " + "end of the file. Function returned %ld instead of %d.\n", + dwRc, + strlen(szText)+20); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify results */ + bRc = SetEndOfFile(hFile); + if ((dwRc = GetFileSize(hFile, NULL)) != strlen(szText)+20) + { + Trace("SetFilePointer: ERROR -> Asked to move back 20 bytes past" + " theend of the file. GetFileSize returned %ld whereas it " + "should have been %d.\n", + dwRc, + strlen(szText)+20); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + + /* + * move the pointer backwards to before the start of the file and verify + */ + + dwOffset = (dwRc + 20) * -1; + dwRc = SetFilePointer(hFile, dwOffset, NULL, FILE_END); + if ((dwRc != INVALID_SET_FILE_POINTER) || + (GetLastError() == ERROR_SUCCESS)) + { + Trace("SetFilePointer: ERROR -> Was able to move the pointer " + "to before the beginning of the file.\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + if (!DeleteFileA(szTextFile)) + { + Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test4/testinfo.dat b/src/pal/tests/palsuite/file_io/SetFilePointer/test4/testinfo.dat new file mode 100644 index 0000000000..dce6f9eb21 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/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 = SetFilePointer +Name = Positive Test for SetFilePointer (test 4) +Type = DEFAULT +EXE1 = setfilepointer +Description +=Tests the FILE_END option + diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test5/CMakeLists.txt b/src/pal/tests/palsuite/file_io/SetFilePointer/test5/CMakeLists.txt new file mode 100644 index 0000000000..b37bf42ffc --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test5/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + SetFilePointer.c +) + +add_executable(paltest_setfilepointer_test5 + ${SOURCES} +) + +add_dependencies(paltest_setfilepointer_test5 coreclrpal) + +target_link_libraries(paltest_setfilepointer_test5 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test5/SetFilePointer.c b/src/pal/tests/palsuite/file_io/SetFilePointer/test5/SetFilePointer.c new file mode 100644 index 0000000000..f1d392da38 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test5/SetFilePointer.c @@ -0,0 +1,182 @@ +// 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: SetFilePointer.c (test 5) +** +** Purpose: Tests the PAL implementation of the SetFilePointer function. +** Test the FILE_BEGIN option using the high word parameter +** +** Assumes Successful: +** CreateFile +** ReadFile +** WriteFile +** strlen +** CloseHandle +** strcmp +** GetFileSize +** +** +**===================================================================*/ + +#include <palsuite.h> + +const char* szTextFile = "text.txt"; + +int __cdecl main(int argc, char *argv[]) +{ + HANDLE hFile = NULL; + DWORD dwOffset = 1; + LONG dwHighWord = 1; + DWORD dwReturnedOffset = 0; + DWORD dwReturnedHighWord = 0; + DWORD dwRc = 0; + DWORD dwError = 0; + BOOL bRc = FALSE; + + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create a test file */ + hFile = CreateFile(szTextFile, + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + dwError = GetLastError(); + Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n with " + "error %ld", + szTextFile, + GetLastError()); + } + + + + /* move -1 from beginning which should fail */ + dwRc = SetFilePointer(hFile, -1, &dwHighWord, FILE_BEGIN); + if (dwRc != INVALID_SET_FILE_POINTER) + { + Trace("SetFilePointer: ERROR -> Succeeded to move the pointer " + "before the beginning of the file using the high word.\n"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* set the pointer past the end of the file and verify */ + dwRc = SetFilePointer(hFile, dwOffset, &dwHighWord, FILE_BEGIN); + if ((dwRc == INVALID_SET_FILE_POINTER) && + ((dwError = GetLastError()) != ERROR_SUCCESS)) + { + Trace("SetFilePointer: ERROR -> Failed to move pointer past EOF.\n"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify */ + bRc = SetEndOfFile(hFile); + if (bRc != TRUE) + { + dwError = GetLastError(); + if (dwError == 112) + { + Trace("SetFilePointer: ERROR -> SetEndOfFile failed due to " + "lack of disk space\n"); + } + else + { + Trace("SetFilePointer: ERROR -> SetEndOfFile call failed with " + "error %ld\n", dwError); + } + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + dwReturnedOffset = GetFileSize(hFile, &dwReturnedHighWord); + if ((dwOffset != dwReturnedOffset) || + (dwHighWord != dwReturnedHighWord)) + { + Trace("SetFilePointer: ERROR -> Failed to move pointer past" + " EOF.\n"); + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + bRc = CloseHandle(hFile); + if (bRc != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + if (!DeleteFileA(szTextFile)) + { + Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test5/testinfo.dat b/src/pal/tests/palsuite/file_io/SetFilePointer/test5/testinfo.dat new file mode 100644 index 0000000000..64745c0e98 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test5/testinfo.dat @@ -0,0 +1,14 @@ +# 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 = SetFilePointer +Name = Positive Test for SetFilePointer (test 5) +Type = DEFAULT +EXE1 = setfilepointer +Description +=Tests the FILE_BEGIN option with the high word parameter. +=This test requires about 4 gig free disk space + diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test6/CMakeLists.txt b/src/pal/tests/palsuite/file_io/SetFilePointer/test6/CMakeLists.txt new file mode 100644 index 0000000000..8f99ed2d76 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test6/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + SetFilePointer.c +) + +add_executable(paltest_setfilepointer_test6 + ${SOURCES} +) + +add_dependencies(paltest_setfilepointer_test6 coreclrpal) + +target_link_libraries(paltest_setfilepointer_test6 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test6/SetFilePointer.c b/src/pal/tests/palsuite/file_io/SetFilePointer/test6/SetFilePointer.c new file mode 100644 index 0000000000..b35247ec24 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test6/SetFilePointer.c @@ -0,0 +1,213 @@ +// 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: SetFilePointer.c (test 6) +** +** Purpose: Tests the PAL implementation of the SetFilePointer function. +** Test the FILE_CURRENT option with high order support +** +** Assumes Successful: +** CreateFile +** ReadFile +** WriteFile +** strlen +** CloseHandle +** strcmp +** GetFileSize +** +** +**===================================================================*/ + +#include <palsuite.h> + +const char* szTextFile = "text.txt"; + + +int __cdecl main(int argc, char *argv[]) +{ + HANDLE hFile = NULL; + DWORD dwOffset = 0; + LONG dwHighOrder = 0; + DWORD dwReturnedOffset = 0; + LONG dwReturnedHighOrder = 0; + DWORD dwRc = 0; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create a test file */ + hFile = CreateFile(szTextFile, + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n", + szTextFile); + } + + + /* move waaaay before the beginning which should fail */ + dwHighOrder = -1; + dwOffset = 0; + dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_CURRENT); + if (dwRc != INVALID_SET_FILE_POINTER) + { + Trace("SetFilePointer: ERROR -> Succeeded to move the pointer " + "before the beginning of the file.\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* move the pointer ahead in the file and verify */ + dwHighOrder = 1; + dwOffset = 10; + dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_CURRENT); + if ((dwRc != 10) || (dwHighOrder != 1)) + { + Trace("SetFilePointer: ERROR -> Asked to move 2GB plus 10 bytes from " + "the beginning of the file but didn't.\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify results */ + if (SetEndOfFile(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Call to SetEndOfFile failed with " + "error code: %d\n", GetLastError()); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + dwReturnedOffset = GetFileSize(hFile, (DWORD*)&dwReturnedHighOrder); + if ((dwReturnedOffset != dwOffset) || + (dwReturnedHighOrder != dwHighOrder)) + { + Trace("SetFilePointer: ERROR -> Asked to move far past the " + "current file pointer. " + "low order sent: %ld low order returned: %ld " + "high order sent: %ld high order returned: %ld", + dwOffset, dwReturnedOffset, + dwHighOrder, dwReturnedHighOrder); + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + + /* + * move the pointer backwards in the file and verify + */ + dwOffset = 0; + dwHighOrder = -1; + dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_CURRENT); + if (dwRc != 10) + { + Trace("SetFilePointer: ERROR -> Asked to move back to 10 bytes from the" + "beginning of the file but moved it to position %ld.\n", dwRc); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify results */ + dwReturnedHighOrder = 0; + dwRc = SetFilePointer(hFile, 0, &dwReturnedHighOrder, FILE_CURRENT); + if (dwRc != 10) + { + Trace("SetFilePointer: ERROR -> Asked for current position. " + "Should be 10 but was %ld.\n", dwRc); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + + /* clean up, clean up, everybody do their share... */ + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + if (!DeleteFileA(szTextFile)) + { + Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test6/testinfo.dat b/src/pal/tests/palsuite/file_io/SetFilePointer/test6/testinfo.dat new file mode 100644 index 0000000000..3138e9bb40 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test6/testinfo.dat @@ -0,0 +1,14 @@ +# 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 = SetFilePointer +Name = Positive Test for SetFilePointer (test 6) +Type = DEFAULT +EXE1 = setfilepointer +Description +=Tests the FILE_CURRENT option with the high word parameter. +=This test requires about 4 GB free disk space. + diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test7/CMakeLists.txt b/src/pal/tests/palsuite/file_io/SetFilePointer/test7/CMakeLists.txt new file mode 100644 index 0000000000..c5a46a531a --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test7/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + SetFilePointer.c +) + +add_executable(paltest_setfilepointer_test7 + ${SOURCES} +) + +add_dependencies(paltest_setfilepointer_test7 coreclrpal) + +target_link_libraries(paltest_setfilepointer_test7 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test7/SetFilePointer.c b/src/pal/tests/palsuite/file_io/SetFilePointer/test7/SetFilePointer.c new file mode 100644 index 0000000000..33dfd5e711 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test7/SetFilePointer.c @@ -0,0 +1,213 @@ +// 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: SetFilePointer.c (test 7) +** +** Purpose: Tests the PAL implementation of the SetFilePointer function. +** Test the FILE_END option with high order support +** +** Assumes Successful: +** CreateFile +** ReadFile +** WriteFile +** strlen +** CloseHandle +** strcmp +** GetFileSize +** +** +**===================================================================*/ + +#include <palsuite.h> + +const char* szTextFile = "text.txt"; + + +int __cdecl main(int argc, char *argv[]) +{ + HANDLE hFile = NULL; + DWORD dwOffset = 0; + LONG dwHighOrder = 0; + DWORD dwReturnedOffset = 0; + LONG dwReturnedHighOrder = 0; + DWORD dwRc = 0; + + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create a test file */ + hFile = CreateFile(szTextFile, + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("SetFilePointer: ERROR -> Unable to create file \"%s\".\n", + szTextFile); + } + + + /* move -1 from beginning which should fail */ + dwHighOrder = -1; + dwOffset = 0; + dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_END); + if (dwRc != INVALID_SET_FILE_POINTER) + { + Trace("SetFilePointer: ERROR -> Succeeded to move the pointer " + "before the beginning of the file.\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + /* move the pointer ahead in the file and verify */ + dwHighOrder = 1; + dwOffset = 10; + dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_END); + if ((dwRc != 10) || (dwHighOrder != 1)) + { + Trace("SetFilePointer: ERROR -> Asked to move 4GB plus 10 bytes from " + "the beginning of the file but didn't.\n"); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify results */ + if (SetEndOfFile(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Call to SetEndOfFile failed with " + "error code: %d\n", + GetLastError()); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + dwReturnedOffset = GetFileSize(hFile, (DWORD*)&dwReturnedHighOrder); + if ((dwReturnedOffset != dwOffset) || (dwReturnedHighOrder != dwHighOrder)) + { + Trace("SetFilePointer: ERROR -> Asked to move far past the " + "end of the file. low order sent: %ld low order returned: %ld " + "high order sent: %ld high order returned: %ld", + dwOffset, dwReturnedOffset, + dwHighOrder, dwReturnedHighOrder); + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + + /* + * move the pointer backwards in the file and verify + */ + dwOffset = 0; + dwHighOrder = -1; + dwRc = SetFilePointer(hFile, dwOffset, &dwHighOrder, FILE_END); + if (dwRc != 10) + { + Trace("SetFilePointer: ERROR -> Asked to move back to 10 bytes from the" + "beginning of the file but moved it to position %ld.\n", dwRc); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + else + { + /* verify results */ + dwReturnedHighOrder = 0; + dwRc = SetFilePointer(hFile, 0, &dwReturnedHighOrder, FILE_CURRENT); + if (dwRc != 10) + { + Trace("SetFilePointer: ERROR -> Asked for current position. " + "Should be 10 but was %ld.\n", dwRc); + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file" + " \"%s\".\n", szTextFile); + } + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file" + " \"%s\".\n", szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + } + + + /* clean up, clean up, everybody do their share... */ + if (CloseHandle(hFile) != TRUE) + { + Trace("SetFilePointer: ERROR -> Unable to close file \"%s\".\n", + szTextFile); + if (!DeleteFileA(szTextFile)) + { + Trace("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + PAL_TerminateEx(FAIL); + return FAIL; + } + + if (!DeleteFileA(szTextFile)) + { + Fail("SetFilePointer: ERROR -> Unable to delete file \"%s\".\n", + szTextFile); + } + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/SetFilePointer/test7/testinfo.dat b/src/pal/tests/palsuite/file_io/SetFilePointer/test7/testinfo.dat new file mode 100644 index 0000000000..6e8826291f --- /dev/null +++ b/src/pal/tests/palsuite/file_io/SetFilePointer/test7/testinfo.dat @@ -0,0 +1,14 @@ +# 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 = SetFilePointer +Name = Positive Test for SetFilePointer (test 7) +Type = DEFAULT +EXE1 = setfilepointer +Description +=Tests the FILE_END option with the high word parameter +=This test requires about 4 GB of free disk space. + |