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: CopyFileW.c |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the CopyFileW function |
10 | ** |
11 | ** |
12 | **===================================================================*/ |
13 | |
14 | /* |
15 | 1. copy an existing file to non-existant with overwrite true |
16 | 2. copy an existing file to non-existant with overwrite false |
17 | 3. copy an existing file to existing with overwrite true |
18 | 4. copy an existing file to existing with overwrite false |
19 | 5. copy non-existant file to non-existant with overwrite true |
20 | 6. copy non-existant file to non-existant with overwrite false |
21 | 7. copy non-existant file to existing with overwrite true |
22 | 8. copy non-existant file to existing with overwrite false |
23 | */ |
24 | |
25 | #include <palsuite.h> |
26 | |
27 | int __cdecl main(int argc, char *argv[]) |
28 | { |
29 | LPSTR lpSource[2] = {"src_existing.tmp" , "src_non-existant.tmp" }; |
30 | LPSTR lpDestination[2] = {"dst_existing.tmp" , "dst_non-existant.tmp" }; |
31 | WCHAR* wcSource; |
32 | WCHAR* wcDest; |
33 | BOOL bFailIfExists[3] = {FALSE, TRUE}; |
34 | BOOL bRc = TRUE; |
35 | BOOL bSuccess = TRUE; |
36 | char results[20]; |
37 | FILE* resultsFile = NULL; |
38 | FILE* tempFile = NULL; |
39 | int nCounter = 0; |
40 | int i, j, k; |
41 | |
42 | if (0 != PAL_Initialize(argc,argv)) |
43 | { |
44 | return FAIL; |
45 | } |
46 | |
47 | /* load the expected results */ |
48 | resultsFile = fopen("expectedresults.txt" , "r" ); |
49 | memset (results, 0, 20); |
50 | fgets(results, 20, resultsFile); |
51 | fclose(resultsFile); |
52 | |
53 | nCounter = 0; |
54 | |
55 | /* create the src_existing file */ |
56 | tempFile = fopen(lpSource[0], "w" ); |
57 | if (tempFile != NULL) |
58 | { |
59 | fprintf(tempFile, "CopyFileW test file: src_existing.tmp\n" ); |
60 | fclose(tempFile); |
61 | } |
62 | else |
63 | { |
64 | Fail("CopyFileW: ERROR-> Couldn't create \"src_existing.tmp\"\n" ); |
65 | } |
66 | |
67 | /* create the dst_existing file */ |
68 | tempFile = fopen(lpDestination[0], "w" ); |
69 | if (tempFile != NULL) |
70 | { |
71 | fprintf(tempFile, "CopyFileW test file: dst_existing.tmp\n" ); |
72 | fclose(tempFile); |
73 | } |
74 | else |
75 | { |
76 | Fail("CopyFileW: ERROR-> Couldn't create \"dst_existing.tmp\"\n" ); |
77 | } |
78 | |
79 | |
80 | /* lpSource loop */ |
81 | for (i = 0; i < 2; i++) |
82 | { |
83 | /* lpDestination loop */ |
84 | for (j = 0; j < 2; j++) |
85 | { |
86 | /* bFailIfExists loop */ |
87 | for (k = 0; k < 2; k++) |
88 | { |
89 | wcSource = convert(lpSource[i]); |
90 | wcDest = convert(lpDestination[j]); |
91 | bRc = CopyFileW(wcSource, |
92 | wcDest, |
93 | bFailIfExists[k]); |
94 | free(wcSource); |
95 | free(wcDest); |
96 | if (!bRc) |
97 | { |
98 | if (results[nCounter] == '1') |
99 | { |
100 | Trace("CopyFileW: FAILED: test[%d][%d][%d]\n" , i, j, k); |
101 | bSuccess = FALSE; |
102 | } |
103 | } |
104 | else |
105 | { |
106 | if (results[nCounter] == '0') |
107 | { |
108 | Trace("CopyFileW: FAILED: test[%d][%d][%d]\n" , i, j, k); |
109 | bSuccess = FALSE; |
110 | } |
111 | else |
112 | { |
113 | /* verify the file was moved */ |
114 | if (GetFileAttributesA(lpDestination[j]) == -1) |
115 | { |
116 | Trace("CopyFileW: GetFileAttributes of destination" |
117 | "file failed on test[%d][%d][%d] with error " |
118 | "code %ld. \n" ,i,j,k,GetLastError()); |
119 | bSuccess = FALSE; |
120 | } |
121 | else if (GetFileAttributesA(lpSource[i]) == -1) |
122 | { |
123 | Trace("CopyFileW: GetFileAttributes of source file " |
124 | "file failed on test[%d][%d][%d] with error " |
125 | "code %ld. \n" ,i,j,k,GetLastError()); |
126 | bSuccess = FALSE; |
127 | } |
128 | else |
129 | { |
130 | /* verify attributes of destination file to |
131 | source file*/ |
132 | if(GetFileAttributes(lpSource[i]) != |
133 | GetFileAttributes(lpDestination[j])) |
134 | { |
135 | Trace("CopyFileW : The file attributes of the " |
136 | "destination file do not match the file " |
137 | "attributes of the source file on test " |
138 | "[%d][%d][%d].\n" ,i,j,k); |
139 | bSuccess = FALSE; |
140 | } |
141 | } |
142 | } |
143 | |
144 | } |
145 | nCounter++; |
146 | /* delete file file but don't worry if it fails */ |
147 | DeleteFileA(lpDestination[1]); |
148 | } |
149 | } |
150 | } |
151 | |
152 | int exitCode = bSuccess ? PASS : FAIL; |
153 | PAL_TerminateEx(exitCode); |
154 | return exitCode; |
155 | } |
156 | |