1 /*
2  * PROJECT:         ReactOS Test applications
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * FILE:            base/applications/testsets/smss/movefile.cpp
5  * PURPOSE:         Provides testing for the "move file after reboot"
6  *                  function of smss.exe/kernel32.dll
7  * PROGRAMMERS:     Dmitriy Philippov (shedon@mail.ru)
8  */
9 
10 
11 #define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
12 #include "windows.h"
13 #include <stdio.h>
14 #include <tchar.h>
15 #include "stdlib.h"
16 #include "string.h"
17 
18 
19 void Usage()
20 {
21 	printf(" Usage: smssTest.exe -g|c|s|d\n\
22 			g - generate test files\n\
23 			c - check files after reboot\n\
24 			s - show registry entry\n\
25 			d - delete registry value\n");
26 }
27 
28 int ShowRegValue()
29 {
30 	BYTE lpBuff[255];
31 	memset(lpBuff, 0, sizeof(lpBuff));
32 
33 	DWORD lSize = sizeof(lpBuff);
34 	HKEY hKey;
35 	LONG retValue;
36 	// test registry entry
37 	retValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", 0, KEY_QUERY_VALUE, &hKey);
38 	if( ERROR_SUCCESS != retValue ) {
39 		printf("RegOpenKeyEx err=%ld \n", retValue);
40 		return 1;
41 	}
42 
43 	retValue = RegQueryValueEx(hKey, "PendingFileRenameOperations", NULL, NULL, lpBuff, &lSize);
44 	if( ERROR_SUCCESS != retValue ) {
45 		printf("RegQueryValueEx err=%ld \n", retValue);
46 		lSize = 0;
47 	}
48 
49 	printf("reg data: \n");
50 	for(UINT i=0; i<lSize; i++) {
51 		printf("%c", lpBuff[i]);
52 	}
53 	printf("\n");
54 
55 	RegCloseKey(hKey);
56 
57 	return 0;
58 }
59 
60 int DeleteValue()
61 {
62 	HKEY hKey;
63 	LONG retValue;
64 	// test registry entry
65 	retValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager", 0, KEY_SET_VALUE, &hKey);
66 	if( ERROR_SUCCESS != retValue ) {
67 		printf("RegOpenKeyEx err=%ld \n", retValue);
68 		return 1;
69 	}
70 
71 	retValue = RegDeleteValue(hKey, "PendingFileRenameOperations");
72 	if( ERROR_SUCCESS != retValue ) {
73 		printf("RegDeleteValue err=%ld \n", retValue);
74 	}
75 
76 	RegCloseKey(hKey);
77 
78 	return 0;
79 }
80 
81 int Generate()
82 {
83 	char sBuf[255];
84 	DWORD dwSize;
85 	HANDLE hFile = NULL;
86 	BOOL fReturnValue;
87 
88 	const char szxReplacedFile[] = "c:\\testFileIsReplaced";
89 	const char szxMovedFileWithRepl[] = "c:\\testFileShouldBeMovedW";
90 	const char szxMovedFile[] = "c:\\testFileShouldBeMoved";
91 	const char szxNewMovedFile[] = "c:\\testFileIsMoved";
92 	const char szxDeletedFile[] = "c:\\testFileShouldBeDeleted";
93 
94 	memset(sBuf, 0xaa, sizeof(sBuf));
95 
96 	// create the first file for moving
97 	hFile = CreateFile(
98 		szxMovedFile,
99 		FILE_ALL_ACCESS,
100 		0,
101 		NULL,
102 		CREATE_ALWAYS,
103 		FILE_ATTRIBUTE_NORMAL,
104 		NULL);
105 	if(NULL == hFile) {
106 		printf("Can't create the %s file, err=%ld \n", szxMovedFile, GetLastError());
107 		return 1;
108 	}
109 	WriteFile(hFile, sBuf, sizeof(sBuf), &dwSize, NULL);
110 	CloseHandle(hFile);
111 
112 	// create the second file for removing
113 	hFile = CreateFile(
114 		szxDeletedFile,
115 		FILE_ALL_ACCESS,
116 		0,
117 		NULL,
118 		CREATE_ALWAYS,
119 		FILE_ATTRIBUTE_NORMAL,
120 		NULL);
121 	if(NULL == hFile) {
122 		printf("Can't create the %s file, err=%ld \n", szxDeletedFile, GetLastError());
123 		return 1;
124 	}
125 	WriteFile(hFile, sBuf, sizeof(sBuf), &dwSize, NULL);
126 	CloseHandle(hFile);
127 
128 	hFile = CreateFile(
129 		szxReplacedFile,
130 		FILE_ALL_ACCESS,
131 		0,
132 		NULL,
133 		CREATE_ALWAYS,
134 		FILE_ATTRIBUTE_NORMAL,
135 		NULL);
136 	if(NULL == hFile) {
137 		printf("Can't create the %s file, err=%ld \n", szxReplacedFile, GetLastError());
138 		return 1;
139 	}
140 	WriteFile(hFile, sBuf, sizeof(sBuf), &dwSize, NULL);
141 	CloseHandle(hFile);
142 
143 
144 	hFile = CreateFile(
145 		szxMovedFileWithRepl,
146 		FILE_ALL_ACCESS,
147 		0,
148 		NULL,
149 		CREATE_ALWAYS,
150 		FILE_ATTRIBUTE_NORMAL,
151 		NULL);
152 	if(NULL == hFile) {
153 		printf("Can't create the %s file, err=%ld \n", szxMovedFileWithRepl, GetLastError());
154 		return 1;
155 	}
156 	WriteFile(hFile, sBuf, sizeof(sBuf), &dwSize, NULL);
157 	CloseHandle(hFile);
158 
159 
160 	fReturnValue = MoveFileEx(
161 		szxDeletedFile,
162 		NULL,
163 		MOVEFILE_DELAY_UNTIL_REBOOT);
164 	if( !fReturnValue ) {
165 		printf("Can't move the %s file, err=%ld \n", szxDeletedFile, GetLastError());
166 		return 1;
167 	}
168 
169 	ShowRegValue();
170 
171 	fReturnValue = MoveFileEx(
172 		szxMovedFile,
173 		szxNewMovedFile,
174 		MOVEFILE_DELAY_UNTIL_REBOOT);
175 	if( !fReturnValue ) {
176 		printf("Can't move the %s file, err=%ld \n", szxMovedFile, GetLastError());
177 		return 1;
178 	}
179 
180 	ShowRegValue();
181 
182 	fReturnValue = MoveFileEx(
183 		szxMovedFileWithRepl,
184 		szxReplacedFile,
185 		MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING);
186 	if( !fReturnValue ) {
187 		printf("Can't move the %s file, err=%ld \n", szxMovedFileWithRepl, GetLastError());
188 		return 1;
189 	}
190 
191 	ShowRegValue();
192 
193 	return 0;
194 }
195 
196 int Check()
197 {
198 	return 0;
199 }
200 
201 int _tmain(int argc, _TCHAR* argv[])
202 {
203 	if( argc<2 ) {
204 		Usage();
205 		return 1;
206 	}
207 
208 	if( 0 == strncmp(argv[1], "-g", 2)  )
209 	{
210 		// generate test files and registry values
211 		return Generate();
212 	}
213 	else if( 0 == strncmp(argv[1], "-c", 2) )
214 	{
215 		// check generated files
216 		return Check();
217 	}
218 	else if( 0 == strncmp(argv[1], "-s", 2) )
219 	{
220 		//
221 		return ShowRegValue();
222 	}
223 	else if( 0 == strncmp(argv[1], "-d", 2) )
224 	{
225 		return DeleteValue();
226 	}
227 	else
228 	{
229 		Usage();
230 		return 1;
231 	}
232 
233 	return 0;
234 }
235 
236