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.TestCase; 40 41 public class U16NameBaseTest extends TestCase 42 { assertEquals( byte[] exp, byte[] act )43 private void assertEquals( byte[] exp, byte[] act ) 44 { 45 assertTrue(java.util.Arrays.equals(exp, act)); 46 } 47 48 private static class TestClass extends U16NameBase 49 { TestClass()50 public TestClass(){} 51 TestClass(Name name, int type, int dclass, long ttl)52 public TestClass(Name name, int type, int dclass, long ttl) 53 { 54 super(name, type, dclass, ttl); 55 } 56 TestClass(Name name, int type, int dclass, long ttl, int u16Field, String u16Description, Name nameField, String nameDescription)57 public TestClass(Name name, int type, int dclass, long ttl, int u16Field, 58 String u16Description, Name nameField, String nameDescription) 59 { 60 super(name, type, dclass, ttl, u16Field, u16Description, nameField, nameDescription); 61 } 62 getU16Field()63 public int getU16Field() 64 { 65 return super.getU16Field(); 66 } 67 getNameField()68 public Name getNameField() 69 { 70 return super.getNameField(); 71 } 72 getObject()73 public Record getObject() 74 { 75 return null; 76 } 77 } 78 test_ctor_0arg()79 public void test_ctor_0arg() 80 { 81 TestClass tc = new TestClass(); 82 assertNull(tc.getName()); 83 assertEquals(0, tc.getType()); 84 assertEquals(0, tc.getDClass()); 85 assertEquals(0, tc.getTTL()); 86 assertEquals(0, tc.getU16Field()); 87 assertNull(tc.getNameField()); 88 } 89 test_ctor_4arg()90 public void test_ctor_4arg() throws TextParseException 91 { 92 Name n = Name.fromString("My.Name."); 93 94 TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xBCDA); 95 96 assertSame(n, tc.getName()); 97 assertEquals(Type.MX, tc.getType()); 98 assertEquals(DClass.IN, tc.getDClass()); 99 assertEquals(0xBCDA, tc.getTTL()); 100 assertEquals(0, tc.getU16Field()); 101 assertNull(tc.getNameField()); 102 } 103 test_ctor_8arg()104 public void test_ctor_8arg() throws TextParseException 105 { 106 Name n = Name.fromString("My.Name."); 107 Name m = Name.fromString("My.Other.Name."); 108 109 TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xB12FL, 110 0x1F2B, "u16 description", 111 m, "name description"); 112 113 assertSame(n, tc.getName()); 114 assertEquals(Type.MX, tc.getType()); 115 assertEquals(DClass.IN, tc.getDClass()); 116 assertEquals(0xB12FL, tc.getTTL()); 117 assertEquals(0x1F2B, tc.getU16Field()); 118 assertEquals(m, tc.getNameField()); 119 120 // an invalid u16 value 121 try { 122 new TestClass(n, Type.MX, DClass.IN, 0xB12FL, 123 0x10000, "u16 description", 124 m, "name description"); 125 fail("IllegalArgumentException not thrown"); 126 } 127 catch( IllegalArgumentException e ){} 128 129 // a relative name 130 Name rel = Name.fromString("My.relative.Name"); 131 try { 132 new TestClass(n, Type.MX, DClass.IN, 0xB12FL, 133 0x1F2B, "u16 description", 134 rel, "name description"); 135 fail("RelativeNameException not thrown"); 136 } 137 catch( RelativeNameException e ){} 138 139 } 140 test_rrFromWire()141 public void test_rrFromWire() throws IOException 142 { 143 byte[] raw = new byte[] { (byte)0xBC, (byte)0x1F, 2, 'M', 'y', 6, 's', 'i', 'N', 'g', 'l', 'E', 4, 'n', 'A', 'm', 'E', 0 }; 144 DNSInput in = new DNSInput(raw); 145 146 TestClass tc = new TestClass(); 147 tc.rrFromWire(in); 148 149 Name exp = Name.fromString("My.single.name."); 150 assertEquals(0xBC1FL, tc.getU16Field()); 151 assertEquals(exp, tc.getNameField()); 152 } 153 test_rdataFromString()154 public void test_rdataFromString() throws IOException 155 { 156 Name exp = Name.fromString("My.Single.Name."); 157 158 Tokenizer t = new Tokenizer(0x19A2 + " My.Single.Name."); 159 TestClass tc = new TestClass(); 160 tc.rdataFromString(t, null); 161 162 assertEquals(0x19A2, tc.getU16Field()); 163 assertEquals(exp, tc.getNameField()); 164 165 t = new Tokenizer("10 My.Relative.Name"); 166 tc = new TestClass(); 167 try { 168 tc.rdataFromString(t, null); 169 fail("RelativeNameException not thrown"); 170 } 171 catch( RelativeNameException e ){} 172 } 173 test_rrToString()174 public void test_rrToString() throws IOException, TextParseException 175 { 176 Name n = Name.fromString("My.Name."); 177 Name m = Name.fromString("My.Other.Name."); 178 179 TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xB12FL, 180 0x1F2B, "u16 description", 181 m, "name description"); 182 183 String out = tc.rrToString(); 184 String exp = 0x1F2B + " My.Other.Name."; 185 186 assertEquals(exp, out); 187 } 188 test_rrToWire()189 public void test_rrToWire() throws IOException, TextParseException 190 { 191 Name n = Name.fromString("My.Name."); 192 Name m = Name.fromString("M.O.n."); 193 194 TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xB12FL, 195 0x1F2B, "u16 description", 196 m, "name description"); 197 198 // canonical 199 DNSOutput dout = new DNSOutput(); 200 tc.rrToWire(dout, null, true); 201 byte[] out = dout.toByteArray(); 202 byte[] exp = new byte[] { 0x1F, 0x2B, 1, 'm', 1, 'o', 1, 'n', 0 }; 203 assertTrue(Arrays.equals(exp, out)); 204 205 // case sensitive 206 dout = new DNSOutput(); 207 tc.rrToWire(dout, null, false); 208 out = dout.toByteArray(); 209 exp = new byte[] { 0x1F, 0x2B, 1, 'M', 1, 'O', 1, 'n', 0 }; 210 assertTrue(Arrays.equals(exp, out)); 211 } 212 } 213