Windows NT 4.0 source code leak
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.

387 lines
14 KiB

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