summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/CopyFileW/test1/CopyFileW.c
blob: 6127cc21bd8ac274f8a0b156943b818ccdb2d664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=====================================================================
**
** Source:  CopyFileW.c
**
** Purpose: Tests the PAL implementation of the CopyFileW function
**
**
**===================================================================*/

/*
1. copy an existing file to non-existant with overwrite true
2. copy an existing file to non-existant with overwrite false
3. copy an existing file to existing with overwrite true
4. copy an existing file to existing with overwrite false
5. copy non-existant file to non-existant with overwrite true
6. copy non-existant file to non-existant with overwrite false
7. copy non-existant file to existing with overwrite true
8. copy non-existant file to existing with overwrite false
*/

#include <palsuite.h>

int __cdecl main(int argc, char *argv[])
{
    LPSTR lpSource[2] = {"src_existing.tmp", "src_non-existant.tmp"};
    LPSTR lpDestination[2] = {"dst_existing.tmp", "dst_non-existant.tmp"};
    WCHAR* wcSource;
    WCHAR* wcDest;
    BOOL bFailIfExists[3] = {FALSE, TRUE};
    BOOL bRc = TRUE;
    BOOL bSuccess = TRUE;
    char results[20];
    FILE* resultsFile = NULL;
    FILE* tempFile = NULL;
    int nCounter = 0;
    int i, j, k;

    if (0 != PAL_Initialize(argc,argv))
    {
        return FAIL;
    }

    /* load the expected results */
    resultsFile = fopen("expectedresults.txt", "r");
    memset (results, 0, 20);
    fgets(results, 20, resultsFile);
    fclose(resultsFile);

    nCounter = 0;

    /* create the src_existing file */
    tempFile = fopen(lpSource[0], "w");
    if (tempFile != NULL)
    {
        fprintf(tempFile, "CopyFileW test file: src_existing.tmp\n");
        fclose(tempFile);
    }
    else
    {
        Fail("CopyFileW: ERROR-> Couldn't create \"src_existing.tmp\"\n");
    }

    /* create the dst_existing file */
    tempFile = fopen(lpDestination[0], "w");
    if (tempFile != NULL)
    {
        fprintf(tempFile, "CopyFileW test file: dst_existing.tmp\n");
        fclose(tempFile);
    }
    else
    {
        Fail("CopyFileW: ERROR-> Couldn't create \"dst_existing.tmp\"\n");
    }


    /* lpSource loop */
    for (i = 0; i < 2; i++)
    {
        /* lpDestination loop */
        for (j = 0; j < 2; j++)
        {
            /* bFailIfExists loop */
            for (k = 0; k < 2; k++)
            {
                wcSource = convert(lpSource[i]);
                wcDest = convert(lpDestination[j]);
                bRc = CopyFileW(wcSource,
                                wcDest,
                                bFailIfExists[k]);
                free(wcSource);
                free(wcDest);
                if (!bRc)
                {
                    if (results[nCounter] == '1')
                    {
                        Trace("CopyFileW: FAILED: test[%d][%d][%d]\n", i, j, k);
                        bSuccess = FALSE;
                    }
                }
                else
                {
                    if (results[nCounter] == '0')
                    {
                        Trace("CopyFileW: FAILED: test[%d][%d][%d]\n", i, j, k);
                        bSuccess = FALSE;
                    }
                    else
                    {
                        /* verify the file was moved */
                        if (GetFileAttributesA(lpDestination[j]) == -1)
                        {
                            Trace("CopyFileW: GetFileAttributes of destination" 
                                "file failed on test[%d][%d][%d] with error "
                                "code %ld. \n",i,j,k,GetLastError());
                            bSuccess = FALSE;
                        }
                        else if (GetFileAttributesA(lpSource[i]) == -1)
                        {
                            Trace("CopyFileW: GetFileAttributes of source file "
                                "file failed on test[%d][%d][%d] with error "
                                "code %ld. \n",i,j,k,GetLastError());
                            bSuccess = FALSE;
                        }
                        else
                        {
                            /* verify attributes of destination file to 
                            source file*/                    
                            if(GetFileAttributes(lpSource[i]) !=
                                    GetFileAttributes(lpDestination[j]))
                            {
                                Trace("CopyFileW : The file attributes of the "
                                    "destination file do not match the file "
                                    "attributes of the source file on test "
                                    "[%d][%d][%d].\n",i,j,k);
                                bSuccess = FALSE;
                            }
                        }
                    }

                }
                nCounter++;
                /* delete file file but don't worry if it fails */
                DeleteFileA(lpDestination[1]);
            }
        }
    }

    int exitCode = bSuccess ? PASS : FAIL;
    PAL_TerminateEx(exitCode);
    return exitCode;
}