| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*===================================================================== |
| 6 | ** |
| 7 | ** Source: test2.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the CopyFileW function |
| 10 | ** Attempt to copy a file to itself |
| 11 | ** |
| 12 | ** |
| 13 | **===================================================================*/ |
| 14 | |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | int __cdecl main(int argc, char *argv[]) |
| 18 | { |
| 19 | LPSTR szSrcExisting = "src_existing.tmp" ; |
| 20 | WCHAR* wcSource; |
| 21 | BOOL bRc = TRUE; |
| 22 | FILE* tempFile = NULL; |
| 23 | DWORD temp; |
| 24 | int retCode; |
| 25 | |
| 26 | if (0 != PAL_Initialize(argc,argv)) |
| 27 | { |
| 28 | return FAIL; |
| 29 | } |
| 30 | |
| 31 | /* create the src_existing file */ |
| 32 | tempFile = fopen(szSrcExisting, "w" ); |
| 33 | if (tempFile != NULL) |
| 34 | { |
| 35 | retCode = fputs("CopyFileA test file: src_existing.tmp " , tempFile); |
| 36 | if(retCode < 0) |
| 37 | { |
| 38 | Fail("CopyFileW: ERROR-> Couldn't write to %s with error " |
| 39 | "%u.\n" , szSrcExisting, GetLastError()); |
| 40 | } |
| 41 | retCode = fclose(tempFile); |
| 42 | if(retCode != 0) |
| 43 | { |
| 44 | Fail("CopyFileW: ERROR-> Couldn't close file: %s with error " |
| 45 | "%u.\n" , szSrcExisting, GetLastError()); |
| 46 | } |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | Fail("CopyFileW: ERROR-> Couldn't create %s.\n" , szSrcExisting); |
| 51 | } |
| 52 | |
| 53 | /* convert source string to wide character */ |
| 54 | wcSource = convert(szSrcExisting); |
| 55 | |
| 56 | /* Get file attributes of source */ |
| 57 | temp = GetFileAttributes(szSrcExisting); |
| 58 | if (temp == -1) |
| 59 | { |
| 60 | free(wcSource); |
| 61 | Fail("CopyFileW: GetFileAttributes of source file " |
| 62 | "failed with error code %ld. \n" , |
| 63 | GetLastError()); |
| 64 | } |
| 65 | |
| 66 | /* make sure a file can't copy to itself |
| 67 | first testing with IfFileExists flag set to true */ |
| 68 | bRc = CopyFileW(wcSource,wcSource,TRUE); |
| 69 | if(bRc) |
| 70 | { |
| 71 | free(wcSource); |
| 72 | Fail("ERROR: Cannot copy a file to itself, %u" ,GetLastError()); |
| 73 | } |
| 74 | |
| 75 | /* try to get file attributes of desitnation */ |
| 76 | if (GetFileAttributesA(szSrcExisting) == -1) |
| 77 | { |
| 78 | free(wcSource); |
| 79 | Fail("CopyFileW: GetFileAttributes of destination file " |
| 80 | "failed with error code %ld. \n" , |
| 81 | GetLastError()); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | /* verify attributes of destination file to source file*/ |
| 86 | if(temp != GetFileAttributes(szSrcExisting)) |
| 87 | { |
| 88 | free(wcSource); |
| 89 | Fail("CopyFileW : The file attributes of the " |
| 90 | "destination file do not match the file " |
| 91 | "attributes of the source file.\n" ); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* testing with IfFileExists flags set to false |
| 96 | should fail in Windows and pass in UNIX */ |
| 97 | bRc = CopyFileW(wcSource,wcSource,FALSE); |
| 98 | free(wcSource); |
| 99 | if(bRc && (GetLastError() != ERROR_ALREADY_EXISTS)) |
| 100 | { |
| 101 | Fail("ERROR: Cannot copy a file to itself, %u" ,GetLastError()); |
| 102 | } |
| 103 | |
| 104 | if (GetFileAttributesA(szSrcExisting) == -1) |
| 105 | { |
| 106 | Fail("CopyFileW: GetFileAttributes of destination file " |
| 107 | "failed with error code %ld. \n" , |
| 108 | GetLastError()); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | /* verify attributes of destination file to source file*/ |
| 113 | |
| 114 | if(temp != GetFileAttributes(szSrcExisting)) |
| 115 | { |
| 116 | Fail("CopyFileW : The file attributes of the " |
| 117 | "destination file do not match the file " |
| 118 | "attributes of the source file.\n" ); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | PAL_Terminate(); |
| 123 | return PASS; |
| 124 | } |
| 125 | |