1 mike 1.2 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
14 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 mike 1.2 //==============================================================================
23 //
24 // Author: Sushma Fernandes (sushma_fernandes@hp.com)
25 //
|
26 kumpf 1.4 // Modified By: Jenny Yu (jenny_yu@hp.com)
|
27 mike 1.2 //
28 //%/////////////////////////////////////////////////////////////////////////////
29
30 #include <fstream>
31 #include <cstring>
32 #include <Pegasus/Common/System.h>
33 #include <Pegasus/Common/Tracer.h>
34
35 PEGASUS_USING_STD;
36 PEGASUS_USING_PEGASUS;
37
38 // If Windows platform then set the EOF_CHAR to 2
39 #if defined(PEGASUS_OS_TYPE_WINDOWS)
40 #define EOF_CHAR 2
41 #else
42 #define EOF_CHAR 1
43 #endif
44
45 // Trace files for test purposes
46 // Will be created in the current directory
47
48 mike 1.2 const char* FILE1 = "testtracer1.trace";
49 const char* FILE2 = "testtracer2.trace";
50 const char* FILE3 = "testtracer1.trace";
|
51 kumpf 1.4 const char* FILE4 = "testtracer4.trace";
|
52 mike 1.2
53 //
54 // Reads the last trace message from a given trace file and compares the
55 // given string with the string read from file
56 //
57 // return 0 if the strings match
58 // return 1 if the strings do not match
59 //
60 Uint32 compare(const char* fileName, const char* compareStr)
61 {
62 Uint32 count=0;
63 Uint32 retCode=0;
64 fstream file;
65 Uint32 size=strlen(compareStr);
66 char* readStr= new char[size+EOF_CHAR+1];
67
68 file.open(fileName,fstream::in);
69 if (!file.good())
70 {
71 delete []readStr;
72 return 1;
73 mike 1.2 }
|
74 kumpf 1.5 file.seekg((Sint32) -(size+EOF_CHAR),fstream::end);
|
75 mike 1.2 file.read(readStr,size+EOF_CHAR);
76 readStr[size]='\0';
77 retCode=strcmp(compareStr,readStr);
78 delete []readStr;
79 file.close();
80 return retCode;
81 }
82
83 //
84 // Description:
85 // Trace properties file, level and component are not set
86 // Should not log a trace message
87 //
88 // Type:
89 // Negative
90 //
91 // return 0 if the test passed
92 // return 1 if the test failed
93 //
94 Uint32 test1()
95 {
96 mike 1.2 const char* METHOD_NAME = "test1";
97 PEG_FUNC_ENTER(TRC_CONFIG,METHOD_NAME);
98 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s %d",
99 "This message should not appear value=",123);
100 PEG_FUNC_EXIT(TRC_CONFIG,METHOD_NAME);
101 return(compare(FILE1,""));
102 }
103
104 //
105 // Description:
106 // Trace properties level and component are not set
107 // Should not log a trace message
108 //
109 // Type:
110 // Negative
111 //
112 // return 0 if the test passed
113 // return 1 if the test failed
114 //
115 Uint32 test2()
116 {
117 mike 1.2 const char* METHOD_NAME = "test2";
118 Tracer::setTraceFile(FILE1);
119 PEG_FUNC_ENTER(TRC_CONFIG,METHOD_NAME);
120 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s %d",
121 "This message should not appear value=",123);
122 return(compare(FILE1,"This message should not appear value=123"));
123 }
124
125 //
126 // Description:
127 // Trace properties component is not set
128 // Should not log a trace message
129 //
130 // Type:
131 // Negative
132 //
133 // return 0 if the test passed
134 // return 1 if the test failed
135 //
136 Uint32 test3()
137 {
138 mike 1.2 const char* METHOD_NAME = "test3";
139 Tracer::setTraceLevel(Tracer::LEVEL1);
140 PEG_FUNC_ENTER(TRC_CONFIG,METHOD_NAME);
141 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s",
142 "This message should not appear");
143 return(compare(FILE1,"This message should not appear"));
144 }
145
146 //
147 // Description:
148 // Trace properties file, level and component are set
149 // should log a trace message
150 //
151 // Type:
152 // Positive
153 //
154 // return 0 if the test passed
155 // return 1 if the test failed
156 //
157 Uint32 test4()
158 {
159 mike 1.2 const char* METHOD_NAME = "test4";
160 Tracer::setTraceComponents("Config");
161 PEG_FUNC_ENTER(TRC_CONFIG,METHOD_NAME);
162 return(compare(FILE1,"Entering method test4"));
163 }
164
165 //
166 // Description:
167 // Trace component is set to an invalid component
168 // should not log a trace message
169 //
170 // Type:
171 // Negative
172 //
173 // return 0 if the test passed
174 // return 1 if the test failed
175 //
176 Uint32 test5()
177 {
178 const char* METHOD_NAME = "test5";
179 Tracer::setTraceComponents("Wrong Component Name");
180 mike 1.2 PEG_FUNC_EXIT(TRC_CONFIG,METHOD_NAME);
181 return(compare(FILE1,"Entering method test4"));
182 }
183
184 //
185 // Description:
186 // Trace level is set to LEVEL 2 and logs a LEVEL 4 message
187 // should not log a trace message
188 //
189 // Type:
190 // Negative
191 //
192 // return 0 if the test passed
193 // return 1 if the test failed
194 //
195
196 Uint32 test6()
197 {
198 const char* METHOD_NAME = "test6";
199 Tracer::setTraceComponents("Config");
200 Tracer::setTraceLevel(Tracer::LEVEL2);
201 mike 1.2 Tracer::trace(TRC_CONFIG,Tracer::LEVEL2,"%s %s",
202 "Test Message for Level2 in",METHOD_NAME);
203 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s %s",
204 "Test Message for Level2 in",METHOD_NAME);
205 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL4,"%s",
206 "This Message should not appear");
207 return(compare(FILE1,"Test Message for Level2 in test6"));
208 }
209
210 //
211 // Description:
212 // Trace level is set to an invalid level
213 // should not log a trace message
214 //
215 // Type:
216 // Negative
217 //
218 // return 0 if the test passed
219 // return 1 if the test failed
220 //
221 Uint32 test7()
222 mike 1.2 {
223 const char* METHOD_NAME = "test7";
224 Tracer::setTraceLevel(100);
225 PEG_FUNC_EXIT(TRC_CONFIG,METHOD_NAME);
226 return(compare(FILE1,"Test Message for Level2 in test6"));
227 }
228
229 //
230 // Description:
231 // Trace level is set to LEVEL1 for a non entry/exit message
232 // should not log a trace message, should log an error
233 //
234 // Type:
235 // Negative
236 //
237 // return 0 if the test passed
238 // return 1 if the test failed
239 //
240 Uint32 test8()
241 {
242 const char* METHOD_NAME = "test8";
243 mike 1.2 Tracer::setTraceLevel(Tracer::LEVEL1);
244 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL1,"%s",
245 "Test Message for Level4");
246 return(compare(FILE1,"Test Message for Level2 in test6"));
247 }
248
249 //
250 // Description:
251 // Changes the trace file to FILE2
252 //
253 // Type:
254 // Positive
255 //
256 // return 0 if the test passed
257 // return 1 if the test failed
258 //
259 Uint32 test9()
260 {
261 const char* METHOD_NAME = "test9";
262 Tracer::setTraceLevel(Tracer::LEVEL3);
263 Tracer::setTraceFile(FILE2);
264 mike 1.2
265 PEG_FUNC_ENTER(TRC_CONFIG,METHOD_NAME);
266 Tracer::trace(TRC_CONFIG,Tracer::LEVEL3,"%s %s",
267 "Test Message for Level3 in",METHOD_NAME);
268 return(compare(FILE2,"Test Message for Level3 in test9"));
269 }
270
271 //
272 // Description:
273 // Passes invalid component in the trace call
274 // should not log a trace message
275 //
276 // Type:
277 // Negative
278 //
279 // return 0 if the test passed
280 // return 1 if the test failed
281 //
282
283 Uint32 test10()
284 {
285 mike 1.2 const char* METHOD_NAME = "test10";
286 Tracer::setTraceComponents("ALL");
|
287 kumpf 1.6 PEG_FUNC_EXIT(Uint32(-1),METHOD_NAME);
|
288 mike 1.2 return(compare(FILE2,"Test Message for Level3 in test9"));
289 }
290
291 //
292 // Description:
293 // Implements trace call for Tracer::Level1
294 // should log a trace message
295 //
296 // Type:
297 // Positive
298 //
299 // return 0 if the test passed
300 // return 1 if the test failed
301 //
302
303 Uint32 test11()
304 {
305 const char* METHOD_NAME = "test11";
306 Tracer::setTraceComponents("ALL");
307 Tracer::setTraceLevel(Tracer::LEVEL4);
308 PEG_FUNC_ENTER(TRC_CONFIG,METHOD_NAME);
309 mike 1.2 return(compare(FILE2,"Entering method test11"));
310 }
311
312 //
313 // Description:
314 // Implements trace call for Tracer::Level1
315 // should log a trace message
316 //
317 // Type:
318 // Positive
319 //
320 // return 0 if the test passed
321 // return 1 if the test failed
322 //
323
324 Uint32 test12()
325 {
326 const char* METHOD_NAME = "test12";
327 Tracer::setTraceComponents("ALL");
328 Tracer::setTraceLevel(Tracer::LEVEL4);
329 PEG_FUNC_EXIT(TRC_CONFIG,METHOD_NAME);
330 mike 1.2 return(compare(FILE2,"Exiting method test12"));
331 }
332
333 //
334 // Description:
335 // Implements trace call for Tracer::Level2
336 // should log a trace message
337 //
338 // Type:
339 // Positive
340 //
341 // return 0 if the test passed
342 // return 1 if the test failed
343 //
344
345 Uint32 test13()
346 {
347 const char* METHOD_NAME = "test13";
348 Tracer::setTraceComponents("ALL");
349 Tracer::setTraceLevel(Tracer::LEVEL4);
350 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL2,"%s %s",
351 mike 1.2 "Test Message for Level2 in",METHOD_NAME);
352 return(compare(FILE2,"Test Message for Level2 in test13"));
353 }
354
355 //
356 // Description:
357 // Implements trace call for Tracer::Level3
358 // should log a trace message
359 //
360 // Type:
361 // Positive
362 //
363 // return 0 if the test passed
364 // return 1 if the test failed
365 //
366
367 Uint32 test14()
368 {
369 const char* METHOD_NAME = "test14";
370 Tracer::setTraceComponents("ALL");
371 Tracer::setTraceLevel(Tracer::LEVEL4);
372 mike 1.2 Tracer::setTraceFile(FILE3);
373 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL3,"%s %s",
374 "Test Message for Level3 in",METHOD_NAME);
375 return(compare(FILE3,"Test Message for Level3 in test14"));
376 }
377
378 //
379 // Description:
380 // Implements trace call for Tracer::Level4
381 // should log a trace message
382 //
383 // Type:
384 // Positive
385 //
386 // return 0 if the test passed
387 // return 1 if the test failed
388 //
389
390 Uint32 test15()
391 {
392 const char* METHOD_NAME = "test15";
393 mike 1.2 Tracer::setTraceComponents("ALL");
394 Tracer::setTraceLevel(Tracer::LEVEL4);
395 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL4,"%s %s",
396 "Test Message for Level4 in",METHOD_NAME);
397 return(compare(FILE3,"Test Message for Level4 in test15"));
398 }
399
400 //
401 // Description:
402 // calls the setTraceComponents with null string
403 // should log a trace message
404 //
405 // Type:
406 // Negative
407 //
408 // return 0 if the test passed
409 // return 1 if the test failed
410 //
411
412 Uint32 test16()
413 {
414 mike 1.2 const char* METHOD_NAME = "test16";
415 Tracer::setTraceComponents("");
416 Tracer::setTraceLevel(Tracer::LEVEL4);
417 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL4,"%s %s",
418 "This Message should not appear in",METHOD_NAME);
419 return(compare(FILE3,"Test Message for Level4 in test15"));
420 }
421
422 //
423 // Description:
424 // calls the setTraceComponents with one valid and another invalid component
425 // should log a trace message
426 //
427 // Type:
428 // Negative
429 //
430 // return 0 if the test passed
431 // return 1 if the test failed
432 //
433
434 Uint32 test17()
435 mike 1.2 {
436 const char* METHOD_NAME = "test17";
|
437 kumpf 1.3 Tracer::setTraceComponents("InvalidComp");
|
438 mike 1.2 Tracer::setTraceLevel(Tracer::LEVEL4);
439 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL4,"%s %s",
440 "This Message should not appear in",METHOD_NAME);
441 return(compare(FILE3,"Test Message for Level4 in test15"));
442 }
|
443 kumpf 1.3 //
444 // Description:
445 // calls the _traceBuffer call
446 // should log a trace message
447 //
448 // Type:
449 // Positive
450 //
451 // return 0 if the test passed
452 // return 1 if the test failed
453 //
454
455 Uint32 test18()
456 {
457 const char* METHOD_NAME = "test18";
458 Tracer::setTraceComponents("Config,InvalidComp");
459 Tracer::setTraceLevel(Tracer::LEVEL4);
460 Tracer::traceBuffer(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL4,
461 "This Message should appear in",4);
462 Tracer::traceBuffer(TRC_CONFIG,Tracer::LEVEL4,
463 "This Message should appear in",4);
464 kumpf 1.3 return(compare(FILE3,"This"));
465 }
466
467 //
468 // Description:
469 // calls the isValid call
470 // should not log a trace message
471 //
472 // Type:
473 // Positive
474 //
475 // return 0 if the test passed
476 // return 1 if the test failed
477 //
478
479 Uint32 test19()
480 {
|
481 kumpf 1.4 const char* METHOD_NAME = "test18";
482 Tracer::setTraceComponents("Config,InvalidComp");
|
483 kumpf 1.3 Tracer::setTraceLevel(Tracer::LEVEL4);
|
484 kumpf 1.4 Tracer::traceBuffer(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL4,
485 "This Message should appear in",4);
486 Tracer::traceBuffer(TRC_CONFIG,Tracer::LEVEL4,
487 "This Message should appear in",4);
|
488 kumpf 1.3 return(compare(FILE3,"This"));
489 }
|
490 mike 1.2
|
491 kumpf 1.4 //
492 // Description:
493 // Trace a string.
494 // Calls the _traceString() method
495 // should log a trace message
496 //
497 // Type:
498 // Positive
499 //
500 // return 0 if the test passed
501 // return 1 if the test failed
502 //
503
504 Uint32 test20()
505 {
506 const char* METHOD_NAME = "test20";
507 Tracer::setTraceFile(FILE4);
508 Tracer::setTraceComponents("ALL");
509 Tracer::setTraceLevel(Tracer::LEVEL4);
510
511 PEG_METHOD_ENTER(TRC_CONFIG, METHOD_NAME);
512 kumpf 1.4 Tracer::trace(__FILE__,__LINE__,TRC_CONFIG,Tracer::LEVEL4,
513 "Test Message for Level4 in test20");
514 return(compare(FILE4,"Test Message for Level4 in test20"));
515 }
516
517 //
518 // Description:
519 // Trace a CIMException.
520 // Calls the traceCIMException() method
521 // should log a trace message
522 //
523 // Type:
524 // Positive
525 //
526 // return 0 if the test passed
527 // return 1 if the test failed
528 //
529
530 Uint32 test21()
531 {
532 const char* METHOD_NAME = "test21";
533 kumpf 1.4 Tracer::setTraceFile(FILE4);
534 Tracer::setTraceComponents("ALL");
535 Tracer::setTraceLevel(Tracer::LEVEL4);
536
537 PEG_METHOD_ENTER(TRC_CONFIG, METHOD_NAME);
538
539 // test tracing CIMException
540 try
541 {
542 throw PEGASUS_CIM_EXCEPTION(
543 CIM_ERR_NOT_SUPPORTED,
544 "CIM Exception Message for Level4 in test21.");
545 }
546 catch (CIMException e)
547 {
548 Tracer::traceCIMException(TRC_CONFIG,Tracer::LEVEL4, e);
549 }
550
551 return 0;
552 }
553
554 kumpf 1.4 //
555 // Description:
556 // Trace a string using macro.
557 // should log a trace message
558 //
559 // Type:
560 // Positive
561 //
562 // return 0 if the test passed
563 // return 1 if the test failed
564 //
565 Uint32 test22()
566 {
567 const char* METHOD_NAME = "test22";
568 Tracer::setTraceFile(FILE4);
569 Tracer::setTraceComponents("ALL");
570 Tracer::setTraceLevel(Tracer::LEVEL4);
571
572 PEG_METHOD_ENTER(TRC_CONFIG, METHOD_NAME);
573
574 PEG_TRACE_STRING(TRC_CONFIG,Tracer::LEVEL4,"Test message for Level4 in test22.");
575 kumpf 1.4
576 return(compare(FILE4,"Test message for Level4 in test22."));
577 }
578
|
579 mike 1.2 int main()
580 {
581
582 // Execute the tests only if trace calls are included
583
584 #ifdef PEGASUS_REMOVE_TRACE
585 cout << "+++++ passed all tests" << endl;
586 return 0;
587 #else
588 System::removeFile(FILE1);
589 System::removeFile(FILE2);
590 System::removeFile(FILE3);
|
591 kumpf 1.4 System::removeFile(FILE4);
|
592 mike 1.2 if (test1() == 0)
593 {
594 cout << "Tracer test (test1) failed" << endl;
595 exit(1);
596 }
597 if (test2() == 0)
598 {
599 cout << "Tracer test (test2) failed" << endl;
600 exit(1);
601 }
602 if (test3() == 0)
603 {
604 cout << "Tracer test (test3) failed" << endl;
605 exit(1);
606 }
607 if (test4() != 0)
608 {
609 cout << "Tracer test (test4) failed" << endl;
610 exit(1);
611 }
612 if (test5() != 0)
613 mike 1.2 {
614 cout << "Tracer test (test5) failed" << endl;
615 exit(1);
616 }
617 if (test6() != 0)
618 {
619 cout << "Tracer test (test6) failed" << endl;
620 exit(1);
621 }
622 if (test7() != 0)
623 {
624 cout << "Tracer test (test7) failed" << endl;
625 exit(1);
626 }
627 if (test8() != 0)
628 {
629 cout << "Tracer test (test8) failed" << endl;
630 exit(1);
631 }
632 if (test9() != 0)
633 {
634 mike 1.2 cout << "Tracer test (test9) failed" << endl;
635 exit(1);
636 }
637 if (test10() != 0)
638 {
639 cout << "Tracer test (test10) failed" << endl;
640 exit(1);
641 }
642 if (test11() != 0)
643 {
644 cout << "Tracer test (test11) failed" << endl;
645 exit(1);
646 }
647 if (test12() != 0)
648 {
649 cout << "Tracer test (test12) failed" << endl;
650 exit(1);
651 }
652 if (test13() != 0)
653 {
654 cout << "Tracer test (test13) failed" << endl;
655 mike 1.2 exit(1);
656 }
657 if (test14() != 0)
658 {
659 cout << "Tracer test (test14) failed" << endl;
660 exit(1);
661 }
662 if (test15() != 0)
663 {
664 cout << "Tracer test (test15) failed" << endl;
665 exit(1);
666 }
667 if (test16() != 0)
668 {
669 cout << "Tracer test (test16) failed" << endl;
|
670 kumpf 1.3 exit(1);
671 }
672 if (test17() != 0)
673 {
674 cout << "Tracer test (test17) failed" << endl;
675 exit(1);
676 }
677 if (test18() != 0)
678 {
679 cout << "Tracer test (test18) failed" << endl;
680 exit(1);
681 }
682 if (test19() != 0)
683 {
684 cout << "Tracer test (test19) failed" << endl;
|
685 mike 1.2 exit(1);
686 }
|
687 kumpf 1.4 if (test20() != 0)
688 {
689 cout << "Tracer test (test20) failed" << endl;
690 exit(1);
691 }
692 if (test21() != 0)
693 {
694 cout << "Tracer test (test21) failed" << endl;
695 exit(1);
696 }
697 if (test22() != 0)
698 {
699 cout << "Tracer test (test22) failed" << endl;
700 exit(1);
701 }
|
702 mike 1.2 cout << "+++++ passed all tests" << endl;
703 System::removeFile(FILE1);
704 System::removeFile(FILE2);
705 System::removeFile(FILE3);
|
706 kumpf 1.4 System::removeFile(FILE4);
|
707 mike 1.2 return 0;
708 #endif
709 }
710
|