summaryrefslogtreecommitdiff
path: root/tests/Config_Test.cpp
blob: 61e7e1d2be9065606f32f972427f61363ba84a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Config_Test.cpp
//
// = DESCRIPTION
//      This is a test that makes sure various classes in
//      <ACE_Configuration> work correctly.
//
// = AUTHOR
//    Michael Searles <msearles@base16.com>,
//    Chris Hafey <chafey@stentor.com>, and
//    Jerry D. Odenwelder Jr. <jerry.o@mindspring.com>
//
// ============================================================================

#include "test_config.h"
#include "Config_Test.h"
#include "ace/Configuration_Import_Export.h"

ACE_RCSID(tests, Config_Test, "$Id$")

static int
test (ACE_Configuration *config)
{
  ACE_Configuration_Section_Key root =
    config->root_section ();

  // Add a section.
  ACE_Configuration_Section_Key testsection;

  if (config->open_section (root,
                            ACE_TEXT ("test"),
                            1,
                            testsection))
    return -2;

  // Set some values.
  else if (config->set_string_value (testsection,
                                     ACE_TEXT ("stvalue"),
                                     ACE_TEXT ("stvaluetest")))
    return -3;

  else if (config->remove_value (testsection,
                                 ACE_TEXT ("stvalue")))
    return -4;

  else if (config->set_string_value (testsection,
                                     ACE_TEXT ("stvalue"),
                                     ACE_TEXT ("stvaluetest")))
    return -3;

  else if (config->set_integer_value (testsection,
                                      ACE_TEXT ("intvalue"),
                                      42))
    return -4;

  u_char data[80];

  for (int i = 0; i < 80; i++)
    data[i] = i + 128;

  if (config->set_binary_value (testsection,
                                ACE_TEXT ("binvalue"),
                                data,
                                80))
    return -5;

  // Get the values and compare
  ACE_TString stvalue;

  if (config->get_string_value (testsection,
                                ACE_TEXT ("stvalue"),
                                stvalue))
    return -6;
  else if (stvalue != "stvaluetest")
    return -7;

  u_int intvalue;

  if (config->get_integer_value (testsection,
                                 ACE_TEXT ("intvalue"),
                                 intvalue))
    return -8;
  else if (intvalue != 42)
    return -9;

  u_char *data_out = 0;
  u_int length = 0;

  if (config->get_binary_value (testsection,
                                ACE_TEXT ("binvalue"),
                                (void*&) data_out,
                                length))
    return -10;

  // compare em
  for (int j = 0; j < 80; j++)
    if (data_out[j] != data[j])
      return -11;

  delete data_out;

  // Test iteration.
  ACE_TString name;
  ACE_Configuration::VALUETYPE type;
  u_int index = 0;
  int count = 0;

  while (!config->enumerate_values (testsection,
                                    index,
                                    name,
                                    type))
    {
      if (name == "stvalue")
        {
          if (type != ACE_Configuration::STRING)
            return -12;
          count++;
        }
      else if (name == "intvalue")
        {
          if (type != ACE_Configuration::INTEGER)
            return -13;
          count++;
        }
      else if (name == "binvalue")
        {
          if (type != ACE_Configuration::BINARY)
            return -14;
          count++;
        }
      index++;
    }

  // Make sure we got three values.
  if (index != 3 || count !=3)
    return -15;

  // Add some subsections.
  ACE_Configuration_Section_Key test2;
  ACE_Configuration_Section_Key test3;
  ACE_Configuration_Section_Key test4;

  if (config->open_section (testsection,
                            ACE_TEXT ("test2"),
                            1,
                            test2))
    return -16;
  else if (config->open_section (testsection,
                                 ACE_TEXT ("test3"),
                                 1,
                                 test3))
    return -17;
  else if (config->open_section (testsection,
                                 ACE_TEXT ("test4"),
                                 1,
                                 test4))
    return -18;

  // Test enumerate sections.
  index = 0;
  count = 0;
  while (!config->enumerate_sections (testsection,
                                      index,
                                      name))
    {
      if (name == ACE_TEXT ("test2"))
        count++;
      else if (name == ACE_TEXT ("test3"))
        count++;
      else if (name == ACE_TEXT ("test4"))
        count++;
      index++;
    }

  if (index != 3 || count != 3)
    return -19;

  // Remove a subsection
  if (config->remove_section (testsection,
                              ACE_TEXT ("test2"),
                              0))
    return -20;

  // Try to remove it again
  if (!config->remove_section (testsection,
                               ACE_TEXT ("test2"),
                               0))
    return -21;

  // Try to remove the testsection root, it should fail since it still
  // has subkeys
  if (!config->remove_section (root,
                               ACE_TEXT ("test"),
                               0))
    return -22;

  // Test find section
  ACE_Configuration_Section_Key result;

  if (config->open_section (root,
                            ACE_TEXT ("test"),
                            0,
                            result))
    return -23;

  // Now test the recursive remove.
  if (config->remove_section (root,
                              ACE_TEXT ("test"),
                              1))
    return -24;

  // Make sure its not there
  if (!config->open_section (root,
                             ACE_TEXT ("test"),
                             0,
                             testsection))
    return -25;

  return 0;
}

