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.

387 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_U 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 = RtlDosPathNameToRelativeNtPathName_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 = 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. RtlReleaseRelativeName(&RelativeName);
  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. }