Source code of Windows XP (NT5)
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.

388 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 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 = RtlDosPathNameToNtPathName_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 = *(PUNICODE_STRING)&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. RtlFreeHeap( RtlProcessHeap(), 0, FreeBuffer );
  181. }
  182. }
  183. // printf( "TEST: opening .\\test1 for write access.\n" );
  184. if (File1 = fopen( "test1.", "w" )) {
  185. fprintf( File1, "This is test file 1\n" );
  186. // printf( "TEST: closing .\\test1\n" );
  187. fclose( File1 );
  188. }
  189. // printf( "TEST: opening .\\test2 for write access (Instaler should noticed contents different)\n" );
  190. if (File2 = fopen( "test2.", "w" )) {
  191. fprintf( File2, "This is test file 2\n" );
  192. // printf( "TEST: closing .\\test2\n" );
  193. fclose( File2 );
  194. }
  195. // printf( "TEST: opening .\\test0.tmp for write access.\n" );
  196. if (File0 = fopen( "test0.tmp", "w" )) {
  197. fprintf( File0, "This is test file tmp files\n" );
  198. // printf( "TEST: closing .\\test0.tmp\n" );
  199. fclose( File0 );
  200. // printf( "TEST: deleting .\\test0 using DeleteFile (open, set, close)\n" );
  201. rename("test0.tmp", "test0.fin");
  202. }
  203. RtlInitUnicodeString( &KeyName, L"\\Registry\\Machine\\Software\\Test" );
  204. InitializeObjectAttributes( &ObjectAttributes,
  205. &KeyName,
  206. OBJ_CASE_INSENSITIVE,
  207. NULL,
  208. NULL
  209. );
  210. // printf( "TEST: opening %wZ for write access\n", &KeyName );
  211. Status = NtOpenKey( &KeyHandle,
  212. KEY_WRITE,
  213. &ObjectAttributes
  214. );
  215. if (NT_SUCCESS( Status )) {
  216. RtlInitUnicodeString( &ValueName, L"Test0" );
  217. // printf( "TEST: setting %wZ . %wZ value\n", &KeyName, &ValueName );
  218. Status = NtSetValueKey( KeyHandle,
  219. &ValueName,
  220. 0,
  221. REG_SZ,
  222. "0",
  223. 2 * sizeof( WCHAR )
  224. );
  225. RtlInitUnicodeString( &ValueName, L"Test1" );
  226. // printf( "TEST: deleting %wZ . %wZ value\n", &KeyName, &ValueName );
  227. Status = NtDeleteValueKey( KeyHandle,
  228. &ValueName
  229. );
  230. RtlInitUnicodeString( &ValueName, L"Test2" );
  231. // printf( "TEST: setting %wZ . %wZ value\n", &KeyName, &ValueName );
  232. Status = NtSetValueKey( KeyHandle,
  233. &ValueName,
  234. 0,
  235. REG_DWORD,
  236. &ValueDWord,
  237. sizeof( ValueDWord )
  238. );
  239. RtlInitUnicodeString( &SubKeyName, L"Test3" );
  240. InitializeObjectAttributes( &ObjectAttributes,
  241. &SubKeyName,
  242. OBJ_CASE_INSENSITIVE,
  243. KeyHandle,
  244. NULL
  245. );
  246. // printf( "TEST: opening %wZ\\%wZ for write access\n", &KeyName, &SubKeyName );
  247. Status = NtOpenKey( &SubKeyHandle,
  248. DELETE | KEY_WRITE,
  249. &ObjectAttributes
  250. );
  251. if (NT_SUCCESS( Status )) {
  252. // printf( "TEST: deleting %wZ\\%wZ key and values\n", &KeyName, &SubKeyName );
  253. Status = NtDeleteKey( SubKeyHandle );
  254. NtClose( SubKeyHandle );
  255. }
  256. RtlInitUnicodeString( &SubKeyName, L"Test4" );
  257. InitializeObjectAttributes( &ObjectAttributes,
  258. &SubKeyName,
  259. OBJ_CASE_INSENSITIVE,
  260. KeyHandle,
  261. NULL
  262. );
  263. // printf( "TEST: creating %wZ\\%wZ for write access\n", &KeyName, &SubKeyName );
  264. Status = NtCreateKey( &SubKeyHandle,
  265. DELETE | KEY_WRITE,
  266. &ObjectAttributes,
  267. 0,
  268. NULL,
  269. 0,
  270. NULL
  271. );
  272. if (NT_SUCCESS( Status )) {
  273. RtlInitUnicodeString( &ValueName, L"Test4" );
  274. // printf( "TEST: creating %wZ\\%wZ %wZ value\n", &KeyName, &SubKeyName, &ValueName );
  275. Status = NtSetValueKey( SubKeyHandle,
  276. &ValueName,
  277. 0,
  278. REG_DWORD,
  279. &ValueDWord,
  280. sizeof( ValueDWord )
  281. );
  282. NtClose( SubKeyHandle );
  283. }
  284. RtlInitUnicodeString( &SubKeyName, L"Test5" );
  285. InitializeObjectAttributes( &ObjectAttributes,
  286. &SubKeyName,
  287. OBJ_CASE_INSENSITIVE,
  288. KeyHandle,
  289. NULL
  290. );
  291. // printf( "TEST: creating %wZ\\%wZ for write access\n", &KeyName, &SubKeyName );
  292. Status = NtCreateKey( &SubKeyHandle,
  293. DELETE | KEY_WRITE,
  294. &ObjectAttributes,
  295. 0,
  296. NULL,
  297. 0,
  298. NULL
  299. );
  300. if (NT_SUCCESS( Status )) {
  301. RtlInitUnicodeString( &ValueName, L"Test5" );
  302. // printf( "TEST: creating %wZ\\%wZ %wZ value\n", &KeyName, &SubKeyName, &ValueName );
  303. Status = NtSetValueKey( SubKeyHandle,
  304. &ValueName,
  305. 0,
  306. REG_DWORD,
  307. &ValueDWord,
  308. sizeof( ValueDWord )
  309. );
  310. // printf( "TEST: deleting %wZ\\%wZ key and values\n", &KeyName, &SubKeyName );
  311. Status = NtDeleteKey( SubKeyHandle );
  312. NtClose( SubKeyHandle );
  313. }
  314. NtClose( KeyHandle );
  315. }
  316. GetPrivateProfileStringA( "test", NULL, "",
  317. AnsiBuffer,
  318. sizeof( AnsiBuffer ),
  319. ".\\test.ini"
  320. );
  321. GetPrivateProfileStringW( L"test", NULL, L"",
  322. UnicodeBuffer,
  323. sizeof( UnicodeBuffer ),
  324. L".\\test.ini"
  325. );
  326. if (!SetCurrentDirectoryA( ".." )) {
  327. printf( "TEST: SetCurrentDirectory to '..' failed (%u)\n", GetLastError() );
  328. }
  329. WriteProfileString( L"fonts", L"FooBar", L"1" );
  330. WriteProfileString( L"fonts", L"Rockwell Italic (TrueType)", L"ROCKI.FOT" );
  331. if (!SetCurrentDirectoryW( L"test" )) {
  332. printf( "TEST: SetCurrentDirectory to 'test' failed (%u)\n", GetLastError() );
  333. }
  334. WritePrivateProfileStringA( "test", "test1", "-1", ".\\test.ini" );
  335. WritePrivateProfileStringW( L"test", L"test1", L"-2", L".\\test.ini" );
  336. WritePrivateProfileSectionA( "test1", "test0=0\0test1=1\0test2=2\0", ".\\test.ini" );
  337. WritePrivateProfileSectionW( L"test2", L"test0=0\0test1=1\0test2=2\0", L".\\test.ini" );
  338. return 0;
  339. }