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.net.InetAddress;
039: import java.net.UnknownHostException;
040: import java.util.Arrays;
041: import junit.framework.TestCase;
042:
043: public class KEYRecordTest extends TestCase {
044: public void test_ctor_0arg() throws UnknownHostException {
045: KEYRecord ar = new KEYRecord();
046: assertNull(ar.getName());
047: assertEquals(0, ar.getType());
048: assertEquals(0, ar.getDClass());
049: assertEquals(0, ar.getTTL());
050: assertEquals(0, ar.getAlgorithm());
051: assertEquals(0, ar.getFlags());
052: assertEquals(0, ar.getFootprint());
053: assertEquals(0, ar.getProtocol());
054: assertNull(ar.getKey());
055: }
056:
057: public void test_getObject() {
058: KEYRecord ar = new KEYRecord();
059: Record r = ar.getObject();
060: assertTrue(r instanceof KEYRecord);
061: }
062:
063: public void test_ctor_7arg() throws TextParseException {
064: Name n = Name.fromString("My.Absolute.Name.");
065: Name r = Name.fromString("My.Relative.Name");
066: byte[] key = new byte[] { 0, 1, 3, 5, 7, 9 };
067:
068: KEYRecord kr = new KEYRecord(n, DClass.IN, 0x24AC, 0x9832,
069: 0x12, 0x67, key);
070: assertEquals(n, kr.getName());
071: assertEquals(Type.KEY, kr.getType());
072: assertEquals(DClass.IN, kr.getDClass());
073: assertEquals(0x24AC, kr.getTTL());
074: assertEquals(0x9832, kr.getFlags());
075: assertEquals(0x12, kr.getProtocol());
076: assertEquals(0x67, kr.getAlgorithm());
077: assertTrue(Arrays.equals(key, kr.getKey()));
078:
079: // a relative name
080: try {
081: new KEYRecord(r, DClass.IN, 0x24AC, 0x9832, 0x12, 0x67, key);
082: fail("RelativeNameException not thrown");
083: } catch (RelativeNameException e) {
084: }
085: }
086:
087: public void test_Protocol_string() {
088: // a regular one
089: assertEquals("DNSSEC", KEYRecord.Protocol
090: .string(KEYRecord.Protocol.DNSSEC));
091: // a unassigned value within range
092: assertEquals("254", KEYRecord.Protocol.string(0xFE));
093: // too low
094: try {
095: KEYRecord.Protocol.string(-1);
096: fail("IllegalArgumentException not thrown");
097: } catch (IllegalArgumentException e) {
098: }
099: // too high
100: try {
101: KEYRecord.Protocol.string(0x100);
102: fail("IllegalArgumentException not thrown");
103: } catch (IllegalArgumentException e) {
104: }
105: }
106:
107: public void test_Protocol_value() {
108: // a regular one
109: assertEquals(KEYRecord.Protocol.IPSEC, KEYRecord.Protocol
110: .value("IPSEC"));
111: // a unassigned value within range
112: assertEquals(254, KEYRecord.Protocol.value("254"));
113: // too low
114: assertEquals(-1, KEYRecord.Protocol.value("-2"));
115: // too high
116: assertEquals(-1, KEYRecord.Protocol.value("256"));
117: }
118:
119: public void test_Flags_value() {
120: // numeric
121:
122: // lower bound
123: assertEquals(-1, KEYRecord.Flags.value("-2"));
124: assertEquals(0, KEYRecord.Flags.value("0"));
125: // in the middle
126: assertEquals(0xAB35, KEYRecord.Flags.value(0xAB35 + ""));
127: // upper bound
128: assertEquals(0xFFFF, KEYRecord.Flags.value(0xFFFF + ""));
129: assertEquals(-1, KEYRecord.Flags.value(0x10000 + ""));
130:
131: // textual
132:
133: // single
134: assertEquals(KEYRecord.Flags.EXTEND, KEYRecord.Flags
135: .value("EXTEND"));
136: // single invalid
137: assertEquals(-1, KEYRecord.Flags.value("NOT_A_VALID_NAME"));
138: // multiple
139: assertEquals(KEYRecord.Flags.NOAUTH | KEYRecord.Flags.FLAG10
140: | KEYRecord.Flags.ZONE, KEYRecord.Flags
141: .value("NOAUTH|ZONE|FLAG10"));
142: // multiple invalid
143: assertEquals(-1, KEYRecord.Flags
144: .value("NOAUTH|INVALID_NAME|FLAG10"));
145: // pathological
146: assertEquals(0, KEYRecord.Flags.value("|"));
147: }
148:
149: public void test_rdataFromString() throws IOException,
150: TextParseException {
151: // basic
152: KEYRecord kr = new KEYRecord();
153: Tokenizer st = new Tokenizer(
154: "NOAUTH|ZONE|FLAG10 EMAIL RSASHA1 AQIDBAUGBwgJ");
155: kr.rdataFromString(st, null);
156: assertEquals(KEYRecord.Flags.NOAUTH | KEYRecord.Flags.FLAG10
157: | KEYRecord.Flags.ZONE, kr.getFlags());
158: assertEquals(KEYRecord.Protocol.EMAIL, kr.getProtocol());
159: assertEquals(DNSSEC.Algorithm.RSASHA1, kr.getAlgorithm());
160: assertTrue(Arrays.equals(
161: new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, kr.getKey()));
162:
163: // basic w/o key
164: kr = new KEYRecord();
165: st = new Tokenizer("NOAUTH|NOKEY|FLAG10 TLS ECC");
166: kr.rdataFromString(st, null);
167: assertEquals(KEYRecord.Flags.NOAUTH | KEYRecord.Flags.FLAG10
168: | KEYRecord.Flags.NOKEY, kr.getFlags());
169: assertEquals(KEYRecord.Protocol.TLS, kr.getProtocol());
170: assertEquals(DNSSEC.Algorithm.ECC, kr.getAlgorithm());
171: assertNull(kr.getKey());
172:
173: // invalid flags
174: kr = new KEYRecord();
175: st = new Tokenizer(
176: "NOAUTH|ZONE|JUNK EMAIL RSASHA1 AQIDBAUGBwgJ");
177: try {
178: kr.rdataFromString(st, null);
179: fail("TextParseException not thrown");
180: } catch (TextParseException e) {
181: }
182:
183: // invalid protocol
184: kr = new KEYRecord();
185: st = new Tokenizer("NOAUTH|ZONE RSASHA1 ECC AQIDBAUGBwgJ");
186: try {
187: kr.rdataFromString(st, null);
188: fail("TextParseException not thrown");
189: } catch (TextParseException e) {
190: }
191:
192: // invalid algorithm
193: kr = new KEYRecord();
194: st = new Tokenizer("NOAUTH|ZONE EMAIL ZONE AQIDBAUGBwgJ");
195: try {
196: kr.rdataFromString(st, null);
197: fail("TextParseException not thrown");
198: } catch (TextParseException e) {
199: }
200: }
201: }
|