static int
run_tests (void)
{
#if defined (ACE_WIN32)
  // test win32 registry implementation.
  HKEY root =
    ACE_Configuration_Win32Registry::resolve_key (HKEY_LOCAL_MACHINE,
                                                  ACE_TEXT ("Software\\ACE\\test"));
  if (!root)
    return -1;

  ACE_Configuration_Win32Registry RegConfig (root);
  {
    int result = test (&RegConfig);
    if (result)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Win32 registry test failed (%d)\n", result),
                        -1);
  }

#endif /* ACE_WIN32 */
  // Test Heap version
  ACE_Configuration_Heap heap_config;

  if (heap_config.open ())
    return 0;
  {
    int result = test (&heap_config);
    if (result)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Heap Configuration test failed (%d)\n",
                         result),
                        -1);
  }

  // Test persistent heap version
  ACE_OS::unlink (ACE_TEXT ("test.reg"));
  ACE_Configuration_Heap pers_config;

  if (pers_config.open (ACE_TEXT ("test.reg")))
    return 0;
  {
    int result = test (&pers_config);
    if (result)
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Persistent Heap Configuration test failed (%d)\n",
                         result),
                        -1);
  }

  ACE_DEBUG ((LM_DEBUG, "Test passed\n"));
  return 0;
}

static int
build_config_object (ACE_Configuration& cfg)
{
  ACE_Configuration_Section_Key root = cfg.root_section ();
  ACE_Configuration_Section_Key NetworkSection;
  ACE_Configuration_Section_Key LoggerSection;
  ACE_Configuration_Section_Key BinarySection;

  if (cfg.open_section (root,
                      ACE_TEXT ("network"),
                      1,
                      NetworkSection))
    return -1;

  if (cfg.set_integer_value (NetworkSection,
                           ACE_TEXT ("TimeToLive"),
                           100))
    return -2;
  else if (cfg.set_string_value (NetworkSection,
                               ACE_TEXT ("Delay"),
                               ACE_TString ("FALSE")))
    return -3;
  else if (cfg.set_string_value (NetworkSection,
                               ACE_TEXT ("DestIPAddress"),
                               ACE_TString ("localhost")))
    return -4;
  else if (cfg.set_integer_value (NetworkSection,
                                ACE_TEXT ("DestPort"),
                                12670))
    return -5;
  else if (cfg.set_integer_value (NetworkSection,
                                ACE_TEXT ("ReconnectInterval"),
                                3))
    return -6;

  if (cfg.open_section (root,
                      ACE_TEXT ("logger"),
                      1,
                      LoggerSection))
    return -7;


  if (cfg.set_string_value (LoggerSection,
                          ACE_TEXT ("Heading"),
                          ACE_TString ("ACE - Adaptive Communication Environment")))
    return -8;
  else if (cfg.set_integer_value (LoggerSection,
                                ACE_TEXT ("SeekIndex"),
                                14))
    return -9;
  else if (cfg.set_integer_value (LoggerSection,
                                ACE_TEXT ("TraceLevel"),
                                6))
    return -10;
  else if (cfg.set_string_value (LoggerSection,
                               ACE_TEXT ("Justification"),
                               ACE_TString ("left_justified")))
    return -11;
  else if (cfg.set_string_value (LoggerSection,
                               ACE_TEXT ("LogFilePath"),
                               ACE_TString ("log/")))
    return -12;
  else if (cfg.set_string_value (LoggerSection,
                               ACE_TEXT ("TransactionFilePath"),
                               ACE_TString ("data/")))
    return -13;

  if (cfg.open_section (root,
                      ACE_TEXT ("binary"),
                      1,
                      BinarySection))
    return -14;

  u_char data[80];

  for (int i = 0; i < 80; i++)
    data[i] = i + 128;

  if (cfg.set_binary_value (BinarySection,
                          ACE_TEXT ("data"),
                          data,
                          80))
    return -15;

  return 0;
}

