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.

693 lines
16 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. TESTWINA.C
  5. Abstract:
  6. Test program for the eventlog service. This program calls the Win
  7. APIs to test out the operation of the service.
  8. Author:
  9. Rajen Shah (rajens) 05-Aug-1991
  10. Revision History:
  11. --*/
  12. /*----------------------*/
  13. /* INCLUDES */
  14. /*----------------------*/
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include <stdio.h> // printf
  19. #include <string.h> // stricmp
  20. #include <stdlib.h>
  21. #include <windows.h>
  22. #include <netevent.h>
  23. //#include <elfcommn.h>
  24. #define READ_BUFFER_SIZE 1024*2 // Use 2K buffer
  25. #define SIZE_DATA_ARRAY 65
  26. //
  27. // Global buffer used to emulate "binary data" when writing an event
  28. // record.
  29. //
  30. DWORD Data[SIZE_DATA_ARRAY];
  31. BOOL bHackTestBackup = FALSE;
  32. PCHAR pServerName = NULL;
  33. VOID
  34. Initialize (
  35. VOID
  36. )
  37. {
  38. DWORD i;
  39. // Initialize the values in the data buffer.
  40. //
  41. for (i=0; i< SIZE_DATA_ARRAY; i++)
  42. Data[i] = i;
  43. }
  44. BOOL
  45. Usage (
  46. VOID
  47. )
  48. {
  49. printf( "usage: \n" );
  50. printf( "-c Tests ClearEventLog API\n");
  51. printf( "-b filename Tests BackupEventLog API\n");
  52. printf( "-rsb Reads event log sequentially backwards\n");
  53. printf( "-rsf Reads event log sequentially forwards\n");
  54. printf( "-rrb <record> Reads event log from <record> backwards\n");
  55. printf( "-rrf <record> Reads event log from <record> forwards\n");
  56. printf( "-w <count> Tests ReportEvent API <count> times\n");
  57. return ERROR_INVALID_PARAMETER;
  58. } // Usage
  59. BOOL
  60. WriteLogEntry ( HANDLE LogHandle, DWORD EventID )
  61. {
  62. #define NUM_STRINGS 2
  63. #define MAX_STRING_SIZE 32767 // Max size is FFFF/2 for ANSI strings
  64. BOOL Status;
  65. WORD EventType;
  66. DWORD i;
  67. DWORD DataSize;
  68. PSID pUserSid;
  69. PCHAR BigString;
  70. // PSTR Strings[NUM_STRINGS] = {"StringAOne","StringATwo" };
  71. PSTR Strings[NUM_STRINGS];
  72. Strings[0] = "StringAOne";
  73. BigString = malloc(MAX_STRING_SIZE);
  74. for (i = 0; i < MAX_STRING_SIZE; i++) {
  75. BigString[i] = 'A';
  76. }
  77. BigString[MAX_STRING_SIZE-1] = '\0';
  78. Strings[1] = BigString;
  79. EventType = EVENTLOG_INFORMATION_TYPE;
  80. pUserSid = NULL;
  81. DataSize = sizeof(DWORD) * SIZE_DATA_ARRAY;
  82. for (i=0; i< SIZE_DATA_ARRAY; i++)
  83. Data[i] += i;
  84. Status = ReportEventA (
  85. LogHandle,
  86. EventType,
  87. 0, // event category
  88. EventID,
  89. pUserSid,
  90. (WORD) NUM_STRINGS,
  91. DataSize,
  92. Strings,
  93. (PVOID)Data
  94. );
  95. free(BigString);
  96. return (Status);
  97. }
  98. BOOL
  99. WriteLogEntryMsg ( HANDLE LogHandle, DWORD EventID )
  100. /*
  101. This function requires a registry entry in the Applications section
  102. of the Eventlog for TESTWINAAPP, it will use the netevent.dll message file.
  103. */
  104. {
  105. #define NUM_STRINGS 2
  106. BOOL Status;
  107. WORD EventType;
  108. DWORD DataSize;
  109. PSID pUserSid;
  110. PCHAR BigString;
  111. PSTR Strings[NUM_STRINGS];
  112. Strings[0] = "This is a BOGUS message for TEST purposes Ignore this substitution text";
  113. Strings[1] = "GHOST SERVICE in the long string format - I wanted a long string to pass into this function";
  114. EventType = EVENTLOG_INFORMATION_TYPE;
  115. pUserSid = NULL;
  116. DataSize = sizeof(DWORD) * SIZE_DATA_ARRAY;
  117. Status = ReportEventA (
  118. LogHandle,
  119. EventType,
  120. 0, // event category
  121. EVENT_SERVICE_START_FAILED_NONE,
  122. pUserSid,
  123. (WORD) NUM_STRINGS,
  124. 0, // DataSize
  125. Strings,
  126. (PVOID)NULL // Data
  127. );
  128. free(BigString);
  129. return (Status);
  130. }
  131. VOID
  132. DisplayEventRecords( PVOID Buffer,
  133. DWORD BufSize,
  134. ULONG *NumRecords)
  135. {
  136. PEVENTLOGRECORD pLogRecord;
  137. PSTR pString;
  138. DWORD Count = 0;
  139. DWORD Offset = 0;
  140. DWORD i;
  141. pLogRecord = (PEVENTLOGRECORD) Buffer;
  142. while ((DWORD)Offset < BufSize) {
  143. Count++;
  144. printf("\n\nRecord # %lu\n", pLogRecord->RecordNumber);
  145. printf("Length: 0x%lx TimeGenerated: 0x%lx EventID: 0x%lx EventType: 0x%x\n",
  146. pLogRecord->Length, pLogRecord->TimeGenerated, pLogRecord->EventID,
  147. pLogRecord->EventType);
  148. printf("NumStrings: 0x%x StringOffset: 0x%lx UserSidLength: 0x%lx TimeWritten: 0x%lx\n",
  149. pLogRecord->NumStrings, pLogRecord->StringOffset,
  150. pLogRecord->UserSidLength, pLogRecord->TimeWritten);
  151. printf("UserSidOffset: 0x%lx DataLength: 0x%lx DataOffset: 0x%lx \n",
  152. pLogRecord->UserSidOffset, pLogRecord->DataLength,
  153. pLogRecord->DataOffset);
  154. //
  155. // Print out module name
  156. //
  157. pString = (PSTR)((DWORD)pLogRecord + sizeof(EVENTLOGRECORD));
  158. printf("ModuleName: %s ", pString);
  159. //
  160. // Display ComputerName
  161. //
  162. pString = (PSTR)((DWORD)pString + strlen(pString) + 1);
  163. printf("ComputerName: %s\n",pString);
  164. //
  165. // Display strings
  166. //
  167. pString = (PSTR)((DWORD)Buffer + pLogRecord->StringOffset);
  168. printf("Strings: ");
  169. for (i=0; i<pLogRecord->NumStrings; i++) {
  170. printf(" %s ", pString);
  171. pString = (PSTR)((DWORD)pString + strlen(pString) + 1);
  172. }
  173. // Get next record
  174. Offset += pLogRecord->Length;
  175. pLogRecord = (PEVENTLOGRECORD)((DWORD)Buffer + Offset);
  176. }
  177. *NumRecords = Count;
  178. }
  179. BOOL
  180. ReadFromLog ( HANDLE LogHandle,
  181. PVOID Buffer,
  182. ULONG *pBytesRead,
  183. DWORD ReadFlag,
  184. DWORD Record
  185. )
  186. {
  187. BOOL Status;
  188. DWORD MinBytesNeeded;
  189. DWORD ErrorCode;
  190. Status = ReadEventLogA (
  191. LogHandle,
  192. ReadFlag,
  193. Record,
  194. Buffer,
  195. READ_BUFFER_SIZE,
  196. pBytesRead,
  197. &MinBytesNeeded
  198. );
  199. if (!Status) {
  200. ErrorCode = GetLastError();
  201. printf("Error from ReadEventLog %d \n", ErrorCode);
  202. if (ErrorCode == ERROR_NO_MORE_FILES)
  203. printf("Buffer too small. Need %lu bytes min\n", MinBytesNeeded);
  204. }
  205. return (Status);
  206. }
  207. BOOL
  208. TestReadEventLog (DWORD Count, DWORD ReadFlag, DWORD Record)
  209. {
  210. BOOL Status, IStatus;
  211. HANDLE LogHandle;
  212. LPSTR ModuleName;
  213. DWORD NumRecords, BytesReturned;
  214. PVOID Buffer;
  215. DWORD RecordOffset;
  216. DWORD NumberOfRecords;
  217. DWORD OldestRecord;
  218. printf("Testing ReadEventLog API to read %lu entries\n",Count);
  219. Buffer = malloc (READ_BUFFER_SIZE);
  220. //
  221. // Initialize the strings
  222. //
  223. NumRecords = Count;
  224. ModuleName = "TESTWINAAPP";
  225. //
  226. // Open the log handle
  227. //
  228. //
  229. // This is just a quick and dirty way to test the api to read a backup
  230. // log, until I can fix test.c to be more general purpose.
  231. //
  232. if (bHackTestBackup) {
  233. printf("OpenBackupEventLog = ");
  234. LogHandle = OpenBackupEventLog(
  235. NULL,
  236. "\\\\danhi386\\roote\\view.log"
  237. );
  238. }
  239. else {
  240. printf("OpenEventLog - ");
  241. LogHandle = OpenEventLog (
  242. pServerName,
  243. ModuleName
  244. );
  245. }
  246. if (LogHandle == NULL) {
  247. printf("Error - %d\n", GetLastError());
  248. } else {
  249. printf("SUCCESS\n");
  250. //
  251. // Get and print record information
  252. //
  253. Status = GetNumberOfEventLogRecords(LogHandle, & NumberOfRecords);
  254. if (NT_SUCCESS(Status)) {
  255. Status = GetOldestEventLogRecord(LogHandle, & OldestRecord);
  256. }
  257. if (!NT_SUCCESS(Status)) {
  258. printf("Get of record information failed with %X", Status);
  259. return(Status);
  260. }
  261. printf("\nThere are %d records in the file, %d is the oldest"
  262. " record number\n", NumberOfRecords, OldestRecord);
  263. RecordOffset = Record;
  264. while (Count && (BytesReturned != 0)) {
  265. printf("Read %u records\n", NumRecords);
  266. //
  267. // Read from the log
  268. //
  269. Status = ReadFromLog ( LogHandle,
  270. Buffer,
  271. &BytesReturned,
  272. ReadFlag,
  273. RecordOffset
  274. );
  275. if (Status) {
  276. printf("Bytes read = 0x%lx\n", BytesReturned);
  277. DisplayEventRecords(Buffer, BytesReturned, &NumRecords);
  278. Count -= NumRecords;
  279. RecordOffset += NumRecords;
  280. } else {
  281. break;
  282. }
  283. }
  284. printf("\n");
  285. if (!Status) {
  286. printf ("Error - %d. Remaining count %lu\n", GetLastError(), Count);
  287. } else {
  288. printf ("SUCCESS\n");
  289. }
  290. printf("Calling CloseEventLog\n");
  291. IStatus = CloseEventLog (LogHandle);
  292. }
  293. free(Buffer);
  294. return (Status);
  295. }
  296. BOOL
  297. TestWriteEventLog (DWORD Count)
  298. {
  299. BOOL Status, IStatus;
  300. HANDLE LogHandle=NULL;
  301. LPSTR ModuleName;
  302. DWORD EventID = 99;
  303. DWORD WriteCount;
  304. printf("Testing ReportEvent API\n");
  305. //
  306. // Initialize the strings
  307. //
  308. ModuleName = "TESTWINAAPP";
  309. //
  310. // Open the log handle
  311. //
  312. while (Count && NT_SUCCESS(Status)) {
  313. //printf("Calling RegisterEventSource for WRITE %lu times - ", Count);
  314. LogHandle = RegisterEventSourceA (
  315. pServerName,
  316. ModuleName
  317. );
  318. if (LogHandle == NULL) {
  319. printf("Error - %d\n", GetLastError());
  320. } else {
  321. printf("Registered - ");
  322. WriteCount = 5;
  323. printf("Record # %u ", Count);
  324. while (WriteCount && NT_SUCCESS(Status)) {
  325. //
  326. // Write an entry into the log
  327. //
  328. Data[0] = Count; // Make data "unique"
  329. EventID = (EventID + Count) % 100; // Vary the eventids
  330. Status = WriteLogEntryMsg ( LogHandle, EventID );
  331. Count--;
  332. WriteCount--;
  333. if (!Status) {
  334. printf ("Error - %d. Remaining count %lu\n", GetLastError(), Count);
  335. } else {
  336. printf ("%d,",WriteCount);
  337. }
  338. }
  339. IStatus = DeregisterEventSource (LogHandle);
  340. printf(" - Deregistered\n");
  341. }
  342. }
  343. return (Status);
  344. }
  345. BOOL
  346. TestClearLogFile ()
  347. {
  348. BOOL Status, IStatus;
  349. HANDLE LogHandle;
  350. LPSTR ModuleName, BackupName;
  351. printf("Testing ClearLogFile API\n");
  352. //
  353. // Initialize the strings
  354. //
  355. ModuleName = "TESTWINAAPP";
  356. //
  357. // Open the log handle
  358. //
  359. printf("Calling OpenEventLog for CLEAR - ");
  360. LogHandle = OpenEventLogA (
  361. pServerName,
  362. ModuleName
  363. );
  364. if (!Status) {
  365. printf("Error - %d\n", GetLastError());
  366. } else {
  367. printf("SUCCESS\n");
  368. //
  369. // Clear the log file and back it up to "view.log"
  370. //
  371. printf("Calling ClearEventLog backing up to view.log ");
  372. BackupName = "view.log";
  373. Status = ClearEventLogA (
  374. LogHandle,
  375. BackupName
  376. );
  377. if (!Status) {
  378. printf ("Error - %d\n", GetLastError());
  379. } else {
  380. printf ("SUCCESS\n");
  381. }
  382. //
  383. // Now just clear the file without backing it up
  384. //
  385. printf("Calling ClearEventLog with no backup ");
  386. Status = ClearEventLogA (
  387. LogHandle,
  388. NULL
  389. );
  390. if (!Status) {
  391. printf ("Error - %d\n", GetLastError());
  392. } else {
  393. printf ("SUCCESS\n");
  394. }
  395. printf("Calling CloseEventLog\n");
  396. IStatus = CloseEventLog (LogHandle);
  397. }
  398. return(Status);
  399. }
  400. BOOL
  401. TestBackupLogFile(
  402. LPSTR FileName
  403. )
  404. {
  405. HANDLE LogHandle;
  406. printf("Testing BackupEventLog API\n");
  407. //
  408. // Open the log handle
  409. //
  410. printf("Calling ElfOpenEventLog for BACKUP - ");
  411. LogHandle = OpenEventLogA (
  412. NULL,
  413. "Application"
  414. );
  415. if (!LogHandle) {
  416. printf("Error - %d\n", GetLastError());
  417. } else {
  418. printf("SUCCESS\n");
  419. //
  420. // Backup the log file
  421. //
  422. printf("Calling BackupEventLogFile backing up to %s\n", FileName);
  423. if (!BackupEventLogA (
  424. LogHandle,
  425. FileName
  426. )) {
  427. printf ("Error - %d\n", GetLastError());
  428. } else {
  429. printf ("SUCCESS\n");
  430. }
  431. printf("Calling CloseEventLog - ");
  432. if (CloseEventLog (LogHandle)) {
  433. printf("Success\n");
  434. }
  435. else {
  436. printf("Failed with code %d\n", GetLastError());
  437. }
  438. }
  439. return(TRUE);
  440. }
  441. /****************************************************************************/
  442. BOOL
  443. main (
  444. IN SHORT argc,
  445. IN PSZ argv[],
  446. )
  447. /*++
  448. *
  449. * Routine Description:
  450. *
  451. *
  452. *
  453. * Arguments:
  454. *
  455. *
  456. *
  457. *
  458. * Return Value:
  459. *
  460. *
  461. *
  462. --*/
  463. /****************************************************************************/
  464. {
  465. DWORD ReadFlags;
  466. Initialize(); // Init any data
  467. //
  468. // Just till I can replace this horrid parm parsing with my own
  469. //
  470. if (getenv("REMOTE")) {
  471. pServerName = "\\\\danhi20";
  472. }
  473. if ( argc < 2 ) {
  474. printf( "Not enough parameters\n" );
  475. return Usage( );
  476. }
  477. if ( stricmp( argv[1], "-c" ) == 0 ) {
  478. if ( argc < 3 ) {
  479. return TestClearLogFile();
  480. }
  481. } else if (stricmp ( argv[1], "-b" ) == 0 ) {
  482. if ( argc < 3 ) {
  483. return Usage();
  484. } else {
  485. return TestBackupLogFile(argv[2]);
  486. }
  487. } else if (stricmp ( argv[1], "-rsf" ) == 0 ) {
  488. ReadFlags = EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ;
  489. if ( argc < 3 ) {
  490. return TestReadEventLog(1,ReadFlags,0 );
  491. } else {
  492. return Usage();
  493. }
  494. } else if (stricmp ( argv[1], "-xsf" ) == 0 ) {
  495. ReadFlags = EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ;
  496. bHackTestBackup = TRUE;
  497. if ( argc < 3 ) {
  498. return TestReadEventLog(1,ReadFlags,0 );
  499. } else {
  500. return Usage();
  501. }
  502. } else if (stricmp ( argv[1], "-rsb" ) == 0 ) {
  503. ReadFlags = EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ;
  504. if ( argc < 3 ) {
  505. return TestReadEventLog(1,ReadFlags,0 );
  506. } else {
  507. return Usage();
  508. }
  509. } else if (stricmp ( argv[1], "-rrf" ) == 0 ) {
  510. ReadFlags = EVENTLOG_SEEK_READ | EVENTLOG_FORWARDS_READ;
  511. if ( argc < 3 ) {
  512. return TestReadEventLog(1,ReadFlags ,1);
  513. } else if (argc == 3) {
  514. return (TestReadEventLog (1, ReadFlags, atoi(argv[2])));
  515. }
  516. } else if (stricmp ( argv[1], "-rrb" ) == 0 ) {
  517. ReadFlags = EVENTLOG_SEEK_READ | EVENTLOG_BACKWARDS_READ;
  518. if ( argc < 3 ) {
  519. return TestReadEventLog(1,ReadFlags, 1);
  520. } else if (argc == 3) {
  521. return (TestReadEventLog (1, ReadFlags, atoi(argv[2])));
  522. }
  523. } else if (stricmp ( argv[1], "-w" ) == 0 ) {
  524. if ( argc < 3 ) {
  525. return TestWriteEventLog(1);
  526. } else if (argc == 3) {
  527. return (TestWriteEventLog (atoi(argv[2])));
  528. }
  529. } else {
  530. return Usage();
  531. }
  532. UNREFERENCED_PARAMETER(argc);
  533. UNREFERENCED_PARAMETER(argv);
  534. }