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.

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