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.IOException;
038: import java.util.Arrays;
039: import junit.framework.Test;
040: import junit.framework.TestCase;
041: import junit.framework.TestSuite;
042:
043: public class DSRecordTest extends TestCase {
044: public void test_ctor_0arg() {
045: DSRecord dr = new DSRecord();
046: assertNull(dr.getName());
047: assertEquals(0, dr.getType());
048: assertEquals(0, dr.getDClass());
049: assertEquals(0, dr.getTTL());
050: assertEquals(0, dr.getAlgorithm());
051: assertEquals(0, dr.getDigestID());
052: assertNull(dr.getDigest());
053: assertEquals(0, dr.getFootprint());
054: }
055:
056: public void test_getObject() {
057: DSRecord dr = new DSRecord();
058: Record r = dr.getObject();
059: assertTrue(r instanceof DSRecord);
060: }
061:
062: public static class Test_Ctor_7arg extends TestCase {
063: private Name m_n;
064: private long m_ttl;
065: private int m_footprint;
066: private int m_algorithm;
067: private int m_digestid;
068: private byte[] m_digest;
069:
070: protected void setUp() throws TextParseException {
071: m_n = Name.fromString("The.Name.");
072: m_ttl = 0xABCDL;
073: m_footprint = 0xEF01;
074: m_algorithm = 0x23;
075: m_digestid = 0x45;
076: m_digest = new byte[] { (byte) 0x67, (byte) 0x89,
077: (byte) 0xAB, (byte) 0xCD, (byte) 0xEF };
078: }
079:
080: public void test_basic() throws TextParseException {
081: DSRecord dr = new DSRecord(m_n, DClass.IN, m_ttl,
082: m_footprint, m_algorithm, m_digestid, m_digest);
083: assertEquals(m_n, dr.getName());
084: assertEquals(DClass.IN, dr.getDClass());
085: assertEquals(Type.DS, dr.getType());
086: assertEquals(m_ttl, dr.getTTL());
087: assertEquals(m_footprint, dr.getFootprint());
088: assertEquals(m_algorithm, dr.getAlgorithm());
089: assertEquals(m_digestid, dr.getDigestID());
090: assertTrue(Arrays.equals(m_digest, dr.getDigest()));
091: }
092:
093: public void test_toosmall_footprint() throws TextParseException {
094: try {
095: new DSRecord(m_n, DClass.IN, m_ttl, -1, m_algorithm,
096: m_digestid, m_digest);
097: fail("IllegalArgumentException not thrown");
098: } catch (IllegalArgumentException e) {
099: }
100: }
101:
102: public void test_toobig_footprint() throws TextParseException {
103: try {
104: new DSRecord(m_n, DClass.IN, m_ttl, 0x10000,
105: m_algorithm, m_digestid, m_digest);
106: fail("IllegalArgumentException not thrown");
107: } catch (IllegalArgumentException e) {
108: }
109: }
110:
111: public void test_toosmall_algorithm() throws TextParseException {
112: try {
113: new DSRecord(m_n, DClass.IN, m_ttl, m_footprint, -1,
114: m_digestid, m_digest);
115: fail("IllegalArgumentException not thrown");
116: } catch (IllegalArgumentException e) {
117: }
118: }
119:
120: public void test_toobig_algorithm() throws TextParseException {
121: try {
122: new DSRecord(m_n, DClass.IN, m_ttl, m_footprint,
123: 0x10000, m_digestid, m_digest);
124: fail("IllegalArgumentException not thrown");
125: } catch (IllegalArgumentException e) {
126: }
127: }
128:
129: public void test_toosmall_digestid() throws TextParseException {
130: try {
131: new DSRecord(m_n, DClass.IN, m_ttl, m_footprint,
132: m_algorithm, -1, m_digest);
133: fail("IllegalArgumentException not thrown");
134: } catch (IllegalArgumentException e) {
135: }
136: }
137:
138: public void test_toobig_digestid() throws TextParseException {
139: try {
140: new DSRecord(m_n, DClass.IN, m_ttl, m_footprint,
141: m_algorithm, 0x10000, m_digest);
142: fail("IllegalArgumentException not thrown");
143: } catch (IllegalArgumentException e) {
144: }
145: }
146:
147: public void test_null_digest() {
148: DSRecord dr = new DSRecord(m_n, DClass.IN, m_ttl,
149: m_footprint, m_algorithm, m_digestid, null);
150: assertEquals(m_n, dr.getName());
151: assertEquals(DClass.IN, dr.getDClass());
152: assertEquals(Type.DS, dr.getType());
153: assertEquals(m_ttl, dr.getTTL());
154: assertEquals(m_footprint, dr.getFootprint());
155: assertEquals(m_algorithm, dr.getAlgorithm());
156: assertEquals(m_digestid, dr.getDigestID());
157: assertNull(dr.getDigest());
158: }
159: }
160:
161: public void test_rrFromWire() throws IOException {
162: byte[] raw = new byte[] { (byte) 0xAB, (byte) 0xCD,
163: (byte) 0xEF, (byte) 0x01, (byte) 0x23, (byte) 0x45,
164: (byte) 0x67, (byte) 0x89 };
165: DNSInput in = new DNSInput(raw);
166:
167: DSRecord dr = new DSRecord();
168: dr.rrFromWire(in);
169: assertEquals(0xABCD, dr.getFootprint());
170: assertEquals(0xEF, dr.getAlgorithm());
171: assertEquals(0x01, dr.getDigestID());
172: assertTrue(Arrays.equals(new byte[] { (byte) 0x23, (byte) 0x45,
173: (byte) 0x67, (byte) 0x89 }, dr.getDigest()));
174: }
175:
176: public void test_rdataFromString() throws IOException {
177: byte[] raw = new byte[] { (byte) 0xAB, (byte) 0xCD,
178: (byte) 0xEF, (byte) 0x01, (byte) 0x23, (byte) 0x45,
179: (byte) 0x67, (byte) 0x89 };
180: Tokenizer t = new Tokenizer(0xABCD + " " + 0xEF + " " + 0x01
181: + " 23456789AB");
182:
183: DSRecord dr = new DSRecord();
184: dr.rdataFromString(t, null);
185: assertEquals(0xABCD, dr.getFootprint());
186: assertEquals(0xEF, dr.getAlgorithm());
187: assertEquals(0x01, dr.getDigestID());
188: assertTrue(Arrays
189: .equals(new byte[] { (byte) 0x23, (byte) 0x45,
190: (byte) 0x67, (byte) 0x89, (byte) 0xAB }, dr
191: .getDigest()));
192: }
193:
194: public void test_rrToString() throws TextParseException {
195: String exp = 0xABCD + " " + 0xEF + " " + 0x01 + " 23456789AB";
196:
197: DSRecord dr = new DSRecord(Name.fromString("The.Name."),
198: DClass.IN, 0x123, 0xABCD, 0xEF, 0x01, new byte[] {
199: (byte) 0x23, (byte) 0x45, (byte) 0x67,
200: (byte) 0x89, (byte) 0xAB });
201: assertEquals(exp, dr.rrToString());
202: }
203:
204: public void test_rrToWire() throws TextParseException {
205: DSRecord dr = new DSRecord(Name.fromString("The.Name."),
206: DClass.IN, 0x123, 0xABCD, 0xEF, 0x01, new byte[] {
207: (byte) 0x23, (byte) 0x45, (byte) 0x67,
208: (byte) 0x89, (byte) 0xAB });
209:
210: byte[] exp = new byte[] { (byte) 0xAB, (byte) 0xCD,
211: (byte) 0xEF, (byte) 0x01, (byte) 0x23, (byte) 0x45,
212: (byte) 0x67, (byte) 0x89, (byte) 0xAB };
213:
214: DNSOutput out = new DNSOutput();
215: dr.rrToWire(out, null, true);
216:
217: assertTrue(Arrays.equals(exp, out.toByteArray()));
218: }
219:
220: public static Test suite() {
221: TestSuite s = new TestSuite();
222: s.addTestSuite(Test_Ctor_7arg.class);
223: s.addTestSuite(DSRecordTest.class);
224: return s;
225: }
226: }
|