001: // -*- Java -*-
002: //
003: // Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
004: // Copyright (c) 2005, University of Colorado at Boulder
005: // All rights reserved.
006: //
007: // Redistribution and use in source and binary forms, with or without
008: // modification, are permitted provided that the following conditions are
009: // met:
010: //
011: // * Redistributions of source code must retain the above copyright
012: // notice, this list of conditions and the following disclaimer.
013: //
014: // * Redistributions in binary form must reproduce the above copyright
015: // notice, this list of conditions and the following disclaimer in the
016: // documentation and/or other materials provided with the distribution.
017: //
018: // * Neither the name of the University of Colorado at Boulder nor the
019: // names of its contributors may be used to endorse or promote
020: // products derived from this software without specific prior written
021: // permission.
022: //
023: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
024: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
025: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
026: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
027: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
028: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
029: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
030: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
031: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
033: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034: //
035: package org.xbill.DNS;
036:
037: import java.io.BufferedInputStream;
038: import java.io.ByteArrayInputStream;
039: import java.io.File;
040: import java.io.FileWriter;
041: import java.io.IOException;
042: import junit.framework.TestCase;
043:
044: public class TokenizerTest extends TestCase {
045: private Tokenizer m_t;
046:
047: protected void setUp() {
048: m_t = null;
049: }
050:
051: private void assertEquals(byte[] exp, byte[] act) {
052: assertTrue(java.util.Arrays.equals(exp, act));
053: }
054:
055: public void test_get() throws IOException {
056: m_t = new Tokenizer(
057: new BufferedInputStream(
058: new ByteArrayInputStream(
059: "AnIdentifier \"a quoted \\\" string\"\r\n; this is \"my\"\t(comment)\nanotherIdentifier (\ramultilineIdentifier\n)"
060: .getBytes())));
061:
062: Tokenizer.Token tt = m_t.get(true, true);
063: assertEquals(Tokenizer.IDENTIFIER, tt.type);
064: assertTrue(tt.isString());
065: assertFalse(tt.isEOL());
066: assertEquals("AnIdentifier", tt.value);
067:
068: tt = m_t.get(true, true);
069: assertEquals(Tokenizer.WHITESPACE, tt.type);
070: assertFalse(tt.isString());
071: assertFalse(tt.isEOL());
072: assertNull(tt.value);
073:
074: tt = m_t.get(true, true);
075: assertEquals(Tokenizer.QUOTED_STRING, tt.type);
076: assertTrue(tt.isString());
077: assertFalse(tt.isEOL());
078: assertEquals("a quoted \\\" string", tt.value);
079:
080: tt = m_t.get(true, true);
081: assertEquals(Tokenizer.EOL, tt.type);
082: assertFalse(tt.isString());
083: assertTrue(tt.isEOL());
084: assertNull(tt.value);
085:
086: tt = m_t.get(true, true);
087: assertEquals(Tokenizer.COMMENT, tt.type);
088: assertFalse(tt.isString());
089: assertFalse(tt.isEOL());
090: assertEquals(" this is \"my\"\t(comment)", tt.value);
091:
092: tt = m_t.get(true, true);
093: assertEquals(Tokenizer.EOL, tt.type);
094: assertFalse(tt.isString());
095: assertTrue(tt.isEOL());
096: assertNull(tt.value);
097:
098: tt = m_t.get(true, true);
099: assertEquals(Tokenizer.IDENTIFIER, tt.type);
100: assertTrue(tt.isString());
101: assertFalse(tt.isEOL());
102: assertEquals("anotherIdentifier", tt.value);
103:
104: tt = m_t.get(true, true);
105: assertEquals(Tokenizer.WHITESPACE, tt.type);
106:
107: tt = m_t.get(true, true);
108: assertEquals(Tokenizer.IDENTIFIER, tt.type);
109: assertTrue(tt.isString());
110: assertFalse(tt.isEOL());
111: assertEquals("amultilineIdentifier", tt.value);
112:
113: tt = m_t.get(true, true);
114: assertEquals(Tokenizer.WHITESPACE, tt.type);
115:
116: tt = m_t.get(true, true);
117: assertEquals(Tokenizer.EOF, tt.type);
118: assertFalse(tt.isString());
119: assertTrue(tt.isEOL());
120: assertNull(tt.value);
121:
122: // should be able to do this repeatedly
123: tt = m_t.get(true, true);
124: assertEquals(Tokenizer.EOF, tt.type);
125: assertFalse(tt.isString());
126: assertTrue(tt.isEOL());
127: assertNull(tt.value);
128:
129: m_t = new Tokenizer("onlyOneIdentifier");
130: tt = m_t.get();
131: assertEquals(Tokenizer.IDENTIFIER, tt.type);
132: assertEquals("onlyOneIdentifier", tt.value);
133:
134: m_t = new Tokenizer("identifier ;");
135: tt = m_t.get();
136: assertEquals("identifier", tt.value);
137: tt = m_t.get();
138: assertEquals(Tokenizer.EOF, tt.type);
139:
140: // some ungets
141: m_t = new Tokenizer("identifier \nidentifier2; junk comment");
142: tt = m_t.get(true, true);
143: assertEquals(Tokenizer.IDENTIFIER, tt.type);
144: assertEquals("identifier", tt.value);
145:
146: m_t.unget();
147:
148: tt = m_t.get(true, true);
149: assertEquals(Tokenizer.IDENTIFIER, tt.type);
150: assertEquals("identifier", tt.value);
151:
152: tt = m_t.get(true, true);
153: assertEquals(Tokenizer.WHITESPACE, tt.type);
154:
155: m_t.unget();
156: tt = m_t.get(true, true);
157: assertEquals(Tokenizer.WHITESPACE, tt.type);
158:
159: tt = m_t.get(true, true);
160: assertEquals(Tokenizer.EOL, tt.type);
161:
162: m_t.unget();
163: tt = m_t.get(true, true);
164: assertEquals(Tokenizer.EOL, tt.type);
165:
166: tt = m_t.get(true, true);
167: assertEquals(Tokenizer.IDENTIFIER, tt.type);
168: assertEquals("identifier2", tt.value);
169:
170: tt = m_t.get(true, true);
171: assertEquals(Tokenizer.COMMENT, tt.type);
172: assertEquals(" junk comment", tt.value);
173:
174: m_t.unget();
175: tt = m_t.get(true, true);
176: assertEquals(Tokenizer.COMMENT, tt.type);
177: assertEquals(" junk comment", tt.value);
178:
179: tt = m_t.get(true, true);
180: assertEquals(Tokenizer.EOF, tt.type);
181:
182: m_t = new Tokenizer("identifier ( junk ; comment\n )");
183: tt = m_t.get();
184: assertEquals(Tokenizer.IDENTIFIER, tt.type);
185: assertEquals(Tokenizer.IDENTIFIER, m_t.get().type);
186: assertEquals(Tokenizer.EOF, m_t.get().type);
187: }
188:
189: public void test_get_invalid() throws IOException {
190: m_t = new Tokenizer("(this ;");
191: m_t.get();
192: try {
193: m_t.get();
194: fail("TextParseException not thrown");
195: } catch (TextParseException e) {
196: }
197:
198: m_t = new Tokenizer("\"bad");
199: try {
200: m_t.get();
201: fail("TextParseException not thrown");
202: } catch (TextParseException e) {
203: }
204:
205: m_t = new Tokenizer(")");
206: try {
207: m_t.get();
208: fail("TextParseException not thrown");
209: } catch (TextParseException e) {
210: }
211:
212: m_t = new Tokenizer("\\");
213: try {
214: m_t.get();
215: fail("TextParseException not thrown");
216: } catch (TextParseException e) {
217: }
218:
219: m_t = new Tokenizer("\"\n");
220: try {
221: m_t.get();
222: fail("TextParseException not thrown");
223: } catch (TextParseException e) {
224: }
225: }
226:
227: public void test_File_input() throws IOException {
228: File tmp = File.createTempFile("dnsjava", "tmp");
229: try {
230: FileWriter fw = new FileWriter(tmp);
231: fw.write("file\ninput; test");
232: fw.close();
233:
234: m_t = new Tokenizer(tmp);
235:
236: Tokenizer.Token tt = m_t.get();
237: assertEquals(Tokenizer.IDENTIFIER, tt.type);
238: assertEquals("file", tt.value);
239:
240: tt = m_t.get();
241: assertEquals(Tokenizer.EOL, tt.type);
242:
243: tt = m_t.get();
244: assertEquals(Tokenizer.IDENTIFIER, tt.type);
245: assertEquals("input", tt.value);
246:
247: tt = m_t.get(false, true);
248: assertEquals(Tokenizer.COMMENT, tt.type);
249: assertEquals(" test", tt.value);
250:
251: m_t.close();
252: } finally {
253: tmp.delete();
254: }
255: }
256:
257: public void test_unwanted_comment() throws IOException {
258: m_t = new Tokenizer("; this whole thing is a comment\n");
259: Tokenizer.Token tt = m_t.get();
260:
261: assertEquals(Tokenizer.EOL, tt.type);
262: }
263:
264: public void test_unwanted_ungotten_whitespace() throws IOException {
265: m_t = new Tokenizer(" ");
266: Tokenizer.Token tt = m_t.get(true, true);
267: m_t.unget();
268: tt = m_t.get();
269: assertEquals(Tokenizer.EOF, tt.type);
270: }
271:
272: public void test_unwanted_ungotten_comment() throws IOException {
273: m_t = new Tokenizer("; this whole thing is a comment");
274: Tokenizer.Token tt = m_t.get(true, true);
275: m_t.unget();
276: tt = m_t.get();
277: assertEquals(Tokenizer.EOF, tt.type);
278: }
279:
280: public void test_empty_string() throws IOException {
281: m_t = new Tokenizer("");
282: Tokenizer.Token tt = m_t.get();
283: assertEquals(Tokenizer.EOF, tt.type);
284:
285: m_t = new Tokenizer(" ");
286: tt = m_t.get();
287: assertEquals(Tokenizer.EOF, tt.type);
288: }
289:
290: public void test_multiple_ungets() throws IOException {
291: m_t = new Tokenizer("a simple one");
292: Tokenizer.Token tt = m_t.get();
293:
294: m_t.unget();
295: try {
296: m_t.unget();
297: fail("IllegalStateException not thrown");
298: } catch (IllegalStateException e) {
299: }
300: }
301:
302: public void test_getString() throws IOException {
303: m_t = new Tokenizer("just_an_identifier");
304: String out = m_t.getString();
305: assertEquals("just_an_identifier", out);
306:
307: m_t = new Tokenizer("\"just a string\"");
308: out = m_t.getString();
309: assertEquals("just a string", out);
310:
311: m_t = new Tokenizer("; just a comment");
312: try {
313: out = m_t.getString();
314: fail("TextParseException not thrown");
315: } catch (TextParseException e) {
316: }
317: }
318:
319: public void test_getIdentifier() throws IOException {
320: m_t = new Tokenizer("just_an_identifier");
321: String out = m_t.getIdentifier();
322: assertEquals("just_an_identifier", out);
323:
324: m_t = new Tokenizer("\"just a string\"");
325: try {
326: m_t.getIdentifier();
327: fail("TextParseException not thrown");
328: } catch (TextParseException e) {
329: }
330: }
331:
332: public void test_getLong() throws IOException {
333: m_t = new Tokenizer((Integer.MAX_VALUE + 1L) + "");
334: long out = m_t.getLong();
335: assertEquals((Integer.MAX_VALUE + 1L), out);
336:
337: m_t = new Tokenizer("-10");
338: try {
339: m_t.getLong();
340: fail("TextParseException not thrown");
341: } catch (TextParseException e) {
342: }
343:
344: m_t = new Tokenizer("19_identifier");
345: try {
346: m_t.getLong();
347: fail("TextParseException not thrown");
348: } catch (TextParseException e) {
349: }
350: }
351:
352: public void test_getUInt32() throws IOException {
353: m_t = new Tokenizer(0xABCDEF12L + "");
354: long out = m_t.getUInt32();
355: assertEquals(0xABCDEF12L, out);
356:
357: m_t = new Tokenizer(0x100000000L + "");
358: try {
359: m_t.getUInt32();
360: fail("TextParseException not thrown");
361: } catch (TextParseException e) {
362: }
363:
364: m_t = new Tokenizer("-12345");
365: try {
366: m_t.getUInt32();
367: fail("TextParseException not thrown");
368: } catch (TextParseException e) {
369: }
370: }
371:
372: public void test_getUInt16() throws IOException {
373: m_t = new Tokenizer(0xABCDL + "");
374: int out = m_t.getUInt16();
375: assertEquals(0xABCDL, out);
376:
377: m_t = new Tokenizer(0x10000 + "");
378: try {
379: m_t.getUInt16();
380: fail("TextParseException not thrown");
381: } catch (TextParseException e) {
382: }
383:
384: m_t = new Tokenizer("-125");
385: try {
386: m_t.getUInt16();
387: fail("TextParseException not thrown");
388: } catch (TextParseException e) {
389: }
390: }
391:
392: public void test_getUInt8() throws IOException {
393: m_t = new Tokenizer(0xCDL + "");
394: int out = m_t.getUInt8();
395: assertEquals(0xCDL, out);
396:
397: m_t = new Tokenizer(0x100 + "");
398: try {
399: m_t.getUInt8();
400: fail("TextParseException not thrown");
401: } catch (TextParseException e) {
402: }
403:
404: m_t = new Tokenizer("-12");
405: try {
406: m_t.getUInt8();
407: fail("TextParseException not thrown");
408: } catch (TextParseException e) {
409: }
410: }
411:
412: public void test_getTTL() throws IOException {
413: m_t = new Tokenizer("59S");
414: assertEquals(59, m_t.getTTL());
415:
416: m_t = new Tokenizer(TTL.MAX_VALUE + "");
417: assertEquals(TTL.MAX_VALUE, m_t.getTTL());
418:
419: m_t = new Tokenizer((TTL.MAX_VALUE + 1L) + "");
420: assertEquals(TTL.MAX_VALUE, m_t.getTTL());
421:
422: m_t = new Tokenizer("Junk");
423: try {
424: m_t.getTTL();
425: fail("TextParseException not thrown");
426: } catch (TextParseException e) {
427: }
428: }
429:
430: public void test_getTTLLike() throws IOException {
431: m_t = new Tokenizer("59S");
432: assertEquals(59, m_t.getTTLLike());
433:
434: m_t = new Tokenizer(TTL.MAX_VALUE + "");
435: assertEquals(TTL.MAX_VALUE, m_t.getTTLLike());
436:
437: m_t = new Tokenizer((TTL.MAX_VALUE + 1L) + "");
438: assertEquals(TTL.MAX_VALUE + 1L, m_t.getTTLLike());
439:
440: m_t = new Tokenizer("Junk");
441: try {
442: m_t.getTTLLike();
443: fail("TextParseException not thrown");
444: } catch (TextParseException e) {
445: }
446: }
447:
448: public void test_getName() throws IOException, TextParseException {
449: Name root = Name.fromString(".");
450: m_t = new Tokenizer("junk");
451: Name exp = Name.fromString("junk.");
452: Name out = m_t.getName(root);
453: assertEquals(exp, out);
454:
455: Name rel = Name.fromString("you.dig");
456: m_t = new Tokenizer("junk");
457: try {
458: m_t.getName(rel);
459: fail("RelativeNameException not thrown");
460: } catch (RelativeNameException e) {
461: }
462:
463: m_t = new Tokenizer("");
464: try {
465: m_t.getName(root);
466: fail("TextParseException not thrown");
467: } catch (TextParseException e) {
468: }
469: }
470:
471: public void test_getEOL() throws IOException {
472: m_t = new Tokenizer("id");
473: m_t.getIdentifier();
474: try {
475: m_t.getEOL();
476: } catch (TextParseException e) {
477: fail(e.getMessage());
478: }
479:
480: m_t = new Tokenizer("\n");
481: try {
482: m_t.getEOL();
483: m_t.getEOL();
484: } catch (TextParseException e) {
485: fail(e.getMessage());
486: }
487:
488: m_t = new Tokenizer("id");
489: try {
490: m_t.getEOL();
491: fail("TextParseException not thrown");
492: } catch (TextParseException e) {
493: }
494: }
495:
496: public void test_getBase64() throws IOException {
497: byte[] exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
498: // basic
499: m_t = new Tokenizer("AQIDBAUGBwgJ");
500: byte[] out = m_t.getBase64();
501: assertEquals(exp, out);
502:
503: // with some whitespace
504: m_t = new Tokenizer("AQIDB AUGB wgJ");
505: out = m_t.getBase64();
506: assertEquals(exp, out);
507:
508: // two base64s separated by newline
509: m_t = new Tokenizer("AQIDBAUGBwgJ\nAB23DK");
510: out = m_t.getBase64();
511: assertEquals(exp, out);
512:
513: // no remaining strings
514: m_t = new Tokenizer("\n");
515: assertNull(m_t.getBase64());
516:
517: m_t = new Tokenizer("\n");
518: try {
519: m_t.getBase64(true);
520: fail("TextParseException not thrown");
521: } catch (TextParseException e) {
522: }
523:
524: // invalid encoding
525: m_t = new Tokenizer("not_base64");
526: try {
527: m_t.getBase64(false);
528: fail("TextParseException not thrown");
529: } catch (TextParseException e) {
530: }
531:
532: m_t = new Tokenizer("not_base64");
533: try {
534: m_t.getBase64(true);
535: fail("TextParseException not thrown");
536: } catch (TextParseException e) {
537: }
538: }
539:
540: public void test_getHex() throws IOException {
541: byte[] exp = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
542: 15 };
543: // basic
544: m_t = new Tokenizer("0102030405060708090A0B0C0D0E0F");
545: byte[] out = m_t.getHex();
546: assertEquals(exp, out);
547:
548: // with some whitespace
549: m_t = new Tokenizer("0102030 405 060708090A0B0C 0D0E0F");
550: out = m_t.getHex();
551: assertEquals(exp, out);
552:
553: // two hexs separated by newline
554: m_t = new Tokenizer("0102030405060708090A0B0C0D0E0F\n01AB3FE");
555: out = m_t.getHex();
556: assertEquals(exp, out);
557:
558: // no remaining strings
559: m_t = new Tokenizer("\n");
560: assertNull(m_t.getHex());
561:
562: m_t = new Tokenizer("\n");
563: try {
564: m_t.getHex(true);
565: fail("TextParseException not thrown");
566: } catch (TextParseException e) {
567: }
568:
569: // invalid encoding
570: m_t = new Tokenizer("not_hex");
571: try {
572: m_t.getHex(false);
573: fail("TextParseException not thrown");
574: } catch (TextParseException e) {
575: }
576:
577: m_t = new Tokenizer("not_hex");
578: try {
579: m_t.getHex(true);
580: fail("TextParseException not thrown");
581: } catch (TextParseException e) {
582: }
583: }
584: }
|