diff options
Diffstat (limited to 'src/pal/tests/palsuite/file_io/CopyFileW')
11 files changed, 583 insertions, 0 deletions
diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/CMakeLists.txt b/src/pal/tests/palsuite/file_io/CopyFileW/CMakeLists.txt new file mode 100644 index 0000000000..1962ade358 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +add_subdirectory(test1) +add_subdirectory(test2) +add_subdirectory(test3) + diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test1/CMakeLists.txt b/src/pal/tests/palsuite/file_io/CopyFileW/test1/CMakeLists.txt new file mode 100644 index 0000000000..766b120035 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/test1/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + CopyFileW.c +) + +add_executable(paltest_copyfilew_test1 + ${SOURCES} +) + +add_dependencies(paltest_copyfilew_test1 coreclrpal) + +target_link_libraries(paltest_copyfilew_test1 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test1/CopyFileW.c b/src/pal/tests/palsuite/file_io/CopyFileW/test1/CopyFileW.c new file mode 100644 index 0000000000..6127cc21bd --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/test1/CopyFileW.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: CopyFileW.c +** +** Purpose: Tests the PAL implementation of the CopyFileW function +** +** +**===================================================================*/ + +/* +1. copy an existing file to non-existant with overwrite true +2. copy an existing file to non-existant with overwrite false +3. copy an existing file to existing with overwrite true +4. copy an existing file to existing with overwrite false +5. copy non-existant file to non-existant with overwrite true +6. copy non-existant file to non-existant with overwrite false +7. copy non-existant file to existing with overwrite true +8. copy non-existant file to existing with overwrite false +*/ + +#include <palsuite.h> + +int __cdecl main(int argc, char *argv[]) +{ + LPSTR lpSource[2] = {"src_existing.tmp", "src_non-existant.tmp"}; + LPSTR lpDestination[2] = {"dst_existing.tmp", "dst_non-existant.tmp"}; + WCHAR* wcSource; + WCHAR* wcDest; + BOOL bFailIfExists[3] = {FALSE, TRUE}; + BOOL bRc = TRUE; + BOOL bSuccess = TRUE; + char results[20]; + FILE* resultsFile = NULL; + FILE* tempFile = NULL; + int nCounter = 0; + int i, j, k; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* load the expected results */ + resultsFile = fopen("expectedresults.txt", "r"); + memset (results, 0, 20); + fgets(results, 20, resultsFile); + fclose(resultsFile); + + nCounter = 0; + + /* create the src_existing file */ + tempFile = fopen(lpSource[0], "w"); + if (tempFile != NULL) + { + fprintf(tempFile, "CopyFileW test file: src_existing.tmp\n"); + fclose(tempFile); + } + else + { + Fail("CopyFileW: ERROR-> Couldn't create \"src_existing.tmp\"\n"); + } + + /* create the dst_existing file */ + tempFile = fopen(lpDestination[0], "w"); + if (tempFile != NULL) + { + fprintf(tempFile, "CopyFileW test file: dst_existing.tmp\n"); + fclose(tempFile); + } + else + { + Fail("CopyFileW: ERROR-> Couldn't create \"dst_existing.tmp\"\n"); + } + + + /* lpSource loop */ + for (i = 0; i < 2; i++) + { + /* lpDestination loop */ + for (j = 0; j < 2; j++) + { + /* bFailIfExists loop */ + for (k = 0; k < 2; k++) + { + wcSource = convert(lpSource[i]); + wcDest = convert(lpDestination[j]); + bRc = CopyFileW(wcSource, + wcDest, + bFailIfExists[k]); + free(wcSource); + free(wcDest); + if (!bRc) + { + if (results[nCounter] == '1') + { + Trace("CopyFileW: FAILED: test[%d][%d][%d]\n", i, j, k); + bSuccess = FALSE; + } + } + else + { + if (results[nCounter] == '0') + { + Trace("CopyFileW: FAILED: test[%d][%d][%d]\n", i, j, k); + bSuccess = FALSE; + } + else + { + /* verify the file was moved */ + if (GetFileAttributesA(lpDestination[j]) == -1) + { + Trace("CopyFileW: GetFileAttributes of destination" + "file failed on test[%d][%d][%d] with error " + "code %ld. \n",i,j,k,GetLastError()); + bSuccess = FALSE; + } + else if (GetFileAttributesA(lpSource[i]) == -1) + { + Trace("CopyFileW: GetFileAttributes of source file " + "file failed on test[%d][%d][%d] with error " + "code %ld. \n",i,j,k,GetLastError()); + bSuccess = FALSE; + } + else + { + /* verify attributes of destination file to + source file*/ + if(GetFileAttributes(lpSource[i]) != + GetFileAttributes(lpDestination[j])) + { + Trace("CopyFileW : The file attributes of the " + "destination file do not match the file " + "attributes of the source file on test " + "[%d][%d][%d].\n",i,j,k); + bSuccess = FALSE; + } + } + } + + } + nCounter++; + /* delete file file but don't worry if it fails */ + DeleteFileA(lpDestination[1]); + } + } + } + + int exitCode = bSuccess ? PASS : FAIL; + PAL_TerminateEx(exitCode); + return exitCode; +} diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test1/ExpectedResults.txt b/src/pal/tests/palsuite/file_io/CopyFileW/test1/ExpectedResults.txt new file mode 100644 index 0000000000..535a89fe50 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/test1/ExpectedResults.txt @@ -0,0 +1 @@ +10110000
\ No newline at end of file diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test1/testinfo.dat b/src/pal/tests/palsuite/file_io/CopyFileW/test1/testinfo.dat new file mode 100644 index 0000000000..b7ff6de2fd --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/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 = CopyFileW +Name = Tests CopyFileW functionality. +Type = DEFAULT +EXE1 = copyfilew +Description += Test the CopyFileW function + diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test2/CMakeLists.txt b/src/pal/tests/palsuite/file_io/CopyFileW/test2/CMakeLists.txt new file mode 100644 index 0000000000..5c0030bc8d --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/test2/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + test2.c +) + +add_executable(paltest_copyfilew_test2 + ${SOURCES} +) + +add_dependencies(paltest_copyfilew_test2 coreclrpal) + +target_link_libraries(paltest_copyfilew_test2 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test2/test2.c b/src/pal/tests/palsuite/file_io/CopyFileW/test2/test2.c new file mode 100644 index 0000000000..5380a181ca --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/test2/test2.c @@ -0,0 +1,124 @@ +// 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: test2.c +** +** Purpose: Tests the PAL implementation of the CopyFileW function +** Attempt to copy a file to itself +** +** +**===================================================================*/ + +#include <palsuite.h> + +int __cdecl main(int argc, char *argv[]) +{ + LPSTR szSrcExisting = "src_existing.tmp"; + WCHAR* wcSource; + BOOL bRc = TRUE; + FILE* tempFile = NULL; + DWORD temp; + int retCode; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* create the src_existing file */ + tempFile = fopen(szSrcExisting, "w"); + if (tempFile != NULL) + { + retCode = fputs("CopyFileA test file: src_existing.tmp ", tempFile); + if(retCode < 0) + { + Fail("CopyFileW: ERROR-> Couldn't write to %s with error " + "%u.\n", szSrcExisting, GetLastError()); + } + retCode = fclose(tempFile); + if(retCode != 0) + { + Fail("CopyFileW: ERROR-> Couldn't close file: %s with error " + "%u.\n", szSrcExisting, GetLastError()); + } + } + else + { + Fail("CopyFileW: ERROR-> Couldn't create %s.\n", szSrcExisting); + } + + /* convert source string to wide character */ + wcSource = convert(szSrcExisting); + + /* Get file attributes of source */ + temp = GetFileAttributes(szSrcExisting); + if (temp == -1) + { + free(wcSource); + Fail("CopyFileW: GetFileAttributes of source file " + "failed with error code %ld. \n", + GetLastError()); + } + + /* make sure a file can't copy to itself + first testing with IfFileExists flag set to true */ + bRc = CopyFileW(wcSource,wcSource,TRUE); + if(bRc) + { + free(wcSource); + Fail("ERROR: Cannot copy a file to itself, %u",GetLastError()); + } + + /* try to get file attributes of desitnation */ + if (GetFileAttributesA(szSrcExisting) == -1) + { + free(wcSource); + Fail("CopyFileW: GetFileAttributes of destination file " + "failed with error code %ld. \n", + GetLastError()); + } + else + { + /* verify attributes of destination file to source file*/ + if(temp != GetFileAttributes(szSrcExisting)) + { + free(wcSource); + Fail("CopyFileW : The file attributes of the " + "destination file do not match the file " + "attributes of the source file.\n"); + } + } + + /* testing with IfFileExists flags set to false + should fail in Windows and pass in UNIX */ + bRc = CopyFileW(wcSource,wcSource,FALSE); + free(wcSource); + if(bRc && (GetLastError() != ERROR_ALREADY_EXISTS)) + { + Fail("ERROR: Cannot copy a file to itself, %u",GetLastError()); + } + + if (GetFileAttributesA(szSrcExisting) == -1) + { + Fail("CopyFileW: GetFileAttributes of destination file " + "failed with error code %ld. \n", + GetLastError()); + } + else + { + /* verify attributes of destination file to source file*/ + + if(temp != GetFileAttributes(szSrcExisting)) + { + Fail("CopyFileW : The file attributes of the " + "destination file do not match the file " + "attributes of the source file.\n"); + } + } + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test2/testinfo.dat b/src/pal/tests/palsuite/file_io/CopyFileW/test2/testinfo.dat new file mode 100644 index 0000000000..7e1591dd72 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/test2/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 = CopyFileW +Name = Tests CopyFileW to check if file can be copied to itself. (test2) +Type = DEFAULT +EXE1 = test2 +Description += Test the CopyFileW function += to see the effect of copying a += file to itself. + diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test3/CMakeLists.txt b/src/pal/tests/palsuite/file_io/CopyFileW/test3/CMakeLists.txt new file mode 100644 index 0000000000..52d9aad1c0 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/test3/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + test3.c +) + +add_executable(paltest_copyfilew_test3 + ${SOURCES} +) + +add_dependencies(paltest_copyfilew_test3 coreclrpal) + +target_link_libraries(paltest_copyfilew_test3 + pthread + m + coreclrpal +) diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test3/test3.c b/src/pal/tests/palsuite/file_io/CopyFileW/test3/test3.c new file mode 100644 index 0000000000..a2eb04c443 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/test3/test3.c @@ -0,0 +1,196 @@ +// 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: test4.c +** +** Purpose: Tests the PAL implementation of the CopyFileW function +** to see if a file can through different users belonging to +** different groups. +** + +=====================================================================*/ + +/* USECASE + Copy a file from a different user, belonging to a different group to + the the current user, who is a member of the current group. Then check + to see that the current user has the basic access rights to the copied + file. + + Thie original file used is the passwd file in the etc directory of a + BSD machine. This file should exist on all machines. +*/ + +#include <palsuite.h> + +int __cdecl main(int argc, char *argv[]) +{ + +#if WIN32 + return PASS; + +#else + + BOOL bRc = TRUE; + WCHAR szSrcExisting[] = {'/','e','t','c','/','p','a','s','s','w','d','\0'}; + WCHAR szDest[] = {'t','e','m','p','.','t','m','p','\0'}; + WCHAR szStringTest[] = {'M','a','r','r','y',' ','h','a','d',' ','a',' ', + 'l','i','t','t','l','e',' ','l','a','m','b','\0'}; + WCHAR szStringRead[30]; /* large enough for string szStringTest */ + + HANDLE hFile = NULL; + DWORD dwBytesWritten=0; + DWORD dwBytesRead=0; + int size=0; + + if (0 != PAL_Initialize(argc,argv)) + { + return FAIL; + } + + /* copy the file */ + bRc = CopyFileW(szSrcExisting,szDest,TRUE); + if(!bRc) + { + Fail("CopyFileW: Cannot copy a file with error, %u",GetLastError()); + } + + /* try to get file attributes of destination file */ + if (GetFileAttributesW(szDest) == -1) + { + Fail("CopyFileW: GetFileAttributes of destination file " + "failed with error code %u. \n", + GetLastError()); + } + + /* set the attributes of the destination file to normal again */ + bRc = SetFileAttributesW(szDest, FILE_ATTRIBUTE_NORMAL); + if(!bRc) + { + Fail("CopyFileW: ERROR-> Couldn't set file attributes for " + "file %S with error %u\n", szDest, GetLastError()); + } + + /* open the file for write purposes */ + hFile = CreateFileW((WCHAR *)szDest, + GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("CopyFileW: ERROR -> Unable to create file \"%S\".\n", + szDest); + } + + /* To account for the size of a WCHAR is twice that of a char */ + size = wcslen(szStringTest); + size = size*sizeof(WCHAR); + + /* Attempt to write to the file */ + bRc = WriteFile(hFile, + szStringTest, + size, + &dwBytesWritten, + NULL); + + if (!bRc) + { + Trace("CopyFileW: ERROR -> Unable to write to copied file with error " + "%u.\n", GetLastError()); + bRc = CloseHandle(hFile); + if (!bRc) + { + Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with " + "error %u.\n",szDest, GetLastError()); + } + Fail(""); + + } + + /* Close the file handle */ + bRc = CloseHandle(hFile); + if (!bRc) + { + Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with error %u " + ".\n",szDest,GetLastError()); + } + + + /* open the file for read purposes */ + hFile = CreateFileW((WCHAR *)szDest, + GENERIC_READ, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL); + + if(hFile == INVALID_HANDLE_VALUE) + { + Fail("CopyFileW: ERROR -> Unable to create file \"%S\".\n", + szDest); + } + + /* Attempt to read from the file */ + bRc = ReadFile(hFile, + szStringRead, + size, + &dwBytesRead, + NULL); + + if (!bRc) + { + Trace("CopyFileW: ERROR -> Unable to read from copied file with " + "error %u.\n",GetLastError()); + bRc = CloseHandle(hFile); + if (!bRc) + { + Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with " + "error %u.\n",szDest, GetLastError()); + } + Fail(""); + + } + + if(wcsncmp(szStringTest,szStringRead, wcslen(szStringTest)) != 0) + { + Trace("CopyFileW: ERROR -> The string which was written '%S' does not " + "match the string '%S' which was read from the copied file.\n", + szStringTest,szStringRead); + bRc = CloseHandle(hFile); + if (!bRc) + { + Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with " + "error %u.\n",szDest, GetLastError()); + } + Fail(""); + } + + /* Close the file handle */ + bRc = CloseHandle(hFile); + if (!bRc) + { + Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with error %u " + ".\n",szDest,GetLastError()); + } + + /* Remove the temporary file */ + bRc = DeleteFileW(szDest); + if(!bRc) + { + Fail("CopyFileW: Could not remove copied file with error %u.\n", + GetLastError()); + } + + PAL_Terminate(); + return PASS; + +#endif + +} diff --git a/src/pal/tests/palsuite/file_io/CopyFileW/test3/testinfo.dat b/src/pal/tests/palsuite/file_io/CopyFileW/test3/testinfo.dat new file mode 100644 index 0000000000..9fd185dad0 --- /dev/null +++ b/src/pal/tests/palsuite/file_io/CopyFileW/test3/testinfo.dat @@ -0,0 +1,16 @@ +# 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 = CopyFileW +Name = CopyFileW - checking file attributes maintained (test3) +Type = DEFAULT +EXE1 = test3 +Description += Copy a file from a different user, belonging to a different group to += the the current user, who is a member of the current group. Then check += to see that the current user has the basic access rights to the copied += file. + |