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.net.InetAddress;
39 import	java.net.UnknownHostException;
40 import	java.util.Arrays;
41 import	junit.framework.TestCase;
42 
43 public class DNSKEYRecordTest extends TestCase
44 {
test_ctor_0arg()45     public void test_ctor_0arg() throws UnknownHostException
46     {
47 	DNSKEYRecord ar = new DNSKEYRecord();
48 	assertNull(ar.getName());
49 	assertEquals(0, ar.getType());
50 	assertEquals(0, ar.getDClass());
51 	assertEquals(0, ar.getTTL());
52 	assertEquals(0, ar.getAlgorithm());
53 	assertEquals(0, ar.getFlags());
54 	assertEquals(0, ar.getFootprint());
55 	assertEquals(0, ar.getProtocol());
56 	assertNull(ar.getKey());
57     }
58 
test_getObject()59     public void test_getObject()
60     {
61 	DNSKEYRecord ar = new DNSKEYRecord();
62 	Record r = ar.getObject();
63 	assertTrue(r instanceof DNSKEYRecord);
64     }
65 
test_ctor_7arg()66     public void test_ctor_7arg() throws TextParseException
67     {
68 	Name n = Name.fromString("My.Absolute.Name.");
69 	Name r = Name.fromString("My.Relative.Name");
70 	byte[] key = new byte[] { 0, 1, 3, 5, 7, 9 };
71 
72 	DNSKEYRecord kr = new DNSKEYRecord(n, DClass.IN, 0x24AC, 0x9832, 0x12, 0x67, key);
73 	assertEquals(n, kr.getName());
74 	assertEquals(Type.DNSKEY, kr.getType());
75 	assertEquals(DClass.IN, kr.getDClass());
76 	assertEquals(0x24AC, kr.getTTL());
77 	assertEquals(0x9832, kr.getFlags());
78 	assertEquals(0x12, kr.getProtocol());
79 	assertEquals(0x67, kr.getAlgorithm());
80 	assertTrue(Arrays.equals(key, kr.getKey()));
81 
82 	// a relative name
83 	try {
84 	    new DNSKEYRecord(r, DClass.IN, 0x24AC, 0x9832, 0x12, 0x67, key);
85 	    fail("RelativeNameException not thrown");
86 	}
87 	catch( RelativeNameException e ){}
88     }
89 
test_rdataFromString()90     public void test_rdataFromString() throws IOException, TextParseException
91     {
92 	// basic
93 	DNSKEYRecord kr = new DNSKEYRecord();
94 	Tokenizer st = new Tokenizer(0xABCD + " " + 0x81 + " RSASHA1 AQIDBAUGBwgJ");
95 	kr.rdataFromString(st, null);
96 	assertEquals(0xABCD, kr.getFlags());
97 	assertEquals(0x81, kr.getProtocol());
98 	assertEquals(DNSSEC.Algorithm.RSASHA1, kr.getAlgorithm());
99 	assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, kr.getKey()));
100 
101 	// invalid algorithm
102 	kr = new DNSKEYRecord();
103 	st = new Tokenizer(0x1212 + " " + 0xAA + " ZONE AQIDBAUGBwgJ");
104 	try {
105 	    kr.rdataFromString(st, null);
106 	    fail("TextParseException not thrown");
107 	}
108 	catch( TextParseException e ){}
109     }
110 }
111