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.Test;
040: import junit.framework.TestCase;
041: import junit.framework.TestSuite;
042:
043: public class GPOSRecordTest extends TestCase {
044: public void test_ctor_0arg() {
045: GPOSRecord gr = new GPOSRecord();
046: assertNull(gr.getName());
047: assertEquals(0, gr.getType());
048: assertEquals(0, gr.getDClass());
049: assertEquals(0, gr.getTTL());
050: }
051:
052: public void test_getObject() {
053: GPOSRecord gr = new GPOSRecord();
054: Record r = gr.getObject();
055: assertTrue(r instanceof GPOSRecord);
056: }
057:
058: public static class Test_Ctor_6arg_doubles extends TestCase {
059: private Name m_n;
060: private long m_ttl;
061: private double m_lat, m_long, m_alt;
062:
063: protected void setUp() throws TextParseException {
064: m_n = Name.fromString("The.Name.");
065: m_ttl = 0xABCDL;
066: m_lat = -10.43;
067: m_long = 76.12;
068: m_alt = 100.101;
069: }
070:
071: public void test_basic() throws TextParseException {
072: GPOSRecord gr = new GPOSRecord(m_n, DClass.IN, m_ttl,
073: m_long, m_lat, m_alt);
074: assertEquals(m_n, gr.getName());
075: assertEquals(DClass.IN, gr.getDClass());
076: assertEquals(Type.GPOS, gr.getType());
077: assertEquals(m_ttl, gr.getTTL());
078: assertEquals(new Double(m_long), new Double(gr
079: .getLongitude()));
080: assertEquals(new Double(m_lat),
081: new Double(gr.getLatitude()));
082: assertEquals(new Double(m_alt),
083: new Double(gr.getAltitude()));
084: assertEquals(new Double(m_long).toString(), gr
085: .getLongitudeString());
086: assertEquals(new Double(m_lat).toString(), gr
087: .getLatitudeString());
088: assertEquals(new Double(m_alt).toString(), gr
089: .getAltitudeString());
090: }
091:
092: public void test_toosmall_longitude() throws TextParseException {
093: try {
094: new GPOSRecord(m_n, DClass.IN, m_ttl, -90.001, m_lat,
095: m_alt);
096: fail("IllegalArgumentException not thrown");
097: } catch (IllegalArgumentException e) {
098: }
099: }
100:
101: public void test_toobig_longitude() throws TextParseException {
102: try {
103: new GPOSRecord(m_n, DClass.IN, m_ttl, 90.001, m_lat,
104: m_alt);
105: fail("IllegalArgumentException not thrown");
106: } catch (IllegalArgumentException e) {
107: }
108: }
109:
110: public void test_toosmall_latitude() throws TextParseException {
111: try {
112: new GPOSRecord(m_n, DClass.IN, m_ttl, m_long, -180.001,
113: m_alt);
114: fail("IllegalArgumentException not thrown");
115: } catch (IllegalArgumentException e) {
116: }
117: }
118:
119: public void test_toobig_latitude() throws TextParseException {
120: try {
121: new GPOSRecord(m_n, DClass.IN, m_ttl, m_long, 180.001,
122: m_alt);
123: fail("IllegalArgumentException not thrown");
124: } catch (IllegalArgumentException e) {
125: }
126: }
127:
128: public void test_invalid_string() {
129: try {
130: new GPOSRecord(m_n, DClass.IN, m_ttl,
131: new Double(m_long).toString(), "120.\\00ABC",
132: new Double(m_alt).toString());
133: fail("IllegalArgumentException not thrown");
134: } catch (IllegalArgumentException e) {
135: }
136: }
137: }
138:
139: public static class Test_Ctor_6arg_Strings extends TestCase {
140: private Name m_n;
141: private long m_ttl;
142: private double m_lat, m_long, m_alt;
143:
144: protected void setUp() throws TextParseException {
145: m_n = Name.fromString("The.Name.");
146: m_ttl = 0xABCDL;
147: m_lat = -10.43;
148: m_long = 76.12;
149: m_alt = 100.101;
150: }
151:
152: public void test_basic() throws TextParseException {
153: GPOSRecord gr = new GPOSRecord(m_n, DClass.IN, m_ttl,
154: new Double(m_long).toString(), new Double(m_lat)
155: .toString(), new Double(m_alt).toString());
156: assertEquals(m_n, gr.getName());
157: assertEquals(DClass.IN, gr.getDClass());
158: assertEquals(Type.GPOS, gr.getType());
159: assertEquals(m_ttl, gr.getTTL());
160: assertEquals(new Double(m_long), new Double(gr
161: .getLongitude()));
162: assertEquals(new Double(m_lat),
163: new Double(gr.getLatitude()));
164: assertEquals(new Double(m_alt),
165: new Double(gr.getAltitude()));
166: assertEquals(new Double(m_long).toString(), gr
167: .getLongitudeString());
168: assertEquals(new Double(m_lat).toString(), gr
169: .getLatitudeString());
170: assertEquals(new Double(m_alt).toString(), gr
171: .getAltitudeString());
172: }
173:
174: public void test_toosmall_longitude() throws TextParseException {
175: try {
176: new GPOSRecord(m_n, DClass.IN, m_ttl, "-90.001",
177: new Double(m_lat).toString(), new Double(m_alt)
178: .toString());
179: fail("IllegalArgumentException not thrown");
180: } catch (IllegalArgumentException e) {
181: }
182: }
183:
184: public void test_toobig_longitude() throws TextParseException {
185: try {
186: new GPOSRecord(m_n, DClass.IN, m_ttl, "90.001",
187: new Double(m_lat).toString(), new Double(m_alt)
188: .toString());
189: fail("IllegalArgumentException not thrown");
190: } catch (IllegalArgumentException e) {
191: }
192: }
193:
194: public void test_toosmall_latitude() throws TextParseException {
195: try {
196: new GPOSRecord(m_n, DClass.IN, m_ttl,
197: new Double(m_long).toString(), "-180.001",
198: new Double(m_alt).toString());
199: fail("IllegalArgumentException not thrown");
200: } catch (IllegalArgumentException e) {
201: }
202: }
203:
204: public void test_toobig_latitude() throws TextParseException {
205: try {
206: new GPOSRecord(m_n, DClass.IN, m_ttl,
207: new Double(m_long).toString(), "180.001",
208: new Double(m_alt).toString());
209: fail("IllegalArgumentException not thrown");
210: } catch (IllegalArgumentException e) {
211: }
212: }
213: }
214:
215: public static class Test_rrFromWire extends TestCase {
216: public void test_basic() throws IOException {
217: byte[] raw = new byte[] { 5, '-', '8', '.', '1', '2', 6,
218: '1', '2', '3', '.', '0', '7', 3, '0', '.', '0' };
219: DNSInput in = new DNSInput(raw);
220:
221: GPOSRecord gr = new GPOSRecord();
222: gr.rrFromWire(in);
223: assertEquals(new Double(-8.12), new Double(gr
224: .getLongitude()));
225: assertEquals(new Double(123.07), new Double(gr
226: .getLatitude()));
227: assertEquals(new Double(0.0), new Double(gr.getAltitude()));
228: }
229:
230: public void test_longitude_toosmall() throws IOException {
231: byte[] raw = new byte[] { 5, '-', '9', '5', '.', '0', 6,
232: '1', '2', '3', '.', '0', '7', 3, '0', '.', '0' };
233: DNSInput in = new DNSInput(raw);
234:
235: GPOSRecord gr = new GPOSRecord();
236: try {
237: gr.rrFromWire(in);
238: fail("WireParseException not thrown");
239: } catch (WireParseException e) {
240: }
241: }
242:
243: public void test_longitude_toobig() throws IOException {
244: byte[] raw = new byte[] { 5, '1', '8', '5', '.', '0', 6,
245: '1', '2', '3', '.', '0', '7', 3, '0', '.', '0' };
246: DNSInput in = new DNSInput(raw);
247:
248: GPOSRecord gr = new GPOSRecord();
249: try {
250: gr.rrFromWire(in);
251: fail("WireParseException not thrown");
252: } catch (WireParseException e) {
253: }
254: }
255:
256: public void test_latitude_toosmall() throws IOException {
257: byte[] raw = new byte[] { 5, '-', '8', '5', '.', '0', 6,
258: '-', '1', '9', '0', '.', '0', 3, '0', '.', '0' };
259: DNSInput in = new DNSInput(raw);
260:
261: GPOSRecord gr = new GPOSRecord();
262: try {
263: gr.rrFromWire(in);
264: fail("WireParseException not thrown");
265: } catch (WireParseException e) {
266: }
267: }
268:
269: public void test_latitude_toobig() throws IOException {
270: byte[] raw = new byte[] { 5, '-', '8', '5', '.', '0', 6,
271: '2', '1', '9', '0', '.', '0', 3, '0', '.', '0' };
272: DNSInput in = new DNSInput(raw);
273:
274: GPOSRecord gr = new GPOSRecord();
275: try {
276: gr.rrFromWire(in);
277: fail("WireParseException not thrown");
278: } catch (WireParseException e) {
279: }
280: }
281: }
282:
283: public static class Test_rdataFromString extends TestCase {
284: public void test_basic() throws IOException {
285: Tokenizer t = new Tokenizer("10.45 171.121212 1010787");
286:
287: GPOSRecord gr = new GPOSRecord();
288: gr.rdataFromString(t, null);
289: assertEquals(new Double(10.45), new Double(gr
290: .getLongitude()));
291: assertEquals(new Double(171.121212), new Double(gr
292: .getLatitude()));
293: assertEquals(new Double(1010787), new Double(gr
294: .getAltitude()));
295: }
296:
297: public void test_longitude_toosmall() throws IOException {
298: Tokenizer t = new Tokenizer("-100.390 171.121212 1010787");
299:
300: GPOSRecord gr = new GPOSRecord();
301: try {
302: gr.rdataFromString(t, null);
303: fail("IOException not thrown");
304: } catch (IOException e) {
305: }
306: }
307:
308: public void test_longitude_toobig() throws IOException {
309: Tokenizer t = new Tokenizer("90.00001 171.121212 1010787");
310:
311: GPOSRecord gr = new GPOSRecord();
312: try {
313: gr.rdataFromString(t, null);
314: fail("IOException not thrown");
315: } catch (IOException e) {
316: }
317: }
318:
319: public void test_latitude_toosmall() throws IOException {
320: Tokenizer t = new Tokenizer("0.0 -180.01 1010787");
321:
322: GPOSRecord gr = new GPOSRecord();
323: try {
324: gr.rdataFromString(t, null);
325: fail("IOException not thrown");
326: } catch (IOException e) {
327: }
328: }
329:
330: public void test_latitude_toobig() throws IOException {
331: Tokenizer t = new Tokenizer("0.0 180.01 1010787");
332:
333: GPOSRecord gr = new GPOSRecord();
334: try {
335: gr.rdataFromString(t, null);
336: fail("IOException not thrown");
337: } catch (IOException e) {
338: }
339: }
340:
341: public void test_invalid_string() throws IOException {
342: Tokenizer t = new Tokenizer("1.0 2.0 \\435");
343: try {
344: GPOSRecord gr = new GPOSRecord();
345: gr.rdataFromString(t, null);
346: } catch (TextParseException e) {
347: }
348: }
349: }
350:
351: public void test_rrToString() throws TextParseException {
352: String exp = "\"10.45\" \"171.121212\" \"1010787.0\"";
353:
354: GPOSRecord gr = new GPOSRecord(Name.fromString("The.Name."),
355: DClass.IN, 0x123, 10.45, 171.121212, 1010787);
356: assertEquals(exp, gr.rrToString());
357: }
358:
359: public void test_rrToWire() throws TextParseException {
360: GPOSRecord gr = new GPOSRecord(Name.fromString("The.Name."),
361: DClass.IN, 0x123, -10.45, 120.0, 111.0);
362:
363: byte[] exp = new byte[] { 6, '-', '1', '0', '.', '4', '5', 5,
364: '1', '2', '0', '.', '0', 5, '1', '1', '1', '.', '0' };
365:
366: DNSOutput out = new DNSOutput();
367: gr.rrToWire(out, null, true);
368:
369: byte[] bar = out.toByteArray();
370:
371: assertEquals(exp.length, bar.length);
372: for (int i = 0; i < exp.length; ++i) {
373: assertEquals("i=" + i, exp[i], bar[i]);
374: }
375: }
376:
377: public static Test suite() {
378: TestSuite s = new TestSuite();
379: s.addTestSuite(Test_Ctor_6arg_doubles.class);
380: s.addTestSuite(Test_Ctor_6arg_Strings.class);
381: s.addTestSuite(Test_rrFromWire.class);
382: s.addTestSuite(Test_rdataFromString.class);
383: s.addTestSuite(GPOSRecordTest.class);
384: return s;
385: }
386: }
|