/*
 * Test ACE_Configuration::operator==
 */
int
Config_Test::testEquality ()
{
  // create and open 2 ACE_Configuration objects.
  ACE_Configuration_Heap  heap1;
  ACE_Configuration_Heap  heap2;
  if ((heap1.open ()) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Cannot open heap1\n"),
                        -1);

    }
  else if ((heap2.open ()) != 0)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Cannot open heap2\n"),
                        -1);
    }

  // populate them equally
  build_config_object (heap1);
  build_config_object (heap2);

  // test equality
  ACE_DEBUG ((LM_DEBUG, "The objects should equal...\n"));
  if (heap1 == heap2)
    {
      ACE_DEBUG ((LM_DEBUG, "And they do ;-)\n"));
    }
  else
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "And they do not :- (\nThe Equality Operator Failed when objects equal\n"),
                        -1);
    }

  // add a section and value to heap1
  ACE_Configuration_Section_Key root1 = heap1.root_section ();
  ACE_Configuration_Section_Key NewSection;
  if (heap1.open_section (root1,
                          ACE_TEXT ("NewSection"),
                          1,
                          NewSection))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding section to heap1\n"),
                        -1);
    }
  else if (heap1.set_integer_value (NewSection,
                                    ACE_TEXT ("TestIntValue"),
                                    100))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding value to heap1\n"),
                        -2);
    }

  // test equality
  ACE_DEBUG ((LM_DEBUG, "The objects should NOT equal...\n"));
  if (heap1 == heap2)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "They Do :- (\nThe Equality Operator Failed when lhs contains data not in rhs\n"),
                        -1);
    }
  else
    {
      ACE_DEBUG ((LM_DEBUG, "And they do not ;-)\n"));
    }

  //
  // add same section to heap2
  //
  ACE_Configuration_Section_Key root2 = heap2.root_section ();
  ACE_Configuration_Section_Key NewSection2;
  if (heap2.open_section (root2,
                          ACE_TEXT ("NewSection"),
                          1,
                          NewSection2))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding section to heap2\n"),
                        -1);
    }
  else if (heap2.set_integer_value (NewSection2,
                                    ACE_TEXT ("TestIntValue"),
                                    100))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding value to heap2\n"),
                        -2);
    }
  else if (heap2.set_integer_value (NewSection2,
                                    ACE_TEXT ("TestIntValue2"),
                                    100))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding second value to heap2\n"),
                        -2);

    }
  // test equality
  ACE_DEBUG ((LM_DEBUG, "The objects should NOT equal...\n"));
  if (heap1 == heap2)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "And They Do :- (\nThe Equality Operator Failed when rhs contains value not in lhs\n"),
                        -1);
    }
  else
    {
      ACE_DEBUG ((LM_DEBUG, "And they do not ;-)\n"));
    }

  // add new value in heap 1
  if (heap1.set_integer_value (NewSection,
                               ACE_TEXT ("TestIntValue2"),
                               100))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding second value to heap1\n"),
                        -2);
    }

  // test equality
  ACE_DEBUG ((LM_DEBUG, "The objects should be equal...\n"));
  if (heap1 == heap2)
    {
      ACE_DEBUG ((LM_DEBUG, "And they are ;-)\n"));
    }
  else
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "And they are not :- (\nThe Equality Operator Failed\n"),
                        -1);
    }

  // Add a new section to heap2
  ACE_Configuration_Section_Key AnotherNewSection2;
  if (heap2.open_section (root2,
                          ACE_TEXT ("AnotherNewSection"),
                          1,
                          AnotherNewSection2))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding second section to heap2\n"),
                        -1);
    }
  else if (heap2.set_integer_value (AnotherNewSection2,
                                    ACE_TEXT ("TestIntValue"),
                                    100))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding value in second section to heap2\n"),
                        -2);
    }

  // test equality
  ACE_DEBUG ((LM_DEBUG, "The objects should NOT equal...\n"));
  if (heap1 == heap2)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "And they do :- (\nThe Equality Operator Failed when rhs contains data not in lhs\n"),
                        -1);
    }
  else
    {
      ACE_DEBUG ((LM_DEBUG, "And they do not :-)\n"));
    }

  // add section back to heap1
  ACE_Configuration_Section_Key AnotherNewSection1;
  if (heap1.open_section (root1,
                          ACE_TEXT ("AnotherNewSection"),
                          1,
                          AnotherNewSection1))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding second section to heap1\n"),
                        -1);
    }
  else if (heap1.set_integer_value (AnotherNewSection1,
                                    ACE_TEXT ("TestIntValue"),
                                    100))
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Error adding second value to second section in heap1\n"),
                        -2);
    }

  // test equality
  ACE_DEBUG ((LM_DEBUG, "The objects should be equal...\n"));
  if (heap1 == heap2)
    {
      ACE_DEBUG ((LM_DEBUG, "And they are ;-)\n"));
    }
  else
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "And they are not :-)\nThe Equality Operator Failed\n"),
                        -1);
    }

  this->equality_tested_ = 1;
  return 0;
}

