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: test3.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#include <palsuite.h>
16
17int __cdecl main(int argc, char *argv[])
18{
19
20 BOOL bRc = TRUE;
21 char* szSrcExisting = "src_existing.tmp";
22 char* szDest = "src_dest.tmp";
23 FILE* tempFile = NULL;
24 int retCode;
25
26 if (0 != PAL_Initialize(argc,argv))
27 {
28 return FAIL;
29 }
30
31 /* create the src_existing file */
32 tempFile = fopen(szSrcExisting, "w");
33 if (tempFile != NULL)
34 {
35 retCode = fputs("CopyFileA test file: src_existing.tmp ", tempFile);
36 if(retCode < 0)
37 {
38 retCode = fclose(tempFile);
39 if(retCode != 0)
40 {
41 Trace("CopyFileA: ERROR-> Couldn't close file: %s with error "
42 "%u.\n", szSrcExisting, GetLastError());
43 }
44
45 Fail("CopyFileA: ERROR-> Couldn't write to %s with error "
46 "%u.\n", szSrcExisting, GetLastError());
47 }
48 retCode = fclose(tempFile);
49 if(retCode != 0)
50 {
51 Fail("CopyFileA: ERROR-> Couldn't close file: %s with error "
52 "%u.\n", szSrcExisting, GetLastError());
53 }
54
55 }
56 else
57 {
58 Fail("CopyFileA: ERROR-> Couldn't create %s with "
59 "error %ld\n",szSrcExisting,GetLastError());
60 }
61
62 /* set the file attributes of the source file to readonly */
63 bRc = SetFileAttributes(szSrcExisting, FILE_ATTRIBUTE_READONLY);
64 if(!bRc)
65 {
66 Fail("CopyFileA: ERROR-> Couldn't set file attributes for "
67 "file %s with error %u\n", szSrcExisting, GetLastError());
68 }
69
70 // Check the file attributes to make sure SetFileAttributes() above actually succeeded
71 DWORD fileAttributes = GetFileAttributesA(szSrcExisting);
72 if (fileAttributes == INVALID_FILE_ATTRIBUTES)
73 {
74 Fail("CopyFileA: Failed to get file attributes for source file, %u\n", GetLastError());
75 }
76 if ((fileAttributes & FILE_ATTRIBUTE_READONLY) == 0)
77 {
78 Fail("CopyFileA: SetFileAttributes(read-only) on source file returned success but did not make it read-only.\n");
79 }
80
81 /* copy the file */
82 bRc = CopyFileA(szSrcExisting,szDest,TRUE);
83 if(!bRc)
84 {
85 Fail("CopyFileA: Cannot copy a file with error, %u",GetLastError());
86 }
87
88
89 /* try to get file attributes of destination file */
90 fileAttributes = GetFileAttributesA(szDest);
91 if (fileAttributes == INVALID_FILE_ATTRIBUTES)
92 {
93 Fail("CopyFileA: GetFileAttributes of destination file "
94 "failed with error code %ld. \n",
95 GetLastError());
96 }
97
98 /* verify attributes of destination file to source file*/
99 if((fileAttributes & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY)
100 {
101 Fail("CopyFileA : The file attributes of the "
102 "destination file do not match the file "
103 "attributes of the source file.\n");
104 }
105
106 /* set the attributes of the destination file to normal again */
107 bRc = SetFileAttributesA(szDest, FILE_ATTRIBUTE_NORMAL);
108 if(!bRc)
109 {
110 Fail("CopyFileA: ERROR-> Couldn't set file attributes for "
111 "file %s with error %u\n", szDest, GetLastError());
112 }
113
114 /* delete the newly copied file */
115 bRc = DeleteFile(szDest);
116 if(!bRc)
117 {
118 Fail("CopyFileA: DeleteFile failed to delete the"
119 "file correctly with error,%u.\n",GetLastError());
120 }
121
122 /* set the attributes of the source file to normal again */
123 bRc = SetFileAttributesA(szSrcExisting, FILE_ATTRIBUTE_NORMAL);
124 if(!bRc)
125 {
126 Fail("CopyFileA: ERROR-> Couldn't set file attributes for "
127 "file %s with error %u\n", szSrcExisting, GetLastError());
128 }
129
130 /* delete the original file */
131 bRc = DeleteFile(szSrcExisting);
132 if(!bRc)
133 {
134 Fail("CopyFileA: DeleteFile failed to delete the"
135 "file correctly with error,%u.\n",GetLastError());
136 }
137
138 PAL_Terminate();
139 return PASS;
140
141}
142