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