/*
 * Compare INI import data in fromFile to origional data exported (in origional)
 *
 * This compare is destructive to the origional object.
 * I realize that normally you would not do such an obscene thing but
 * this funciton has a special purpose and I know my origional is not needed
 * after calling this routine.
 * This is done because configuration objects that are imported using the INI
 * import store all data as strings. My origional has type information and I need to
 * know if the import worked.
*/
static int
iniCompare (ACE_Configuration_Heap& fromFile, ACE_Configuration_Heap& original)
{
  int rc = 1; // start by guessing they are equal

  int         sectionIndex = 0;
  ACE_TString sectionName;

  const ACE_Configuration_Section_Key& fromFileRoot  = fromFile.root_section ();
  const ACE_Configuration_Section_Key& originalRoot = original.root_section ();
  ACE_Configuration_Section_Key originalSection;
  ACE_Configuration_Section_Key fromFileSection;

  // loop through each section in the fromFile object
  while ((rc) &&
         (!fromFile.enumerate_sections (fromFileRoot,
                                        sectionIndex,
                                        sectionName)) )
    {
      // find that section in the original object
      if (original.open_section (originalRoot,
                                  sectionName.c_str (),
                                  0,
                                  originalSection) != 0)
        {
          // If the original object does not contain the section then we are not equal.
          rc = 0;
        }
      else if (fromFile.open_section (fromFileRoot,
                                      sectionName.c_str (),
                                      0,
                                      fromFileSection) != 0)
        {
          // if there is some error opening the section in the fromFile
          rc = 0;
        }
      else
        {
          // Well the sections match
          int         valueIndex = 0;
          ACE_TString valueName;
          ACE_Configuration::VALUETYPE   valueType;
          ACE_Configuration::VALUETYPE   originalType;

          // Enumerate each value in the fromFile section
          while ((rc) &&
                 (!fromFile.enumerate_values (fromFileSection,
                                              valueIndex,
                                              valueName,
                                              valueType)))
            {
              // look for the same value in the original section
              if (original.find_value (originalSection,
                                        valueName.c_str (),
                                        originalType) != 0)
                {
                  // We're not equal if the same value cannot
                  // be found in the original object.
                  rc = 0;
                }
              else
                {
                  ACE_TString fromFileString, originalString;


                  if (fromFile.get_string_value (fromFileSection,
                                                 valueName.c_str (),
                                                 fromFileString) != 0)
                    {
                      // we're not equal if we cannot get this string
                      rc = 0;
                    }
                  else if (originalType != ACE_Configuration::STRING)  // If the original type is not a string
                    {
                      // convert original data to a string.

                      if (originalType == ACE_Configuration::INTEGER)
                        {
                          u_int intValue;
                          ACE_TCHAR int_value[32];

                          if (original.get_integer_value (originalSection,
                                                           valueName.c_str (),
                                                           intValue) != 0)
                            {
                              // we're not equal if we cannot get rhs int
                              rc = 0;
                            }
                          ACE_OS::sprintf (int_value, ACE_LIB_TEXT ("%08x"), intValue);
                          originalString = int_value;
                        }
                      else if (originalType == ACE_Configuration::BINARY)
                        {

                          void* binary_data;
                          u_int binary_length;

                          if (original.get_binary_value (originalSection,
                                                          valueName.c_str (),
                                                          binary_data,
                                                          binary_length))
                            {
                              // we're not equal if we cannot get this string
                              rc = 0;
                            }
                          else
                            {
                              ACE_TCHAR bin_value[3];

                              unsigned char* ptr = (unsigned char*)binary_data;
                              while (binary_length)
                                {
                                  if (ptr != binary_data)
                                    {
                                      originalString += ACE_LIB_TEXT (",");
                                    }
                                  ACE_OS::sprintf (bin_value, ACE_LIB_TEXT ("%02x"), *ptr);
                                  originalString += bin_value;
                                  --binary_length;
                                  ++ptr;
                                }
                              delete (char *)binary_data;
                            }// end successful binary read
                        }// end if originalType was binary
                      else
                        {
                          // if the type is invalid, then go ahead and fail it.
                          rc = 0;
                        }

                    }// end if the original type was not a string.
                  else
                    {
                      if (original.get_string_value (originalSection,
                                                      valueName.c_str (),
                                                      originalString) != 0)
                        {
                          // we're not equal if we cannot get rhs string
                          rc = 0;
                        }

                    }

                  rc &= fromFileString == originalString;

                  if (rc)
                    {
                      // before we move on remove this value from the original.
                      original.remove_value (originalSection,
                                              valueName.c_str ());
                    }

                }// end else if values match.

              valueIndex++;

            }// end value while loop

          // at this point the original should have no values.
          // look for values in the original section
          valueIndex = 0;
          while ((rc) &&
                 (!original.enumerate_values (originalSection,
                                               valueIndex,
                                               valueName,
                                               originalType)))
            {
              valueIndex++;
            }// end while for rhs values not in this.

          // having a value indicates a mismatch
          rc = valueIndex == 0;

        }// end else if sections match.

      if (rc)
        {
          // before we move on remove the section from the original.
          original.remove_section (originalRoot,
                                   sectionName.c_str (),
                                   0); // do not remove subsections.
        }

      sectionIndex++;

    }// end section while loop

  // Finally, if the original has any sections, then we're not equal
  sectionIndex = 0;
  while ((rc) &&
         (!original.enumerate_sections (originalRoot,
                                         sectionIndex,
                                         sectionName)) )
    {
      sectionIndex++;
    }

  rc = sectionIndex == 0;

  return rc;
}

