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	java.io.IOException;
38 import	java.util.Arrays;
39 import	junit.framework.Test;
40 import	junit.framework.TestCase;
41 import	junit.framework.TestSuite;
42 
43 public class DSRecordTest extends TestCase
44 {
test_ctor_0arg()45     public void test_ctor_0arg()
46     {
47 	DSRecord dr = new DSRecord();
48 	assertNull(dr.getName());
49 	assertEquals(0, dr.getType());
50 	assertEquals(0, dr.getDClass());
51 	assertEquals(0, dr.getTTL());
52 	assertEquals(0, dr.getAlgorithm());
53 	assertEquals(0, dr.getDigestID());
54 	assertNull(dr.getDigest());
55 	assertEquals(0, dr.getFootprint());
56     }
57 
test_getObject()58     public void test_getObject()
59     {
60 	DSRecord dr = new DSRecord();
61 	Record r = dr.getObject();
62 	assertTrue(r instanceof DSRecord);
63     }
64 
65     public static class Test_Ctor_7arg extends TestCase
66     {
67 	private Name	m_n;
68 	private long	m_ttl;
69 	private int	m_footprint;
70 	private int	m_algorithm;
71 	private int	m_digestid;
72 	private byte[]	m_digest;
73 
setUp()74 	protected void setUp() throws TextParseException
75 	{
76 	    m_n = Name.fromString("The.Name.");
77 	    m_ttl = 0xABCDL;
78 	    m_footprint = 0xEF01;
79 	    m_algorithm = 0x23;
80 	    m_digestid = 0x45;
81 	    m_digest = new byte[] { (byte)0x67, (byte)0x89, (byte)0xAB, (byte)0xCD, (byte)0xEF };
82 	}
83 
test_basic()84 	public void test_basic() throws TextParseException
85 	{
86 	    DSRecord dr = new DSRecord(m_n, DClass.IN, m_ttl,
87 				       m_footprint, m_algorithm, m_digestid, m_digest);
88 	    assertEquals(m_n, dr.getName());
89 	    assertEquals(DClass.IN, dr.getDClass());
90 	    assertEquals(Type.DS, dr.getType());
91 	    assertEquals(m_ttl, dr.getTTL());
92 	    assertEquals(m_footprint, dr.getFootprint());
93 	    assertEquals(m_algorithm, dr.getAlgorithm());
94 	    assertEquals(m_digestid, dr.getDigestID());
95 	    assertTrue(Arrays.equals(m_digest, dr.getDigest()));
96 	}
97 
test_toosmall_footprint()98 	public void test_toosmall_footprint() throws TextParseException
99 	{
100 	    try {
101 		new DSRecord(m_n, DClass.IN, m_ttl,
102 			     -1, m_algorithm, m_digestid, m_digest);
103 		fail("IllegalArgumentException not thrown");
104 	    }
105 	    catch(IllegalArgumentException e){}
106 	}
107 
test_toobig_footprint()108 	public void test_toobig_footprint() throws TextParseException
109 	{
110 	    try {
111 		new DSRecord(m_n, DClass.IN, m_ttl,
112 			     0x10000, m_algorithm, m_digestid, m_digest);
113 		fail("IllegalArgumentException not thrown");
114 	    }
115 	    catch(IllegalArgumentException e){}
116 	}
117 
test_toosmall_algorithm()118 	public void test_toosmall_algorithm() throws TextParseException
119 	{
120 	    try {
121 		new DSRecord(m_n, DClass.IN, m_ttl,
122 			     m_footprint, -1, m_digestid, m_digest);
123 		fail("IllegalArgumentException not thrown");
124 	    }
125 	    catch(IllegalArgumentException e){}
126 	}
127 
test_toobig_algorithm()128 	public void test_toobig_algorithm() throws TextParseException
129 	{
130 	    try {
131 		new DSRecord(m_n, DClass.IN, m_ttl,
132 			     m_footprint, 0x10000, m_digestid, m_digest);
133 		fail("IllegalArgumentException not thrown");
134 	    }
135 	    catch(IllegalArgumentException e){}
136 	}
137 
test_toosmall_digestid()138 	public void test_toosmall_digestid() throws TextParseException
139 	{
140 	    try {
141 		new DSRecord(m_n, DClass.IN, m_ttl,
142 			     m_footprint, m_algorithm, -1, m_digest);
143 		fail("IllegalArgumentException not thrown");
144 	    }
145 	    catch(IllegalArgumentException e){}
146 	}
147 
test_toobig_digestid()148 	public void test_toobig_digestid() throws TextParseException
149 	{
150 	    try {
151 		new DSRecord(m_n, DClass.IN, m_ttl,
152 			     m_footprint, m_algorithm, 0x10000, m_digest);
153 		fail("IllegalArgumentException not thrown");
154 	    }
155 	    catch(IllegalArgumentException e){}
156 	}
157 
test_null_digest()158 	public void test_null_digest()
159 	{
160 	    DSRecord dr = new DSRecord(m_n, DClass.IN, m_ttl,
161 				       m_footprint, m_algorithm, m_digestid, null);
162 	    assertEquals(m_n, dr.getName());
163 	    assertEquals(DClass.IN, dr.getDClass());
164 	    assertEquals(Type.DS, dr.getType());
165 	    assertEquals(m_ttl, dr.getTTL());
166 	    assertEquals(m_footprint, dr.getFootprint());
167 	    assertEquals(m_algorithm, dr.getAlgorithm());
168 	    assertEquals(m_digestid, dr.getDigestID());
169 	    assertNull(dr.getDigest());
170 	}
171     }
172 
test_rrFromWire()173     public void test_rrFromWire() throws IOException
174     {
175 	byte[] raw = new byte[] { (byte)0xAB, (byte)0xCD, (byte)0xEF,
176 				  (byte)0x01, (byte)0x23, (byte)0x45,
177 				  (byte)0x67, (byte)0x89 };
178 	DNSInput in = new DNSInput(raw);
179 
180 	DSRecord dr = new DSRecord();
181 	dr.rrFromWire(in);
182 	assertEquals(0xABCD, dr.getFootprint());
183 	assertEquals(0xEF, dr.getAlgorithm());
184 	assertEquals(0x01, dr.getDigestID());
185 	assertTrue(Arrays.equals(new byte[] { (byte)0x23, (byte)0x45, (byte)0x67, (byte)0x89 },
186 				 dr.getDigest()));
187     }
188 
test_rdataFromString()189     public void test_rdataFromString() throws IOException
190     {
191 	byte[] raw = new byte[] { (byte)0xAB, (byte)0xCD, (byte)0xEF,
192 				  (byte)0x01, (byte)0x23, (byte)0x45,
193 				  (byte)0x67, (byte)0x89 };
194 	Tokenizer t = new Tokenizer(0xABCD + " " + 0xEF + " " + 0x01 + " 23456789AB");
195 
196 	DSRecord dr = new DSRecord();
197 	dr.rdataFromString(t, null);
198 	assertEquals(0xABCD, dr.getFootprint());
199 	assertEquals(0xEF, dr.getAlgorithm());
200 	assertEquals(0x01, dr.getDigestID());
201 	assertTrue(Arrays.equals(new byte[] { (byte)0x23, (byte)0x45, (byte)0x67, (byte)0x89, (byte)0xAB },
202 				 dr.getDigest()));
203     }
204 
test_rrToString()205     public void test_rrToString() throws TextParseException
206     {
207 	String exp = 0xABCD + " " + 0xEF + " " + 0x01 + " 23456789AB";
208 
209 	DSRecord dr = new DSRecord(Name.fromString("The.Name."), DClass.IN, 0x123,
210 				   0xABCD, 0xEF, 0x01,
211 				   new byte[] { (byte)0x23, (byte)0x45, (byte)0x67,
212 						(byte)0x89, (byte)0xAB });
213 	assertEquals(exp, dr.rrToString());
214     }
215 
test_rrToWire()216     public void test_rrToWire() throws TextParseException
217     {
218 	DSRecord dr = new DSRecord(Name.fromString("The.Name."), DClass.IN, 0x123,
219 				   0xABCD, 0xEF, 0x01,
220 				   new byte[] { (byte)0x23, (byte)0x45, (byte)0x67,
221 						(byte)0x89, (byte)0xAB });
222 
223 	byte[] exp = new byte[] { (byte)0xAB, (byte)0xCD, (byte)0xEF,
224 				  (byte)0x01, (byte)0x23, (byte)0x45,
225 				  (byte)0x67, (byte)0x89, (byte)0xAB };
226 
227 	DNSOutput out = new DNSOutput();
228 	dr.rrToWire(out, null, true);
229 
230 	assertTrue(Arrays.equals(exp, out.toByteArray()));
231     }
232 
suite()233     public static Test suite()
234     {
235 	TestSuite s = new TestSuite();
236 	s.addTestSuite(Test_Ctor_7arg.class);
237 	s.addTestSuite(DSRecordTest.class);
238 	return s;
239     }
240 }
241