File: Canonicalizer20010315Test.java

package info (click to toggle)
libxml-security-java 1.4.3-2%2Bdeb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 14,184 kB
  • ctags: 5,406
  • sloc: java: 41,126; xml: 22,042; sh: 196; makefile: 17
file content (1234 lines) | stat: -rw-r--r-- 46,898 bytes parent folder | download | duplicates (4)
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
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
/*
 * Copyright  1999-2004 The Apache Software Foundation.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.xml.security.test.c14n.implementations;



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.xml.security.c14n.CanonicalizationException;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
import org.apache.xml.security.test.TestUtils;
import org.apache.xml.security.test.resource.TestVectorResolver;
import org.apache.xml.security.utils.Constants;
import org.apache.xml.security.utils.IgnoreAllErrorHandler;
import org.apache.xml.security.utils.JavaUtils;
import org.apache.xml.security.utils.XMLUtils;
import org.apache.xpath.CachedXPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;


/**
 * Unit test for {@link org.apache.xml.security.c14n.implementations.Canonicalizer20010315WithXPath}
 *
 * @author Christian Geuer-Pollmann
 */
public class Canonicalizer20010315Test extends TestCase {

   /** {@link org.apache.commons.logging} logging facility */
    static org.apache.commons.logging.Log log = 
        org.apache.commons.logging.LogFactory.getLog(Canonicalizer20010315Test.class.getName());

   /**
    * Method suite
    *
    *
    */
   public static Test suite() {
      return new TestSuite(Canonicalizer20010315Test.class);
   }

   /**
    *  Constructor Canonicalizer20010315WithXPathTest
    *
    *  @param Name_
    */
   public Canonicalizer20010315Test(String Name_) {
      super(Name_);
   }

   /**
    * Method main
    *
    * @param args
    */
   public static void main(String[] args) {

      String[] testCaseName = { "-noloading",
                                Canonicalizer20010315Test.class.getName() };

      junit.textui.TestRunner.main(testCaseName);
   }

   /** Field prefix */
   private static String prefix;

