2 //=============================================================================
4 * @file Config_Test.cpp
6 * This is a test that makes sure various classes in
7 * <ACE_Configuration> work correctly.
9 * @author Michael Searles <msearles@base16.com>
10 * @author Chris Hafey <chafey@stentor.com>
11 * @author and Jerry D. Odenwelder Jr. <jerry.o@mindspring.com>
13 //=============================================================================
16 #include "test_config.h"
17 #include "Config_Test.h"
18 #include "ace/Configuration_Import_Export.h"
19 #include "ace/OS_NS_ctype.h"
20 #include "ace/OS_NS_errno.h"
21 #include "ace/OS_NS_stdio.h"
22 #include "ace/OS_NS_string.h"
23 #include "ace/OS_NS_unistd.h"
28 test (ACE_Configuration
*config
,
29 ACE_Configuration_Section_Key
&testsection
)
34 if (config
->set_string_value (testsection
,
36 ACE_TEXT ("stvaluetest")))
39 else if (config
->remove_value (testsection
,
40 ACE_TEXT ("stvalue")))
42 // Make sure it's really gone
43 else if (0 == config
->get_string_value (testsection
,
46 ACE_ERROR_RETURN ((LM_ERROR
,
47 ACE_TEXT ("test:remove_value didn't remove\n")),
50 else if (config
->set_string_value (testsection
,
52 ACE_TEXT ("stvaluetest")))
54 else if (config
->set_string_value (testsection
,
56 ACE_TEXT ("second stvaluetest")))
57 ACE_ERROR_RETURN ((LM_ERROR
,
58 ACE_TEXT ("test:set_string_value twice failed\n")),
61 else if (config
->set_integer_value (testsection
,
62 ACE_TEXT ("intvalue"),
65 // Reset to the value we test for below
66 else if (config
->set_integer_value (testsection
,
67 ACE_TEXT ("intvalue"),
71 static size_t const data_len
= 80;
72 u_char data
[data_len
];
74 for (size_t i
= 0; i
< data_len
; ++i
)
75 data
[i
] = static_cast<u_char
> (i
+ 128);
77 if (config
->set_binary_value (testsection
,
78 ACE_TEXT ("binvalue"),
83 // Get the values and compare
84 if (config
->get_string_value (testsection
,
88 else if (stvalue
!= ACE_TEXT ("second stvaluetest"))
93 if (config
->get_integer_value (testsection
,
94 ACE_TEXT ("intvalue"),
97 else if (intvalue
!= 42)
100 u_char
*data_out (0);
103 void *data_tmp
= 0; // Workaround for GCC strict aliasing warning.
106 if (config
->get_binary_value (testsection
,
107 ACE_TEXT ("binvalue"),
112 data_out
= reinterpret_cast <u_char
*> (data_tmp
);
115 u_char
* the_data
= static_cast<u_char
*> (data_out
);
118 for (size_t j
= 0; j
< data_len
; ++j
)
119 if (the_data
[j
] != data
[j
])
126 ACE_Configuration::VALUETYPE type
;
128 int found
[3] = { 0, 0, 0 }; // One for each expected value
130 while (!config
->enumerate_values (testsection
,
135 if (name
== ACE_TEXT ("stvalue"))
137 if (type
!= ACE_Configuration::STRING
)
143 else if (name
== ACE_TEXT ("intvalue"))
145 if (type
!= ACE_Configuration::INTEGER
)
151 else if (name
== ACE_TEXT ("binvalue"))
153 if (type
!= ACE_Configuration::BINARY
)
162 // Make sure we got three values.
163 if (index
!= 3 || !found
[0] || !found
[1] || !found
[2])
167 // Add some subsections. This part is a separate scope to be sure
168 // the test2, test3, test4 keys are closed before further
169 // manipulating/deleting the sections further down in the test.
170 ACE_Configuration_Section_Key test2
;
171 ACE_Configuration_Section_Key test3
;
172 ACE_Configuration_Section_Key test4
;
174 if (config
->open_section (testsection
,
179 else if (config
->open_section (testsection
,
184 else if (config
->open_section (testsection
,
191 // Test enumerate sections.
193 found
[0] = found
[1] = found
[2] = 0;
194 while (!config
->enumerate_sections (testsection
,
198 if (name
== ACE_TEXT ("test2"))
201 ACE_ERROR_RETURN ((LM_ERROR
,
202 ACE_TEXT ("enumerate_sections, dupl test2\n")),
206 else if (name
== ACE_TEXT ("test3"))
209 ACE_ERROR_RETURN ((LM_ERROR
,
210 ACE_TEXT ("enumerate_sections, dupl test3\n")),
214 else if (name
== ACE_TEXT ("test4"))
217 ACE_ERROR_RETURN ((LM_ERROR
,
218 ACE_TEXT ("enumerate_sections, dupl test4\n")),
225 if (index
!= 3 || !found
[0] || !found
[1] || !found
[2])
228 // Remove a subsection
229 if (config
->remove_section (testsection
,
232 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p (%d)\n"),
233 ACE_TEXT ("remove_section test2"),
234 ACE_OS::last_error ()),
237 // Try to remove it again
238 if (!config
->remove_section (testsection
,
247 test (ACE_Configuration
*config
)
249 const ACE_Configuration_Section_Key
& root
=
250 config
->root_section ();
253 // Scope this so the testsection key is closed before trying to
254 // remove the "test" section.
255 ACE_Configuration_Section_Key testsection
;
257 if (config
->open_section (root
,
263 int ret_val
= test (config
, testsection
);
268 // Try to remove the testsection root, it should fail since it still
270 if (!config
->remove_section (root
,
276 // Test find section, and be sure the key is closed before testing the
278 ACE_Configuration_Section_Key result
;
280 if (config
->open_section (root
,
287 // Now test the recursive remove.
288 if (config
->remove_section (root
,
293 // Make sure its not there
294 ACE_Configuration_Section_Key testsectiongone
;
295 if (!config
->open_section (root
,
305 test_subkey_path (ACE_Configuration
* config
)
307 ACE_Configuration_Section_Key root
=
308 config
->root_section ();
310 ACE_Configuration_Section_Key testsection
;
312 if (config
->open_section (root
,
313 ACE_TEXT ("Software\\ACE\\test"),
318 int ret_val
= test (config
, testsection
);
322 if (config
->open_section (root
,
323 ACE_TEXT ("Software"),
328 if (config
->remove_section (testsection
,
342 // Test import of a legit INI format from a previously-existing file.
343 ACE_Configuration_Heap cf
;
344 if ((status
= cf
.open ()) != 0)
345 ACE_ERROR ((LM_ERROR
,
346 ACE_TEXT ("ACE_Configuration_Heap::open returned %d\n"),
348 ACE_Ini_ImpExp
import (cf
);
349 // This one should work...
350 ACE_TCHAR import_file_name
[MAXPATHLEN
];
351 #if defined (TEST_DIR)
352 ACE_OS::strcpy (import_file_name
, TEST_DIR
);
353 ACE_OS::strcat (import_file_name
, ACE_DIRECTORY_SEPARATOR_STR
);
354 ACE_OS::strcat (import_file_name
, ACE_TEXT ("Config_Test_Import_1.ini"));
356 ACE_OS::strcpy (import_file_name
, ACE_TEXT ("Config_Test_Import_1.ini"));
359 status
= import
.import_config (import_file_name
);
361 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p: %s\n"),
362 ACE_TEXT ("Config_Test_Import_1.ini failed"),
366 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%s imported\n"), import_file_name
));
368 // Imported clean; verify content. See ini file for expected content.
369 // Verify the expected sections are there, but no others. Verify the
370 // expected keys are there, but no others.
371 int section1_seen
= 0, section2_seen
= 0;
372 int somekey_seen
= 0, someotherkey_seen
= 0;
374 ACE_TString sect_name
;
375 const ACE_Configuration_Section_Key
&root
= cf
.root_section ();
377 (status
= cf
.enumerate_sections (root
, index
, sect_name
)) == 0;
379 if (index
> 1) // There are only two sections.
380 ACE_ERROR ((LM_ERROR
,
381 ACE_TEXT ("Enumerated %d sections; expected 2\n"),
384 ACE_DEBUG ((LM_DEBUG
,
385 ACE_TEXT ("Enumerated to section %s\n"),
386 sect_name
.c_str ()));
387 if (sect_name
== ACE_TEXT ("SectionOne")) {
389 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Saw %s multiple times!\n"),
390 sect_name
.c_str ()));
392 // Check for values in this section.
393 ACE_Configuration_Section_Key sect1
;
394 if (cf
.open_section (root
, sect_name
.c_str (), 0, sect1
) != 0)
395 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Failed to open section: %s\n"),
396 sect_name
.c_str ()));
398 int val_index
= 0, val_status
;
399 ACE_TString val_name
, value
;
400 ACE_Configuration::VALUETYPE val_type
;
403 (sect1
, val_index
, val_name
, val_type
)) == 0) {
404 ACE_DEBUG ((LM_DEBUG
,
405 ACE_TEXT ("Enumerated %s, type %d\n"),
408 if (val_type
!= ACE_Configuration::STRING
)
409 ACE_ERROR ((LM_ERROR
,
410 ACE_TEXT ("Expected %s to be STRING, but %d\n"),
413 if (val_name
== ACE_TEXT ("SomeKey")) {
415 ACE_ERROR ((LM_ERROR
,
416 ACE_TEXT ("Saw %s more than once\n"),
421 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Unexpected key %s\n"),
423 if ((val_status
= cf
.get_string_value
424 (sect1
, val_name
.c_str (), value
)) != 0)
425 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Can't get value of %s\n"),
428 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%s value: %s\n"),
429 val_name
.c_str (), value
.c_str ()));
430 if (value
!= ACE_TEXT ("SomeValue")) {
431 ACE_ERROR ((LM_ERROR
,
432 ACE_TEXT ("SomeKey: %s; expected SomeValue\n")));
437 if (val_status
== 1) {
438 if (val_index
!= 1) // Should have only seen 1
439 ACE_ERROR ((LM_ERROR
,
440 ACE_TEXT ("Expected 1 value; saw %d\n"),
444 ACE_ERROR ((LM_ERROR
,
445 ACE_TEXT ("Error enumerating %s; status %d\n"),
446 sect_name
.c_str (), val_status
));
449 else if (sect_name
== ACE_TEXT ("SectionTwo")) {
451 ACE_ERROR ((LM_ERROR
,
452 ACE_TEXT ("Saw %s multiple times!\n"),
453 sect_name
.c_str ()));
455 // Check for values in this section.
456 ACE_Configuration_Section_Key sect2
;
457 if (cf
.open_section (root
, sect_name
.c_str (), 0, sect2
) != 0)
458 ACE_ERROR ((LM_ERROR
,
459 ACE_TEXT ("Failed to open section: %s\n"),
460 sect_name
.c_str ()));
462 int val_index
= 0, val_status
;
463 ACE_TString val_name
, value
;
464 ACE_Configuration::VALUETYPE val_type
;
465 while ((val_status
= cf
.enumerate_values
466 (sect2
, val_index
, val_name
, val_type
)) == 0) {
467 ACE_DEBUG ((LM_DEBUG
,
468 ACE_TEXT ("Enumerated %s, type %d\n"),
471 if (val_type
!= ACE_Configuration::STRING
)
472 ACE_ERROR ((LM_ERROR
,
473 ACE_TEXT ("Expected %s to be STRING, but %d\n"),
474 val_name
.c_str (), val_type
));
475 if (val_name
== ACE_TEXT ("SomeOtherKey")) {
476 if (someotherkey_seen
)
477 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Saw %s more than once\n"),
479 someotherkey_seen
= 1;
482 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Unexpected key %s\n"),
484 if ((val_status
= cf
.get_string_value
485 (sect2
, val_name
.c_str (), value
)) != 0)
486 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Can't get value of %s\n"),
489 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%s value: %s\n"),
490 val_name
.c_str (), value
.c_str ()));
491 if (value
!= ACE_TEXT ("SomeOtherValue")) {
492 ACE_ERROR ((LM_ERROR
,
493 ACE_TEXT ("SomeOtherKey: %s; expected SomeOtherValue\n")));
498 if (val_status
== 1) {
499 if (val_index
!= 1) // Should have only seen 1
500 ACE_ERROR ((LM_ERROR
,
501 ACE_TEXT ("Expected 1 value; saw %d\n"),
505 ACE_ERROR ((LM_ERROR
,
506 ACE_TEXT ("Error enumerating %s; status %d\n"),
507 sect_name
.c_str (), val_status
));
511 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Saw unexpected section: %s\n"),
512 sect_name
.c_str ()));
516 if (status
== 1) { // Ran out of sections
518 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Saw %d sections; expected 2\n"),
522 ACE_ERROR ((LM_ERROR
,
523 ACE_TEXT ("Error enumerating sections; status %d\n"),
528 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
530 ACE_Configuration_Win32Registry
RegConfig (HKEY_LOCAL_MACHINE
);
531 int result
= test_subkey_path (&RegConfig
);
533 ACE_ERROR_RETURN ((LM_ERROR
,
534 ACE_TEXT ("Win32Registry test HKEY_LOCAL_MACHINE")
535 ACE_TEXT (" failed (%d)\n"), result
),
538 // test win32 registry implementation.
540 ACE_Configuration_Win32Registry::resolve_key (HKEY_LOCAL_MACHINE
,
541 ACE_TEXT ("Software\\ACE\\test"));
543 ACE_ERROR_RETURN ((LM_ERROR
,
544 ACE_TEXT ("resolve_key is broken\n")), -2);
546 // test resolving of forward slashes
548 ACE_Configuration_Win32Registry::resolve_key (HKEY_LOCAL_MACHINE
,
549 ACE_TEXT ("Software/ACE/test"), 0);
551 ACE_ERROR_RETURN ((LM_ERROR
,
552 ACE_TEXT ("resolve_key resolving slashes is broken\n")),
555 ACE_Configuration_Win32Registry
RegConfig (root
);
557 int const result
= test (&RegConfig
);
559 ACE_ERROR_RETURN ((LM_ERROR
,
560 ACE_TEXT ("Win32 registry test root failed (%d)\n"),
565 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_REGISTRY */
568 ACE_Configuration_Heap heap_config
;
570 if (heap_config
.open () != 0)
571 ACE_ERROR_RETURN ((LM_ERROR
,
572 ACE_TEXT ("Cannot open %p\n"),
573 ACE_TEXT ("local-heap config")),
575 if (heap_config
.open () == 0)
577 ACE_ERROR_RETURN ((LM_ERROR
,
578 ACE_TEXT ("Re-open heap allowed; bugzilla 3724\n")),
581 else if (errno
!= EBUSY
)
583 ACE_ERROR_RETURN ((LM_ERROR
,
584 ACE_TEXT ("Re-open heap expected EBUSY (%d), ")
585 ACE_TEXT ("got %d: bugzilla 3724\n"),
586 EBUSY
, ACE_ERRNO_GET
),
590 int result
= test_subkey_path (&heap_config
);
592 ACE_ERROR_RETURN ((LM_ERROR
,
593 ACE_TEXT ("Heap Config subkey test failed (%d)\n"),
599 int result
= test (&heap_config
);
601 ACE_ERROR_RETURN ((LM_ERROR
,
602 ACE_TEXT ("Heap Configuration test failed (%d)\n"),
607 #if !defined (ACE_LACKS_MMAP)
608 // Test persistent heap version
609 ACE_OS::unlink (ACE_TEXT ("test.reg"));
610 ACE_Configuration_Heap pers_config
;
612 if (pers_config
.open (ACE_TEXT ("test.reg")))
613 ACE_ERROR_RETURN ((LM_ERROR
,
614 ACE_TEXT ("Cannot open %p\n"),
615 ACE_TEXT ("test.reg")),
617 if (pers_config
.open (ACE_TEXT ("test.reg")) == 0)
619 ACE_ERROR_RETURN ((LM_ERROR
,
620 ACE_TEXT ("Re-open(mmap) allowed; bugzilla 3724\n")),
623 else if (errno
!= EBUSY
)
625 ACE_ERROR_RETURN ((LM_ERROR
,
626 ACE_TEXT ("Re-open(mmap) expected EBUSY (%d), ")
627 ACE_TEXT ("got %d: bugzilla 3724\n"),
628 EBUSY
, ACE_ERRNO_GET
),
631 if (pers_config
.open () == 0)
633 ACE_ERROR_RETURN ((LM_ERROR
,
634 ACE_TEXT ("Re-open(new) allowed; bugzilla 3724\n")),
637 else if (errno
!= EBUSY
)
639 ACE_ERROR_RETURN ((LM_ERROR
,
640 ACE_TEXT ("Re-open(new) expected EBUSY (%d), ")
641 ACE_TEXT ("got %d: bugzilla 3724\n"),
642 EBUSY
, ACE_ERRNO_GET
),
647 int result
= test (&pers_config
);
649 ACE_ERROR_RETURN ((LM_ERROR
,
650 ACE_TEXT ("Persistent Heap Config test failed (%d)\n"),
654 #endif /* !ACE_LACKS_MMAP */
656 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Test passed\n")));
661 build_config_object (ACE_Configuration
& cfg
)
663 ACE_Configuration_Section_Key root
= cfg
.root_section ();
664 ACE_Configuration_Section_Key NetworkSection
;
665 ACE_Configuration_Section_Key LoggerSection
;
666 ACE_Configuration_Section_Key BinarySection
;
668 if (cfg
.open_section (root
,
669 ACE_TEXT ("network"),
674 if (cfg
.set_integer_value (NetworkSection
,
675 ACE_TEXT ("TimeToLive"),
678 else if (cfg
.set_string_value (NetworkSection
,
680 ACE_TString (ACE_TEXT ("FALSE"))))
682 else if (cfg
.set_string_value (NetworkSection
,
683 ACE_TEXT ("DestIPAddress"),
684 ACE_TString (ACE_TEXT ("localhost"))))
686 else if (cfg
.set_integer_value (NetworkSection
,
687 ACE_TEXT ("DestPort"),
690 else if (cfg
.set_integer_value (NetworkSection
,
691 ACE_TEXT ("ReconnectInterval"),
695 if (cfg
.open_section (root
,
701 if (cfg
.set_string_value (LoggerSection
,
702 ACE_TEXT ("Heading"),
703 ACE_TString (ACE_TEXT ("ACE - Adaptive Communication Environment"))))
705 else if (cfg
.set_integer_value (LoggerSection
,
706 ACE_TEXT ("SeekIndex"),
709 else if (cfg
.set_integer_value (LoggerSection
,
710 ACE_TEXT ("TraceLevel"),
713 else if (cfg
.set_string_value (LoggerSection
,
714 ACE_TEXT ("Justification"),
715 ACE_TString (ACE_TEXT ("left_justified"))))
717 else if (cfg
.set_string_value (LoggerSection
,
718 ACE_TEXT ("LogFilePath"),
719 ACE_TString (ACE_TEXT ("log/"))))
721 else if (cfg
.set_string_value (LoggerSection
,
722 ACE_TEXT ("TransactionFilePath"),
723 ACE_TString (ACE_TEXT ("data/"))))
726 if (cfg
.open_section (root
,
734 for (int i
= 0; i
< 80; i
++)
737 if (cfg
.set_binary_value (BinarySection
,
743 ACE_TString
string((ACE_TCHAR
*) 0);// = '0';
744 // Try to set the unnamed, default value.
745 if (cfg
.set_string_value (LoggerSection
,
746 0,//string.c_str (),//0, //ACE_TEXT ("x"),
747 ACE_TString (ACE_TEXT ("some string"))))
748 ACE_ERROR_RETURN ((LM_ERROR
,
749 ACE_TEXT ("could not set value with null name\n")),
752 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("here\n")));
754 //ACE_TString string;
755 ACE_TString
name ((ACE_TCHAR
*)0);
756 if (cfg
.get_string_value (LoggerSection
,
757 name
.c_str (), //0, //ACE_TEXT ("x"),
759 ACE_ERROR_RETURN ((LM_ERROR
,
760 ACE_TEXT ("error using ACE_TString::c_str() == %d, len == %d\n"),
761 name
.c_str(), name
.length ()),
764 ACE_DEBUG ((LM_DEBUG
,
765 ACE_TEXT ("the value for the unnamed var=(%s)\n"),
772 * Test ACE_Configuration::operator==
775 Config_Test::testEquality ()
777 // create and open 2 ACE_Configuration objects.
778 ACE_Configuration_Heap heap1
;
779 ACE_Configuration_Heap heap2
;
780 if ((heap1
.open ()) != 0)
782 ACE_ERROR_RETURN ((LM_ERROR
,
783 ACE_TEXT ("Cannot open heap1\n")),
787 else if ((heap2
.open ()) != 0)
789 ACE_ERROR_RETURN ((LM_ERROR
,
790 ACE_TEXT ("Cannot open heap2\n")),
794 // populate them equally
795 if (build_config_object (heap1
) != 0 ||
796 build_config_object (heap2
) != 0)
797 ACE_ERROR_RETURN ((LM_ERROR
,
798 ACE_TEXT ("could not build config object\n")),
802 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should equal...\n")));
805 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they do ;-)\n")));
809 ACE_ERROR_RETURN ((LM_ERROR
,
810 ACE_TEXT ("And they do not :-(\n")
811 ACE_TEXT ("operator== Failed when objects equal\n")),
815 // add a section and value to heap1
816 ACE_Configuration_Section_Key root1
= heap1
.root_section ();
817 ACE_Configuration_Section_Key NewSection
;
818 if (heap1
.open_section (root1
,
819 ACE_TEXT ("NewSection"),
822 ACE_ERROR_RETURN ((LM_ERROR
,
823 ACE_TEXT ("Error adding section to heap1\n")),
825 else if (heap1
.set_integer_value (NewSection
,
826 ACE_TEXT ("TestIntValue"),
828 ACE_ERROR_RETURN ((LM_ERROR
,
829 ACE_TEXT ("Error adding value to heap1\n")),
833 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should NOT equal...\n")));
835 ACE_ERROR_RETURN ((LM_ERROR
,
836 ACE_TEXT ("They Do :-(\noperator== Failed ")
837 ACE_TEXT ("when lhs contains data not in rhs\n")),
840 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they do not ;-)\n")));
843 // add same section to heap2
845 ACE_Configuration_Section_Key root2
= heap2
.root_section ();
846 ACE_Configuration_Section_Key NewSection2
;
847 if (heap2
.open_section (root2
,
848 ACE_TEXT ("NewSection"),
851 ACE_ERROR_RETURN ((LM_ERROR
,
852 ACE_TEXT ("Error adding section to heap2\n")),
854 else if (heap2
.set_integer_value (NewSection2
,
855 ACE_TEXT ("TestIntValue"),
857 ACE_ERROR_RETURN ((LM_ERROR
,
858 ACE_TEXT ("Error adding value to heap2\n")),
860 else if (heap2
.set_integer_value (NewSection2
,
861 ACE_TEXT ("TestIntValue2"),
863 ACE_ERROR_RETURN ((LM_ERROR
,
864 ACE_TEXT ("Error adding second value to heap2\n")),
868 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should NOT equal...\n")));
870 ACE_ERROR_RETURN ((LM_ERROR
,
871 ACE_TEXT ("And They Do :-(\noperator== Failed ")
872 ACE_TEXT ("when rhs contains value not in lhs\n")),
875 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they do not ;-)\n")));
877 // add new value in heap 1
878 if (heap1
.set_integer_value (NewSection
,
879 ACE_TEXT ("TestIntValue2"),
881 ACE_ERROR_RETURN ((LM_ERROR
,
882 ACE_TEXT ("Error adding second value to heap1\n")),
886 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should be equal...\n")));
888 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they are ;-)\n")));
890 ACE_ERROR_RETURN ((LM_ERROR
,
891 ACE_TEXT ("And they are not :-(\n")
892 ACE_TEXT ("operator== Failed\n")),
895 // Add a new section to heap2
896 ACE_Configuration_Section_Key AnotherNewSection2
;
897 if (heap2
.open_section (root2
,
898 ACE_TEXT ("AnotherNewSection"),
901 ACE_ERROR_RETURN ((LM_ERROR
,
902 ACE_TEXT ("Error adding second section to heap2\n")),
904 else if (heap2
.set_integer_value (AnotherNewSection2
,
905 ACE_TEXT ("TestIntValue"),
907 ACE_ERROR_RETURN ((LM_ERROR
,
908 ACE_TEXT ("Error adding value in second section")
909 ACE_TEXT (" to heap2\n")),
913 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should NOT equal...\n")));
915 ACE_ERROR_RETURN ((LM_ERROR
,
916 ACE_TEXT ("And they do :-(\noperator== Failed ")
917 ACE_TEXT ("when rhs contains data not in lhs\n")),
920 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they do not :-)\n")));
922 // add section back to heap1
923 ACE_Configuration_Section_Key AnotherNewSection1
;
924 if (heap1
.open_section (root1
,
925 ACE_TEXT ("AnotherNewSection"),
928 ACE_ERROR_RETURN ((LM_ERROR
,
929 ACE_TEXT ("Error adding second section to heap1\n")),
931 else if (heap1
.set_integer_value (AnotherNewSection1
,
932 ACE_TEXT ("TestIntValue"),
934 ACE_ERROR_RETURN ((LM_ERROR
,
935 ACE_TEXT ("Error adding second value to second ")
936 ACE_TEXT ("section in heap1\n")),
940 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should be equal...\n")));
942 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they are ;-)\n")));
944 ACE_ERROR_RETURN ((LM_ERROR
,
945 ACE_TEXT ("And they are not :-(\n")
946 ACE_TEXT ("operator== Failed\n")),
949 this->equality_tested_
= 1;
954 * Compare INI import data in fromFile to origional data exported (in origional)
956 * This compare is destructive to the origional object.
957 * I realize that normally you would not do such an obscene thing but
958 * this funciton has a special purpose and I know my origional is not needed
959 * after calling this routine.
960 * This is done because configuration objects that are imported using the INI
961 * import store all data as strings. My origional has type information and I need to
962 * know if the import worked.
965 iniCompare (ACE_Configuration_Heap
& fromFile
, ACE_Configuration_Heap
& original
)
967 bool rc
= true; // start by guessing they are equal
969 int sectionIndex
= 0;
970 ACE_TString sectionName
;
972 const ACE_Configuration_Section_Key
& fromFileRoot
= fromFile
.root_section ();
973 const ACE_Configuration_Section_Key
& originalRoot
= original
.root_section ();
974 ACE_Configuration_Section_Key originalSection
;
975 ACE_Configuration_Section_Key fromFileSection
;
977 // loop through each section in the fromFile object
979 (!fromFile
.enumerate_sections (fromFileRoot
,
983 // find that section in the original object
984 if (original
.open_section (originalRoot
,
985 sectionName
.c_str (),
987 originalSection
) != 0)
988 // If the original object does not contain the section then we
991 else if (fromFile
.open_section (fromFileRoot
,
992 sectionName
.c_str (),
994 fromFileSection
) != 0)
995 // if there is some error opening the section in the fromFile
999 // Well the sections match
1001 ACE_TString valueName
;
1002 ACE_Configuration::VALUETYPE valueType
;
1003 ACE_Configuration::VALUETYPE originalType
;
1005 // Enumerate each value in the fromFile section
1007 (!fromFile
.enumerate_values (fromFileSection
,
1012 // look for the same value in the original section
1013 if (original
.find_value (originalSection
,
1016 // We're not equal if the same value cannot be found
1017 // in the original object.
1021 ACE_TString fromFileString
, originalString
;
1024 if (fromFile
.get_string_value (fromFileSection
,
1026 fromFileString
) != 0)
1027 // we're not equal if we cannot get this string
1029 else if (originalType
!= ACE_Configuration::STRING
) // If the original type is not a string
1031 // convert original data to a string.
1033 if (originalType
== ACE_Configuration::INTEGER
)
1036 ACE_TCHAR int_value
[32];
1038 if (original
.get_integer_value (originalSection
,
1041 // we're not equal if we cannot get rhs int
1044 ACE_OS::snprintf (int_value
, 32, ACE_TEXT ("%08x"),
1046 originalString
= int_value
;
1048 else if (originalType
== ACE_Configuration::BINARY
)
1051 size_t binary_length
;
1053 if (original
.get_binary_value (originalSection
,
1057 // we're not equal if we cannot get this string
1061 ACE_TCHAR bin_value
[3];
1063 unsigned char* ptr
= (unsigned char*)binary_data
;
1064 while (binary_length
)
1066 if (ptr
!= binary_data
)
1067 originalString
+= ACE_TEXT (",");
1069 ACE_OS::snprintf (bin_value
, 3,
1072 originalString
+= bin_value
;
1076 delete [] (char *)binary_data
;
1077 }// end successful binary read
1078 }// end if originalType was binary
1080 // if the type is invalid, then go ahead and fail it.
1083 }// end if the original type was not a string.
1086 if (original
.get_string_value (originalSection
,
1088 originalString
) != 0)
1090 // we're not equal if we cannot get rhs string
1096 rc
&= fromFileString
== originalString
;
1099 // before we move on remove this value from the original.
1100 original
.remove_value (originalSection
,
1101 valueName
.c_str ());
1103 }// end else if values match.
1107 }// end value while loop
1109 // at this point the original should have no values. look
1110 // for values in the original section
1113 (!original
.enumerate_values (originalSection
,
1119 // having a value indicates a mismatch
1120 rc
= valueIndex
== 0;
1122 }// end else if sections match.
1125 // before we move on remove the section from the original.
1126 original
.remove_section (originalRoot
,
1127 sectionName
.c_str (),
1128 0); // do not remove subsections.
1132 }// end section while loop
1134 // Finally, if the original has any sections, then we're not equal
1137 (!original
.enumerate_sections (originalRoot
,
1142 rc
= sectionIndex
== 0;
1147 // change a network section value
1148 int Config_Test::change_one (ACE_Configuration
&cfg
, u_int a
)
1150 ACE_Configuration_Section_Key root
= cfg
.root_section ();
1151 ACE_Configuration_Section_Key NetworkSection
;
1152 ACE_Configuration_Section_Key LoggerSection
;
1153 ACE_Configuration_Section_Key BinarySection
;
1155 if (cfg
.open_section (root
,
1156 ACE_TEXT ("network"),
1161 if (cfg
.set_integer_value (NetworkSection
,
1162 ACE_TEXT ("TimeToLive"),
1169 // Used to test INI Import Export class
1172 Config_Test::testIniFormat ()
1175 if (!this->equality_tested_
)
1177 rc
= this->testEquality ();
1180 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Equality Test Failed\n")));
1185 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Testing INI Format Import/Export\n")));
1186 ACE_Configuration_Heap fromFile
;
1188 // 1. Creates an ACE_Configuration_Heap object
1189 ACE_Configuration_Heap original
;
1191 rc
= original
.open ();
1194 rc
= build_config_object (original
);
1195 // 2. Calls build_config_object to populate
1197 ACE_ERROR_RETURN ((LM_ERROR
,
1198 ACE_TEXT ("Error populating original config ")
1199 ACE_TEXT ("object (%d)\n"),
1203 ACE_Ini_ImpExp
importExport (original
);
1205 rc
= importExport
.export_config (ACE_TEXT ("testConfig.ini"));
1207 ACE_ERROR_RETURN ((LM_ERROR
,
1208 ACE_TEXT ("Error Exporting (%d)\n"),
1213 ACE_ERROR_RETURN ((LM_ERROR
,
1214 ACE_TEXT ("Could not open original object (%d)\n"),
1218 // At this point we've successfully created, populated and written
1219 // the configuration object
1220 // 5. Creates a new ACE_Configuration_Heap object
1221 rc
= fromFile
.open ();
1225 ACE_Ini_ImpExp
importExport (fromFile
);
1227 rc
= importExport
.import_config (ACE_TEXT ("testConfig.ini"));
1229 ACE_ERROR_RETURN ((LM_ERROR
,
1230 ACE_TEXT ("Error Exporting (%d)\n"),
1234 // 7. Compares to original.
1235 // This is a special compare since files imported using the
1236 // INI file import do not contain type information
1238 // NOTE: After this call the original object will be invalid!!!
1240 if (!iniCompare (fromFile
, original
))
1241 ACE_ERROR_RETURN ((LM_ERROR
,
1242 ACE_TEXT ("Object read from file does not ")
1243 ACE_TEXT ("equal original (%d)\n"),
1246 }// end if heap could not be opened.
1248 ACE_ERROR_RETURN ((LM_ERROR
,
1249 ACE_TEXT ("Could not open fromFile object (%d)\n"),
1253 // 8. Calls old "read_config" methods on the new object
1258 int nReconnectInterval
;
1261 ACE_TCHAR pszDestIPAddress
[TEST_MAX_STRING
];
1262 ACE_TCHAR pszLogFilePath
[TEST_MAX_STRING
];
1263 ACE_TCHAR pszTransactionFilePath
[TEST_MAX_STRING
];
1264 ACE_TCHAR pszHeading
[TEST_MAX_STRING
];
1265 ACE_TCHAR pszJustification
[TEST_MAX_STRING
];
1267 ACE_Configuration_Section_Key root
= fromFile
.root_section ();
1269 // Process [network] section
1270 ACE_Configuration_Section_Key NetworkSection
;
1271 if (fromFile
.open_section (root
,
1272 ACE_TEXT ("network"),
1274 NetworkSection
) == 0)
1276 this->get_section_integer (fromFile
,
1278 ACE_TEXT ("TimeToLive"),
1283 this->get_section_boolean (fromFile
,
1288 this->get_section_string (fromFile
,
1290 ACE_TEXT ("DestIPAddress"),
1294 this->get_section_integer (fromFile
,
1296 ACE_TEXT ("DestPort"),
1301 this->get_section_integer (fromFile
,
1303 ACE_TEXT ("ReconnectInterval"),
1304 &nReconnectInterval
,
1307 }// end of "network" section
1309 // Process [logger] section
1310 ACE_Configuration_Section_Key LoggerSection
;
1311 if (fromFile
.open_section (root
,
1312 ACE_TEXT ("logger"),
1314 LoggerSection
) == 0)
1316 this->get_section_string (fromFile
,
1318 ACE_TEXT ("Heading"),
1321 this->get_section_integer (fromFile
,
1323 ACE_TEXT ("TraceLevel"),
1327 this->get_section_string (fromFile
,
1329 ACE_TEXT ("Justification"),
1332 this->get_section_string (fromFile
,
1334 ACE_TEXT ("LogFilePath"),
1337 this->get_section_string (fromFile
,
1339 ACE_TEXT ("TransactionFilePath"),
1340 pszTransactionFilePath
,
1342 }// end of "logger" section
1345 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("INI Format Import/Export Works ;-)\n")));
1349 // Used to test registry Import Export class
1352 Config_Test::testRegFormat ()
1355 if (!this->equality_tested_
)
1357 rc
= this->testEquality ();
1360 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Equality Test Failed\n")));
1365 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Testing Registry Format Import/Export\n")));
1366 ACE_Configuration_Heap fromFile
;
1368 // 1. Creates an ACE_Configuration_Heap object
1369 ACE_Configuration_Heap original
;
1371 rc
= original
.open ();
1374 // 2. Calls build_config_object to populate
1375 rc
= build_config_object (original
);
1377 ACE_ERROR_RETURN ((LM_ERROR
,
1378 ACE_TEXT ("Error populating original config ")
1379 ACE_TEXT ("object (%d)\n"),
1383 ACE_Registry_ImpExp
importExport (original
);
1385 rc
= importExport
.export_config (ACE_TEXT ("testConfig.ini"));
1387 ACE_ERROR_RETURN ((LM_ERROR
,
1388 ACE_TEXT ("Error Exporting (%d)\n"),
1393 ACE_ERROR_RETURN ((LM_ERROR
,
1394 ACE_TEXT ("Could not open original object (%d)\n"),
1398 // At this point we've successfully created, populated and written
1399 // the configuration object
1400 // 5. Creates a new ACE_Configuration_Heap object
1401 rc
= fromFile
.open ();
1405 ACE_Registry_ImpExp
importExport (fromFile
);
1407 rc
= importExport
.import_config (ACE_TEXT ("testConfig.ini"));
1409 ACE_ERROR_RETURN ((LM_ERROR
,
1410 ACE_TEXT("Error Exporting (%d)\n"),
1414 // 7. Compares to original.
1415 if (fromFile
!= original
)
1416 ACE_ERROR_RETURN ((LM_ERROR
,
1417 ACE_TEXT ("Object read from file does not ")
1418 ACE_TEXT ("equal original (%d)\n"),
1422 // 7.1 Change a value and test NOT equal case
1423 change_one (original
, 101);
1424 if (fromFile
== original
)
1426 ACE_ERROR_RETURN ((LM_ERROR
,
1427 ACE_TEXT ("not pass value change test (%d)\n"),
1432 // 7.2 change value back, they should be equal now
1433 change_one (original
, 100);
1434 if (fromFile
!= original
)
1436 ACE_ERROR_RETURN ((LM_ERROR
,
1437 ACE_TEXT ("not pass value change test (%d)\n"),
1442 }// end if heap could not be opened.
1444 ACE_ERROR_RETURN ((LM_ERROR
,
1445 ACE_TEXT ("Could not open fromFile object (%d)\n"),
1450 ACE_DEBUG ((LM_DEBUG
,
1451 ACE_TEXT ("Registry Format Import/Export Works ;-)\n")));
1456 // Reads a string value from a configuration object.
1459 Config_Test::get_section_string (ACE_Configuration
& config
,
1460 ACE_Configuration_Section_Key
& SectionKey
,
1461 const ACE_TCHAR
* pszName
,
1462 ACE_TCHAR
* pszVariable
,
1465 ACE_TString StringValue
;
1467 if (config
.get_string_value (SectionKey
,
1471 ACE_OS::strncpy (pszVariable
,
1472 StringValue
.c_str (),
1474 ACE_DEBUG ((LM_DEBUG
,
1475 ACE_TEXT ("%s = %s\n"),
1481 // Reads an integer value from a configuration object (when it's
1482 // stored as a string)
1484 Config_Test::get_section_integer (ACE_Configuration
& config
,
1485 ACE_Configuration_Section_Key
& SectionKey
,
1486 const ACE_TCHAR
* pszName
,
1491 ACE_TString StringValue
;
1492 ACE_TCHAR pszString
[30];
1493 ACE_OS::strcpy (pszString
, ACE_TEXT ("0"));
1494 int IntegerValue
= 0;
1496 if (config
.get_string_value (SectionKey
,
1500 ACE_OS::strncpy (pszString
,
1501 StringValue
.c_str (),
1503 ACE_DEBUG ((LM_DEBUG
,
1504 ACE_TEXT ("%s = %s\n"),
1509 // convert to integer
1510 IntegerValue
= ACE_OS::atoi (pszString
);
1511 IntegerValue
= (IntegerValue
< nMinValue
) ? nMinValue
: IntegerValue
;
1512 IntegerValue
= (IntegerValue
> nMaxValue
) ? nMaxValue
: IntegerValue
;
1513 *nVariable
= IntegerValue
;
1516 // Reads a boolean value from a configuration object (when it's stored as a string).
1519 Config_Test::get_section_boolean (ACE_Configuration
& config
,
1520 ACE_Configuration_Section_Key
& SectionKey
,
1521 const ACE_TCHAR
* pszName
,
1524 ACE_TString StringValue
;
1525 ACE_TCHAR pszString
[10];
1526 ACE_OS::strcpy (pszString
, ACE_TEXT ("0"));
1528 if (config
.get_string_value (SectionKey
,
1532 ACE_OS::strncpy (pszString
,
1533 StringValue
.c_str (),
1535 for (ACE_TCHAR
* pSrc
= pszString
;
1536 *pSrc
!= ACE_TEXT ('\0');
1538 // Convert to uppercase
1539 if (ACE_OS::ace_islower (*pSrc
))
1540 *pSrc
= ACE_OS::ace_tolower (*pSrc
);
1542 ACE_DEBUG ((LM_DEBUG
,
1543 ACE_TEXT ("%s = %s\n"),
1547 if (ACE_OS::strcmp (pszString
,
1548 ACE_TEXT ("TRUE")) == 0)
1550 else if (ACE_OS::strcmp (pszString
,
1551 ACE_TEXT ("FALSE")) == 0)
1557 run_main (int, ACE_TCHAR
*[])
1559 ACE_START_TEST (ACE_TEXT ("Config_Test"));
1561 Config_Test manager
;
1563 if ((errors
+= manager
.testEquality ()) != 0)
1564 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Failed the equality Test\n")));
1566 if ((errors
+= manager
.testRegFormat ()) != 0)
1567 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Failed the REG Format Test\n")));
1569 if ((errors
+= manager
.testIniFormat ()) != 0)
1570 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Failed the INI Format Test\n")));
1572 if ((errors
+= run_tests ()) != 0)
1573 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Failed in run_tests\n")));
1576 return errors
== 0 ? 0 : 1;