// Used to test INI Import Export class

int
Config_Test::testIniFormat ()
{
  int rc = 0;
  if (!this->equality_tested_)
    {
      rc = this->testEquality ();
      if (rc != 0)
        {
          ACE_DEBUG ((LM_DEBUG, "Equality Test Failed\n"));
          return rc;
        }
    }
  ACE_DEBUG ((LM_DEBUG, "Testing INI Format Import/Export\n"));
  ACE_Configuration_Heap  fromFile;

  //  1. Creates an ACE_Configuration_Heap object
  ACE_Configuration_Heap original;

  rc = original.open ();
  if (rc == 0)
    {
      rc = build_config_object (original);
      //  2. Calls build_config_object to populate
      if (rc != 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Error populating original config object (%d)\n",
                             rc),
                            -1);
        }

      //  3. Export
      ACE_Ini_ImpExp importExport (original);

      rc = importExport.export_config (ACE_TEXT ("testConfig.ini"));
      if (rc != 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Error Exporting (%d)\n",
                             rc),
                            -1);
        }

    }
  else
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Could not open original object (%d)\n",
                         rc),
                        -1);
    }


  // At this point we've successfully created, populated and written
  // the configuration object
  //  5. Creates a new ACE_Configuration_Heap object
  rc = fromFile.open ();
  if (rc == 0)
    {
      //  6. Imports
      ACE_Ini_ImpExp importExport (fromFile);

      rc = importExport.import_config (ACE_TEXT ("testConfig.ini"));
      if (rc != 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Error Exporting (%d)\n",
                             rc),
                            -1);
        }

      //  7. Compares to original.
      // This is a special compare since files imported using the
      // INI file import do not contain type information
      //
      // NOTE: After this call the original object will be invalid!!!
      //
      if (!iniCompare (fromFile, original))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Object read from file does not equal original (%d)\n",
                             rc),
                            -1);

        }
    }// end if heap could not be opened.
  else
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Could not open fromFile object (%d)\n",
                         rc),
                        -1);
    }

  //  8. Calls old "read_config" methods on the new object

  int       nTimeToLive;
  int       bDelay;
  int       nDestPort;
  int       nReconnectInterval;
  int       nTraceLevel;

  ACE_TCHAR pszDestIPAddress[TEST_MAX_STRING];
  ACE_TCHAR pszLogFilePath[TEST_MAX_STRING];
  ACE_TCHAR pszTransactionFilePath[TEST_MAX_STRING];
  ACE_TCHAR pszHeading[TEST_MAX_STRING];
  ACE_TCHAR pszJustification[TEST_MAX_STRING];

  ACE_Configuration_Section_Key root = fromFile.root_section ();

  // Process [network] section
  ACE_Configuration_Section_Key NetworkSection;
  if (fromFile.open_section (root,
                             ACE_TEXT ("network"),
                             1,
                             NetworkSection) == 0)
    {
      this->get_section_integer (fromFile,
                                 NetworkSection,
                                 ACE_TEXT ("TimeToLive"),
                                 &nTimeToLive,
                                 1,
                                 20);

      this->get_section_boolean (fromFile,
                                 NetworkSection,
                                 ACE_TEXT ("Delay"),
                                 &bDelay);

      this->get_section_string (fromFile,
                                NetworkSection,
                                ACE_TEXT ("DestIPAddress"),
                                pszDestIPAddress,
                                TEST_MAX_STRING);

      this->get_section_integer (fromFile,
                                 NetworkSection,
                                 ACE_TEXT ("DestPort"),
                                 &nDestPort,
                                 0,
                                 65535);

      this->get_section_integer (fromFile,
                                 NetworkSection,
                                 ACE_TEXT ("ReconnectInterval"),
                                 &nReconnectInterval,
                                 0,
                                 65535);
    }// end of "network" section

  // Process [logger] section
  ACE_Configuration_Section_Key LoggerSection;
  if (fromFile.open_section (root,
                             ACE_TEXT ("logger"),
                             1,
                             LoggerSection) == 0)
    {
      this->get_section_string (fromFile,
                                LoggerSection,
                                ACE_TEXT ("Heading"),
                                pszHeading,
                                TEST_MAX_STRING);
      this->get_section_integer (fromFile,
                                 LoggerSection,
                                 ACE_TEXT ("TraceLevel"),
                                 &nTraceLevel,
                                 1,
                                 20);
      this->get_section_string (fromFile,
                                LoggerSection,
                                ACE_TEXT ("Justification"),
                                pszJustification,
                                TEST_MAX_STRING);
      this->get_section_string (fromFile,
                                LoggerSection,
                                ACE_TEXT ("LogFilePath"),
                                pszLogFilePath,
                                TEST_MAX_STRING);
      this->get_section_string (fromFile,
                                LoggerSection,
                                ACE_TEXT ("TransactionFilePath"),
                                pszTransactionFilePath,
                                TEST_MAX_STRING);
    }// end of "logger" section

  if (!rc)
    ACE_DEBUG ((LM_DEBUG, "INI Format Import/Export Works ;-)\n"));
  return rc;
}

