summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/SetEndOfFile/test4/setendoffile.cpp
blob: 98a6ec63daff3e6b817c1b8a27ab794a66be0f59 (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
// 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:  setendoffile.c (test 4)
**
** Purpose: Tests the PAL implementation of the SetEndOfFile function.
**          Verify that the file pointer is the same before
**          and after a SetEndOfFile using SetFilePointer with
**          FILE_BEGIN, FILE_CURRENT and FILE_END
**
**
**===================================================================*/

#include <palsuite.h>


const char* szStringTest = "The quick fox jumped over the lazy dog's back.";
const char* szTextFile = "test.tmp";

static void Cleanup(HANDLE hFile)
{
    if (!CloseHandle(hFile))
    {
        Trace("SetEndOfFile: ERROR -> Unable to close file \"%s\". ", 
            "GetLastError returned %u.\n", 
            szTextFile,
            GetLastError());
    }
    if (!DeleteFileA(szTextFile))
    {
        Trace("SetEndOfFile: ERROR -> Unable to delete file \"%s\". ", 
            "GetLastError returned %u.\n", 
            szTextFile,
            GetLastError());
    }
}

static void DoTest(HANDLE hFile, DWORD dwOffset, DWORD dwMethod)
{
    DWORD dwFP1 = 0;
    DWORD dwFP2 = 0;
    DWORD dwError;

    /* set the pointer*/
    dwFP1 = SetFilePointer(hFile, dwOffset, NULL, dwMethod);
    if ((dwFP1 == INVALID_SET_FILE_POINTER) &&
        ((dwError = GetLastError()) != ERROR_SUCCESS))
    {
        Trace("SetEndOfFile: ERROR -> Unable to set the pointer to the "
            "end of the file. GetLastError returned %u.\n",
            dwError);
        Cleanup(hFile);
        Fail("");
    }

    /* set EOF */
    if (!SetEndOfFile(hFile))
    {
        Trace("SetEndOfFile: ERROR -> Unable to set end of file. "
            "GetLastError returned %u.\n",
            GetLastError());
        Cleanup(hFile);
        Fail("");
    }

    /* get current file pointer pointer */
    dwFP2 = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
    if ((dwFP1 == INVALID_SET_FILE_POINTER) &&
        ((dwError = GetLastError()) != ERROR_SUCCESS))
    {
        Trace("SetEndOfFile: ERROR -> Unable to set the pointer to the "
            "end of the file. GetLastError returned %u.\n",
            dwError);
        Cleanup(hFile);
        Fail("");
    }

    /* are they the same? */
    if (dwFP1 != dwFP2)
    {
        Trace("SetEndOfFile: ERROR -> File pointer before (%u) the "
            "SetEndOfFile call was different than after (%u).\n",
            dwFP1,
            dwFP2);
        Cleanup(hFile);
        Fail("");
    }
}

int __cdecl main(int argc, char *argv[])
{
    HANDLE hFile = NULL;
    DWORD dwBytesWritten;

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

    /* create a test file */
    hFile = CreateFile(szTextFile, 
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    if(hFile == INVALID_HANDLE_VALUE)
    {
        Fail("SetEndOfFile: ERROR -> Unable to create file \"%s\". "
            "GetLastError returned %u.\n", 
            szTextFile,
            GetLastError());
    }

    if (!WriteFile(hFile, szStringTest, strlen(szStringTest), &dwBytesWritten, NULL))
    {
        Trace("SetEndOfFile: ERROR -> Unable to write to \"%s\". ", 
            "GetLastError returned %u.\n", 
            szTextFile,
            GetLastError());
        Cleanup(hFile);
        Fail("");
    }

    DoTest(hFile, -2, FILE_END);        /* test the end */
    DoTest(hFile, -10, FILE_CURRENT);   /* test the middle-ish */
    DoTest(hFile, 0, FILE_BEGIN);       /* test the start */

    Cleanup(hFile);

    PAL_Terminate();
    return PASS;
}