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.TestCase;
040: import org.xbill.DNS.utils.base64;
041:
042: public class KEYBaseTest extends TestCase {
043: private static class TestClass extends KEYBase {
044: public TestClass() {
045: }
046:
047: public TestClass(Name name, int type, int dclass, long ttl,
048: int flags, int proto, int alg, byte[] key) {
049: super (name, type, dclass, ttl, flags, proto, alg, key);
050: }
051:
052: public Record getObject() {
053: return null;
054: }
055:
056: void rdataFromString(Tokenizer st, Name origin)
057: throws IOException {
058: }
059: }
060:
061: public void test_ctor() throws TextParseException {
062: TestClass tc = new TestClass();
063: assertEquals(0, tc.getFlags());
064: assertEquals(0, tc.getProtocol());
065: assertEquals(0, tc.getAlgorithm());
066: assertNull(tc.getKey());
067:
068: Name n = Name.fromString("my.name.");
069: byte[] key = new byte[] { 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
070: 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
071:
072: tc = new TestClass(n, Type.KEY, DClass.IN, 100L, 0xFF, 0xF,
073: 0xE, key);
074:
075: assertSame(n, tc.getName());
076: assertEquals(Type.KEY, tc.getType());
077: assertEquals(DClass.IN, tc.getDClass());
078: assertEquals(100L, tc.getTTL());
079: assertEquals(0xFF, tc.getFlags());
080: assertEquals(0xF, tc.getProtocol());
081: assertEquals(0xE, tc.getAlgorithm());
082: assertTrue(Arrays.equals(key, tc.getKey()));
083: }
084:
085: public void test_rrFromWire() throws IOException {
086: byte[] raw = new byte[] { (byte) 0xAB, (byte) 0xCD,
087: (byte) 0xEF, (byte) 0x19, 1, 2, 3, 4, 5 };
088: DNSInput in = new DNSInput(raw);
089:
090: TestClass tc = new TestClass();
091: tc.rrFromWire(in);
092:
093: assertEquals(0xABCD, tc.getFlags());
094: assertEquals(0xEF, tc.getProtocol());
095: assertEquals(0x19, tc.getAlgorithm());
096: assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 4, 5 }, tc
097: .getKey()));
098:
099: raw = new byte[] { (byte) 0xBA, (byte) 0xDA, (byte) 0xFF,
100: (byte) 0x28 };
101: in = new DNSInput(raw);
102:
103: tc = new TestClass();
104: tc.rrFromWire(in);
105:
106: assertEquals(0xBADA, tc.getFlags());
107: assertEquals(0xFF, tc.getProtocol());
108: assertEquals(0x28, tc.getAlgorithm());
109: assertNull(tc.getKey());
110: }
111:
112: public void test_rrToString() throws IOException,
113: TextParseException {
114: Name n = Name.fromString("my.name.");
115: byte[] key = new byte[] { 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
116: 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
117:
118: TestClass tc = new TestClass(n, Type.KEY, DClass.IN, 100L,
119: 0xFF, 0xF, 0xE, null);
120:
121: String out = tc.rrToString();
122:
123: assertEquals("255 15 14", out);
124:
125: tc = new TestClass(n, Type.KEY, DClass.IN, 100L, 0xFF, 0xF,
126: 0xE, key);
127: out = tc.rrToString();
128:
129: assertEquals("255 15 14 " + base64.toString(key), out);
130:
131: Options.set("multiline");
132: out = tc.rrToString();
133: assertEquals("255 15 14 (\n\t" + base64.toString(key)
134: + " ) ; key_tag = 18509", out);
135:
136: Options.unset("multiline");
137: }
138:
139: public void test_getFootprint() throws TextParseException {
140: Name n = Name.fromString("my.name.");
141: byte[] key = new byte[] { 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
142: 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
143:
144: TestClass tc = new TestClass(n, Type.KEY, DClass.IN, 100L,
145: 0xFF, 0xF, DNSSEC.Algorithm.RSAMD5, key);
146:
147: int foot = tc.getFootprint();
148: // second-to-last and third-to-last bytes of key for RSAMD5
149: assertEquals(0xD0E, foot);
150: assertEquals(foot, tc.getFootprint());
151:
152: // key with an odd number of bytes
153: tc = new TestClass(n, Type.KEY, DClass.IN, 100L, 0x89AB, 0xCD,
154: 0xEF, new byte[] { 0x12, 0x34, 0x56 });
155:
156: // rrToWire gives: { 0x89, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56 }
157: // 89AB + CDEF + 1234 + 5600 = 1BCFE
158: // 1BFCE + 1 = 1BFCF & FFFF = BFCF
159: foot = tc.getFootprint();
160: assertEquals(0xBFCF, foot);
161: assertEquals(foot, tc.getFootprint());
162:
163: // empty
164: tc = new TestClass();
165: assertEquals(0, tc.getFootprint());
166: }
167:
168: public void test_rrToWire() throws IOException, TextParseException {
169: Name n = Name.fromString("my.name.");
170: byte[] key = new byte[] { 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
171: 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
172:
173: TestClass tc = new TestClass(n, Type.KEY, DClass.IN, 100L,
174: 0x7689, 0xAB, 0xCD, key);
175:
176: byte[] exp = new byte[] { (byte) 0x76, (byte) 0x89,
177: (byte) 0xAB, (byte) 0xCD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
178: 10, 11, 12, 13, 14, 15 };
179:
180: DNSOutput o = new DNSOutput();
181:
182: // canonical
183: tc.rrToWire(o, null, true);
184: assertTrue(Arrays.equals(exp, o.toByteArray()));
185:
186: // not canonical
187: o = new DNSOutput();
188: tc.rrToWire(o, null, false);
189: assertTrue(Arrays.equals(exp, o.toByteArray()));
190: }
191: }
|