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.

386 lines
14 KiB

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