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"
26 test (ACE_Configuration
*config
,
27 ACE_Configuration_Section_Key
&testsection
)
32 if (config
->set_string_value (testsection
,
34 ACE_TEXT ("stvaluetest")))
37 else if (config
->remove_value (testsection
,
38 ACE_TEXT ("stvalue")))
40 // Make sure it's really gone
41 else if (0 == config
->get_string_value (testsection
,
44 ACE_ERROR_RETURN ((LM_ERROR
,
45 ACE_TEXT ("test:remove_value didn't remove\n")),
48 else if (config
->set_string_value (testsection
,
50 ACE_TEXT ("stvaluetest")))
52 else if (config
->set_string_value (testsection
,
54 ACE_TEXT ("second stvaluetest")))
55 ACE_ERROR_RETURN ((LM_ERROR
,
56 ACE_TEXT ("test:set_string_value twice failed\n")),
59 else if (config
->set_integer_value (testsection
,
60 ACE_TEXT ("intvalue"),
63 // Reset to the value we test for below
64 else if (config
->set_integer_value (testsection
,
65 ACE_TEXT ("intvalue"),
69 static size_t const data_len
= 80;
70 u_char data
[data_len
];
72 for (size_t i
= 0; i
< data_len
; ++i
)
73 data
[i
] = static_cast<u_char
> (i
+ 128);
75 if (config
->set_binary_value (testsection
,
76 ACE_TEXT ("binvalue"),
81 // Get the values and compare
82 if (config
->get_string_value (testsection
,
86 else if (stvalue
!= ACE_TEXT ("second stvaluetest"))
91 if (config
->get_integer_value (testsection
,
92 ACE_TEXT ("intvalue"),
95 else if (intvalue
!= 42)
101 void *data_tmp
= 0; // Workaround for GCC strict aliasing warning.
104 if (config
->get_binary_value (testsection
,
105 ACE_TEXT ("binvalue"),
110 data_out
= reinterpret_cast <u_char
*> (data_tmp
);
113 u_char
* the_data
= static_cast<u_char
*> (data_out
);
116 for (size_t j
= 0; j
< data_len
; ++j
)
117 if (the_data
[j
] != data
[j
])
124 ACE_Configuration::VALUETYPE type
;
126 int found
[3] = { 0, 0, 0 }; // One for each expected value
128 while (!config
->enumerate_values (testsection
,
133 if (name
== ACE_TEXT ("stvalue"))
135 if (type
!= ACE_Configuration::STRING
)
141 else if (name
== ACE_TEXT ("intvalue"))
143 if (type
!= ACE_Configuration::INTEGER
)
149 else if (name
== ACE_TEXT ("binvalue"))
151 if (type
!= ACE_Configuration::BINARY
)
160 // Make sure we got three values.
161 if (index
!= 3 || !found
[0] || !found
[1] || !found
[2])
165 // Add some subsections. This part is a separate scope to be sure
166 // the test2, test3, test4 keys are closed before further
167 // manipulating/deleting the sections further down in the test.
168 ACE_Configuration_Section_Key test2
;
169 ACE_Configuration_Section_Key test3
;
170 ACE_Configuration_Section_Key test4
;
172 if (config
->open_section (testsection
,
177 else if (config
->open_section (testsection
,
182 else if (config
->open_section (testsection
,
189 // Test enumerate sections.
191 found
[0] = found
[1] = found
[2] = 0;
192 while (!config
->enumerate_sections (testsection
,
196 if (name
== ACE_TEXT ("test2"))
199 ACE_ERROR_RETURN ((LM_ERROR
,
200 ACE_TEXT ("enumerate_sections, dupl test2\n")),
204 else if (name
== ACE_TEXT ("test3"))
207 ACE_ERROR_RETURN ((LM_ERROR
,
208 ACE_TEXT ("enumerate_sections, dupl test3\n")),
212 else if (name
== ACE_TEXT ("test4"))
215 ACE_ERROR_RETURN ((LM_ERROR
,
216 ACE_TEXT ("enumerate_sections, dupl test4\n")),
223 if (index
!= 3 || !found
[0] || !found
[1] || !found
[2])
226 // Remove a subsection
227 if (config
->remove_section (testsection
,
230 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p (%d)\n"),
231 ACE_TEXT ("remove_section test2"),
232 ACE_OS::last_error ()),
235 // Try to remove it again
236 if (!config
->remove_section (testsection
,
245 test (ACE_Configuration
*config
)
247 const ACE_Configuration_Section_Key
& root
=
248 config
->root_section ();
251 // Scope this so the testsection key is closed before trying to
252 // remove the "test" section.
253 ACE_Configuration_Section_Key testsection
;
255 if (config
->open_section (root
,
261 int ret_val
= test (config
, testsection
);
266 // Try to remove the testsection root, it should fail since it still
268 if (!config
->remove_section (root
,
274 // Test find section, and be sure the key is closed before testing the
276 ACE_Configuration_Section_Key result
;
278 if (config
->open_section (root
,
285 // Now test the recursive remove.
286 if (config
->remove_section (root
,
291 // Make sure its not there
292 ACE_Configuration_Section_Key testsectiongone
;
293 if (!config
->open_section (root
,
303 test_subkey_path (ACE_Configuration
* config
)
305 ACE_Configuration_Section_Key root
=
306 config
->root_section ();
308 ACE_Configuration_Section_Key testsection
;
310 if (config
->open_section (root
,
311 ACE_TEXT ("Software\\ACE\\test"),
316 int ret_val
= test (config
, testsection
);
320 if (config
->open_section (root
,
321 ACE_TEXT ("Software"),
326 if (config
->remove_section (testsection
,
340 // Test import of a legit INI format from a previously-existing file.
341 ACE_Configuration_Heap cf
;
342 if ((status
= cf
.open ()) != 0)
343 ACE_ERROR ((LM_ERROR
,
344 ACE_TEXT ("ACE_Configuration_Heap::open returned %d\n"),
346 ACE_Ini_ImpExp
import (cf
);
347 // This one should work...
348 ACE_TCHAR import_file_name
[MAXPATHLEN
];
349 #if defined (TEST_DIR)
350 ACE_OS::strcpy (import_file_name
, TEST_DIR
);
351 ACE_OS::strcat (import_file_name
, ACE_DIRECTORY_SEPARATOR_STR
);
352 ACE_OS::strcat (import_file_name
, ACE_TEXT ("Config_Test_Import_1.ini"));
354 ACE_OS::strcpy (import_file_name
, ACE_TEXT ("Config_Test_Import_1.ini"));
357 status
= import
.import_config (import_file_name
);
359 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%p: %s\n"),
360 ACE_TEXT ("Config_Test_Import_1.ini failed"),
364 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%s imported\n"), import_file_name
));
366 // Imported clean; verify content. See ini file for expected content.
367 // Verify the expected sections are there, but no others. Verify the
368 // expected keys are there, but no others.
369 int section1_seen
= 0, section2_seen
= 0;
370 int somekey_seen
= 0, someotherkey_seen
= 0;
372 ACE_TString sect_name
;
373 const ACE_Configuration_Section_Key
&root
= cf
.root_section ();
375 (status
= cf
.enumerate_sections (root
, index
, sect_name
)) == 0;
377 if (index
> 1) // There are only two sections.
378 ACE_ERROR ((LM_ERROR
,
379 ACE_TEXT ("Enumerated %d sections; expected 2\n"),
382 ACE_DEBUG ((LM_DEBUG
,
383 ACE_TEXT ("Enumerated to section %s\n"),
384 sect_name
.c_str ()));
385 if (sect_name
== ACE_TEXT ("SectionOne")) {
387 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Saw %s multiple times!\n"),
388 sect_name
.c_str ()));
390 // Check for values in this section.
391 ACE_Configuration_Section_Key sect1
;
392 if (cf
.open_section (root
, sect_name
.c_str (), 0, sect1
) != 0)
393 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Failed to open section: %s\n"),
394 sect_name
.c_str ()));
396 int val_index
= 0, val_status
;
397 ACE_TString val_name
, value
;
398 ACE_Configuration::VALUETYPE val_type
;
401 (sect1
, val_index
, val_name
, val_type
)) == 0) {
402 ACE_DEBUG ((LM_DEBUG
,
403 ACE_TEXT ("Enumerated %s, type %d\n"),
406 if (val_type
!= ACE_Configuration::STRING
)
407 ACE_ERROR ((LM_ERROR
,
408 ACE_TEXT ("Expected %s to be STRING, but %d\n"),
411 if (val_name
== ACE_TEXT ("SomeKey")) {
413 ACE_ERROR ((LM_ERROR
,
414 ACE_TEXT ("Saw %s more than once\n"),
419 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Unexpected key %s\n"),
421 if ((val_status
= cf
.get_string_value
422 (sect1
, val_name
.c_str (), value
)) != 0)
423 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Can't get value of %s\n"),
426 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%s value: %s\n"),
427 val_name
.c_str (), value
.c_str ()));
428 if (value
!= ACE_TEXT ("SomeValue")) {
429 ACE_ERROR ((LM_ERROR
,
430 ACE_TEXT ("SomeKey: %s; expected SomeValue\n")));
435 if (val_status
== 1) {
436 if (val_index
!= 1) // Should have only seen 1
437 ACE_ERROR ((LM_ERROR
,
438 ACE_TEXT ("Expected 1 value; saw %d\n"),
442 ACE_ERROR ((LM_ERROR
,
443 ACE_TEXT ("Error enumerating %s; status %d\n"),
444 sect_name
.c_str (), val_status
));
447 else if (sect_name
== ACE_TEXT ("SectionTwo")) {
449 ACE_ERROR ((LM_ERROR
,
450 ACE_TEXT ("Saw %s multiple times!\n"),
451 sect_name
.c_str ()));
453 // Check for values in this section.
454 ACE_Configuration_Section_Key sect2
;
455 if (cf
.open_section (root
, sect_name
.c_str (), 0, sect2
) != 0)
456 ACE_ERROR ((LM_ERROR
,
457 ACE_TEXT ("Failed to open section: %s\n"),
458 sect_name
.c_str ()));
460 int val_index
= 0, val_status
;
461 ACE_TString val_name
, value
;
462 ACE_Configuration::VALUETYPE val_type
;
463 while ((val_status
= cf
.enumerate_values
464 (sect2
, val_index
, val_name
, val_type
)) == 0) {
465 ACE_DEBUG ((LM_DEBUG
,
466 ACE_TEXT ("Enumerated %s, type %d\n"),
469 if (val_type
!= ACE_Configuration::STRING
)
470 ACE_ERROR ((LM_ERROR
,
471 ACE_TEXT ("Expected %s to be STRING, but %d\n"),
472 val_name
.c_str (), val_type
));
473 if (val_name
== ACE_TEXT ("SomeOtherKey")) {
474 if (someotherkey_seen
)
475 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Saw %s more than once\n"),
477 someotherkey_seen
= 1;
480 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Unexpected key %s\n"),
482 if ((val_status
= cf
.get_string_value
483 (sect2
, val_name
.c_str (), value
)) != 0)
484 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Can't get value of %s\n"),
487 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("%s value: %s\n"),
488 val_name
.c_str (), value
.c_str ()));
489 if (value
!= ACE_TEXT ("SomeOtherValue")) {
490 ACE_ERROR ((LM_ERROR
,
491 ACE_TEXT ("SomeOtherKey: %s; expected SomeOtherValue\n")));
496 if (val_status
== 1) {
497 if (val_index
!= 1) // Should have only seen 1
498 ACE_ERROR ((LM_ERROR
,
499 ACE_TEXT ("Expected 1 value; saw %d\n"),
503 ACE_ERROR ((LM_ERROR
,
504 ACE_TEXT ("Error enumerating %s; status %d\n"),
505 sect_name
.c_str (), val_status
));
509 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Saw unexpected section: %s\n"),
510 sect_name
.c_str ()));
514 if (status
== 1) { // Ran out of sections
516 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("Saw %d sections; expected 2\n"),
520 ACE_ERROR ((LM_ERROR
,
521 ACE_TEXT ("Error enumerating sections; status %d\n"),
526 #if defined (ACE_WIN32) && !defined (ACE_LACKS_WIN32_REGISTRY)
528 ACE_Configuration_Win32Registry
RegConfig (HKEY_LOCAL_MACHINE
);
529 int result
= test_subkey_path (&RegConfig
);
531 ACE_ERROR_RETURN ((LM_ERROR
,
532 ACE_TEXT ("Win32Registry test HKEY_LOCAL_MACHINE")
533 ACE_TEXT (" failed (%d)\n"), result
),
536 // test win32 registry implementation.
538 ACE_Configuration_Win32Registry::resolve_key (HKEY_LOCAL_MACHINE
,
539 ACE_TEXT ("Software\\ACE\\test"));
541 ACE_ERROR_RETURN ((LM_ERROR
,
542 ACE_TEXT ("resolve_key is broken\n")), -2);
544 // test resolving of forward slashes
546 ACE_Configuration_Win32Registry::resolve_key (HKEY_LOCAL_MACHINE
,
547 ACE_TEXT ("Software/ACE/test"), 0);
549 ACE_ERROR_RETURN ((LM_ERROR
,
550 ACE_TEXT ("resolve_key resolving slashes is broken\n")),
553 ACE_Configuration_Win32Registry
RegConfig (root
);
555 int const result
= test (&RegConfig
);
557 ACE_ERROR_RETURN ((LM_ERROR
,
558 ACE_TEXT ("Win32 registry test root failed (%d)\n"),
563 #endif /* ACE_WIN32 && !ACE_LACKS_WIN32_REGISTRY */
566 ACE_Configuration_Heap heap_config
;
568 if (heap_config
.open () != 0)
569 ACE_ERROR_RETURN ((LM_ERROR
,
570 ACE_TEXT ("Cannot open %p\n"),
571 ACE_TEXT ("local-heap config")),
573 if (heap_config
.open () == 0)
575 ACE_ERROR_RETURN ((LM_ERROR
,
576 ACE_TEXT ("Re-open heap allowed; bugzilla 3724\n")),
579 else if (errno
!= EBUSY
)
581 ACE_ERROR_RETURN ((LM_ERROR
,
582 ACE_TEXT ("Re-open heap expected EBUSY (%d), ")
583 ACE_TEXT ("got %d: bugzilla 3724\n"),
584 EBUSY
, ACE_ERRNO_GET
),
588 int result
= test_subkey_path (&heap_config
);
590 ACE_ERROR_RETURN ((LM_ERROR
,
591 ACE_TEXT ("Heap Config subkey test failed (%d)\n"),
597 int result
= test (&heap_config
);
599 ACE_ERROR_RETURN ((LM_ERROR
,
600 ACE_TEXT ("Heap Configuration test failed (%d)\n"),
605 #if !defined (ACE_LACKS_MMAP)
606 // Test persistent heap version
607 ACE_OS::unlink (ACE_TEXT ("test.reg"));
608 ACE_Configuration_Heap pers_config
;
610 if (pers_config
.open (ACE_TEXT ("test.reg")))
611 ACE_ERROR_RETURN ((LM_ERROR
,
612 ACE_TEXT ("Cannot open %p\n"),
613 ACE_TEXT ("test.reg")),
615 if (pers_config
.open (ACE_TEXT ("test.reg")) == 0)
617 ACE_ERROR_RETURN ((LM_ERROR
,
618 ACE_TEXT ("Re-open(mmap) allowed; bugzilla 3724\n")),
621 else if (errno
!= EBUSY
)
623 ACE_ERROR_RETURN ((LM_ERROR
,
624 ACE_TEXT ("Re-open(mmap) expected EBUSY (%d), ")
625 ACE_TEXT ("got %d: bugzilla 3724\n"),
626 EBUSY
, ACE_ERRNO_GET
),
629 if (pers_config
.open () == 0)
631 ACE_ERROR_RETURN ((LM_ERROR
,
632 ACE_TEXT ("Re-open(new) allowed; bugzilla 3724\n")),
635 else if (errno
!= EBUSY
)
637 ACE_ERROR_RETURN ((LM_ERROR
,
638 ACE_TEXT ("Re-open(new) expected EBUSY (%d), ")
639 ACE_TEXT ("got %d: bugzilla 3724\n"),
640 EBUSY
, ACE_ERRNO_GET
),
645 int result
= test (&pers_config
);
647 ACE_ERROR_RETURN ((LM_ERROR
,
648 ACE_TEXT ("Persistent Heap Config test failed (%d)\n"),
652 #endif /* !ACE_LACKS_MMAP */
654 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Test passed\n")));
659 build_config_object (ACE_Configuration
& cfg
)
661 ACE_Configuration_Section_Key root
= cfg
.root_section ();
662 ACE_Configuration_Section_Key NetworkSection
;
663 ACE_Configuration_Section_Key LoggerSection
;
664 ACE_Configuration_Section_Key BinarySection
;
666 if (cfg
.open_section (root
,
667 ACE_TEXT ("network"),
672 if (cfg
.set_integer_value (NetworkSection
,
673 ACE_TEXT ("TimeToLive"),
676 else if (cfg
.set_string_value (NetworkSection
,
678 ACE_TString (ACE_TEXT ("FALSE"))))
680 else if (cfg
.set_string_value (NetworkSection
,
681 ACE_TEXT ("DestIPAddress"),
682 ACE_TString (ACE_TEXT ("localhost"))))
684 else if (cfg
.set_integer_value (NetworkSection
,
685 ACE_TEXT ("DestPort"),
688 else if (cfg
.set_integer_value (NetworkSection
,
689 ACE_TEXT ("ReconnectInterval"),
693 if (cfg
.open_section (root
,
699 if (cfg
.set_string_value (LoggerSection
,
700 ACE_TEXT ("Heading"),
701 ACE_TString (ACE_TEXT ("ACE - Adaptive Communication Environment"))))
703 else if (cfg
.set_integer_value (LoggerSection
,
704 ACE_TEXT ("SeekIndex"),
707 else if (cfg
.set_integer_value (LoggerSection
,
708 ACE_TEXT ("TraceLevel"),
711 else if (cfg
.set_string_value (LoggerSection
,
712 ACE_TEXT ("Justification"),
713 ACE_TString (ACE_TEXT ("left_justified"))))
715 else if (cfg
.set_string_value (LoggerSection
,
716 ACE_TEXT ("LogFilePath"),
717 ACE_TString (ACE_TEXT ("log/"))))
719 else if (cfg
.set_string_value (LoggerSection
,
720 ACE_TEXT ("TransactionFilePath"),
721 ACE_TString (ACE_TEXT ("data/"))))
724 if (cfg
.open_section (root
,
732 for (int i
= 0; i
< 80; i
++)
735 if (cfg
.set_binary_value (BinarySection
,
741 ACE_TString
string((ACE_TCHAR
*) 0);// = '0';
742 // Try to set the unnamed, default value.
743 if (cfg
.set_string_value (LoggerSection
,
744 0,//string.c_str (),//0, //ACE_TEXT ("x"),
745 ACE_TString (ACE_TEXT ("some string"))))
746 ACE_ERROR_RETURN ((LM_ERROR
,
747 ACE_TEXT ("could not set value with null name\n")),
750 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("here\n")));
752 //ACE_TString string;
753 ACE_TString
name ((ACE_TCHAR
*)0);
754 if (cfg
.get_string_value (LoggerSection
,
755 name
.c_str (), //0, //ACE_TEXT ("x"),
757 ACE_ERROR_RETURN ((LM_ERROR
,
758 ACE_TEXT ("error using ACE_TString::c_str() == %d, len == %d\n"),
759 name
.c_str(), name
.length ()),
762 ACE_DEBUG ((LM_DEBUG
,
763 ACE_TEXT ("the value for the unnamed var=(%s)\n"),
770 * Test ACE_Configuration::operator==
773 Config_Test::testEquality ()
775 // create and open 2 ACE_Configuration objects.
776 ACE_Configuration_Heap heap1
;
777 ACE_Configuration_Heap heap2
;
778 if ((heap1
.open ()) != 0)
780 ACE_ERROR_RETURN ((LM_ERROR
,
781 ACE_TEXT ("Cannot open heap1\n")),
784 else if ((heap2
.open ()) != 0)
786 ACE_ERROR_RETURN ((LM_ERROR
,
787 ACE_TEXT ("Cannot open heap2\n")),
791 // populate them equally
792 if (build_config_object (heap1
) != 0 ||
793 build_config_object (heap2
) != 0)
794 ACE_ERROR_RETURN ((LM_ERROR
,
795 ACE_TEXT ("could not build config object\n")),
799 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should equal...\n")));
802 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they do ;-)\n")));
806 ACE_ERROR_RETURN ((LM_ERROR
,
807 ACE_TEXT ("And they do not :-(\n")
808 ACE_TEXT ("operator== Failed when objects equal\n")),
812 // add a section and value to heap1
813 ACE_Configuration_Section_Key root1
= heap1
.root_section ();
814 ACE_Configuration_Section_Key NewSection
;
815 if (heap1
.open_section (root1
,
816 ACE_TEXT ("NewSection"),
819 ACE_ERROR_RETURN ((LM_ERROR
,
820 ACE_TEXT ("Error adding section to heap1\n")),
822 else if (heap1
.set_integer_value (NewSection
,
823 ACE_TEXT ("TestIntValue"),
825 ACE_ERROR_RETURN ((LM_ERROR
,
826 ACE_TEXT ("Error adding value to heap1\n")),
830 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should NOT equal...\n")));
832 ACE_ERROR_RETURN ((LM_ERROR
,
833 ACE_TEXT ("They Do :-(\noperator== Failed ")
834 ACE_TEXT ("when lhs contains data not in rhs\n")),
837 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they do not ;-)\n")));
840 // add same section to heap2
842 ACE_Configuration_Section_Key root2
= heap2
.root_section ();
843 ACE_Configuration_Section_Key NewSection2
;
844 if (heap2
.open_section (root2
,
845 ACE_TEXT ("NewSection"),
848 ACE_ERROR_RETURN ((LM_ERROR
,
849 ACE_TEXT ("Error adding section to heap2\n")),
851 else if (heap2
.set_integer_value (NewSection2
,
852 ACE_TEXT ("TestIntValue"),
854 ACE_ERROR_RETURN ((LM_ERROR
,
855 ACE_TEXT ("Error adding value to heap2\n")),
857 else if (heap2
.set_integer_value (NewSection2
,
858 ACE_TEXT ("TestIntValue2"),
860 ACE_ERROR_RETURN ((LM_ERROR
,
861 ACE_TEXT ("Error adding second value to heap2\n")),
865 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should NOT equal...\n")));
867 ACE_ERROR_RETURN ((LM_ERROR
,
868 ACE_TEXT ("And They Do :-(\noperator== Failed ")
869 ACE_TEXT ("when rhs contains value not in lhs\n")),
872 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they do not ;-)\n")));
874 // add new value in heap 1
875 if (heap1
.set_integer_value (NewSection
,
876 ACE_TEXT ("TestIntValue2"),
878 ACE_ERROR_RETURN ((LM_ERROR
,
879 ACE_TEXT ("Error adding second value to heap1\n")),
883 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should be equal...\n")));
885 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they are ;-)\n")));
887 ACE_ERROR_RETURN ((LM_ERROR
,
888 ACE_TEXT ("And they are not :-(\n")
889 ACE_TEXT ("operator== Failed\n")),
892 // Add a new section to heap2
893 ACE_Configuration_Section_Key AnotherNewSection2
;
894 if (heap2
.open_section (root2
,
895 ACE_TEXT ("AnotherNewSection"),
898 ACE_ERROR_RETURN ((LM_ERROR
,
899 ACE_TEXT ("Error adding second section to heap2\n")),
901 else if (heap2
.set_integer_value (AnotherNewSection2
,
902 ACE_TEXT ("TestIntValue"),
904 ACE_ERROR_RETURN ((LM_ERROR
,
905 ACE_TEXT ("Error adding value in second section")
906 ACE_TEXT (" to heap2\n")),
910 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should NOT equal...\n")));
912 ACE_ERROR_RETURN ((LM_ERROR
,
913 ACE_TEXT ("And they do :-(\noperator== Failed ")
914 ACE_TEXT ("when rhs contains data not in lhs\n")),
917 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they do not :-)\n")));
919 // add section back to heap1
920 ACE_Configuration_Section_Key AnotherNewSection1
;
921 if (heap1
.open_section (root1
,
922 ACE_TEXT ("AnotherNewSection"),
925 ACE_ERROR_RETURN ((LM_ERROR
,
926 ACE_TEXT ("Error adding second section to heap1\n")),
928 else if (heap1
.set_integer_value (AnotherNewSection1
,
929 ACE_TEXT ("TestIntValue"),
931 ACE_ERROR_RETURN ((LM_ERROR
,
932 ACE_TEXT ("Error adding second value to second ")
933 ACE_TEXT ("section in heap1\n")),
937 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("The objects should be equal...\n")));
939 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("And they are ;-)\n")));
941 ACE_ERROR_RETURN ((LM_ERROR
,
942 ACE_TEXT ("And they are not :-(\n")
943 ACE_TEXT ("operator== Failed\n")),
946 this->equality_tested_
= 1;
951 * Compare INI import data in fromFile to origional data exported (in origional)
953 * This compare is destructive to the origional object.
954 * I realize that normally you would not do such an obscene thing but
955 * this funciton has a special purpose and I know my origional is not needed
956 * after calling this routine.
957 * This is done because configuration objects that are imported using the INI
958 * import store all data as strings. My origional has type information and I need to
959 * know if the import worked.
962 iniCompare (ACE_Configuration_Heap
& fromFile
, ACE_Configuration_Heap
& original
)
964 bool rc
= true; // start by guessing they are equal
966 int sectionIndex
= 0;
967 ACE_TString sectionName
;
969 const ACE_Configuration_Section_Key
& fromFileRoot
= fromFile
.root_section ();
970 const ACE_Configuration_Section_Key
& originalRoot
= original
.root_section ();
971 ACE_Configuration_Section_Key originalSection
;
972 ACE_Configuration_Section_Key fromFileSection
;
974 // loop through each section in the fromFile object
976 (!fromFile
.enumerate_sections (fromFileRoot
,
980 // find that section in the original object
981 if (original
.open_section (originalRoot
,
982 sectionName
.c_str (),
984 originalSection
) != 0)
985 // If the original object does not contain the section then we
988 else if (fromFile
.open_section (fromFileRoot
,
989 sectionName
.c_str (),
991 fromFileSection
) != 0)
992 // if there is some error opening the section in the fromFile
996 // Well the sections match
998 ACE_TString valueName
;
999 ACE_Configuration::VALUETYPE valueType
;
1000 ACE_Configuration::VALUETYPE originalType
;
1002 // Enumerate each value in the fromFile section
1004 (!fromFile
.enumerate_values (fromFileSection
,
1009 // look for the same value in the original section
1010 if (original
.find_value (originalSection
,
1013 // We're not equal if the same value cannot be found
1014 // in the original object.
1018 ACE_TString fromFileString
, originalString
;
1021 if (fromFile
.get_string_value (fromFileSection
,
1023 fromFileString
) != 0)
1024 // we're not equal if we cannot get this string
1026 else if (originalType
!= ACE_Configuration::STRING
) // If the original type is not a string
1028 // convert original data to a string.
1030 if (originalType
== ACE_Configuration::INTEGER
)
1033 ACE_TCHAR int_value
[32];
1035 if (original
.get_integer_value (originalSection
,
1038 // we're not equal if we cannot get rhs int
1041 ACE_OS::snprintf (int_value
, 32, ACE_TEXT ("%08x"),
1043 originalString
= int_value
;
1045 else if (originalType
== ACE_Configuration::BINARY
)
1048 size_t binary_length
;
1050 if (original
.get_binary_value (originalSection
,
1054 // we're not equal if we cannot get this string
1058 ACE_TCHAR bin_value
[3];
1060 unsigned char* ptr
= (unsigned char*)binary_data
;
1061 while (binary_length
)
1063 if (ptr
!= binary_data
)
1064 originalString
+= ACE_TEXT (",");
1066 ACE_OS::snprintf (bin_value
, 3,
1069 originalString
+= bin_value
;
1073 delete [] (char *)binary_data
;
1074 }// end successful binary read
1075 }// end if originalType was binary
1077 // if the type is invalid, then go ahead and fail it.
1079 }// end if the original type was not a string.
1082 if (original
.get_string_value (originalSection
,
1084 originalString
) != 0)
1086 // we're not equal if we cannot get rhs string
1092 rc
&= fromFileString
== originalString
;
1095 // before we move on remove this value from the original.
1096 original
.remove_value (originalSection
,
1097 valueName
.c_str ());
1098 }// end else if values match.
1101 }// end value while loop
1103 // at this point the original should have no values. look
1104 // for values in the original section
1107 (!original
.enumerate_values (originalSection
,
1113 // having a value indicates a mismatch
1114 rc
= valueIndex
== 0;
1115 }// end else if sections match.
1118 // before we move on remove the section from the original.
1119 original
.remove_section (originalRoot
,
1120 sectionName
.c_str (),
1121 0); // do not remove subsections.
1124 }// end section while loop
1126 // Finally, if the original has any sections, then we're not equal
1129 (!original
.enumerate_sections (originalRoot
,
1134 rc
= sectionIndex
== 0;
1139 // change a network section value
1140 int Config_Test::change_one (ACE_Configuration
&cfg
, u_int a
)
1142 ACE_Configuration_Section_Key root
= cfg
.root_section ();
1143 ACE_Configuration_Section_Key NetworkSection
;
1144 ACE_Configuration_Section_Key LoggerSection
;
1145 ACE_Configuration_Section_Key BinarySection
;
1147 if (cfg
.open_section (root
,
1148 ACE_TEXT ("network"),
1153 if (cfg
.set_integer_value (NetworkSection
,
1154 ACE_TEXT ("TimeToLive"),
1161 // Used to test INI Import Export class
1164 Config_Test::testIniFormat ()
1167 if (!this->equality_tested_
)
1169 rc
= this->testEquality ();
1172 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Equality Test Failed\n")));
1177 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Testing INI Format Import/Export\n")));
1178 ACE_Configuration_Heap fromFile
;
1180 // 1. Creates an ACE_Configuration_Heap object
1181 ACE_Configuration_Heap original
;
1183 rc
= original
.open ();
1186 rc
= build_config_object (original
);
1187 // 2. Calls build_config_object to populate
1189 ACE_ERROR_RETURN ((LM_ERROR
,
1190 ACE_TEXT ("Error populating original config ")
1191 ACE_TEXT ("object (%d)\n"),
1195 ACE_Ini_ImpExp
importExport (original
);
1197 rc
= importExport
.export_config (ACE_TEXT ("testConfig.ini"));
1199 ACE_ERROR_RETURN ((LM_ERROR
,
1200 ACE_TEXT ("Error Exporting (%d)\n"),
1205 ACE_ERROR_RETURN ((LM_ERROR
,
1206 ACE_TEXT ("Could not open original object (%d)\n"),
1210 // At this point we've successfully created, populated and written
1211 // the configuration object
1212 // 5. Creates a new ACE_Configuration_Heap object
1213 rc
= fromFile
.open ();
1217 ACE_Ini_ImpExp
importExport (fromFile
);
1219 rc
= importExport
.import_config (ACE_TEXT ("testConfig.ini"));
1221 ACE_ERROR_RETURN ((LM_ERROR
,
1222 ACE_TEXT ("Error Exporting (%d)\n"),
1226 // 7. Compares to original.
1227 // This is a special compare since files imported using the
1228 // INI file import do not contain type information
1230 // NOTE: After this call the original object will be invalid!!!
1232 if (!iniCompare (fromFile
, original
))
1233 ACE_ERROR_RETURN ((LM_ERROR
,
1234 ACE_TEXT ("Object read from file does not ")
1235 ACE_TEXT ("equal original (%d)\n"),
1238 }// end if heap could not be opened.
1240 ACE_ERROR_RETURN ((LM_ERROR
,
1241 ACE_TEXT ("Could not open fromFile object (%d)\n"),
1245 // 8. Calls old "read_config" methods on the new object
1250 int nReconnectInterval
;
1253 ACE_TCHAR pszDestIPAddress
[TEST_MAX_STRING
];
1254 ACE_TCHAR pszLogFilePath
[TEST_MAX_STRING
];
1255 ACE_TCHAR pszTransactionFilePath
[TEST_MAX_STRING
];
1256 ACE_TCHAR pszHeading
[TEST_MAX_STRING
];
1257 ACE_TCHAR pszJustification
[TEST_MAX_STRING
];
1259 ACE_Configuration_Section_Key root
= fromFile
.root_section ();
1261 // Process [network] section
1262 ACE_Configuration_Section_Key NetworkSection
;
1263 if (fromFile
.open_section (root
,
1264 ACE_TEXT ("network"),
1266 NetworkSection
) == 0)
1268 this->get_section_integer (fromFile
,
1270 ACE_TEXT ("TimeToLive"),
1275 this->get_section_boolean (fromFile
,
1280 this->get_section_string (fromFile
,
1282 ACE_TEXT ("DestIPAddress"),
1286 this->get_section_integer (fromFile
,
1288 ACE_TEXT ("DestPort"),
1293 this->get_section_integer (fromFile
,
1295 ACE_TEXT ("ReconnectInterval"),
1296 &nReconnectInterval
,
1299 }// end of "network" section
1301 // Process [logger] section
1302 ACE_Configuration_Section_Key LoggerSection
;
1303 if (fromFile
.open_section (root
,
1304 ACE_TEXT ("logger"),
1306 LoggerSection
) == 0)
1308 this->get_section_string (fromFile
,
1310 ACE_TEXT ("Heading"),
1313 this->get_section_integer (fromFile
,
1315 ACE_TEXT ("TraceLevel"),
1319 this->get_section_string (fromFile
,
1321 ACE_TEXT ("Justification"),
1324 this->get_section_string (fromFile
,
1326 ACE_TEXT ("LogFilePath"),
1329 this->get_section_string (fromFile
,
1331 ACE_TEXT ("TransactionFilePath"),
1332 pszTransactionFilePath
,
1334 }// end of "logger" section
1337 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("INI Format Import/Export Works ;-)\n")));
1341 // Used to test registry Import Export class
1344 Config_Test::testRegFormat ()
1347 if (!this->equality_tested_
)
1349 rc
= this->testEquality ();
1352 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Equality Test Failed\n")));
1357 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Testing Registry Format Import/Export\n")));
1358 ACE_Configuration_Heap fromFile
;
1360 // 1. Creates an ACE_Configuration_Heap object
1361 ACE_Configuration_Heap original
;
1363 rc
= original
.open ();
1366 // 2. Calls build_config_object to populate
1367 rc
= build_config_object (original
);
1369 ACE_ERROR_RETURN ((LM_ERROR
,
1370 ACE_TEXT ("Error populating original config ")
1371 ACE_TEXT ("object (%d)\n"),
1375 ACE_Registry_ImpExp
importExport (original
);
1377 rc
= importExport
.export_config (ACE_TEXT ("testConfig.ini"));
1379 ACE_ERROR_RETURN ((LM_ERROR
,
1380 ACE_TEXT ("Error Exporting (%d)\n"),
1385 ACE_ERROR_RETURN ((LM_ERROR
,
1386 ACE_TEXT ("Could not open original object (%d)\n"),
1390 // At this point we've successfully created, populated and written
1391 // the configuration object
1392 // 5. Creates a new ACE_Configuration_Heap object
1393 rc
= fromFile
.open ();
1397 ACE_Registry_ImpExp
importExport (fromFile
);
1399 rc
= importExport
.import_config (ACE_TEXT ("testConfig.ini"));
1401 ACE_ERROR_RETURN ((LM_ERROR
,
1402 ACE_TEXT("Error Exporting (%d)\n"),
1406 // 7. Compares to original.
1407 if (fromFile
!= original
)
1408 ACE_ERROR_RETURN ((LM_ERROR
,
1409 ACE_TEXT ("Object read from file does not ")
1410 ACE_TEXT ("equal original (%d)\n"),
1414 // 7.1 Change a value and test NOT equal case
1415 change_one (original
, 101);
1416 if (fromFile
== original
)
1418 ACE_ERROR_RETURN ((LM_ERROR
,
1419 ACE_TEXT ("not pass value change test (%d)\n"),
1424 // 7.2 change value back, they should be equal now
1425 change_one (original
, 100);
1426 if (fromFile
!= original
)
1428 ACE_ERROR_RETURN ((LM_ERROR
,
1429 ACE_TEXT ("not pass value change test (%d)\n"),
1434 }// end if heap could not be opened.
1436 ACE_ERROR_RETURN ((LM_ERROR
,
1437 ACE_TEXT ("Could not open fromFile object (%d)\n"),
1442 ACE_DEBUG ((LM_DEBUG
,
1443 ACE_TEXT ("Registry Format Import/Export Works ;-)\n")));
1448 // Reads a string value from a configuration object.
1451 Config_Test::get_section_string (ACE_Configuration
& config
,
1452 ACE_Configuration_Section_Key
& SectionKey
,
1453 const ACE_TCHAR
* pszName
,
1454 ACE_TCHAR
* pszVariable
,
1457 ACE_TString StringValue
;
1459 if (config
.get_string_value (SectionKey
,
1463 ACE_OS::strncpy (pszVariable
,
1464 StringValue
.c_str (),
1466 ACE_DEBUG ((LM_DEBUG
,
1467 ACE_TEXT ("%s = %s\n"),
1473 // Reads an integer value from a configuration object (when it's
1474 // stored as a string)
1476 Config_Test::get_section_integer (ACE_Configuration
& config
,
1477 ACE_Configuration_Section_Key
& SectionKey
,
1478 const ACE_TCHAR
* pszName
,
1483 ACE_TString StringValue
;
1484 ACE_TCHAR pszString
[30];
1485 ACE_OS::strcpy (pszString
, ACE_TEXT ("0"));
1486 int IntegerValue
= 0;
1488 if (config
.get_string_value (SectionKey
,
1492 ACE_OS::strncpy (pszString
,
1493 StringValue
.c_str (),
1495 ACE_DEBUG ((LM_DEBUG
,
1496 ACE_TEXT ("%s = %s\n"),
1501 // convert to integer
1502 IntegerValue
= ACE_OS::atoi (pszString
);
1503 IntegerValue
= (IntegerValue
< nMinValue
) ? nMinValue
: IntegerValue
;
1504 IntegerValue
= (IntegerValue
> nMaxValue
) ? nMaxValue
: IntegerValue
;
1505 *nVariable
= IntegerValue
;
1508 // Reads a boolean value from a configuration object (when it's stored as a string).
1511 Config_Test::get_section_boolean (ACE_Configuration
& config
,
1512 ACE_Configuration_Section_Key
& SectionKey
,
1513 const ACE_TCHAR
* pszName
,
1516 ACE_TString StringValue
;
1517 ACE_TCHAR pszString
[10];
1518 ACE_OS::strcpy (pszString
, ACE_TEXT ("0"));
1520 if (config
.get_string_value (SectionKey
,
1524 ACE_OS::strncpy (pszString
,
1525 StringValue
.c_str (),
1527 for (ACE_TCHAR
* pSrc
= pszString
;
1528 *pSrc
!= ACE_TEXT ('\0');
1530 // Convert to uppercase
1531 if (ACE_OS::ace_islower (*pSrc
))
1532 *pSrc
= ACE_OS::ace_tolower (*pSrc
);
1534 ACE_DEBUG ((LM_DEBUG
,
1535 ACE_TEXT ("%s = %s\n"),
1539 if (ACE_OS::strcmp (pszString
,
1540 ACE_TEXT ("TRUE")) == 0)
1542 else if (ACE_OS::strcmp (pszString
,
1543 ACE_TEXT ("FALSE")) == 0)
1549 run_main (int, ACE_TCHAR
*[])
1551 ACE_START_TEST (ACE_TEXT ("Config_Test"));
1553 Config_Test manager
;
1555 if ((errors
+= manager
.testEquality ()) != 0)
1556 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Failed the equality Test\n")));
1558 if ((errors
+= manager
.testRegFormat ()) != 0)
1559 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Failed the REG Format Test\n")));
1561 if ((errors
+= manager
.testIniFormat ()) != 0)
1562 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Failed the INI Format Test\n")));
1564 if ((errors
+= run_tests ()) != 0)
1565 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Failed in run_tests\n")));
1568 return errors
== 0 ? 0 : 1;