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