summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/CopyFileA/test1/CopyFileA.c
blob: bfea85b7cb0362163e6456780d5b750b72cd21f9 (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
156
157
158
159
// 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:  CopyFileA.c
**
** Purpose: Tests the PAL implementation of the CopyFileA function
**
**
**===================================================================*/

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

#include <palsuite.h>

struct TESTS{
    char* lpSource;
    char* lpDestination;
    BOOL bFailIfExists;
    int nResult;
    };


int __cdecl main(int argc, char *argv[])
{
    char szSrcExisting[] =     {"src_existing.tmp"};
    char szSrcNonExistant[] =  {"src_non-existant.tmp"};
    char szDstExisting[] =     {"dst_existing.tmp"};
    char szDstNonExistant[] =  {"dst_non-existant.tmp"};
    BOOL bRc = TRUE;
    BOOL bSuccess = TRUE;
    FILE* tempFile = NULL;
    int i;
    struct TESTS testCase[] =
    {
        {szSrcExisting, szDstExisting, FALSE, 1},
        {szSrcExisting, szDstExisting, TRUE, 0},
        {szSrcExisting, szDstNonExistant, FALSE, 1},
        {szSrcExisting, szDstNonExistant, TRUE, 1},
        {szSrcNonExistant, szDstExisting, FALSE, 0},
        {szSrcNonExistant, szDstExisting, TRUE, 0},
        {szSrcNonExistant, szDstNonExistant, FALSE, 0},
        {szSrcNonExistant, szDstNonExistant, TRUE, 0}
    };


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

    /* create the src_existing file */
    tempFile = fopen(szSrcExisting, "w");
    if (tempFile != NULL)
    {
        fprintf(tempFile, "CopyFileA test file: src_existing.tmp\n");
        fclose(tempFile);
    }
    else
    {
        Fail("CopyFileA: ERROR-> Couldn't create \"src_existing.tmp\" with "
            "error %ld\n",
            GetLastError());
    }

    /* create the dst_existing file */
    tempFile = fopen(szDstExisting, "w");
    if (tempFile != NULL)
    {
        fprintf(tempFile, "CopyFileA test file: dst_existing.tmp\n");
        fclose(tempFile);
    }
    else
    {
        Fail("CopyFileA: ERROR-> Couldn't create \"dst_existing.tmp\" with "
            "error %ld\n",
            GetLastError());
    }



    for (i = 0; i < (sizeof(testCase) / sizeof(struct TESTS)); i++)
    {
        bRc = CopyFileA(testCase[i].lpSource,
                        testCase[i].lpDestination,
                        testCase[i].bFailIfExists);
        if (!bRc)
        {
            if (testCase[i].nResult == 1)
            {
                Trace("CopyFileA: FAILED: %s -> %s with bFailIfExists = %d "
                    "with error %ld\n",
                    testCase[i].lpSource,
                    testCase[i].lpDestination,
                    testCase[i].bFailIfExists,
                    GetLastError());
                bSuccess = FALSE;
            }
        }
        else
        {
            if (testCase[i].nResult == 0)
            {
                Trace("CopyFileA: FAILED: %s -> %s with bFailIfExists = %d\n",
                    testCase[i].lpSource,
                    testCase[i].lpDestination,
                    testCase[i].bFailIfExists);
                bSuccess = FALSE;
            }
            else
            {
                /* verify the file was moved */
                if (GetFileAttributesA(testCase[i].lpDestination) == -1)
                {
                    Trace("CopyFileA: GetFileAttributes of destination file "
                        "failed with error code %ld. \n",
                        GetLastError());
                    bSuccess = FALSE;
                }
                else if (GetFileAttributesA(testCase[i].lpSource) == -1)
                {
                    Trace("CopyFileA: GetFileAttributes of source file "
                        "failed with error code %ld. \n",
                        GetLastError());
                    bSuccess = FALSE;
                }
                else
                {
                    /* verify attributes of destination file to source file*/                    
                    if(GetFileAttributes(testCase[i].lpSource) !=
                            GetFileAttributes(testCase[i].lpDestination))
                    {
                        Trace("CopyFileA : The file attributes of the "
                            "destination file do not match the file "
                            "attributes of the source file.\n");
                        bSuccess = FALSE;
                    }
                }
            }
        }
        /* delete file file but don't worry if it fails */
        DeleteFileA(szDstNonExistant);
    }

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