1 // -*- Java -*-
2 //
3 // Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
4 // Copyright (c) 2005, University of Colorado at Boulder
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // * Redistributions of source code must retain the above copyright
12 //   notice, this list of conditions and the following disclaimer.
13 //
14 // * Redistributions in binary form must reproduce the above copyright
15 //   notice, this list of conditions and the following disclaimer in the
16 //   documentation and/or other materials provided with the distribution.
17 //
18 // * Neither the name of the University of Colorado at Boulder nor the
19 //   names of its contributors may be used to endorse or promote
20 //   products derived from this software without specific prior written
21 //   permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //
35 package org.xbill.DNS;
36 
37 import	junit.framework.TestCase;
38 
39 public class TTLTest extends TestCase
40 {
41     private final long S = 1;
42     private final long M = 60*S;
43     private final long H = 60*M;
44     private final long D = 24*H;
45     private final long W = 7*D;
46 
test_parseTTL()47     public void test_parseTTL()
48     {
49 	assertEquals(9876, TTL.parseTTL("9876"));
50 
51 	assertEquals(0, TTL.parseTTL("0S"));
52 	assertEquals(0, TTL.parseTTL("0M"));
53 	assertEquals(0, TTL.parseTTL("0H"));
54 	assertEquals(0, TTL.parseTTL("0D"));
55 	assertEquals(0, TTL.parseTTL("0W"));
56 
57 	assertEquals(S, TTL.parseTTL("1s"));
58 	assertEquals(M, TTL.parseTTL("1m"));
59 	assertEquals(H, TTL.parseTTL("1h"));
60 	assertEquals(D, TTL.parseTTL("1d"));
61 	assertEquals(W, TTL.parseTTL("1w"));
62 
63 	assertEquals(98*S, TTL.parseTTL("98S"));
64 	assertEquals(76*M, TTL.parseTTL("76M"));
65 	assertEquals(54*H, TTL.parseTTL("54H"));
66 	assertEquals(32*D, TTL.parseTTL("32D"));
67 	assertEquals(10*W, TTL.parseTTL("10W"));
68 
69 	assertEquals(98*S+11*M+1234*H+2*D+W, TTL.parseTTL("98S11M1234H2D01W"));
70     }
71 
test_parseTTL_invalid()72     public void test_parseTTL_invalid()
73     {
74 	try {TTL.parseTTL(null); fail("NumberFormatException not throw");}
75 	catch( NumberFormatException e ){}
76 
77 	try {TTL.parseTTL(""); fail("NumberFormatException not throw");}
78 	catch( NumberFormatException e ){}
79 
80 	try {TTL.parseTTL("S"); fail("NumberFormatException not throw");}
81 	catch( NumberFormatException e ){}
82 
83 	try {TTL.parseTTL("10S4B"); fail("NumberFormatException not throw");}
84 	catch( NumberFormatException e ){}
85 
86 	try {TTL.parseTTL("1S"+0xFFFFFFFFL+"S"); fail("NumberFormatException not throw");}
87 	catch( NumberFormatException e ){}
88 
89 	try {TTL.parseTTL(""+0x100000000L); fail("NumberFormatException not throw");}
90 	catch( NumberFormatException e ){}
91     }
92 
test_format()93     public void test_format()
94     {
95 	assertEquals("0S", TTL.format(0));
96 	assertEquals("1S", TTL.format(1));
97 	assertEquals("59S", TTL.format(59));
98 	assertEquals("1M", TTL.format(60));
99 	assertEquals("59M", TTL.format(59*M));
100 	assertEquals("1M33S", TTL.format(M+33));
101 	assertEquals("59M59S", TTL.format(59*M+59*S));
102 	assertEquals("1H", TTL.format(H));
103 	assertEquals("10H1M21S", TTL.format(10*H+M+21));
104 	assertEquals("23H59M59S", TTL.format(23*H+59*M+59));
105 	assertEquals("1D", TTL.format(D));
106 	assertEquals("4D18H45M30S", TTL.format(4*D+18*H+45*M+30));
107 	assertEquals("6D23H59M59S", TTL.format(6*D+23*H+59*M+59));
108 	assertEquals("1W", TTL.format(W));
109 	assertEquals("10W4D1H21M29S", TTL.format(10*W+4*D+H+21*M+29));
110 	assertEquals("3550W5D3H14M7S", TTL.format(0x7FFFFFFFL));
111     }
112 
test_format_invalid()113     public void test_format_invalid()
114     {
115 	try {TTL.format(-1); fail("InvalidTTLException not thrown");
116 	} catch( InvalidTTLException e ){}
117 
118 	try {TTL.format(0x100000000L); fail("InvalidTTLException not thrown");
119 	} catch( InvalidTTLException e ){}
120     }
121 }
122