~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/VerifierTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

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