DatagramPacketTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.net; 19 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetNew; 24 25import java.io.IOException; 26import java.net.DatagramPacket; 27import java.net.DatagramSocket; 28import java.net.InetAddress; 29import java.net.InetSocketAddress; 30import java.net.SocketAddress; 31import java.net.SocketException; 32import java.net.UnknownHostException; 33 34import tests.support.Support_Configuration; 35import tests.support.Support_PortManager; 36 37@TestTargetClass(DatagramPacket.class) 38public class DatagramPacketTest extends junit.framework.TestCase { 39 40 DatagramPacket dp; 41 42 volatile boolean started = false; 43 44 /** 45 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int) 46 */ 47 @TestTargetNew( 48 level = TestLevel.COMPLETE, 49 notes = "", 50 method = "DatagramPacket", 51 args = {byte[].class, int.class} 52 ) 53 public void test_Constructor$BI() { 54 // Test for method java.net.DatagramPacket(byte [], int) 55 try { 56 dp = new DatagramPacket("Hello".getBytes(), 5); 57 assertEquals("Created incorrect packet", "Hello", new String(dp.getData(), 0, 58 dp.getData().length)); 59 assertEquals("Wrong length", 5, dp.getLength()); 60 } catch (Exception e) { 61 fail("Exception during Constructor test: " + e.toString()); 62 } 63 //regression for Harmony-890 64 dp = new DatagramPacket(new byte[942],4); 65 assertEquals(-1, dp.getPort()); 66 try{ 67 dp.getSocketAddress(); 68 fail("Should throw IllegalArgumentException"); 69 }catch(IllegalArgumentException e){ 70 //expected 71 } 72 } 73 74 /** 75 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int) 76 */ 77 @TestTargetNew( 78 level = TestLevel.COMPLETE, 79 notes = "", 80 method = "DatagramPacket", 81 args = {byte[].class, int.class, int.class} 82 ) 83 public void test_Constructor$BII() { 84 try { 85 dp = new DatagramPacket("Hello".getBytes(), 2, 3); 86 assertEquals("Created incorrect packet", "Hello", new String(dp.getData(), 0, 87 dp.getData().length)); 88 assertEquals("Wrong length", 3, dp.getLength()); 89 assertEquals("Wrong offset", 2, dp.getOffset()); 90 } catch (Exception e) { 91 fail("Exception during Constructor test: " + e.toString()); 92 } 93 } 94 95 /** 96 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int, 97 * java.net.InetAddress, int) 98 */ 99 @TestTargetNew( 100 level = TestLevel.COMPLETE, 101 notes = "", 102 method = "DatagramPacket", 103 args = {byte[].class, int.class, int.class, java.net.InetAddress.class, int.class} 104 ) 105 public void test_Constructor$BIILjava_net_InetAddressI() { 106 try { 107 dp = new DatagramPacket("Hello".getBytes(), 2, 3, InetAddress 108 .getLocalHost(), 0); 109 assertTrue("Created incorrect packet", dp.getAddress().equals( 110 InetAddress.getLocalHost()) 111 && dp.getPort() == 0); 112 assertEquals("Wrong length", 3, dp.getLength()); 113 assertEquals("Wrong offset", 2, dp.getOffset()); 114 } catch (Exception e) { 115 fail("Exception during Constructor test: " + e.toString()); 116 } 117 } 118 119 /** 120 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, 121 * java.net.InetAddress, int) 122 */ 123 @TestTargetNew( 124 level = TestLevel.COMPLETE, 125 notes = "", 126 method = "DatagramPacket", 127 args = {byte[].class, int.class, java.net.InetAddress.class, int.class} 128 ) 129 public void test_Constructor$BILjava_net_InetAddressI() { 130 // Test for method java.net.DatagramPacket(byte [], int, 131 // java.net.InetAddress, int) 132 try { 133 dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress 134 .getLocalHost(), 0); 135 assertTrue("Created incorrect packet", dp.getAddress().equals( 136 InetAddress.getLocalHost()) 137 && dp.getPort() == 0); 138 assertEquals("Wrong length", 5, dp.getLength()); 139 } catch (Exception e) { 140 fail("Exception during Constructor test: " + e.toString()); 141 } 142 } 143 144 /** 145 * @tests java.net.DatagramPacket#getAddress() 146 */ 147 @TestTargetNew( 148 level = TestLevel.COMPLETE, 149 notes = "", 150 method = "getAddress", 151 args = {} 152 ) 153 public void test_getAddress() { 154 // Test for method java.net.InetAddress 155 // java.net.DatagramPacket.getAddress() 156 try { 157 dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress 158 .getLocalHost(), 0); 159 assertTrue("Incorrect address returned", dp.getAddress().equals( 160 InetAddress.getLocalHost())); 161 } catch (Exception e) { 162 fail("Exception during getAddress test:" + e.toString()); 163 } 164 } 165 166 /** 167 * @tests java.net.DatagramPacket#getData() 168 */ 169 @TestTargetNew( 170 level = TestLevel.COMPLETE, 171 notes = "", 172 method = "getData", 173 args = {} 174 ) 175 public void test_getData() { 176 // Test for method byte [] java.net.DatagramPacket.getData() 177 178 dp = new DatagramPacket("Hello".getBytes(), 5); 179 assertEquals("Incorrect length returned", "Hello", new String(dp.getData(), 0, dp 180 .getData().length)); 181 } 182 183 /** 184 * @tests java.net.DatagramPacket#getLength() 185 */ 186 @TestTargetNew( 187 level = TestLevel.COMPLETE, 188 notes = "", 189 method = "getLength", 190 args = {} 191 ) 192 public void test_getLength() { 193 // Test for method int java.net.DatagramPacket.getLength() 194 195 dp = new DatagramPacket("Hello".getBytes(), 5); 196 assertEquals("Incorrect length returned", 5, dp.getLength()); 197 } 198 199 /** 200 * @tests java.net.DatagramPacket#getOffset() 201 */ 202 @TestTargetNew( 203 level = TestLevel.COMPLETE, 204 notes = "", 205 method = "getOffset", 206 args = {} 207 ) 208 public void test_getOffset() { 209 dp = new DatagramPacket("Hello".getBytes(), 3, 2); 210 assertEquals("Incorrect length returned", 3, dp.getOffset()); 211 } 212 213 /** 214 * @tests java.net.DatagramPacket#getPort() 215 */ 216 @TestTargetNew( 217 level = TestLevel.COMPLETE, 218 notes = "", 219 method = "getPort", 220 args = {} 221 ) 222 public void test_getPort() { 223 // Test for method int java.net.DatagramPacket.getPort() 224 try { 225 dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress 226 .getLocalHost(), 1000); 227 assertEquals("Incorrect port returned", 1000, dp.getPort()); 228 } catch (Exception e) { 229 fail("Exception during getPort test : " + e.getMessage()); 230 } 231 232 InetAddress localhost = null; 233 try { 234 localhost = InetAddress.getByName("localhost"); 235 } catch (UnknownHostException e) { 236 fail("Unexpected UnknownHostException : " + e.getMessage()); 237 } 238 int[] ports = Support_PortManager.getNextPortsForUDP(2); 239 final int port = ports[0]; 240 final Object lock = new Object(); 241 242 Thread thread = new Thread(new Runnable() { 243 public void run() { 244 DatagramSocket socket = null; 245 try { 246 socket = new DatagramSocket(port); 247 synchronized (lock) { 248 started = true; 249 lock.notifyAll(); 250 } 251 socket.setSoTimeout(3000); 252 DatagramPacket packet = new DatagramPacket(new byte[256], 253 256); 254 socket.receive(packet); 255 socket.send(packet); 256 socket.close(); 257 } catch (IOException e) { 258 System.out.println("thread exception: " + e); 259 if (socket != null) 260 socket.close(); 261 } 262 } 263 }); 264 thread.start(); 265 266 DatagramSocket socket = null; 267 try { 268 socket = new DatagramSocket(ports[1]); 269 socket.setSoTimeout(3000); 270 DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4, 271 5, 6 }, 6, localhost, port); 272 synchronized (lock) { 273 try { 274 if (!started) 275 lock.wait(); 276 } catch (InterruptedException e) { 277 fail(e.toString()); 278 } 279 } 280 socket.send(packet); 281 socket.receive(packet); 282 socket.close(); 283 assertTrue("datagram received wrong port: " + packet.getPort(), 284 packet.getPort() == port); 285 } catch (IOException e) { 286 if (socket != null) 287 socket.close(); 288 System.err.println("port: " + port + " datagram server error: "); 289 e.printStackTrace(); 290 fail("port : " + port + " datagram server error : " 291 + e.getMessage()); 292 } 293 } 294 295 /** 296 * @tests java.net.DatagramPacket#setAddress(java.net.InetAddress) 297 */ 298 @TestTargetNew( 299 level = TestLevel.COMPLETE, 300 notes = "", 301 method = "setAddress", 302 args = {java.net.InetAddress.class} 303 ) 304 public void test_setAddressLjava_net_InetAddress() { 305 // Test for method void 306 // java.net.DatagramPacket.setAddress(java.net.InetAddress) 307 try { 308 InetAddress ia = InetAddress 309 .getByName(Support_Configuration.InetTestIP); 310 dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress 311 .getLocalHost(), 0); 312 dp.setAddress(ia); 313 assertTrue("Incorrect address returned", dp.getAddress().equals(ia)); 314 } catch (Exception e) { 315 fail("Exception during getAddress test:" + e.toString()); 316 } 317 } 318 319 /** 320 * @tests java.net.DatagramPacket#setData(byte[], int, int) 321 */ 322 @TestTargetNew( 323 level = TestLevel.COMPLETE, 324 notes = "", 325 method = "setData", 326 args = {byte[].class, int.class, int.class} 327 ) 328 public void test_setData$BII() { 329 dp = new DatagramPacket("Hello".getBytes(), 5); 330 dp.setData("Wagga Wagga".getBytes(), 2, 3); 331 assertEquals("Incorrect data set", "Wagga Wagga", new String(dp.getData()) 332 ); 333 try { 334 dp.setData(null, 2, 3); 335 fail("NullPointerException was not thrown."); 336 } catch(NullPointerException npe) { 337 //expected 338 } 339 } 340 341 /** 342 * @tests java.net.DatagramPacket#setData(byte[]) 343 */ 344 @TestTargetNew( 345 level = TestLevel.COMPLETE, 346 notes = "", 347 method = "setData", 348 args = {byte[].class} 349 ) 350 public void test_setData$B() { 351 // Test for method void java.net.DatagramPacket.setData(byte []) 352 dp = new DatagramPacket("Hello".getBytes(), 5); 353 dp.setData("Ralph".getBytes()); 354 assertEquals("Incorrect data set", "Ralph", new String(dp.getData(), 0, dp 355 .getData().length)); 356 357 try { 358 dp.setData(null); 359 fail("NullPointerException was not thrown."); 360 } catch(NullPointerException npe) { 361 //expected 362 } 363 } 364 365 /** 366 * @tests java.net.DatagramPacket#setLength(int) 367 */ 368 @TestTargetNew( 369 level = TestLevel.COMPLETE, 370 notes = "", 371 method = "setLength", 372 args = {int.class} 373 ) 374 public void test_setLengthI() { 375 // Test for method void java.net.DatagramPacket.setLength(int) 376 dp = new DatagramPacket("Hello".getBytes(), 5); 377 dp.setLength(1); 378 assertEquals("Failed to set packet length", 1, dp.getLength()); 379 380 try { 381 new DatagramPacket("Hello".getBytes(), 6); 382 fail("IllegalArgumentException was not thrown."); 383 } catch(IllegalArgumentException iae) { 384 //expected 385 } 386 387 try { 388 new DatagramPacket("Hello".getBytes(), -1); 389 fail("IllegalArgumentException was not thrown."); 390 } catch(IllegalArgumentException iae) { 391 //expected 392 } 393 } 394 395 /** 396 * @tests java.net.DatagramPacket#setPort(int) 397 */ 398 @TestTargetNew( 399 level = TestLevel.COMPLETE, 400 notes = "", 401 method = "setPort", 402 args = {int.class} 403 ) 404 public void test_setPortI() { 405 // Test for method void java.net.DatagramPacket.setPort(int) 406 try { 407 dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress 408 .getLocalHost(), 1000); 409 dp.setPort(2000); 410 assertEquals("Port not set", 2000, dp.getPort()); 411 } catch (Exception e) { 412 fail("Exception during setPort test : " + e.getMessage()); 413 } 414 } 415 416 /** 417 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, 418 * java.net.SocketAddress) 419 */ 420 @TestTargetNew( 421 level = TestLevel.SUFFICIENT, 422 notes = "SocketException checking missed.", 423 method = "DatagramPacket", 424 args = {byte[].class, int.class, java.net.SocketAddress.class} 425 ) 426 public void test_Constructor$BILjava_net_SocketAddress() { 427 class mySocketAddress extends SocketAddress { 428 429 public mySocketAddress() { 430 } 431 } 432 433 try { 434 // unsupported SocketAddress subclass 435 byte buf[] = new byte[1]; 436 try { 437 DatagramPacket thePacket = new DatagramPacket(buf, 1, 438 new mySocketAddress()); 439 fail("No exception when constructing using unsupported SocketAddress subclass"); 440 } catch (IllegalArgumentException ex) { 441 } 442 443 // case were we try to pass in null 444 // unsupported SocketAddress subclass 445 446 try { 447 DatagramPacket thePacket = new DatagramPacket(buf, 1, null); 448 fail("No exception when constructing address using null"); 449 } catch (IllegalArgumentException ex) { 450 } 451 452 // now validate we can construct 453 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 454 .getLocalHost(), Support_PortManager.getNextPortForUDP()); 455 DatagramPacket thePacket = new DatagramPacket(buf, 1, theAddress); 456 assertTrue("Socket address not set correctly (1)", theAddress 457 .equals(thePacket.getSocketAddress())); 458 assertTrue("Socket address not set correctly (2)", theAddress 459 .equals(new InetSocketAddress(thePacket.getAddress(), 460 thePacket.getPort()))); 461 } catch (Exception e) { 462 fail("Exception during constructor test(1):" + e.toString()); 463 } 464 } 465 466 /** 467 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int, 468 * java.net.SocketAddress) 469 */ 470 @TestTargetNew( 471 level = TestLevel.SUFFICIENT, 472 notes = "SocketException checking missed.", 473 method = "DatagramPacket", 474 args = {byte[].class, int.class, int.class, java.net.SocketAddress.class} 475 ) 476 public void test_Constructor$BIILjava_net_SocketAddress() { 477 class mySocketAddress extends SocketAddress { 478 479 public mySocketAddress() { 480 } 481 } 482 483 try { 484 // unsupported SocketAddress subclass 485 byte buf[] = new byte[2]; 486 try { 487 DatagramPacket thePacket = new DatagramPacket(buf, 1, 1, 488 new mySocketAddress()); 489 fail("No exception when constructing using unsupported SocketAddress subclass"); 490 } catch (IllegalArgumentException ex) { 491 } 492 493 // case were we try to pass in null 494 // unsupported SocketAddress subclass 495 496 try { 497 DatagramPacket thePacket = new DatagramPacket(buf, 1, 1, null); 498 fail("No exception when constructing address using null"); 499 } catch (IllegalArgumentException ex) { 500 } 501 502 // now validate we can construct 503 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 504 .getLocalHost(), Support_PortManager.getNextPortForUDP()); 505 DatagramPacket thePacket = new DatagramPacket(buf, 1, 1, theAddress); 506 assertTrue("Socket address not set correctly (1)", theAddress 507 .equals(thePacket.getSocketAddress())); 508 assertTrue("Socket address not set correctly (2)", theAddress 509 .equals(new InetSocketAddress(thePacket.getAddress(), 510 thePacket.getPort()))); 511 assertEquals("Offset not set correctly", 1, thePacket.getOffset()); 512 } catch (Exception e) { 513 fail("Exception during constructor test(2):" + e.toString()); 514 } 515 } 516 517 /** 518 * @tests java.net.DatagramPacket#getSocketAddress() 519 */ 520 @TestTargetNew( 521 level = TestLevel.COMPLETE, 522 notes = "", 523 method = "getSocketAddress", 524 args = {} 525 ) 526 public void test_getSocketAddress() { 527 try { 528 byte buf[] = new byte[1]; 529 DatagramPacket thePacket = new DatagramPacket(buf, 1); 530 531 // validate get returns the value we set 532 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 533 .getLocalHost(), Support_PortManager.getNextPortForUDP()); 534 thePacket = new DatagramPacket(buf, 1); 535 thePacket.setSocketAddress(theAddress); 536 assertTrue("Socket address not set correctly (1)", theAddress 537 .equals(thePacket.getSocketAddress())); 538 } catch (Exception e) { 539 fail( 540 "Exception during getSocketAddress test:" + e.toString()); 541 } 542 } 543 544 /** 545 * @tests java.net.DatagramPacket#setSocketAddress(java.net.SocketAddress) 546 */ 547 @TestTargetNew( 548 level = TestLevel.COMPLETE, 549 notes = "", 550 method = "setSocketAddress", 551 args = {java.net.SocketAddress.class} 552 ) 553 public void test_setSocketAddressLjava_net_SocketAddress() { 554 555 class mySocketAddress extends SocketAddress { 556 557 public mySocketAddress() { 558 } 559 } 560 561 try { 562 // unsupported SocketAddress subclass 563 byte buf[] = new byte[1]; 564 DatagramPacket thePacket = new DatagramPacket(buf, 1); 565 try { 566 thePacket.setSocketAddress(new mySocketAddress()); 567 fail("No exception when setting address using unsupported SocketAddress subclass"); 568 } catch (IllegalArgumentException ex) { 569 } 570 571 // case were we try to pass in null 572 // unsupported SocketAddress subclass 573 thePacket = new DatagramPacket(buf, 1); 574 try { 575 thePacket.setSocketAddress(null); 576 fail("No exception when setting address using null"); 577 } catch (IllegalArgumentException ex) { 578 } 579 580 // now validate we can set it correctly 581 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 582 .getLocalHost(), Support_PortManager.getNextPortForUDP()); 583 thePacket = new DatagramPacket(buf, 1); 584 thePacket.setSocketAddress(theAddress); 585 assertTrue("Socket address not set correctly (1)", theAddress 586 .equals(thePacket.getSocketAddress())); 587 assertTrue("Socket address not set correctly (2)", theAddress 588 .equals(new InetSocketAddress(thePacket.getAddress(), 589 thePacket.getPort()))); 590 } catch (Exception e) { 591 fail( 592 "Exception during setSocketAddress test:" + e.toString()); 593 } 594 } 595 596 /** 597 * Sets up the fixture, for example, open a network connection. This method 598 * is called before a test is executed. 599 */ 600 protected void setUp() { 601 } 602 603 /** 604 * Tears down the fixture, for example, close a network connection. This 605 * method is called after a test is executed. 606 */ 607 protected void tearDown() { 608 } 609 610 protected void doneSuite() { 611 } 612} 613