| 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: test4.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the CopyFileA function |
| 10 | ** to see if a file can through different users belonging to |
| 11 | ** different groups. |
| 12 | ** |
| 13 | |
| 14 | =====================================================================*/ |
| 15 | |
| 16 | /* USECASE |
| 17 | Copy a file from a different user, belonging to a different group to |
| 18 | the the current user, who is a member of the current group. Then check |
| 19 | to see that the current user has the basic access rights to the copied |
| 20 | file. |
| 21 | |
| 22 | Thie original file used is the passwd file in the etc directory of a |
| 23 | BSD machine. This file should exist on all machines. |
| 24 | */ |
| 25 | |
| 26 | #include <palsuite.h> |
| 27 | |
| 28 | int __cdecl main(int argc, char *argv[]) |
| 29 | { |
| 30 | |
| 31 | #if WIN32 |
| 32 | return PASS; |
| 33 | |
| 34 | #else |
| 35 | |
| 36 | BOOL bRc = TRUE; |
| 37 | char* szSrcExisting = "/etc/passwd" ; |
| 38 | char* szDest = "temp.tmp" ; |
| 39 | char* szStringTest = "Marry had a little lamb" ; |
| 40 | char szStringRead[30]; /* large enough for string szStringTest */ |
| 41 | |
| 42 | HANDLE hFile = NULL; |
| 43 | DWORD dwBytesWritten=0; |
| 44 | DWORD dwBytesRead=0; |
| 45 | |
| 46 | if (0 != PAL_Initialize(argc,argv)) |
| 47 | { |
| 48 | return FAIL; |
| 49 | } |
| 50 | |
| 51 | /* copy the file */ |
| 52 | bRc = CopyFileA(szSrcExisting,szDest,TRUE); |
| 53 | if(!bRc) |
| 54 | { |
| 55 | Fail("CopyFileA: Cannot copy a file with error, %u" ,GetLastError()); |
| 56 | } |
| 57 | |
| 58 | /* try to get file attributes of destination file */ |
| 59 | if (GetFileAttributesA(szDest) == -1) |
| 60 | { |
| 61 | Fail("CopyFileA: GetFileAttributes of destination file " |
| 62 | "failed with error code %u. \n" , |
| 63 | GetLastError()); |
| 64 | } |
| 65 | |
| 66 | /* set the attributes of the destination file to normal again */ |
| 67 | bRc = SetFileAttributes(szDest, FILE_ATTRIBUTE_NORMAL); |
| 68 | if(!bRc) |
| 69 | { |
| 70 | Fail("CopyFileA: ERROR-> Couldn't set file attributes for " |
| 71 | "file %s with error %u\n" , szDest, GetLastError()); |
| 72 | } |
| 73 | |
| 74 | /* open the file for write purposes */ |
| 75 | hFile = CreateFile(szDest, |
| 76 | GENERIC_WRITE, |
| 77 | 0, |
| 78 | NULL, |
| 79 | OPEN_EXISTING, |
| 80 | FILE_ATTRIBUTE_NORMAL, |
| 81 | NULL); |
| 82 | |
| 83 | if(hFile == INVALID_HANDLE_VALUE) |
| 84 | { |
| 85 | Fail("CopyFileA: ERROR -> Unable to create file \"%s\".\n" , |
| 86 | szDest); |
| 87 | } |
| 88 | |
| 89 | /* Attempt to write to the file */ |
| 90 | bRc = WriteFile(hFile, szStringTest, strlen(szStringTest), &dwBytesWritten, NULL); |
| 91 | if (!bRc) |
| 92 | { |
| 93 | Trace("CopyFileA: ERROR -> Unable to write to copied file with error " |
| 94 | "%u.\n" , GetLastError()); |
| 95 | bRc = CloseHandle(hFile); |
| 96 | if (!bRc) |
| 97 | { |
| 98 | Fail("CopyFileA: ERROR -> Unable to close file \"%s\" with " |
| 99 | "error %u.\n" ,szDest, GetLastError()); |
| 100 | } |
| 101 | Fail("" ); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | /* Close the file handle */ |
| 106 | bRc = CloseHandle(hFile); |
| 107 | if (!bRc) |
| 108 | { |
| 109 | Fail("CopyFileA: ERROR -> Unable to close file \"%s\" with error %u " |
| 110 | ".\n" ,szDest,GetLastError()); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /* open the file for read purposes */ |
| 115 | hFile = CreateFile(szDest, |
| 116 | GENERIC_READ, |
| 117 | 0, |
| 118 | NULL, |
| 119 | OPEN_EXISTING, |
| 120 | FILE_ATTRIBUTE_NORMAL, |
| 121 | NULL); |
| 122 | |
| 123 | if(hFile == INVALID_HANDLE_VALUE) |
| 124 | { |
| 125 | Fail("CopyFileA: ERROR -> Unable to create file \"%s\".\n" , |
| 126 | szDest); |
| 127 | } |
| 128 | |
| 129 | /* Attempt to read from the file */ |
| 130 | bRc = ReadFile(hFile, szStringRead, strlen(szStringTest), &dwBytesRead, NULL); |
| 131 | if (!bRc) |
| 132 | { |
| 133 | Trace("CopyFileA: ERROR -> Unable to read from copied file with " |
| 134 | "error %u.\n" ,GetLastError()); |
| 135 | bRc = CloseHandle(hFile); |
| 136 | if (!bRc) |
| 137 | { |
| 138 | Fail("CopyFileA: ERROR -> Unable to close file \"%s\" with " |
| 139 | "error %u.\n" ,szDest, GetLastError()); |
| 140 | } |
| 141 | Fail("" ); |
| 142 | |
| 143 | } |
| 144 | |
| 145 | if(strncmp(szStringTest,szStringRead, strlen(szStringTest)) != 0) |
| 146 | { |
| 147 | Trace("CopyFileA: ERROR -> The string which was written '%s' does not " |
| 148 | "match the string '%s' which was read from the copied file.\n" , |
| 149 | szStringTest,szStringRead); |
| 150 | bRc = CloseHandle(hFile); |
| 151 | if (!bRc) |
| 152 | { |
| 153 | Fail("CopyFileA: ERROR -> Unable to close file \"%s\" with " |
| 154 | "error %u.\n" ,szDest, GetLastError()); |
| 155 | } |
| 156 | Fail("" ); |
| 157 | } |
| 158 | |
| 159 | /* Close the file handle */ |
| 160 | bRc = CloseHandle(hFile); |
| 161 | if (!bRc) |
| 162 | { |
| 163 | Fail("CopyFileA: ERROR -> Unable to close file \"%s\" with error %u " |
| 164 | ".\n" ,szDest,GetLastError()); |
| 165 | } |
| 166 | |
| 167 | /* Remove the temporary file */ |
| 168 | bRc = DeleteFile(szDest); |
| 169 | if(!bRc) |
| 170 | { |
| 171 | Fail("CopyFileA: Could not remove copied file with error %u\n" , |
| 172 | GetLastError()); |
| 173 | } |
| 174 | |
| 175 | PAL_Terminate(); |
| 176 | return PASS; |
| 177 | |
| 178 | #endif |
| 179 | |
| 180 | } |
| 181 | |