[KERNEL32_APITEST]
[reactos.git] / rostests / apitests / kernel32 / PrivMoveFileIdentityW.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for PrivMoveFileIdentityW
5 * PROGRAMMER: Pierre Schweitzer <pierre@reactos.org>
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #include <stdio.h>
12 #include <ndk/iofuncs.h>
13 #include <ndk/rtltypes.h>
14
15 static const WCHAR FileName[] = L"TestFile.xxx";
16
17 static BOOL (WINAPI * pPrivMoveFileIdentityW)(LPCWSTR, LPCWSTR, DWORD);
18
19 static
20 BOOL
21 QueryFileInfo(
22 LPCWSTR File,
23 PFILE_BASIC_INFORMATION FileBasicInfo,
24 PFILE_STANDARD_INFORMATION FileStandardInfo)
25 {
26 HANDLE hFile;
27 IO_STATUS_BLOCK IoStatusBlock;
28 NTSTATUS Status;
29
30 hFile = CreateFileW(File, FILE_READ_ATTRIBUTES | SYNCHRONIZE,
31 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
32 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_SYNCHRONOUS_IO_NONALERT,
33 NULL);
34 if (hFile == INVALID_HANDLE_VALUE)
35 {
36 return FALSE;
37 }
38
39 Status = NtQueryInformationFile(hFile, &IoStatusBlock, FileBasicInfo,
40 sizeof(FILE_BASIC_INFORMATION), FileBasicInformation);
41 if (!NT_SUCCESS(Status))
42 {
43 CloseHandle(hFile);
44 return FALSE;
45 }
46
47 Status = NtQueryInformationFile(hFile, &IoStatusBlock, FileStandardInfo,
48 sizeof(FILE_STANDARD_INFORMATION), FileStandardInformation);
49
50 CloseHandle(hFile);
51 return NT_SUCCESS(Status);
52 }
53
54 static
55 VOID
56 TestPrivMoveFileIdentityW(VOID)
57 {
58 FILE_BASIC_INFORMATION FileBasicInfo;
59 FILE_STANDARD_INFORMATION FileStandardInfo;
60 LARGE_INTEGER CreationTime, EndOfFile;
61 HANDLE hDest;
62 NTSTATUS Status;
63 WCHAR Self[MAX_PATH];
64
65 DeleteFileW(FileName);
66
67 if (GetModuleFileNameW(NULL, Self, MAX_PATH) == 0)
68 {
69 win_skip("Failed finding self\n");
70 return;
71 }
72
73 if (!QueryFileInfo(Self, &FileBasicInfo, &FileStandardInfo))
74 {
75 win_skip("Failed querying self\n");
76 return;
77 }
78
79 CreationTime = FileBasicInfo.CreationTime;
80 EndOfFile = FileStandardInfo.EndOfFile;
81
82 Sleep(150);
83
84 hDest = CreateFileW(FileName, GENERIC_WRITE | SYNCHRONIZE,
85 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
86 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_SYNCHRONOUS_IO_NONALERT,
87 NULL);
88 if (hDest == INVALID_HANDLE_VALUE)
89 {
90 win_skip("Failed creating new\n");
91 return;
92 }
93
94 CloseHandle(hDest);
95
96 ok(QueryFileInfo(FileName, &FileBasicInfo, &FileStandardInfo) == TRUE, "QueryFileInfo returned FALSE\n");
97 ok(FileBasicInfo.CreationTime.QuadPart != CreationTime.QuadPart, "Equal creation times\n");
98 ok(FileStandardInfo.EndOfFile.QuadPart == 0LL, "File wasn't created empty: %I64d\n", FileStandardInfo.EndOfFile.QuadPart);
99 Status = (NTSTATUS)pPrivMoveFileIdentityW(Self, FileName, 0);
100 ok(Status == STATUS_SUCCESS, "PrivMoveFileIdentityW failed with %lx\n", Status);
101 ok(QueryFileInfo(FileName, &FileBasicInfo, &FileStandardInfo) == TRUE, "QueryFileInfo returned FALSE\n");
102 ok(FileBasicInfo.CreationTime.QuadPart == CreationTime.QuadPart, "Creation time didn't change\n");
103 ok(FileStandardInfo.EndOfFile.QuadPart == 0LL, "File not empty anymore: %I64d\n", FileStandardInfo.EndOfFile.QuadPart);
104 ok(QueryFileInfo(Self, &FileBasicInfo, &FileStandardInfo) == TRUE, "QueryFileInfo returned FALSE\n");
105 ok(FileBasicInfo.CreationTime.QuadPart == CreationTime.QuadPart, "Creation time changed\n");
106 ok(FileStandardInfo.EndOfFile.QuadPart == EndOfFile.QuadPart, "File size changed: %I64d\n", FileStandardInfo.EndOfFile.QuadPart);
107
108 DeleteFileW(FileName);
109 }
110
111 START_TEST(PrivMoveFileIdentityW)
112 {
113 HMODULE hKern = GetModuleHandleA("kernel32.dll");
114 pPrivMoveFileIdentityW = (void *)GetProcAddress(hKern, "PrivMoveFileIdentityW");
115
116 TestPrivMoveFileIdentityW();
117 }