// Used to test registry Import Export class

int
Config_Test::testRegFormat ()
{
  int rc = 0;
  if (!this->equality_tested_)
    {
      rc = this->testEquality ();
      if (rc != 0)
        {
          ACE_DEBUG ((LM_DEBUG, "Equality Test Failed\n"));
          return rc;
        }

    }
  ACE_DEBUG ((LM_DEBUG, "Testing Registry Format Import/Export\n"));
  ACE_Configuration_Heap  fromFile;

  //  1. Creates an ACE_Configuration_Heap object
  ACE_Configuration_Heap original;

  rc = original.open ();
  if (rc == 0)
    {
      //  2. Calls build_config_object to populate
      rc = build_config_object (original);
      if (rc != 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Error populating original config object (%d)\n",
                             rc),
                            -1);
        }

      //  3. Export
      ACE_Registry_ImpExp importExport (original);

      rc = importExport.export_config (ACE_TEXT ("testConfig.ini"));
      if (rc != 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Error Exporting (%d)\n",
                             rc),
                            -1);
        }

    }
  else
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Could not open original object (%d)\n",
                         rc),
                        -1);
    }


  // At this point we've successfully created, populated and written
  // the configuration object
  //  5. Creates a new ACE_Configuration_Heap object
  rc = fromFile.open ();
  if (rc == 0)
    {
      //  6. Imports
      ACE_Registry_ImpExp importExport (fromFile);

      rc = importExport.import_config (ACE_TEXT ("testConfig.ini"));
      if (rc != 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Error Exporting (%d)\n",
                             rc),
                            -1);
        }

      //  7. Compares to original.
      if (fromFile != original)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Object read from file does not equal original (%d)\n",
                             rc),
                            -1);

        }
    }// end if heap could not be opened.
  else
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         "Could not open fromFile object (%d)\n",
                         rc),
                        -1);
    }

  if (!rc)
    ACE_DEBUG ((LM_DEBUG, "Registry Format Import/Export Works ;-)\n"));
  return rc;
}