   /**
    * 3.1 PIs, Comments, and Outside of Document Element
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void test31withCommentsSubtree()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri =
         "3.1: PIs, Comments, and Outside of Document Element. (commented)";


      String fileIn = prefix + "in/31_input.xml";
      String fileRef = prefix + "in/31_c14n-comments.xml";
      String fileOut = prefix + "out/xpath_31_output-comments.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;
      boolean validating = true;
      String xpath = null;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }
   /**
    * 3.1 PIs, Comments, and Outside of Document Element
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void test31withCommentsSubset()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri =
         "3.1: PIs, Comments, and Outside of Document Element. (commented)";


      String fileIn = prefix + "in/31_input.xml";
      String fileRef = prefix + "in/31_c14n-comments.xml";
      String fileOut = prefix + "out/xpath_31_output-comments.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;
      boolean validating = true;
      String xpath = Canonicalizer.XPATH_C14N_WITH_COMMENTS_SINGLE_NODE;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.1 PIs, Comments, and Outside of Document Element
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-OutsideDoc">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test31subtree()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri =
         "3.1: PIs, Comments, and Outside of Document Element. (uncommented)";
      String fileIn = prefix + "in/31_input.xml";
      String fileRef = prefix + "in/31_c14n.xml";
      String fileOut = prefix + "out/xpath_31_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = null;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }
   /**
    * 3.1 PIs, Comments, and Outside of Document Element
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-OutsideDoc">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test31subset()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri =
         "3.1: PIs, Comments, and Outside of Document Element. (uncommented)";
      String fileIn = prefix + "in/31_input.xml";
      String fileRef = prefix + "in/31_c14n.xml";
      String fileOut = prefix + "out/xpath_31_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = Canonicalizer.XPATH_C14N_WITH_COMMENTS_SINGLE_NODE;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.2 Whitespace in Document Content
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-WhitespaceInContent">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test32subtree()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri = "3.2 Whitespace in Document Content. (uncommented)";
      String fileIn = prefix + "in/32_input.xml";
      String fileRef = prefix + "in/32_c14n.xml";
      String fileOut = prefix + "out/xpath_32_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = null;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.2 Whitespace in Document Content
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-WhitespaceInContent">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test32subset()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri = "3.2 Whitespace in Document Content. (uncommented)";
      String fileIn = prefix + "in/32_input.xml";
      String fileRef = prefix + "in/32_c14n.xml";
      String fileOut = prefix + "out/xpath_32_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = Canonicalizer.XPATH_C14N_WITH_COMMENTS_SINGLE_NODE;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.3 Start and End Tags
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-SETags">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test33subtree()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri = "3.3 Start and End Tags. (uncommented)";
      String fileIn = prefix + "in/33_input.xml";
      String fileRef = prefix + "in/33_c14n.xml";
      String fileOut = prefix + "out/xpath_33_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = null;    // Canonicalizer.XPATH_C14N_OMIT_COMMENTS_SINGLE_NODE;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   public static void test33subset()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri = "3.3 Start and End Tags. (uncommented)";
      String fileIn = prefix + "in/33_input.xml";
      String fileRef = prefix + "in/33_c14n.xml";
      String fileOut = prefix + "out/xpath_33_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = Canonicalizer.XPATH_C14N_WITH_COMMENTS_SINGLE_NODE;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.4 Character Modifications and Character References
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see #test34validatingParser
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-Chars">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test34()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException
   {
   	

      String descri =
         "3.4 Character Modifications and Character References. (uncommented)";
      String fileIn = prefix + "in/34_input.xml";
      String fileRef = prefix + "in/34_c14n.xml";
      String fileOut = prefix + "out/xpath_34_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = false;
      String xpath = null;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.4 Character Modifications and Character References (patched to run on validating Parsers)
    * <P>
    * <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119"> The spec</A> states that:
    * <P>
    * Note: The last element, normId, is well-formed but violates a validity
    * constraint for attributes of type ID. For testing canonical XML
    * implementations based on validating processors, remove the line
    * containing this element from the input and canonical form. In general,
    * XML consumers should be discouraged from using this feature of XML.
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-Chars">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test34subtree()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri =
         "3.4 Character Modifications and Character References. (uncommented, patched to run on validating Parsers)";
      String fileIn = prefix + "in/34_input_validatingParser.xml";
      String fileRef = prefix + "in/34_c14n_validatingParser.xml";
      String fileOut = prefix + "out/xpath_34_output_validatingParser.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = null;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }
   /**
    * 3.4 Character Modifications and Character References (patched to run on validating Parsers)
    * <P>
    * <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119"> The spec</A> states that:
    * <P>
    * Note: The last element, normId, is well-formed but violates a validity
    * constraint for attributes of type ID. For testing canonical XML
    * implementations based on validating processors, remove the line
    * containing this element from the input and canonical form. In general,
    * XML consumers should be discouraged from using this feature of XML.
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-Chars">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test34subset()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri =
         "3.4 Character Modifications and Character References. (uncommented, patched to run on validating Parsers)";
      String fileIn = prefix + "in/34_input_validatingParser.xml";
      String fileRef = prefix + "in/34_c14n_validatingParser.xml";
      String fileOut = prefix + "out/xpath_34_output_validatingParser.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = Canonicalizer.XPATH_C14N_WITH_COMMENTS_SINGLE_NODE;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.5 Entity References
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-Entities">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test35subtree()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri = "3.5 Entity References. (uncommented)";
      String fileIn = prefix + "in/35_input.xml";
      String fileRef = prefix + "in/35_c14n.xml";
      String fileOut = prefix + "out/xpath_35_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = null;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }
   /**
    * 3.5 Entity References
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-Entities">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test35subset()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri = "3.5 Entity References. (uncommented)";
      String fileIn = prefix + "in/35_input.xml";
      String fileRef = prefix + "in/35_c14n.xml";
      String fileOut = prefix + "out/xpath_35_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = Canonicalizer.XPATH_C14N_WITH_COMMENTS_SINGLE_NODE;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.6 UTF-8 Encoding
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-UTF8">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test36subtree()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri = "3.6 UTF-8 Encoding. (uncommented)";
      String fileIn = prefix + "in/36_input.xml";
      String fileRef = prefix + "in/36_c14n.xml";
      String fileOut = prefix + "out/xpath_36_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = null;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }
   /**
    * 3.6 UTF-8 Encoding
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-UTF8">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test36subset()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri = "3.6 UTF-8 Encoding. (uncommented)";
      String fileIn = prefix + "in/36_input.xml";
      String fileRef = prefix + "in/36_c14n.xml";
      String fileOut = prefix + "out/xpath_36_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      String xpath = Canonicalizer.XPATH_C14N_WITH_COMMENTS_SINGLE_NODE;

      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.7 Document Subsets
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-DocSubsets">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test37()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String descri = "3.7 Document Subsets. (uncommented)";
      String fileIn = prefix + "in/37_input.xml";
      String fileRef = prefix + "in/37_c14n.xml";
      String fileOut = prefix + "out/xpath_37_output.xml";
      String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      boolean validating = true;
      Element xpath = null;
      DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

      dfactory.setNamespaceAware(true);

      DocumentBuilder db = dfactory.newDocumentBuilder();
      Document doc = db.newDocument();

      xpath = XMLUtils.createElementInSignatureSpace(doc, Constants._TAG_XPATH);

      xpath.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:ietf", "http://www.ietf.org");

      //J-
         String xpathFromSpec =
              "(//. | //@* | //namespace::*)"
            + "[ "
            + "self::ietf:e1 or "
            + "(parent::ietf:e1 and not(self::text() or self::e2)) or "
            + "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node()) "
            + "]";

         //J+
      xpath.appendChild(doc.createTextNode(xpathFromSpec));
      assertTrue(descri,
                 c14nAndCompare(fileIn, fileRef, fileOut, c14nURI, validating,
                                xpath));
   }

   /**
    * 3.7 Document Subsets
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @see <A HREF="http://www.w3.org/TR/2001/PR-xml-c14n-20010119#Example-DocSubsets">the example from the spec</A>
    * @throws TransformerException
    */
   public static void test37byNodeList()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      //String descri = "3.7 Document Subsets. (uncommented), c14n by NodeList";
      String fileIn = prefix + "in/37_input.xml";
      String fileRef = prefix + "in/37_c14n.xml";
      //String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
      //boolean validating = true;
      DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

