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.Test; 40 import junit.framework.TestCase; 41 import junit.framework.TestSuite; 42 43 public class GPOSRecordTest extends TestCase 44 { test_ctor_0arg()45 public void test_ctor_0arg() 46 { 47 GPOSRecord gr = new GPOSRecord(); 48 assertNull(gr.getName()); 49 assertEquals(0, gr.getType()); 50 assertEquals(0, gr.getDClass()); 51 assertEquals(0, gr.getTTL()); 52 } 53 test_getObject()54 public void test_getObject() 55 { 56 GPOSRecord gr = new GPOSRecord(); 57 Record r = gr.getObject(); 58 assertTrue(r instanceof GPOSRecord); 59 } 60 61 public static class Test_Ctor_6arg_doubles extends TestCase 62 { 63 private Name m_n; 64 private long m_ttl; 65 private double m_lat, m_long, m_alt; 66 setUp()67 protected void setUp() throws TextParseException 68 { 69 m_n = Name.fromString("The.Name."); 70 m_ttl = 0xABCDL; 71 m_lat = -10.43; 72 m_long = 76.12; 73 m_alt = 100.101; 74 } 75 test_basic()76 public void test_basic() throws TextParseException 77 { 78 GPOSRecord gr = new GPOSRecord(m_n, DClass.IN, m_ttl, 79 m_long, m_lat, m_alt); 80 assertEquals(m_n, gr.getName()); 81 assertEquals(DClass.IN, gr.getDClass()); 82 assertEquals(Type.GPOS, gr.getType()); 83 assertEquals(m_ttl, gr.getTTL()); 84 assertEquals(new Double(m_long), new Double(gr.getLongitude())); 85 assertEquals(new Double(m_lat), new Double(gr.getLatitude())); 86 assertEquals(new Double(m_alt), new Double(gr.getAltitude())); 87 assertEquals(new Double(m_long).toString(), gr.getLongitudeString()); 88 assertEquals(new Double(m_lat).toString(), gr.getLatitudeString()); 89 assertEquals(new Double(m_alt).toString(), gr.getAltitudeString()); 90 } 91 test_toosmall_longitude()92 public void test_toosmall_longitude() throws TextParseException 93 { 94 try { 95 new GPOSRecord(m_n, DClass.IN, m_ttl, 96 -90.001, m_lat, m_alt); 97 fail("IllegalArgumentException not thrown"); 98 } 99 catch(IllegalArgumentException e){} 100 } 101 test_toobig_longitude()102 public void test_toobig_longitude() throws TextParseException 103 { 104 try { 105 new GPOSRecord(m_n, DClass.IN, m_ttl, 106 90.001, m_lat, m_alt); 107 fail("IllegalArgumentException not thrown"); 108 } 109 catch(IllegalArgumentException e){} 110 } 111 test_toosmall_latitude()112 public void test_toosmall_latitude() throws TextParseException 113 { 114 try { 115 new GPOSRecord(m_n, DClass.IN, m_ttl, 116 m_long, -180.001, m_alt); 117 fail("IllegalArgumentException not thrown"); 118 } 119 catch(IllegalArgumentException e){} 120 } 121 test_toobig_latitude()122 public void test_toobig_latitude() throws TextParseException 123 { 124 try { 125 new GPOSRecord(m_n, DClass.IN, m_ttl, 126 m_long, 180.001, m_alt); 127 fail("IllegalArgumentException not thrown"); 128 } 129 catch(IllegalArgumentException e){} 130 } 131 test_invalid_string()132 public void test_invalid_string() 133 { 134 try { 135 new GPOSRecord(m_n, DClass.IN, m_ttl, 136 new Double(m_long).toString(), 137 "120.\\00ABC", new Double(m_alt).toString()); 138 fail("IllegalArgumentException not thrown"); 139 } 140 catch(IllegalArgumentException e){} 141 } 142 } 143 144 public static class Test_Ctor_6arg_Strings extends TestCase 145 { 146 private Name m_n; 147 private long m_ttl; 148 private double m_lat, m_long, m_alt; 149 setUp()150 protected void setUp() throws TextParseException 151 { 152 m_n = Name.fromString("The.Name."); 153 m_ttl = 0xABCDL; 154 m_lat = -10.43; 155 m_long = 76.12; 156 m_alt = 100.101; 157 } 158 test_basic()159 public void test_basic() throws TextParseException 160 { 161 GPOSRecord gr = new GPOSRecord(m_n, DClass.IN, m_ttl, 162 new Double(m_long).toString(), 163 new Double(m_lat).toString(), 164 new Double(m_alt).toString()); 165 assertEquals(m_n, gr.getName()); 166 assertEquals(DClass.IN, gr.getDClass()); 167 assertEquals(Type.GPOS, gr.getType()); 168 assertEquals(m_ttl, gr.getTTL()); 169 assertEquals(new Double(m_long), new Double(gr.getLongitude())); 170 assertEquals(new Double(m_lat), new Double(gr.getLatitude())); 171 assertEquals(new Double(m_alt), new Double(gr.getAltitude())); 172 assertEquals(new Double(m_long).toString(), gr.getLongitudeString()); 173 assertEquals(new Double(m_lat).toString(), gr.getLatitudeString()); 174 assertEquals(new Double(m_alt).toString(), gr.getAltitudeString()); 175 } 176 test_toosmall_longitude()177 public void test_toosmall_longitude() throws TextParseException 178 { 179 try { 180 new GPOSRecord(m_n, DClass.IN, m_ttl, 181 "-90.001", new Double(m_lat).toString(), 182 new Double(m_alt).toString()); 183 fail("IllegalArgumentException not thrown"); 184 } 185 catch(IllegalArgumentException e){} 186 } 187 test_toobig_longitude()188 public void test_toobig_longitude() throws TextParseException 189 { 190 try { 191 new GPOSRecord(m_n, DClass.IN, m_ttl, 192 "90.001", new Double(m_lat).toString(), 193 new Double(m_alt).toString()); 194 fail("IllegalArgumentException not thrown"); 195 } 196 catch(IllegalArgumentException e){} 197 } 198 test_toosmall_latitude()199 public void test_toosmall_latitude() throws TextParseException 200 { 201 try { 202 new GPOSRecord(m_n, DClass.IN, m_ttl, 203 new Double(m_long).toString(), "-180.001", 204 new Double(m_alt).toString()); 205 fail("IllegalArgumentException not thrown"); 206 } 207 catch(IllegalArgumentException e){} 208 } 209 test_toobig_latitude()210 public void test_toobig_latitude() throws TextParseException 211 { 212 try { 213 new GPOSRecord(m_n, DClass.IN, m_ttl, 214 new Double(m_long).toString(), "180.001", new Double(m_alt).toString()); 215 fail("IllegalArgumentException not thrown"); 216 } 217 catch(IllegalArgumentException e){} 218 } 219 } 220 221 public static class Test_rrFromWire extends TestCase 222 { test_basic()223 public void test_basic() throws IOException 224 { 225 byte[] raw = new byte[] { 5, '-', '8', '.', '1', '2', 226 6, '1', '2', '3', '.', '0', '7', 227 3, '0', '.', '0' }; 228 DNSInput in = new DNSInput(raw); 229 230 GPOSRecord gr = new GPOSRecord(); 231 gr.rrFromWire(in); 232 assertEquals(new Double(-8.12), new Double(gr.getLongitude())); 233 assertEquals(new Double(123.07), new Double(gr.getLatitude())); 234 assertEquals(new Double(0.0), new Double(gr.getAltitude())); 235 } 236 test_longitude_toosmall()237 public void test_longitude_toosmall() throws IOException 238 { 239 byte[] raw = new byte[] { 5, '-', '9', '5', '.', '0', 240 6, '1', '2', '3', '.', '0', '7', 241 3, '0', '.', '0' }; 242 DNSInput in = new DNSInput(raw); 243 244 GPOSRecord gr = new GPOSRecord(); 245 try { 246 gr.rrFromWire(in); 247 fail("WireParseException not thrown"); 248 } 249 catch(WireParseException e){} 250 } 251 test_longitude_toobig()252 public void test_longitude_toobig() throws IOException 253 { 254 byte[] raw = new byte[] { 5, '1', '8', '5', '.', '0', 255 6, '1', '2', '3', '.', '0', '7', 256 3, '0', '.', '0' }; 257 DNSInput in = new DNSInput(raw); 258 259 GPOSRecord gr = new GPOSRecord(); 260 try { 261 gr.rrFromWire(in); 262 fail("WireParseException not thrown"); 263 } 264 catch(WireParseException e){} 265 } 266 test_latitude_toosmall()267 public void test_latitude_toosmall() throws IOException 268 { 269 byte[] raw = new byte[] { 5, '-', '8', '5', '.', '0', 270 6, '-', '1', '9', '0', '.', '0', 271 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 } 279 catch(WireParseException e){} 280 } 281 test_latitude_toobig()282 public void test_latitude_toobig() throws IOException 283 { 284 byte[] raw = new byte[] { 5, '-', '8', '5', '.', '0', 285 6, '2', '1', '9', '0', '.', '0', 286 3, '0', '.', '0' }; 287 DNSInput in = new DNSInput(raw); 288 289 GPOSRecord gr = new GPOSRecord(); 290 try { 291 gr.rrFromWire(in); 292 fail("WireParseException not thrown"); 293 } 294 catch(WireParseException e){} 295 } 296 } 297 298 public static class Test_rdataFromString extends TestCase 299 { test_basic()300 public void test_basic() throws IOException 301 { 302 Tokenizer t = new Tokenizer("10.45 171.121212 1010787"); 303 304 GPOSRecord gr = new GPOSRecord(); 305 gr.rdataFromString(t, null); 306 assertEquals(new Double(10.45), new Double(gr.getLongitude())); 307 assertEquals(new Double(171.121212), new Double(gr.getLatitude())); 308 assertEquals(new Double(1010787), new Double(gr.getAltitude())); 309 } 310 test_longitude_toosmall()311 public void test_longitude_toosmall() throws IOException 312 { 313 Tokenizer t = new Tokenizer("-100.390 171.121212 1010787"); 314 315 GPOSRecord gr = new GPOSRecord(); 316 try { 317 gr.rdataFromString(t, null); 318 fail("IOException not thrown"); 319 } 320 catch(IOException e){} 321 } 322 test_longitude_toobig()323 public void test_longitude_toobig() throws IOException 324 { 325 Tokenizer t = new Tokenizer("90.00001 171.121212 1010787"); 326 327 GPOSRecord gr = new GPOSRecord(); 328 try { 329 gr.rdataFromString(t, null); 330 fail("IOException not thrown"); 331 } 332 catch(IOException e){} 333 } 334 test_latitude_toosmall()335 public void test_latitude_toosmall() throws IOException 336 { 337 Tokenizer t = new Tokenizer("0.0 -180.01 1010787"); 338 339 GPOSRecord gr = new GPOSRecord(); 340 try { 341 gr.rdataFromString(t, null); 342 fail("IOException not thrown"); 343 } 344 catch(IOException e){} 345 } 346 test_latitude_toobig()347 public void test_latitude_toobig() throws IOException 348 { 349 Tokenizer t = new Tokenizer("0.0 180.01 1010787"); 350 351 GPOSRecord gr = new GPOSRecord(); 352 try { 353 gr.rdataFromString(t, null); 354 fail("IOException not thrown"); 355 } 356 catch(IOException e){} 357 } 358 test_invalid_string()359 public void test_invalid_string() throws IOException 360 { 361 Tokenizer t = new Tokenizer("1.0 2.0 \\435"); 362 try { 363 GPOSRecord gr = new GPOSRecord(); 364 gr.rdataFromString(t, null); 365 } 366 catch(TextParseException e){}} 367 } 368 test_rrToString()369 public void test_rrToString() throws TextParseException 370 { 371 String exp = "\"10.45\" \"171.121212\" \"1010787.0\""; 372 373 GPOSRecord gr = new GPOSRecord(Name.fromString("The.Name."), DClass.IN, 0x123, 374 10.45, 171.121212, 1010787); 375 assertEquals(exp, gr.rrToString()); 376 } 377 test_rrToWire()378 public void test_rrToWire() throws TextParseException 379 { 380 GPOSRecord gr = new GPOSRecord(Name.fromString("The.Name."), DClass.IN, 0x123, 381 -10.45, 120.0, 111.0); 382 383 byte[] exp = new byte[] { 6, '-', '1', '0', '.', '4', '5', 384 5, '1', '2', '0', '.', '0', 385 5, '1', '1', '1', '.', '0' }; 386 387 DNSOutput out = new DNSOutput(); 388 gr.rrToWire(out, null, true); 389 390 byte[] bar = out.toByteArray(); 391 392 assertEquals(exp.length, bar.length); 393 for( int i=0; i<exp.length; ++i){ 394 assertEquals("i=" + i, exp[i], bar[i]); 395 } 396 } 397 suite()398 public static Test suite() 399 { 400 TestSuite s = new TestSuite(); 401 s.addTestSuite(Test_Ctor_6arg_doubles.class); 402 s.addTestSuite(Test_Ctor_6arg_Strings.class); 403 s.addTestSuite(Test_rrFromWire.class); 404 s.addTestSuite(Test_rdataFromString.class); 405 s.addTestSuite(GPOSRecordTest.class); 406 return s; 407 } 408 } 409