// Reads a string value from a configuration object.

void
Config_Test::get_section_string (ACE_Configuration&             config,
                                 ACE_Configuration_Section_Key& SectionKey,
                                 const ACE_TCHAR*               pszName,
                                 ACE_TCHAR*                     pszVariable,
                                 int                            nMaxLength)
{
  ACE_TString StringValue;

  if (config.get_string_value (SectionKey,
                               pszName,
                               StringValue) == 0)
    {
      ACE_OS::strncpy (pszVariable,
                       StringValue.c_str (),
                       nMaxLength);
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s = %s\n"),
                  pszName,
                  pszVariable));
    }
}

// Reads an integer value from a congifuration object (when it's stored as a string)
void
Config_Test::get_section_integer (ACE_Configuration&             config,
                                  ACE_Configuration_Section_Key& SectionKey,
                                  const ACE_TCHAR*               pszName,
                                  int*                           nVariable,
                                  int                            nMinValue,
                                  int                            nMaxValue)
{
  ACE_TString StringValue;
  ACE_TCHAR pszString[30];
  ACE_OS::strcpy (pszString, ACE_TEXT ("0"));
  int IntegerValue = 0;

  if (config.get_string_value (SectionKey,
                               pszName,
                               StringValue) == 0)
    {
      ACE_OS::strncpy (pszString,
                       StringValue.c_str (),
                       30);
      ACE_DEBUG ((LM_DEBUG,
                  "%s = %s\n",
                  pszName,
                  pszString));
    }

  // convert to integer
  IntegerValue = ACE_OS::atoi (pszString);
  IntegerValue = (IntegerValue < nMinValue) ? nMinValue : IntegerValue;
  IntegerValue = (IntegerValue > nMaxValue) ? nMaxValue : IntegerValue;
  *nVariable = IntegerValue;
}

// Reads a boolean value from a configuration object (when it's stored as a string).

void
Config_Test::get_section_boolean (ACE_Configuration&             config,
                                  ACE_Configuration_Section_Key& SectionKey,
                                  const ACE_TCHAR*               pszName,
                                  int*                           pVariable)
{
  ACE_TString StringValue;
  ACE_TCHAR pszString[10];
  ACE_OS::strcpy (pszString, ACE_TEXT ("0"));

  if (config.get_string_value (SectionKey,
                               pszName,
                               StringValue) == 0)
    {
      ACE_OS::strncpy (pszString,
                       StringValue.c_str (),
                       10);
      for (ACE_TCHAR* pSrc = pszString;
           *pSrc != ACE_TEXT ('\0');
           pSrc++)
        // Convert to uppercase
        if (islower (*pSrc))
          *pSrc = tolower (*pSrc);

      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("%s = %s\n"),
                  pszName,
                  pszString));

      if (ACE_OS::strcmp (pszString,
                          ACE_TEXT ("TRUE")) == 0)
        *pVariable = 1;
      else if (ACE_OS::strcmp (pszString,
                               ACE_TEXT ("FALSE")) == 0)
        *pVariable = 0;
    }
}

int
main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Config_Test"));

  Config_Test manager;

  if (manager.testEquality () != 0)
    ACE_DEBUG ((LM_DEBUG, "Failed the equality Test\n"));

  if (manager.testRegFormat () != 0)
    ACE_DEBUG ((LM_DEBUG, "Failed the REG Format Test\n"));

  if (manager.testIniFormat () != 0)
    ACE_DEBUG ((LM_DEBUG, "Failed the INI Format Test\n"));

  run_tests ();

  ACE_END_TEST;
  return 0;
}