KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > VerifierTest


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.tests;
23
24 import nu.xom.Attribute;
25 import nu.xom.DocType;
26 import nu.xom.Element;
27 import nu.xom.IllegalDataException;
28 import nu.xom.IllegalNameException;
29 import nu.xom.MalformedURIException;
30 import nu.xom.Text;
31
32 import org.apache.xerces.util.XMLChar;
33
34 import com.ibm.icu.text.UTF16;
35
36 /**
37  * <p>
38  * Tests to make sure name and character rules are enforced.
39  * The rules are tested by comparison with the rules in
40  * the org.apache.xerces.util.XMLChar class.
41  * This is an undocumented class so this is potentially dangerous
42  * in the long run. it also means the tests depend on Xerces 2
43  * specifically. However, this dependence does not extend into the
44  * core API.
45  * </p>
46  *
47  * @author Elliotte Rusty Harold
48  * @version 1.0
49  *
50  */

51 public class VerifierTest extends XOMTestCase {
52  
53     private final static char[] subdelims = {'!', '$', '&', '\'', '(', ')' , '*', '+', ',', ';', '='};
54     private final static char[] unreserved = {'-', '.', '_', '~'};
55     private final static char[] unwise = {'{', '}', '|', '\\', '^', '[', ']', '`'};
56     private final static char[] delims = {'<', '>', '#', '%', '^', '"'};
57     
58     public VerifierTest(String JavaDoc name) {
59         super(name);
60     }
61
62     
63     public void testElementNames() {
64         
65         for (char c = 0; c < 65535; c++) {
66
67             if (XMLChar.isNCNameStart(c)) {
68                String JavaDoc name = String.valueOf(c);
69                Element e = new Element(name);
70                assertEquals(name, e.getLocalName());
71             }
72             else {
73                try {
74                    new Element(String.valueOf(c));
75                    fail("Allowed illegal name start character "
76                      + Integer.toHexString(c) + " in element name");
77                }
78                catch (IllegalNameException success) {
79                    assertNotNull(success.getMessage());
80                }
81             }
82             
83             if (XMLChar.isNCName(c)) {
84                String JavaDoc name = "a" + c;
85                Element e = new Element(name);
86                assertEquals(name, e.getLocalName());
87             }
88             else {
89                try {
90                    new Element(String.valueOf(c));
91                    fail("Allowed illegal character "
92                      + Integer.toHexString(c) + " in element name");
93                }
94                catch (IllegalNameException success) {
95                    assertNotNull(success.getMessage());
96                    assertEquals(String.valueOf(c), success.getData());
97                }
98             }
99             
100         }
101         
102     }
103     
104     
105     // From IRI draft:
106
/* ucschar = %xA0-D7FF / %xF900-FDCF / %xFDF0-FFEF /
107            / %x10000-1FFFD / %x20000-2FFFD / %x30000-3FFFD
108            / %x40000-4FFFD / %x50000-5FFFD / %x60000-6FFFD
109            / %x70000-7FFFD / %x80000-8FFFD / %x90000-9FFFD
110            / %xA0000-AFFFD / %xB0000-BFFFD / %xC0000-CFFFD
111            / %xD0000-DFFFD / %xE1000-EFFFD */

112   
113     // From RFC 2396 reallowed into IRIs
114
// "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
115
public void testLegalIRIs() {
116         
117         int[] legalChars = {
118           '{', '}', '<', '>', '"', '|', '\\', '^', '`', '\u007F',
119           0xA0, 0xD7FF, 0xF900, 0xFDCF, 0xFDF0,
120           0xFFEF, 0x10000, 0x1FFFD, 0x20000, 0x2FFFD, 0x30000,
121           0x3FFFD, 0x40000, 0x4FFFD, 0x50000, 0x5FFFD, 0x60000,
122           0x6FFFD, 0x70000, 0x7FFFD, 0x80000, 0x8FFFD, 0x90000,
123           0x9FFFD, 0xA0000, 0xAFFFD, 0xB0000, 0xBFFFD, 0xC0000,
124           0xCFFFD, 0xD0000, 0xDFFFD, 0xE1000, 0xEFFFD, 0xCFFFD};
125         
126         Element element = new Element("test");
127         for (int i = 0; i < legalChars.length; i++) {
128             String JavaDoc utf16 = convertToUTF16(legalChars[i]);
129             String JavaDoc url = "http://www.example.com/" + utf16 + ".xml";
130             element.addAttribute(new Attribute("xml:base",
131               "http://www.w3.org/XML/1998/namespace", url));
132             assertEquals(url, element.getAttributeValue("base",
133               "http://www.w3.org/XML/1998/namespace"));
134         }
135         
136     }
137     
138     
139     public void testAllASCIILettersAllowedToBeginSchemeNames() {
140         
141         Element e = new Element("e");
142         
143         for (char c = 'A'; c <= 'Z'; c++) {
144             String JavaDoc uri = c + "scheme:schemeSpecificData";
145             e.setNamespaceURI(uri);
146             assertEquals(uri, e.getNamespaceURI());
147         }
148         
149         for (char c = 'a'; c <= 'z'; c++) {
150             String JavaDoc uri = c + "scheme:schemeSpecificData";
151             e.setNamespaceURI(uri);
152             assertEquals(uri, e.getNamespaceURI());
153         }
154         
155     }
156
157     
158     public void testAllASCIILettersAllowedInSchemeNames() {
159         
160         Element e = new Element("e");
161         
162         for (char c = 'A'; c <= 'Z'; c++) {
163             String JavaDoc uri = "scheme" + c + ":schemeSpecificData";
164             e.setNamespaceURI(uri);
165             assertEquals(uri, e.getNamespaceURI());
166         }
167         
168         for (char c = 'a'; c <= 'z'; c++) {
169             String JavaDoc uri = "scheme" + c + ":schemeSpecificData";
170             e.setNamespaceURI(uri);
171             assertEquals(uri, e.getNamespaceURI());
172         }
173         
174     }
175     
176     
177     public void testAllASCIILettersAllowedInQueryStrings() {
178         
179         Element e = new Element("e");
180         
181         for (char c = 'A'; c <= 'Z'; c++) {
182             String JavaDoc uri = "http://www.example.com/?name=" + c;
183             e.setNamespaceURI(uri);
184             assertEquals(uri, e.getNamespaceURI());
185         }
186         
187         for (char c = 'a'; c <= 'z'; c++) {
188             String JavaDoc uri = "http://www.example.com/?name=" + c;
189             e.setNamespaceURI(uri);
190             assertEquals(uri, e.getNamespaceURI());
191         }
192         
193     }
194     
195     
196     public void testAllASCIIDigitsAllowedInQueryStrings() {
197         
198         Element e = new Element("e");
199         
200         for (char c = '0'; c <= '9'; c++) {
201             String JavaDoc uri = "http://www.example.com/?value=" + c;
202             e.setNamespaceURI(uri);
203             assertEquals(uri, e.getNamespaceURI());
204         }
205         
206     }
207     
208     
209     public void testSlashAllowedInQueryString() {
210         
211         Element e = new Element("e");
212         
213         String JavaDoc uri = "http://www.example.com/?path=/home/elharo/docs/";
214         e.setNamespaceURI(uri);
215         assertEquals(uri, e.getNamespaceURI());
216         
217     }
218     
219     
220     public void testQuestionMarkAllowedInQueryString() {
221         
222         Element e = new Element("e");
223         
224         String JavaDoc uri = "http://www.example.com/?path=?home?elharo?docs?";
225         e.setNamespaceURI(uri);
226         assertEquals(uri, e.getNamespaceURI());
227         
228     }
229     
230     
231     public void testColonAllowedInQueryString() {
232         
233         Element e = new Element("e");
234         
235         String JavaDoc uri = "http://www.example.com/?path=:home:elharo:docs:";
236         e.setNamespaceURI(uri);
237         assertEquals(uri, e.getNamespaceURI());
238         
239     }
240     
241     
242     public void testAtSignAllowedInQueryString() {
243         
244         Element e = new Element("e");
245         
246         String JavaDoc uri = "http://www.example.com/?path=@home@elharo@docs@";
247         e.setNamespaceURI(uri);
248         assertEquals(uri, e.getNamespaceURI());
249         
250     }
251     
252     
253     public void testNonASCIICharactersNotAllowedInQueryStrings() {
254         
255         Element e = new Element("e");
256         
257         for (char c = 128; c <= 1024; c++) {
258             String JavaDoc uri = "http://www.example.com/?value=" + c;
259             try {
260                 e.setNamespaceURI(uri);
261                 fail("Allowed unescaped non-ASCII character " + c + " in query string");
262             }
263             catch (MalformedURIException success) {
264                 assertEquals(uri, success.getData());
265             }
266         }
267         
268     }
269
270     
271     public void testDelimsNotAllowedInQueryStrings() {
272         
273         Element e = new Element("e");
274         
275         for (int i = 0; i < delims.length; i++) {
276             String JavaDoc uri = "http://www.example.com/?value=" + delims[i] + "#Must_Use_Fragment_ID";
277             try {
278                 e.setNamespaceURI(uri);
279                 fail("Allowed delimiter character " + delims[i] + " in query string");
280             }
281             catch (MalformedURIException success) {
282                 assertEquals(uri, success.getData());
283             }
284         }
285         
286     }
287
288     
289     public void testUnwiseCharactersNotAllowedInQueryStrings() {
290         
291         Element e = new Element("e");
292         
293         for (int i = 0; i < unwise.length; i++) {
294             String JavaDoc uri = "http://www.example.com/?value=" + unwise[i];
295             try {
296                 e.setNamespaceURI(uri);
297                 fail("Allowed unwise character " + unwise[i] + " in query string");
298             }
299             catch (MalformedURIException success) {
300                 assertEquals(uri, success.getData());
301             }
302         }
303         
304     }
305
306     
307     public void testUnwiseCharactersNotAllowedInUserInfo() {
308         
309         Element e = new Element("e");
310         
311         for (int i = 0; i < unwise.length; i++) {
312             String JavaDoc uri = "http://user" + unwise[i] + "name@www.example.com/?value=" + unwise[i];
313             try {
314                 e.setNamespaceURI(uri);
315                 fail("Allowed unwise character " + unwise[i] + " in user info");
316             }
317             catch (MalformedURIException success) {
318                 assertEquals(uri, success.getData());
319             }
320         }
321         
322     }
323
324     
325     public void testUnwiseCharactersNotAllowedInHost() {
326         
327         Element e = new Element("e");
328         
329         for (int i = 0; i < unwise.length; i++) {
330             String JavaDoc uri = "http://u" + unwise[i] + "www.example.com/";
331             try {
332                 e.setNamespaceURI(uri);
333                 fail("Allowed unwise character " + unwise[i] + " in host");
334             }
335             catch (MalformedURIException success) {
336                 assertEquals(uri, success.getData());
337             }
338         }
339         
340     }
341
342     
343     public void testDelimsNotAllowedInHost() {
344         
345         Element e = new Element("e");
346         
347         for (int i = 0; i < delims.length; i++) {
348             String JavaDoc uri = "http://u" + delims[i] + "www.example.com/#value";
349             try {
350                 e.setNamespaceURI(uri);
351                 fail("Allowed unwise character " + delims[i] + " in host");
352             }
353             catch (MalformedURIException success) {
354                 assertEquals(uri, success.getData());
355             }
356         }
357         
358     }
359
360     
361     public void testUnwiseCharactersNotAllowedInPath() {
362         
363         Element e = new Element("e");
364         
365         for (int i = 0; i < unwise.length; i++) {
366             String JavaDoc uri = "http://www.example.com/path" + unwise[i] + "/path";
367             try {
368                 e.setNamespaceURI(uri);
369                 fail("Allowed unwise character " + unwise[i] + " in path");
370             }
371             catch (MalformedURIException success) {
372                 assertEquals(uri, success.getData());
373             }
374         }
375         
376     }
377
378     
379     public void testAllASCIILettersAllowedInHostNames() {
380         
381         Element e = new Element("e");
382         
383         for (char c = 'A'; c <= 'Z'; c++) {
384             String JavaDoc uri = "http://" + c + ".com/";
385             e.setNamespaceURI(uri);
386             assertEquals(uri, e.getNamespaceURI());
387         }
388         
389         for (char c = 'a'; c <= 'z'; c++) {
390             String JavaDoc uri = "http://" + c + ".com/";
391             e.setNamespaceURI(uri);
392             assertEquals(uri, e.getNamespaceURI());
393         }
394         
395     }
396     
397     
398     public void testAllASCIIDigitsAllowedInHostNames() {
399         
400         Element e = new Element("e");
401         
402         for (char c = '0'; c <= '9'; c++) {
403             String JavaDoc uri = "http://c" + c + ".com/";
404             e.setNamespaceURI(uri);
405             assertEquals(uri, e.getNamespaceURI());
406         }
407         
408     }
409     
410     
411     public void testNonASCIICharactersNotAllowedInHostNames() {
412         
413         Element e = new Element("e");
414         
415         for (char c = 128; c <= 1024; c++) {
416             String JavaDoc uri = "http://c" + c + ".com/";
417             try {
418                 e.setNamespaceURI(uri);
419                 fail("Allowed unescaped non-ASCII character " + c + " in host name");
420             }
421             catch (MalformedURIException success) {
422                 assertEquals(uri, success.getData());
423             }
424         }
425         
426     }
427
428     
429     public void testAllASCIILettersAllowedInUserInfo() {
430         
431         Element e = new Element("e");
432         
433         for (char c = 'A'; c <= 'Z'; c++) {
434             String JavaDoc uri = "http://" + c + "@c.com/";
435             e.setNamespaceURI(uri);
436             assertEquals(uri, e.getNamespaceURI());
437         }
438         
439         for (char c = 'a'; c <= 'z'; c++) {
440             String JavaDoc uri = "http://" + c + "@c.com/";
441             e.setNamespaceURI(uri);
442             assertEquals(uri, e.getNamespaceURI());
443         }
444         
445     }
446     
447     
448     public void testAllSubDelimsAllowedInUserInfo() {
449         
450         Element e = new Element("e");
451         
452         for (int i = 0; i < subdelims.length; i++) {
453             String JavaDoc uri = "http://c" + subdelims[i] + "x@c.com/";
454             e.setNamespaceURI(uri);
455             assertEquals(uri, e.getNamespaceURI());
456         }
457         
458     }
459     
460     
461     public void testAllSubDelimsAllowedInPath() {
462         
463         Element e = new Element("e");
464         
465         for (int i = 0; i < subdelims.length; i++) {
466             String JavaDoc uri = "http://cc.com/path" + subdelims[i] +".html";
467             e.setNamespaceURI(uri);
468             assertEquals(uri, e.getNamespaceURI());
469         }
470         
471     }
472     
473     
474     public void testAllUnreservedPunctuationMarksAllowedInUserInfo() {
475         
476         Element e = new Element("e");
477         
478         for (int i = 0; i < unreserved.length; i++) {
479             String JavaDoc uri = "http://c" + unreserved[i] + "x@c.com/";
480             e.setNamespaceURI(uri);
481             assertEquals(uri, e.getNamespaceURI());
482         }
483         
484     }
485     
486     
487     public void testAllUnreservedPunctuationMarksAllowedInHost() {
488         
489         Element e = new Element("e");
490         
491         for (int i = 0; i < unreserved.length; i++) {
492             String JavaDoc uri = "http://c" + unreserved[i] + "xc.com/";
493             e.setNamespaceURI(uri);
494             assertEquals(uri, e.getNamespaceURI());
495         }
496         
497     }
498     
499     
500     public void testAllSubDelimsAllowedInQueryString() {
501         
502         Element e = new Element("e");
503         
504         for (int i = 0; i < subdelims.length; i++) {
505             String JavaDoc uri = "http://cx@c.com/?name=" + subdelims[i];
506             e.setNamespaceURI(uri);
507             assertEquals(uri, e.getNamespaceURI());
508         }
509         
510     }
511     
512     
513     public void testAllSubDelimsAllowedInHost() {
514         
515         Element e = new Element("e");
516         
517         for (int i = 0; i < subdelims.length; i++) {
518             String JavaDoc uri = "http://cx" + subdelims[i] + "c.com/";
519             e.setNamespaceURI(uri);
520             assertEquals(uri, e.getNamespaceURI());
521         }
522         
523     }
524     
525     
526     public void testAllUnreservedPunctuationMarksAllowedInQueryString() {
527         
528         Element e = new Element("e");
529         
530         for (int i = 0; i < unreserved.length; i++) {
531             String JavaDoc uri = "http://cx@c.com/?name=" + unreserved[i];
532             e.setNamespaceURI(uri);
533             assertEquals(uri, e.getNamespaceURI());
534         }
535         
536     }
537     
538     
539     public void testAllASCIIDigitsAllowedInUserInfo() {
540         
541         Element e = new Element("e");
542         
543         for (char c = '0'; c <= '9'; c++) {
544             String JavaDoc uri = "http://" + c + "@c.com/";
545             e.setNamespaceURI(uri);
546             assertEquals(uri, e.getNamespaceURI());
547         }
548         
549     }
550     
551     
552     public void testNonASCIICharactersNotAllowedInUserInfo() {
553         
554         Element e = new Element("e");
555         
556         for (char c = 128; c <= 1024; c++) {
557             String JavaDoc uri = "http://" + c + "@c.com/";
558             try {
559                 e.setNamespaceURI(uri);
560                 fail("Allowed unescaped non-ASCII character " + c + " in user info");
561             }
562             catch (MalformedURIException success) {
563                 assertEquals(uri, success.getData());
564             }
565         }
566         
567     }
568
569     
570     public void testDelimCharactersNotAllowedInUserInfo() {
571         
572         Element e = new Element("e");
573         
574         for (int i = 0; i < delims.length; i++) {
575             String JavaDoc uri = "http://c" + delims[i] + "c@c.com/?name=value#fragID";
576             try {
577                 e.setNamespaceURI(uri);
578                 fail("Allowed delim character " + delims[i] + " in user info");
579             }
580             catch (MalformedURIException success) {
581                 assertEquals(uri, success.getData());
582             }
583         }
584         
585     }
586     
587     
588     public void testMalformedURI() {
589         
590         Element e = new Element("e");
591     
592         String JavaDoc uri = "http://c#c@c.com/?name=value#fragID";
593         try {
594             e.setNamespaceURI(uri);
595             fail("Allowed http://c#c@c.com/?name=value#fragID as URI");
596         }
597         catch (MalformedURIException success) {
598             assertEquals(uri, success.getData());
599         }
600             
601     }
602
603     
604     public void testFragmentIDContainsQuestionMark() {
605         
606         Element e = new Element("e");
607     
608         String JavaDoc uri = "http://cc@c.com/?name=value#fragID?home/?elharo?";
609         e.setNamespaceURI(uri);
610         assertEquals(uri, e.getNamespaceURI());
611             
612         uri = "http://cc@c.com/#fragID?name=value";
613         e.setNamespaceURI(uri);
614         assertEquals(uri, e.getNamespaceURI());
615             
616     }
617
618     
619     public void testFragmentIDContainsFirstColon() {
620         
621         Element e = new Element("e");
622     
623         String JavaDoc uri = "http://c.com/#fragID:home";
624         e.setNamespaceURI(uri);
625         assertEquals(uri, e.getNamespaceURI());
626             
627         uri = "http://c.com/#fragID:home@eharo.com/somewhere";
628         e.setNamespaceURI(uri);
629         assertEquals(uri, e.getNamespaceURI());
630             
631     }
632
633     
634     public void testEmptyHostAllowed() {
635         
636         Element e = new Element("e");
637     
638         String JavaDoc uri = "scheme://elharo@:80/data";
639         e.setNamespaceURI(uri);
640         assertEquals(uri, e.getNamespaceURI());
641             
642     }
643
644     
645     public void testC0ControlsNotAllowedInUserInfo() {
646         
647         Element e = new Element("e");
648         
649         for (char c = 0; c <= ' '; c++) {
650             String JavaDoc uri = "http://" + c + "@c.com/";
651             try {
652                 e.setNamespaceURI(uri);
653                 fail("Allowed C0 control 0x" + Integer.toHexString(c) + " in user info");
654             }
655             catch (MalformedURIException success) {
656                 assertEquals(uri, success.getData());
657             }
658         }
659         
660     }
661
662     
663     public void testC0ControlsNotAllowedInPath() {
664         
665         Element e = new Element("e");
666         
667         for (char c = 0; c <= ' '; c++) {
668             String JavaDoc uri = "http://www.example.com/test/" + c + "data/";
669             try {
670                 e.setNamespaceURI(uri);
671                 fail("Allowed C0 control 0x" + Integer.toHexString(c) + " in path");
672             }
673             catch (MalformedURIException success) {
674                 assertEquals(uri, success.getData());
675             }
676         }
677         
678     }
679
680     
681     public void testC0ControlsNotAllowedInQueryString() {
682         
683         Element e = new Element("e");
684         
685         for (char c = 0; c <= ' '; c++) {
686             String JavaDoc uri = "http://www.c.com/?name=" + c + "&value=7";
687             try {
688                 e.setNamespaceURI(uri);
689                 fail("Allowed C0 control 0x" + Integer.toHexString(c) + " in query string");
690             }
691             catch (MalformedURIException success) {
692                 assertEquals(uri, success.getData());
693             }
694         }
695         
696     }
697
698     
699     public void testHostNameTooLong() {
700      
701         StringBuffer JavaDoc uri = new StringBuffer JavaDoc("http://");
702         for (int i = 0; i < 255; i++) uri.append('c');
703         uri.append(".com/");
704         Element e = new Element("e");
705         try {
706             e.setNamespaceURI(uri.toString());
707             fail("Allowed excessively long host name");
708         }
709         catch (MalformedURIException success) {
710             assertNotNull(success.getMessage());
711         }
712         
713     }
714     
715     
716     public void testSymbolsNotAllowedInSchemeNames() {
717         
718         Element e = new Element("e");
719         
720         char[] disallowed = { ';', '@', '&', '=', '$', ',', '"', '?', '#', '/', '\\', '|',
721                  '_', '!', '~', '*', '\'', '(', ')', '<', '>', '[', ']', '{', '}', '^', '`'};
722         
723         for (int i = 0; i < disallowed.length; i++) {
724             String JavaDoc uri = "scheme" + disallowed[i] + ":schemeSpecificData";
725             try {
726                 e.setNamespaceURI(uri);
727                 fail("allowed " + uri + " as namespace URI");
728             }
729             catch (MalformedURIException success) {
730                 assertEquals(uri, success.getData());
731             }
732         }
733         
734     }
735     
736     
737     public void testNonASCIILettersNotAllowedToBeginSchemeNames() {
738         
739         Element e = new Element("e");
740         
741         for (char c = 'Z' +1; c < 'a'; c++) {
742             String JavaDoc uri = c + "scheme:schemeSpecificData";
743             try {
744                 e.setNamespaceURI(uri);
745                 fail("allowed " + uri + " as namespace URI");
746             }
747             catch (MalformedURIException success) {
748                 assertEquals(uri, success.getData());
749             }
750         }
751         
752     }
753
754     
755     public void testBadHexEscapeInQueryString() {
756         
757         Element e = new Element("e");
758         
759         String JavaDoc uri = "scheme:schemeSpecificData?test%5test";
760         try {
761             e.setNamespaceURI(uri);
762             fail("allowed " + uri + " as namespace URI");
763         }
764         catch (MalformedURIException success) {
765             assertEquals(uri, success.getData());
766         }
767         
768         uri = "scheme:schemeSpecificData?test%5";
769         try {
770             e.setNamespaceURI(uri);
771             fail("allowed " + uri + " as namespace URI");
772         }
773         catch (MalformedURIException success) {
774             assertEquals(uri, success.getData());
775         }
776         
777     }
778     
779
780     public void testHexEscapeInUserInfo() {
781         
782         Element e = new Element("e");
783         
784         String JavaDoc uri = "scheme://user%C3%80TED@www.example.com/";
785         e.setNamespaceURI(uri);
786         assertEquals(uri, e.getNamespaceURI());
787         
788     }
789
790     
791     public void testHexEscapeInHost() {
792         
793         Element e = new Element("e");
794         
795         String JavaDoc uri = "scheme://user%C3%80www.example.com/";
796         e.setNamespaceURI(uri);
797         assertEquals(uri, e.getNamespaceURI());
798         
799     }
800
801     
802     public void testBadHexEscapeInUserInfo() {
803         
804         Element e = new Element("e");
805         
806         String JavaDoc uri = "scheme://user%5TED@www.example.com/";
807         try {
808             e.setNamespaceURI(uri);
809             fail("allowed " + uri + " as namespace URI");
810         }
811         catch (MalformedURIException success) {
812             assertEquals(uri, success.getData());
813         }
814         
815         uri = "scheme://user%5@www.example.com/";
816         try {
817             e.setNamespaceURI(uri);
818             fail("allowed " + uri + " as namespace URI");
819         }
820         catch (MalformedURIException success) {
821             assertEquals(uri, success.getData());
822         }
823         
824     }
825
826
827     public void testBadHexEscapeInHost() {
828         
829         Element e = new Element("e");
830         
831         String JavaDoc uri = "scheme://user%5TEDwww.example.com/";
832         try {
833             e.setNamespaceURI(uri);
834             fail("allowed " + uri + " as namespace URI");
835         }
836         catch (MalformedURIException success) {
837             assertEquals(uri, success.getData());
838         }
839         
840         uri = "scheme://www.example.co%5/";
841         try {
842             e.setNamespaceURI(uri);
843             fail("allowed " + uri + " as namespace URI");
844         }
845         catch (MalformedURIException success) {
846             assertEquals(uri, success.getData());
847         }
848         
849     }
850
851
852     public void testQuestionmarkIsNotAHexDigit() {
853         
854         Element e = new Element("e");
855         
856         // Have to do this in a fragment ID to keep it from being
857
// interpreted as a query string separator
858
String JavaDoc uri = "scheme://user@www.example.com/#fragment%?Adata";
859         try {
860             e.setNamespaceURI(uri);
861             fail("allowed " + uri + " as namespace URI");
862         }
863         catch (MalformedURIException success) {
864             assertEquals(uri, success.getData());
865         }
866         
867     }
868     
869     
870     public void testIllegalIRIs() {
871         
872         int[] illegalChars = {0x00, 0xDC00, 0xE7FF, 0xF899, 0xD800,
873             0xFDE0, 0xFFFF, 0x1FFFE, 0x2FFFE, 0x3FFFF, 0x4FFFE,
874             0x4FFFF, 0x5FFFE, 0x6FFFF, 0x7FFFE, 0x8FFFF, 0x9FFFE,
875             0xAFFFE, 0xBFFFF, 0xCFFFE, 0xDFFFE, 0xEFFFF, 0xFDDF};
876         
877         for (int i = 0; i < illegalChars.length; i++) {
878             String JavaDoc utf16 = convertToUTF16(illegalChars[i]);
879             String JavaDoc url = "http://www.example.com/" + utf16 + ".xml";
880             try {
881                 new DocType("root", url);
882                 fail("Allowed URL containing 0x" +
883                   Integer.toHexString(illegalChars[i]).toUpperCase());
884             }
885             catch (MalformedURIException success) {
886                 assertNotNull(success.getMessage());
887                 assertEquals(url, success.getData());
888             }
889         }
890         
891     }
892
893     
894     public void testLegalIP6Addresses() {
895         
896         String JavaDoc[] addresses = {
897           "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210",
898           "1080:0:0:0:8:800:200C:4171",
899           "3ffe:2a00:100:7031::1",
900           "1080::8:800:200C:417A",
901           "::192.9.5.5",
902           "::FFFF:129.144.52.38",
903           "2010:836B:4179::836B:4179",
904           "1080:0:0:0:8:800:200C:417A",
905           "FF01:0:0:0:0:0:0:101",
906           "0:0:0:0:0:0:0:1",
907           "0:0:0:0:0:0:0:0",
908           "1080::8:800:200C:417A",
909           "FF01::101",
910           "::1",
911           "::",
912           "0:0:0:0:0:0:13.1.68.3",
913           "0:0:0:0:0:FFFF:129.144.52.38",
914           "::13.1.68.3",
915           "::FFFF:129.144.52.38"
916         };
917         
918         Element element = new Element("test");
919         for (int i = 0; i < addresses.length; i++) {
920             String JavaDoc url = "http://[" + addresses[i] + "]/";
921             element.addAttribute(new Attribute("xml:base",
922               "http://www.w3.org/XML/1998/namespace", url));
923             assertEquals(url, element.getBaseURI());
924         }
925         
926     }
927     
928     
929     public void testIllegalIP6Addresses() {
930         
931         String JavaDoc[] addresses = {
932           "FEDC:BA98:7654:3210:GEDC:BA98:7654:3 210",
933           "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:4352",
934           "FEDC:BA98:7654:3210:GEDC:BA98:7654:3210",
935           "FEDC:BA98:7654:3210:GEDC:BA98:7654:G210",
936           "FEDC:BA98:7654:3210:GEDC:BA98:7654: 3210",
937           "FEDC:BA98:7654:3210:GEDC:BA98:7654:+3210",
938           "FEDC:BA98:7654:3210:GEDC:BA98:7654:3210 ",
939           "FEDC:BA98:7654:3210:GEDC:BA98:7654:32 10",
940           "1080:0:::8:800:200C:4171",
941           "3ffe::100:7031::1",
942           "::192.9.5",
943           "::FFFF:129.144.52.38.56",
944           "::FFFF:129.144.52.A3",
945           "::FFFF:129.144.52.-22",
946           "::FFFF:129.144.52.+22",
947           "::FFFF:256.144.52.+22",
948           "::FFFF:www.apple.com",
949           "1080:0:0:0:8:800:-200C:417A",
950           "1080:0:0:0:-8:800:-200C:417A"
951         };
952         
953         for (int i = 0; i < addresses.length; i++) {
954             String JavaDoc url = "http://[" + addresses[i] + "]/";
955             try {
956                 new DocType("root", url);
957                 fail("Allowed illegal IPv6 address: " + addresses[i] );
958             }
959             catch (MalformedURIException success) {
960                 assertNotNull(success.getMessage());
961                 assertTrue(success.getData().indexOf(addresses[i]) >= 0);
962             }
963         }
964         
965     }
966     
967     
968     private static String JavaDoc convertToUTF16(int c) {
969         
970         if (c <= 0xFFFF) return "" + (char) c;
971         char high = UTF16.getLeadSurrogate(c);
972         char low = UTF16.getTrailSurrogate(c);
973         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(2);
974         sb.append(high);
975         sb.append(low);
976         return sb.toString().toLowerCase();
977         
978     }
979     
980     
981     public void testC0Controls() {
982         
983          for (char c = 0; c < '\t'; c++) {
984              try {
985                  new Text(String.valueOf(c));
986              }
987              catch (IllegalDataException success) {
988                  assertNotNull(success.getMessage());
989              }
990          }
991          
992          for (char c = '\r'+1; c < ' '; c++) {
993              try {
994                  new Text(String.valueOf(c));
995              }
996              catch (IllegalDataException success) {
997                  assertNotNull(success.getMessage());
998                  assertEquals(String.valueOf(c), success.getData());
999              }
1000         }
1001         
1002    }
1003    
1004    
1005    public void testAttributeNameThatEndsWithAColon() {
1006        
1007        try {
1008            new Attribute("name:", "http://www.example.com", "value", Attribute.Type.CDATA);
1009            fail("Allowed attribute name that ends with a colon");
1010        }
1011        catch (IllegalNameException success) {
1012            assertNotNull(success.getMessage());
1013            assertEquals("name:", success.getData());
1014        }
1015        
1016    }
1017
1018
1019    public void testAttributeNameThatBeginsWithAColon() {
1020        
1021        try {
1022            new Attribute(":name", "http://www.example.com", "value", Attribute.Type.CDATA);
1023            fail("Allowed attribute name that begins with a colon");
1024        }
1025        catch (IllegalNameException success) {
1026            assertNotNull(success.getMessage());
1027            assertEquals(":name", success.getData());
1028        }
1029        
1030    }
1031
1032
1033    public void testElementNameThatEndsWithAColon() {
1034        
1035        try {
1036            new Element("name:", "http://www.example.com");
1037            fail("Allowed element name that ends with a colon");
1038        }
1039        catch (IllegalNameException success) {
1040            assertNotNull(success.getMessage());
1041            assertEquals("name:", success.getData());
1042        }
1043        
1044    }
1045
1046
1047    public void testElementNameThatBeginsWithAColon() {
1048        
1049        try {
1050            new Element(":name", "http://www.example.com");
1051            fail("Allowed element name that begins with a colon");
1052        }
1053        catch (IllegalNameException success) {
1054            assertNotNull(success.getMessage());
1055            assertEquals(":name", success.getData());
1056        }
1057        
1058    }
1059
1060
1061}
1062
Popular Tags