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.

383 lines
14 KiB

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