3
// Copyright (c) 2005, Matthew J. Rutherford <rutherfo@cs.colorado.edu>
4
// Copyright (c) 2005, University of Colorado at Boulder
5
// All rights reserved.
7
// Redistribution and use in source and binary forms, with or without
8
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
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.
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
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.
35
package org.xbill.DNS;
37
import java.io.IOException;
38
import java.util.Arrays;
39
import junit.framework.TestCase;
41
public class U16NameBaseTest extends TestCase
43
private void assertEquals( byte[] exp, byte[] act )
45
assertTrue(java.util.Arrays.equals(exp, act));
48
private static class TestClass extends U16NameBase
52
public TestClass(Name name, int type, int dclass, long ttl)
54
super(name, type, dclass, ttl);
57
public TestClass(Name name, int type, int dclass, long ttl, int u16Field,
58
String u16Description, Name nameField, String nameDescription)
60
super(name, type, dclass, ttl, u16Field, u16Description, nameField, nameDescription);
63
public int getU16Field()
65
return super.getU16Field();
68
public Name getNameField()
70
return super.getNameField();
73
public Record getObject()
79
public void test_ctor_0arg()
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());
90
public void test_ctor_4arg() throws TextParseException
92
Name n = Name.fromString("My.Name.");
94
TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xBCDA);
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());
104
public void test_ctor_8arg() throws TextParseException
106
Name n = Name.fromString("My.Name.");
107
Name m = Name.fromString("My.Other.Name.");
109
TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xB12FL,
110
0x1F2B, "u16 description",
111
m, "name description");
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());
120
// an invalid u16 value
122
new TestClass(n, Type.MX, DClass.IN, 0xB12FL,
123
0x10000, "u16 description",
124
m, "name description");
125
fail("IllegalArgumentException not thrown");
127
catch( IllegalArgumentException e ){}
130
Name rel = Name.fromString("My.relative.Name");
132
new TestClass(n, Type.MX, DClass.IN, 0xB12FL,
133
0x1F2B, "u16 description",
134
rel, "name description");
135
fail("RelativeNameException not thrown");
137
catch( RelativeNameException e ){}
141
public void test_rrFromWire() throws IOException
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);
146
TestClass tc = new TestClass();
149
Name exp = Name.fromString("My.single.name.");
150
assertEquals(0xBC1FL, tc.getU16Field());
151
assertEquals(exp, tc.getNameField());
154
public void test_rdataFromString() throws IOException
156
Name exp = Name.fromString("My.Single.Name.");
158
Tokenizer t = new Tokenizer(0x19A2 + " My.Single.Name.");
159
TestClass tc = new TestClass();
160
tc.rdataFromString(t, null);
162
assertEquals(0x19A2, tc.getU16Field());
163
assertEquals(exp, tc.getNameField());
165
t = new Tokenizer("10 My.Relative.Name");
166
tc = new TestClass();
168
tc.rdataFromString(t, null);
169
fail("RelativeNameException not thrown");
171
catch( RelativeNameException e ){}
174
public void test_rrToString() throws IOException, TextParseException
176
Name n = Name.fromString("My.Name.");
177
Name m = Name.fromString("My.Other.Name.");
179
TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xB12FL,
180
0x1F2B, "u16 description",
181
m, "name description");
183
String out = tc.rrToString();
184
String exp = 0x1F2B + " My.Other.Name.";
186
assertEquals(exp, out);
189
public void test_rrToWire() throws IOException, TextParseException
191
Name n = Name.fromString("My.Name.");
192
Name m = Name.fromString("M.O.n.");
194
TestClass tc = new TestClass(n, Type.MX, DClass.IN, 0xB12FL,
195
0x1F2B, "u16 description",
196
m, "name description");
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));
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));