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 CopyFileW 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 | WCHAR szSrcExisting[] = {'/','e','t','c','/','p','a','s','s','w','d','\0'}; |
38 | WCHAR szDest[] = {'t','e','m','p','.','t','m','p','\0'}; |
39 | WCHAR szStringTest[] = {'M','a','r','r','y',' ','h','a','d',' ','a',' ', |
40 | 'l','i','t','t','l','e',' ','l','a','m','b','\0'}; |
41 | WCHAR szStringRead[30]; /* large enough for string szStringTest */ |
42 | |
43 | HANDLE hFile = NULL; |
44 | DWORD dwBytesWritten=0; |
45 | DWORD dwBytesRead=0; |
46 | int size=0; |
47 | |
48 | if (0 != PAL_Initialize(argc,argv)) |
49 | { |
50 | return FAIL; |
51 | } |
52 | |
53 | /* copy the file */ |
54 | bRc = CopyFileW(szSrcExisting,szDest,TRUE); |
55 | if(!bRc) |
56 | { |
57 | Fail("CopyFileW: Cannot copy a file with error, %u" ,GetLastError()); |
58 | } |
59 | |
60 | /* try to get file attributes of destination file */ |
61 | if (GetFileAttributesW(szDest) == -1) |
62 | { |
63 | Fail("CopyFileW: GetFileAttributes of destination file " |
64 | "failed with error code %u. \n" , |
65 | GetLastError()); |
66 | } |
67 | |
68 | /* set the attributes of the destination file to normal again */ |
69 | bRc = SetFileAttributesW(szDest, FILE_ATTRIBUTE_NORMAL); |
70 | if(!bRc) |
71 | { |
72 | Fail("CopyFileW: ERROR-> Couldn't set file attributes for " |
73 | "file %S with error %u\n" , szDest, GetLastError()); |
74 | } |
75 | |
76 | /* open the file for write purposes */ |
77 | hFile = CreateFileW((WCHAR *)szDest, |
78 | GENERIC_WRITE, |
79 | 0, |
80 | NULL, |
81 | OPEN_EXISTING, |
82 | FILE_ATTRIBUTE_NORMAL, |
83 | NULL); |
84 | |
85 | if(hFile == INVALID_HANDLE_VALUE) |
86 | { |
87 | Fail("CopyFileW: ERROR -> Unable to create file \"%S\".\n" , |
88 | szDest); |
89 | } |
90 | |
91 | /* To account for the size of a WCHAR is twice that of a char */ |
92 | size = wcslen(szStringTest); |
93 | size = size*sizeof(WCHAR); |
94 | |
95 | /* Attempt to write to the file */ |
96 | bRc = WriteFile(hFile, |
97 | szStringTest, |
98 | size, |
99 | &dwBytesWritten, |
100 | NULL); |
101 | |
102 | if (!bRc) |
103 | { |
104 | Trace("CopyFileW: ERROR -> Unable to write to copied file with error " |
105 | "%u.\n" , GetLastError()); |
106 | bRc = CloseHandle(hFile); |
107 | if (!bRc) |
108 | { |
109 | Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with " |
110 | "error %u.\n" ,szDest, GetLastError()); |
111 | } |
112 | Fail("" ); |
113 | |
114 | } |
115 | |
116 | /* Close the file handle */ |
117 | bRc = CloseHandle(hFile); |
118 | if (!bRc) |
119 | { |
120 | Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with error %u " |
121 | ".\n" ,szDest,GetLastError()); |
122 | } |
123 | |
124 | |
125 | /* open the file for read purposes */ |
126 | hFile = CreateFileW((WCHAR *)szDest, |
127 | GENERIC_READ, |
128 | 0, |
129 | NULL, |
130 | OPEN_EXISTING, |
131 | FILE_ATTRIBUTE_NORMAL, |
132 | NULL); |
133 | |
134 | if(hFile == INVALID_HANDLE_VALUE) |
135 | { |
136 | Fail("CopyFileW: ERROR -> Unable to create file \"%S\".\n" , |
137 | szDest); |
138 | } |
139 | |
140 | /* Attempt to read from the file */ |
141 | bRc = ReadFile(hFile, |
142 | szStringRead, |
143 | size, |
144 | &dwBytesRead, |
145 | NULL); |
146 | |
147 | if (!bRc) |
148 | { |
149 | Trace("CopyFileW: ERROR -> Unable to read from copied file with " |
150 | "error %u.\n" ,GetLastError()); |
151 | bRc = CloseHandle(hFile); |
152 | if (!bRc) |
153 | { |
154 | Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with " |
155 | "error %u.\n" ,szDest, GetLastError()); |
156 | } |
157 | Fail("" ); |
158 | |
159 | } |
160 | |
161 | if(wcsncmp(szStringTest,szStringRead, wcslen(szStringTest)) != 0) |
162 | { |
163 | Trace("CopyFileW: ERROR -> The string which was written '%S' does not " |
164 | "match the string '%S' which was read from the copied file.\n" , |
165 | szStringTest,szStringRead); |
166 | bRc = CloseHandle(hFile); |
167 | if (!bRc) |
168 | { |
169 | Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with " |
170 | "error %u.\n" ,szDest, GetLastError()); |
171 | } |
172 | Fail("" ); |
173 | } |
174 | |
175 | /* Close the file handle */ |
176 | bRc = CloseHandle(hFile); |
177 | if (!bRc) |
178 | { |
179 | Fail("CopyFileW: ERROR -> Unable to close file \"%S\" with error %u " |
180 | ".\n" ,szDest,GetLastError()); |
181 | } |
182 | |
183 | /* Remove the temporary file */ |
184 | bRc = DeleteFileW(szDest); |
185 | if(!bRc) |
186 | { |
187 | Fail("CopyFileW: Could not remove copied file with error %u.\n" , |
188 | GetLastError()); |
189 | } |
190 | |
191 | PAL_Terminate(); |
192 | return PASS; |
193 | |
194 | #endif |
195 | |
196 | } |
197 | |