Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
14 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. test.c
  5. Abstract:
  6. This is a test program that issues various Kernel 32 file/registry/INI file
  7. API calls so that we can see if the INSTALER program figures out correctly
  8. what is being done.
  9. This program assumes that the following conditions exist:
  10. TEST subdirectory
  11. Author:
  12. Steve Wood (stevewo) 13-Aug-1994
  13. --*/
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. UCHAR AnsiBuffer[ MAX_PATH ];
  21. WCHAR UnicodeBuffer[ MAX_PATH ];
  22. int
  23. __cdecl
  24. main(
  25. int argc,
  26. char *argv[]
  27. )
  28. {
  29. FILE *File0;
  30. FILE *File1;
  31. FILE *File2;
  32. NTSTATUS Status;
  33. IO_STATUS_BLOCK IoStatusBlock;
  34. OBJECT_ATTRIBUTES ObjectAttributes;
  35. BOOLEAN TranslationStatus;
  36. UNICODE_STRING FileName;
  37. RTL_RELATIVE_NAME_U RelativeName;
  38. PVOID FreeBuffer;
  39. UNICODE_STRING KeyName;
  40. UNICODE_STRING SubKeyName;
  41. UNICODE_STRING ValueName;
  42. HANDLE FileHandle, FindHandle, KeyHandle, SubKeyHandle;
  43. ULONG ValueDWord = 0x12345678;
  44. WIN32_FIND_DATAW FindFileData;
  45. LPSTR s1;
  46. PWSTR s2;
  47. UCHAR AnsiBuffer[ MAX_PATH ];
  48. WCHAR UnicodeBuffer[ MAX_PATH ];
  49. DWORD dwVersion;
  50. OSVERSIONINFO VersionInfo;
  51. HKEY hKey;
  52. //
  53. // File operations we want to test:
  54. //
  55. // Creating a new file.
  56. // Deleting that file using DeleteFile (which does NtOpenFile and NtSetInformationFile
  57. // with Delete Dispostion). (INSTALER should use this to forget about create).
  58. // Creating a new file with the same name.
  59. // Deleting that file using NtDeleteFile. (again INSTALER should not care).
  60. //
  61. // Open the TEST1 file for read access (INSTALER should not care).
  62. // Open the TEST2 file for write access (INSTALER should not care).
  63. // Open the TEST2 file for write access (INSTALER should not care).
  64. // Open the TEST2 file for write access (INSTALER should not care).
  65. //
  66. //
  67. // printf( "TEST: GetFileAttributes( .\\test1 )\n" );
  68. GetFileAttributesA( ".\\test1" );
  69. #if 0
  70. dwVersion = GetVersion();
  71. if ((dwVersion >> 30) ^ 0x2 == VER_PLATFORM_WIN32_WINDOWS) {
  72. printf( "GetVersion returns Windows 95\n" );
  73. }
  74. else
  75. if ((dwVersion >> 30) ^ 0x2 == VER_PLATFORM_WIN32_NT) {
  76. printf( "GetVersion returns Windows NT\n" );
  77. }
  78. else {
  79. printf( "GetVersion returns %x\n", dwVersion );
  80. }
  81. fflush(stdout);
  82. VersionInfo.dwOSVersionInfoSize = sizeof( VersionInfo );
  83. GetVersionEx( &VersionInfo );
  84. if (VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
  85. printf( "GetVersionEx returns Windows 95\n" );
  86. }
  87. else
  88. if (VersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
  89. printf( "GetVersionEx returns Windows NT\n" );
  90. }
  91. else {
  92. printf( "GetVersionEx returns %x\n", VersionInfo.dwPlatformId );
  93. }
  94. fflush(stdout);
  95. if (RegConnectRegistryA( "\\\\stevewo_dbgr", HKEY_USERS, &hKey )) {
  96. printf( "RegConnectRegistryA( \\stevewo_dbgr ) failed (hKey == %x).\n", hKey );
  97. }
  98. else {
  99. printf( "RegConnectRegistryA( \\stevewo_dbgr ) succeeded (hKey == %x).\n", hKey );
  100. RegCloseKey( hKey );
  101. }
  102. if (RegConnectRegistryW( L"\\\\stevewo_dbgr", HKEY_USERS, &hKey )) {
  103. printf( "RegConnectRegistryW( \\stevewo_dbgr ) failed (hKey == %x).\n", hKey );
  104. }
  105. else {
  106. printf( "RegConnectRegistryW( \\stevewo_dbgr ) succeeded (hKey == %x).\n", hKey );
  107. RegCloseKey( hKey );
  108. }
  109. #endif
  110. RtlInitUnicodeString( &FileName, L"\\DosDevices\\A:" );
  111. InitializeObjectAttributes( &ObjectAttributes,
  112. &FileName,
  113. OBJ_CASE_INSENSITIVE,
  114. NULL,
  115. NULL
  116. );
  117. // printf( "TEST: NtOpenFile( %wZ ) should succeed without touching floppy.\n", &FileName );
  118. Status = NtOpenFile( &FileHandle,
  119. SYNCHRONIZE | FILE_READ_ATTRIBUTES,
  120. &ObjectAttributes,
  121. &IoStatusBlock,
  122. FILE_SHARE_READ | FILE_SHARE_WRITE,
  123. FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE
  124. );
  125. if (!NT_SUCCESS( Status )) {
  126. printf( "TEST: Failed - Status == %x\n", Status );
  127. }
  128. else {
  129. NtClose( FileHandle );
  130. }
  131. // printf( "TEST: FindFirstFileW( C:\\*.* should fail.\n" );
  132. FindHandle = FindFirstFileW( L"C:\\*.*", &FindFileData );
  133. if (FindHandle != INVALID_HANDLE_VALUE) {
  134. printf( "TEST: *** oops, it worked.\n" );
  135. FindClose( FindHandle );
  136. }
  137. // printf( "TEST: FindFirstFileW( \\TMP\\*.* should work.\n" );
  138. FindHandle = FindFirstFileW( L"\\TMP\\*.*", &FindFileData );
  139. if (FindHandle == INVALID_HANDLE_VALUE) {
  140. printf( "TEST: *** oops, it failed.\n" );
  141. }
  142. else {
  143. FindClose( FindHandle );
  144. }
  145. // printf( "TEST: opening .\\test0 for write access.\n" );
  146. if (File0 = fopen( "test0.", "w" )) {
  147. fprintf( File0, "This is test file 0\n" );
  148. // printf( "TEST: closing .\\test0\n" );
  149. fclose( File0 );
  150. // printf( "TEST: deleting .\\test0 using DeleteFile (open, set, close)\n" );
  151. DeleteFile( L"test0" );
  152. }
  153. // printf( "TEST: opening .\\test0 for write access.\n" );
  154. if (File0 = fopen( "test0.", "w" )) {
  155. fprintf( File0, "This is test file 0\n" );
  156. // printf( "TEST: closing .\\test0\n" );
  157. fclose( File0 );
  158. TranslationStatus = RtlDosPathNameToRelativeNtPathName_U(
  159. L"test0",
  160. &FileName,
  161. NULL,
  162. &RelativeName
  163. );
  164. if (TranslationStatus ) {
  165. FreeBuffer = FileName.Buffer;
  166. if ( RelativeName.RelativeName.Length ) {
  167. FileName = RelativeName.RelativeName;
  168. }
  169. else {
  170. RelativeName.ContainingDirectory = NULL;
  171. }
  172. InitializeObjectAttributes( &ObjectAttributes,
  173. &FileName,
  174. OBJ_CASE_INSENSITIVE,
  175. RelativeName.ContainingDirectory,
  176. NULL
  177. );
  178. // printf( "TEST: deleting .\\test0 using NtDeleteFile\n" );
  179. Status = NtDeleteFile( &ObjectAttributes );
  180. RtlReleaseRelativeName(&RelativeName);
  181. RtlFreeHeap( RtlProcessHeap(), 0, FreeBuffer );
  182. }
  183. }
  184. // printf( "TEST: opening .\\test1 for write access.\n" );
  185. if (File1 = fopen( "test1.", "w" )) {
  186. fprintf( File1, "This is test file 1\n" );
  187. // printf( "TEST: closing .\\test1\n" );
  188. fclose( File1 );
  189. }
  190. // printf( "TEST: opening .\\test2 for write access (Instaler should noticed contents different)\n" );
  191. if (File2 = fopen( "test2.", "w" )) {
  192. fprintf( File2, "This is test file 2\n" );
  193. // printf( "TEST: closing .\\test2\n" );
  194. fclose( File2 );
  195. }
  196. // printf( "TEST: opening .\\test0.tmp for write access.\n" );
  197. if (File0 = fopen( "test0.tmp", "w" )) {
  198. fprintf( File0, "This is test file tmp files\n" );
  199. // printf( "TEST: closing .\\test0.tmp\n" );
  200. fclose( File0 );
  201. // printf( "TEST: deleting .\\test0 using DeleteFile (open, set, close)\n" );
  202. rename("test0.tmp", "test0.fin");
  203. }
  204. RtlInitUnicodeString( &KeyName, L"\\Registry\\Machine\\Software\\Test" );
  205. InitializeObjectAttributes( &ObjectAttributes,
  206. &KeyName,
  207. OBJ_CASE_INSENSITIVE,
  208. NULL,
  209. NULL
  210. );
  211. // printf( "TEST: opening %wZ for write access\n", &KeyName );
  212. Status = NtOpenKey( &KeyHandle,
  213. KEY_WRITE,
  214. &ObjectAttributes
  215. );
  216. if (NT_SUCCESS( Status )) {
  217. RtlInitUnicodeString( &ValueName, L"Test0" );
  218. // printf( "TEST: setting %wZ . %wZ value\n", &KeyName, &ValueName );
  219. Status = NtSetValueKey( KeyHandle,
  220. &ValueName,
  221. 0,
  222. REG_SZ,
  223. "0",
  224. 2 * sizeof( WCHAR )
  225. );
  226. RtlInitUnicodeString( &ValueName, L"Test1" );
  227. // printf( "TEST: deleting %wZ . %wZ value\n", &KeyName, &ValueName );
  228. Status = NtDeleteValueKey( KeyHandle,
  229. &ValueName
  230. );
  231. RtlInitUnicodeString( &ValueName, L"Test2" );
  232. // printf( "TEST: setting %wZ . %wZ value\n", &KeyName, &ValueName );
  233. Status = NtSetValueKey( KeyHandle,
  234. &ValueName,
  235. 0,
  236. REG_DWORD,
  237. &ValueDWord,
  238. sizeof( ValueDWord )
  239. );
  240. RtlInitUnicodeString( &SubKeyName, L"Test3" );
  241. InitializeObjectAttributes( &ObjectAttributes,
  242. &SubKeyName,
  243. OBJ_CASE_INSENSITIVE,
  244. KeyHandle,
  245. NULL
  246. );
  247. // printf( "TEST: opening %wZ\\%wZ for write access\n", &KeyName, &SubKeyName );
  248. Status = NtOpenKey( &SubKeyHandle,
  249. DELETE | KEY_WRITE,
  250. &ObjectAttributes
  251. );
  252. if (NT_SUCCESS( Status )) {
  253. // printf( "TEST: deleting %wZ\\%wZ key and values\n", &KeyName, &SubKeyName );
  254. Status = NtDeleteKey( SubKeyHandle );
  255. NtClose( SubKeyHandle );
  256. }
  257. RtlInitUnicodeString( &SubKeyName, L"Test4" );
  258. InitializeObjectAttributes( &ObjectAttributes,
  259. &SubKeyName,
  260. OBJ_CASE_INSENSITIVE,
  261. KeyHandle,
  262. NULL
  263. );
  264. // printf( "TEST: creating %wZ\\%wZ for write access\n", &KeyName, &SubKeyName );
  265. Status = NtCreateKey( &SubKeyHandle,
  266. DELETE | KEY_WRITE,
  267. &ObjectAttributes,
  268. 0,
  269. NULL,
  270. 0,
  271. NULL
  272. );
  273. if (NT_SUCCESS( Status )) {
  274. RtlInitUnicodeString( &ValueName, L"Test4" );
  275. // printf( "TEST: creating %wZ\\%wZ %wZ value\n", &KeyName, &SubKeyName, &ValueName );
  276. Status = NtSetValueKey( SubKeyHandle,
  277. &ValueName,
  278. 0,
  279. REG_DWORD,
  280. &ValueDWord,
  281. sizeof( ValueDWord )
  282. );
  283. NtClose( SubKeyHandle );
  284. }
  285. RtlInitUnicodeString( &SubKeyName, L"Test5" );
  286. InitializeObjectAttributes( &ObjectAttributes,
  287. &SubKeyName,
  288. OBJ_CASE_INSENSITIVE,
  289. KeyHandle,
  290. NULL
  291. );
  292. // printf( "TEST: creating %wZ\\%wZ for write access\n", &KeyName, &SubKeyName );
  293. Status = NtCreateKey( &SubKeyHandle,
  294. DELETE | KEY_WRITE,
  295. &ObjectAttributes,
  296. 0,
  297. NULL,
  298. 0,
  299. NULL
  300. );
  301. if (NT_SUCCESS( Status )) {
  302. RtlInitUnicodeString( &ValueName, L"Test5" );
  303. // printf( "TEST: creating %wZ\\%wZ %wZ value\n", &KeyName, &SubKeyName, &ValueName );
  304. Status = NtSetValueKey( SubKeyHandle,
  305. &ValueName,
  306. 0,
  307. REG_DWORD,
  308. &ValueDWord,
  309. sizeof( ValueDWord )
  310. );
  311. // printf( "TEST: deleting %wZ\\%wZ key and values\n", &KeyName, &SubKeyName );
  312. Status = NtDeleteKey( SubKeyHandle );
  313. NtClose( SubKeyHandle );
  314. }
  315. NtClose( KeyHandle );
  316. }
  317. GetPrivateProfileStringA( "test", NULL, "",
  318. AnsiBuffer,
  319. sizeof( AnsiBuffer ),
  320. ".\\test.ini"
  321. );
  322. GetPrivateProfileStringW( L"test", NULL, L"",
  323. UnicodeBuffer,
  324. sizeof( UnicodeBuffer ),
  325. L".\\test.ini"
  326. );
  327. if (!SetCurrentDirectoryA( ".." )) {
  328. printf( "TEST: SetCurrentDirectory to '..' failed (%u)\n", GetLastError() );
  329. }
  330. WriteProfileString( L"fonts", L"FooBar", L"1" );
  331. WriteProfileString( L"fonts", L"Rockwell Italic (TrueType)", L"ROCKI.FOT" );
  332. if (!SetCurrentDirectoryW( L"test" )) {
  333. printf( "TEST: SetCurrentDirectory to 'test' failed (%u)\n", GetLastError() );
  334. }
  335. WritePrivateProfileStringA( "test", "test1", "-1", ".\\test.ini" );
  336. WritePrivateProfileStringW( L"test", L"test1", L"-2", L".\\test.ini" );
  337. WritePrivateProfileSectionA( "test1", "test0=0\0test1=1\0test2=2\0", ".\\test.ini" );
  338. WritePrivateProfileSectionW( L"test2", L"test0=0\0test1=1\0test2=2\0", L".\\test.ini" );
  339. return 0;
  340. }