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: setendoffile.c (test 4) |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the SetEndOfFile function. |
10 | ** Verify that the file pointer is the same before |
11 | ** and after a SetEndOfFile using SetFilePointer with |
12 | ** FILE_BEGIN, FILE_CURRENT and FILE_END |
13 | ** |
14 | ** |
15 | **===================================================================*/ |
16 | |
17 | #include <palsuite.h> |
18 | |
19 | |
20 | const char* szStringTest = "The quick fox jumped over the lazy dog's back." ; |
21 | const char* szTextFile = "test.tmp" ; |
22 | |
23 | static void Cleanup(HANDLE hFile) |
24 | { |
25 | if (!CloseHandle(hFile)) |
26 | { |
27 | Trace("SetEndOfFile: ERROR -> Unable to close file \"%s\". " , |
28 | "GetLastError returned %u.\n" , |
29 | szTextFile, |
30 | GetLastError()); |
31 | } |
32 | if (!DeleteFileA(szTextFile)) |
33 | { |
34 | Trace("SetEndOfFile: ERROR -> Unable to delete file \"%s\". " , |
35 | "GetLastError returned %u.\n" , |
36 | szTextFile, |
37 | GetLastError()); |
38 | } |
39 | } |
40 | |
41 | static void DoTest(HANDLE hFile, DWORD dwOffset, DWORD dwMethod) |
42 | { |
43 | DWORD dwFP1 = 0; |
44 | DWORD dwFP2 = 0; |
45 | DWORD dwError; |
46 | |
47 | /* set the pointer*/ |
48 | dwFP1 = SetFilePointer(hFile, dwOffset, NULL, dwMethod); |
49 | if ((dwFP1 == INVALID_SET_FILE_POINTER) && |
50 | ((dwError = GetLastError()) != ERROR_SUCCESS)) |
51 | { |
52 | Trace("SetEndOfFile: ERROR -> Unable to set the pointer to the " |
53 | "end of the file. GetLastError returned %u.\n" , |
54 | dwError); |
55 | Cleanup(hFile); |
56 | Fail("" ); |
57 | } |
58 | |
59 | /* set EOF */ |
60 | if (!SetEndOfFile(hFile)) |
61 | { |
62 | Trace("SetEndOfFile: ERROR -> Unable to set end of file. " |
63 | "GetLastError returned %u.\n" , |
64 | GetLastError()); |
65 | Cleanup(hFile); |
66 | Fail("" ); |
67 | } |
68 | |
69 | /* get current file pointer pointer */ |
70 | dwFP2 = SetFilePointer(hFile, 0, NULL, FILE_CURRENT); |
71 | if ((dwFP1 == INVALID_SET_FILE_POINTER) && |
72 | ((dwError = GetLastError()) != ERROR_SUCCESS)) |
73 | { |
74 | Trace("SetEndOfFile: ERROR -> Unable to set the pointer to the " |
75 | "end of the file. GetLastError returned %u.\n" , |
76 | dwError); |
77 | Cleanup(hFile); |
78 | Fail("" ); |
79 | } |
80 | |
81 | /* are they the same? */ |
82 | if (dwFP1 != dwFP2) |
83 | { |
84 | Trace("SetEndOfFile: ERROR -> File pointer before (%u) the " |
85 | "SetEndOfFile call was different than after (%u).\n" , |
86 | dwFP1, |
87 | dwFP2); |
88 | Cleanup(hFile); |
89 | Fail("" ); |
90 | } |
91 | } |
92 | |
93 | int __cdecl main(int argc, char *argv[]) |
94 | { |
95 | HANDLE hFile = NULL; |
96 | DWORD dwBytesWritten; |
97 | |
98 | if (0 != PAL_Initialize(argc,argv)) |
99 | { |
100 | return FAIL; |
101 | } |
102 | |
103 | /* create a test file */ |
104 | hFile = CreateFile(szTextFile, |
105 | GENERIC_READ | GENERIC_WRITE, |
106 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
107 | NULL, |
108 | OPEN_ALWAYS, |
109 | FILE_ATTRIBUTE_NORMAL, |
110 | NULL); |
111 | |
112 | if(hFile == INVALID_HANDLE_VALUE) |
113 | { |
114 | Fail("SetEndOfFile: ERROR -> Unable to create file \"%s\". " |
115 | "GetLastError returned %u.\n" , |
116 | szTextFile, |
117 | GetLastError()); |
118 | } |
119 | |
120 | if (!WriteFile(hFile, szStringTest, strlen(szStringTest), &dwBytesWritten, NULL)) |
121 | { |
122 | Trace("SetEndOfFile: ERROR -> Unable to write to \"%s\". " , |
123 | "GetLastError returned %u.\n" , |
124 | szTextFile, |
125 | GetLastError()); |
126 | Cleanup(hFile); |
127 | Fail("" ); |
128 | } |
129 | |
130 | DoTest(hFile, -2, FILE_END); /* test the end */ |
131 | DoTest(hFile, -10, FILE_CURRENT); /* test the middle-ish */ |
132 | DoTest(hFile, 0, FILE_BEGIN); /* test the start */ |
133 | |
134 | Cleanup(hFile); |
135 | |
136 | PAL_Terminate(); |
137 | return PASS; |
138 | } |
139 | |