      dfactory.setNamespaceAware(true);

      DocumentBuilder db = dfactory.newDocumentBuilder();
      org.xml.sax.EntityResolver resolver = new TestVectorResolver();

      db.setEntityResolver(resolver);

      Document doc = db.parse(resolver.resolveEntity(null, fileIn));
      //J-
      Element nscontext = TestUtils.createDSctx(doc, "ietf", "http://www.ietf.org");

      String xpath = "(//. | //@* | //namespace::*)"
         + "[ "
         + "self::ietf:e1 or "
         + "(parent::ietf:e1 and not(self::text() or self::e2)) or "
         + "count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node()) "
         + "]";
      //J+
      CachedXPathAPI xpathAPI = new CachedXPathAPI();

      NodeList nodes = xpathAPI.selectNodeList(doc, xpath, nscontext);
      Canonicalizer c14n =
         Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
      byte c14nBytes[] = c14n.canonicalizeXPathNodeSet(nodes);
      InputStream refStream = resolver.resolveEntity(null,
                                 fileRef).getByteStream();
      byte refBytes[] = JavaUtils.getBytesFromStream(refStream);
      assertEquals(new String(refBytes),new String(c14nBytes));
   }

   /**
    * Method testDefaultNamespaceOverridden
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    * public static void _testDefaultNamespaceOverridden()
    *       throws IOException, FileNotFoundException, SAXException,
    *              ParserConfigurationException, CanonicalizationException,
    *              InvalidCanonicalizerException, TransformerException {
    *
    *  String descri = "Default namespace overridden";
    *  String fileIn =
    *     "data/org/apache/xml/security/temp/key/retrieval-from-same-doc.xml";
    *  String fileRef =
    *     "data/org/apache/xml/security/temp/key/retrieval-from-same-doc-key.xml";
    *  String fileOut =
    *     "data/org/apache/xml/security/temp/key/retrieval-from-same-doc-key-error.xml";
    *  String c14nURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
    *  boolean validating = false;
    *  DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
    *
    *  dfactory.setNamespaceAware(true);
    *
    *  DocumentBuilder db = dfactory.newDocumentBuilder();
    *  // org.xml.sax.EntityResolver resolver = new TestVectorResolver();
    *  // db.setEntityResolver(resolver);
    *
    *  // Document doc = db.parse(resolver.resolveEntity(null, fileIn));
    *  Document doc = db.parse(fileIn);
    *  Element nscontext = doc.createElement("container");
    *
    *  nscontext.setAttribute("xmlns:ds", "http://www.w3.org/2000/09/xmldsig#");
    *  nscontext.setAttribute("xmlns:my",
    *                         "http://www.xmlsecurity.org/temp/mytempns");
    *
    *  String xpath =
    *     "//my:KeyMaterials[1]/descendant::node()[not(self::text())]";
    *  CachedXPathAPI xpathAPI = new CachedXPathAPI();
    *  NodeList nodes = xpathAPI.selectNodeList(doc, xpath, nscontext);
    *  Canonicalizer c14n =
    *     Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
    *  byte c14nBytes[] = c14n.canonicalizeXPathNodeSet(nodes);
    *  // InputStream refStream = resolver.resolveEntity(null, fileRef).getByteStream();
    *  // byte refBytes[] = JavaUtils.getBytesFromStream(refStream);
    *  byte refBytes[] = JavaUtils.getBytesFromFile(fileRef);
    *  boolean equal = JavaUtils.binaryCompare(refBytes, c14nBytes);
    *
    *  if (!equal) {
    *     JavaUtils.writeBytesToFilename(fileOut, c14nBytes);
    *     log.debug("Wrote malicious output from Test \"" + descri
    *               + "\" to file " + fileOut);
    *  }
    *
    *  assertTrue(equal);
    * }
    */

   /**
    * Note: This specification supports the recent XML plenary decision to
    * deprecate relative namespace URIs as follows: implementations of XML
    * canonicalization MUST report an operation failure on documents containing
    * relative namespace URIs. XML canonicalization MUST NOT be implemented
    * with an XML parser that converts relative URIs to absolute URIs.
    *
    * Implementations MUST report an operation failure on documents containing
    * relative namespace URIs.
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void testRelativeNSbehaviour()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      //J-
      String inputStr = ""
         + "<absolute:correct      xmlns:absolute='http://www.absolute.org/#likeVodka'>"
         + "<relative:incorrect    xmlns:relative='../cheating#away'>"
         + "</relative:incorrect>"
         + "</absolute:correct>"
         + "\n"
         + "";
      //J+
      DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

      dfactory.setNamespaceAware(true);

      DocumentBuilder db = dfactory.newDocumentBuilder();
      Document doc = db.parse(new ByteArrayInputStream(inputStr.getBytes()));
      boolean weCatchedTheRelativeNS = false;

      try {
         Canonicalizer c14n =
            Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
         c14n.canonicalizeSubtree(doc);

      } catch (CanonicalizationException cex) {

         // if we reach this point - good.
         log.debug("We catched the C14nEx, that's good: " + cex.getMessage());

         weCatchedTheRelativeNS = true;
      }

      assertTrue("We did not catch the relative namespace",
                 weCatchedTheRelativeNS);
   }

   /**
    * The XPath data model represents data using UCS characters.
    * Implementations MUST use XML processors that support UTF-8 and UTF-16
    * and translate to the UCS character domain. For UTF-16, the leading byte
    * order mark is treated as an artifact of encoding and stripped from the
    * UCS character data (subsequent zero width non-breaking spaces appearing
    * within the UTF-16 data are not removed) [UTF-16, Section 3.2]. Support
    * for ISO-8859-1 encoding is RECOMMENDED, and all other character encodings
    * are OPTIONAL.
    *
    * $todo$ implement the test
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void testTranslationFromUTF16toUTF8()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {

      String val =
         "<UTF16>The german &amp;auml (which is Unicode &amp;#xE4;):  &quot;&#xE4;&quot;</UTF16>";
      byte utf16[] = convertToUTF16(val.getBytes());
      Canonicalizer c14n =
         Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
      byte c14nBytes[] = c14n.canonicalize(utf16);
      org.xml.sax.EntityResolver resolver = new TestVectorResolver();
      InputStream refStream = resolver.resolveEntity(
         null,
            prefix + "/in/testTranslationFromUTF16toUTF8.xml")
               .getByteStream();
      byte refBytes[] = JavaUtils.getBytesFromStream(refStream);
      boolean equal = java.security.MessageDigest.isEqual(refBytes, c14nBytes);

      assertTrue("Parser does not translate to UCS character domain", equal);
   }

   /**
    * Method testXMLAttributes1
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void testXMLAttributes1()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {
      //J-
      String input = ""
         + "<included    xml:lang='de'>"
         + "<notIncluded xml:lang='de'>"
         + "<notIncluded xml:lang='uk'>"
         + "<included                 >"
         + "</included>"
         + "</notIncluded>"
         + "</notIncluded>"
         + "</included>";

      String definedOutput = ""
         + "<included xml:lang=\"de\">"
         + "<included xml:lang=\"uk\">"
         + "</included>"
         + "</included>";
      //J+
      assertTrue(doTestXMLAttributes(input, definedOutput));
   }

   /**
    * Method testXMLAttributes2
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void testXMLAttributes2()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {
      //J-
      String input = ""
         + "<included    xml:lang='uk'>"
         + "<notIncluded xml:lang='de'>"
         + "<notIncluded xml:lang='uk'>"
         + "<included                 >"
         + "</included>"
         + "</notIncluded>"
         + "</notIncluded>"
         + "</included>";

      String definedOutput = ""
         + "<included xml:lang=\"uk\">"
         + "<included xml:lang=\"uk\">"
         + "</included>"
         + "</included>";
      //J+
      assertTrue(doTestXMLAttributes(input, definedOutput));
   }

   /**
    * Method testXMLAttributes3
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void testXMLAttributes3()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {
      //J-
      String input = ""
         + "<included    xml:lang='de'>"
         + "<notIncluded xml:lang='de'>"
         + "<notIncluded xml:lang='uk'>"
         + "<included    xml:lang='de'>"
         + "</included>"
         + "</notIncluded>"
         + "</notIncluded>"
         + "</included>";

      String definedOutput = ""
         + "<included xml:lang=\"de\">"
         + "<included xml:lang=\"de\">"
         + "</included>"
         + "</included>";
      //J+
      assertTrue(doTestXMLAttributes(input, definedOutput));
   }

   /**
    * Method testXMLAttributes4
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void _testXMLAttributes4()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {
      //J-
      String input = ""
         + "<included    xml:lang='de'>"
         + "<included    xml:lang='de'>"
         + "<notIncluded xml:lang='uk'>"
         + "<included                 >"
         + "</included>"
         + "</notIncluded>"
         + "</included>"
         + "</included>";

      String definedOutput = ""
         + "<included xml:lang=\"de\">"
         + "<included>"
         + "<included xml:lang=\"uk\">"
         + "</included>"
         + "</included>"
         + "</included>";
      //J+
      assertTrue(doTestXMLAttributes(input, definedOutput));
   }

   /**
    * Method testXMLAttributes5
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void _testXMLAttributes5()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {
      //J-
      String input = ""
         + "<included                         xml:lang='de'>"
         + "<included                         xml:lang='de'>"
         + "<notIncluded xml:space='preserve' xml:lang='uk'>"
         + "<included                 >"
         + "</included>"
         + "</notIncluded>"
         + "</included>"
         + "</included>";

      String definedOutput = ""
         + "<included xml:lang=\"de\">"
         + "<included>"
         + "<included xml:lang=\"uk\" xml:space=\"preserve\">"
         + "</included>"
         + "</included>"
         + "</included>";
      //J+
      assertTrue(doTestXMLAttributes(input, definedOutput));
   }

   /**
    * Method testXMLAttributes6
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   public static void _testXMLAttributes6()
           throws IOException, FileNotFoundException, SAXException,
                  ParserConfigurationException, CanonicalizationException,
                  InvalidCanonicalizerException, TransformerException {
      //J-
      String input = ""
         + "<included   xml:space='preserve'  xml:lang='de'>"
         + "<included                         xml:lang='de'>"
         + "<notIncluded                      xml:lang='uk'>"
         + "<included>"
         + "</included>"
         + "</notIncluded>"
         + "</included>"
         + "</included>";

      String definedOutput = ""
         + "<included xml:lang=\"de\" xml:space=\"preserve\">"
         + "<included>"
         + "<included xml:lang=\"uk\" xml:space=\"preserve\">"
         + "</included>"
         + "</included>"
         + "</included>";
      //J+
      assertTrue(doTestXMLAttributes(input, definedOutput));
   }

   /**
    * Method doTestXMLAttributes
    *
    * @param input
    * @param definedOutput
    * @param writeResultsToFile
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   private static boolean doTestXMLAttributes(
           String input, String definedOutput)
              throws IOException, FileNotFoundException, SAXException,
                     ParserConfigurationException, CanonicalizationException,
                     InvalidCanonicalizerException, TransformerException {

      DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

      dfactory.setNamespaceAware(true);
      dfactory.setValidating(true);

      DocumentBuilder db = dfactory.newDocumentBuilder();

      db.setErrorHandler(new org.apache.xml.security.utils
         .IgnoreAllErrorHandler());

      Document doc = db.parse(new ByteArrayInputStream(input.getBytes()));
      Canonicalizer c14nizer =
         Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
      CachedXPathAPI xpathAPI = new CachedXPathAPI();

      //XMLUtils.circumventBug2650(doc);

      NodeList nodes =
         xpathAPI.selectNodeList(doc, "(//*[local-name()='included'] | //@*[parent::node()[local-name()='included']])");
      byte result[] = c14nizer.canonicalizeXPathNodeSet(nodes);
      byte defined[] = definedOutput.getBytes();
      assertEquals(definedOutput, new String(result));
      return java.security.MessageDigest.isEqual(defined, result);
   }

   /**
    * Method c14nAndCompare
    *
    * @param fileIn
    * @param fileRef
    * @param fileOut
    * @param c14nURI
    * @param validating
    * @param xpath
    *
    * @throws CanonicalizationException
    * @throws FileNotFoundException
    * @throws IOException
    * @throws InvalidCanonicalizerException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerException
    */
   private static boolean c14nAndCompare(
           String fileIn, String fileRef, String fileOut, String c14nURI, boolean validating, Object xpath)
              throws IOException, FileNotFoundException, SAXException,
                     ParserConfigurationException, CanonicalizationException,
                     InvalidCanonicalizerException, TransformerException {

      DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();

      dfactory.setNamespaceAware(true);
      dfactory.setValidating(validating);

      DocumentBuilder documentBuilder = dfactory.newDocumentBuilder();

      // throw away all warnings and errors
      documentBuilder.setErrorHandler(new IgnoreAllErrorHandler());

      // org.xml.sax.EntityResolver resolver = new TestVectorResolver();
      // documentBuilder.setEntityResolver(resolver);
      // Document doc = documentBuilder.parse(resolver.resolveEntity(null, fileIn));
            
      Document doc = documentBuilder.parse(fileIn);


      Canonicalizer c14n = Canonicalizer.getInstance(c14nURI);
      byte c14nBytes[] = null;

      if (xpath == null) {
         c14nBytes = c14n.canonicalizeSubtree(doc);
      } else {
         CachedXPathAPI xpathAPI = new CachedXPathAPI();
         NodeList nl = null;

         if (xpath instanceof String) {
            nl = xpathAPI.selectNodeList(doc, (String) xpath);
         } else {
            Element xpathElement = (Element) xpath;
            String xpathStr = ((Text) xpathElement.getFirstChild()).getData();

            nl = xpathAPI.selectNodeList(doc, xpathStr, xpathElement);
         }

         c14nBytes = c14n.canonicalizeXPathNodeSet(nl);
      }

      // org.xml.sax.InputSource refIs = resolver.resolveEntity(null, fileRef);
      // byte refBytes[] = JavaUtils.getBytesFromStream(refIs.getByteStream());
      byte refBytes[] = JavaUtils.getBytesFromFile(fileRef);

      // if everything is OK, result is true; we do a binary compare, byte by byte
      boolean result = java.security.MessageDigest.isEqual(refBytes, c14nBytes);

      if (result == false) {    	  
         File f = new File(fileOut);
         if (!f.exists()) {
         	File parent = new File(f.getParent());
         	parent.mkdirs();
         	f.createNewFile();
         }
         FileOutputStream fos = new FileOutputStream(f);

         fos.write(c14nBytes);
         log.debug("Wrote errornous result to file " + f.toURL().toString());
         assertEquals(new String(refBytes),new String(c14nBytes));
      }

      return result;
   }

   /**
    * This method takes the input bytes as XML Document and converts it to an
    * UTF-16 encoded XML document which is serialized to byte[] and returned.
    *
    * @param input
    *
    * @throws IOException
    * @throws ParserConfigurationException
    * @throws SAXException
    * @throws TransformerConfigurationException
    * @throws TransformerException
    */
   public static byte[] convertToUTF16(byte input[])
           throws ParserConfigurationException, IOException, SAXException,
                  TransformerConfigurationException, TransformerException {

      //String ENCODING_ISO8859_1 = "ISO-8859-1";
      //String ENCODING_UTF8 = "UTF-8";
      String ENCODING_UTF16 = "UTF-16";
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(new ByteArrayInputStream(input));
      TransformerFactory tFactory = TransformerFactory.newInstance();
      Transformer transformer = tFactory.newTransformer();

      transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING_UTF16);
      transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

      DOMSource source = new DOMSource(doc);
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      StreamResult result = new StreamResult(os);

      transformer.transform(source, result);

      return os.toByteArray();
   }

   static {
      /*
      try {
         // String wd = new File(".").toURL().toExternalForm();
         // prefix = wd.substring(0, wd.length()-2) + "data/org/apache/xml/security/c14n/";
      } catch (java.net.MalformedURLException urlEx) {
         throw new RuntimeException(urlEx.getMessage());
      }
      */
   	  prefix = "data/org/apache/xml/security/c14n/";
   	  String basedir = System.getProperty("basedir");
   	  if(basedir != null && !"".equals(basedir)) {
   		prefix = basedir + "/" + prefix;
      }
      
      org.apache.xml.security.Init.init();
   }
}