DatagramPacketTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 org.apache.harmony.luni.tests.java.net; 19 20import java.io.IOException; 21import java.net.DatagramPacket; 22import java.net.DatagramSocket; 23import java.net.InetAddress; 24import java.net.InetSocketAddress; 25import java.net.SocketAddress; 26 27import tests.support.Support_Configuration; 28 29public class DatagramPacketTest extends junit.framework.TestCase { 30 31 volatile boolean started = false; 32 33 /** 34 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int) 35 */ 36 public void test_Constructor$BI() { 37 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5); 38 assertEquals("Created incorrect packet", "Hello", new String(dp 39 .getData(), 0, dp.getData().length)); 40 assertEquals("Wrong length", 5, dp.getLength()); 41 42 // Regression for HARMONY-890 43 dp = new DatagramPacket(new byte[942], 4); 44 assertEquals(-1, dp.getPort()); 45 try { 46 dp.getSocketAddress(); 47 fail("Should throw IllegalArgumentException"); 48 } catch (IllegalArgumentException e) { 49 // expected 50 } 51 } 52 53 /** 54 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int) 55 */ 56 public void test_Constructor$BII() { 57 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 2, 3); 58 assertEquals("Created incorrect packet", "Hello", new String(dp 59 .getData(), 0, dp.getData().length)); 60 assertEquals("Wrong length", 3, dp.getLength()); 61 assertEquals("Wrong offset", 2, dp.getOffset()); 62 } 63 64 /** 65 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int, 66 * java.net.InetAddress, int) 67 */ 68 public void test_Constructor$BIILjava_net_InetAddressI() throws IOException { 69 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 2, 3, 70 InetAddress.getLocalHost(), 0); 71 assertEquals("Wrong host", InetAddress.getLocalHost(), dp.getAddress()); 72 assertEquals("Wrong port", 0, dp.getPort()); 73 assertEquals("Wrong length", 3, dp.getLength()); 74 assertEquals("Wrong offset", 2, dp.getOffset()); 75 } 76 77 /** 78 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, 79 * java.net.InetAddress, int) 80 */ 81 public void test_Constructor$BILjava_net_InetAddressI() throws IOException { 82 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5, 83 InetAddress.getLocalHost(), 0); 84 assertEquals("Wrong address", InetAddress.getLocalHost(), dp 85 .getAddress()); 86 assertEquals("Wrong port", 0, dp.getPort()); 87 assertEquals("Wrong length", 5, dp.getLength()); 88 } 89 90 /** 91 * @tests java.net.DatagramPacket#getAddress() 92 */ 93 public void test_getAddress() throws IOException { 94 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5, 95 InetAddress.getLocalHost(), 0); 96 assertEquals("Incorrect address returned", InetAddress.getLocalHost(), 97 dp.getAddress()); 98 } 99 100 /** 101 * @tests java.net.DatagramPacket#getData() 102 */ 103 public void test_getData() { 104 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5); 105 assertEquals("Incorrect length returned", "Hello", new String(dp 106 .getData(), 0, dp.getData().length)); 107 } 108 109 /** 110 * @tests java.net.DatagramPacket#getLength() 111 */ 112 public void test_getLength() { 113 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5); 114 assertEquals("Incorrect length returned", 5, dp.getLength()); 115 } 116 117 /** 118 * @tests java.net.DatagramPacket#getOffset() 119 */ 120 public void test_getOffset() { 121 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 3, 2); 122 assertEquals("Incorrect length returned", 3, dp.getOffset()); 123 } 124 125 /** 126 * @tests java.net.DatagramPacket#getPort() 127 */ 128 public void test_getPort() throws IOException { 129 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5, 130 InetAddress.getLocalHost(), 1000); 131 assertEquals("Incorrect port returned", 1000, dp.getPort()); 132 133 final InetAddress localhost = InetAddress.getLocalHost(); 134 DatagramSocket socket = new DatagramSocket(0, localhost); 135 final int port = socket.getLocalPort(); 136 137 socket.setSoTimeout(3000); 138 DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4, 5, 139 6 }, 6, localhost, port); 140 socket.send(packet); 141 socket.receive(packet); 142 socket.close(); 143 assertTrue("datagram received wrong port: " + packet.getPort(), packet 144 .getPort() == port); 145 } 146 147 /** 148 * @tests java.net.DatagramPacket#setAddress(java.net.InetAddress) 149 */ 150 public void test_setAddressLjava_net_InetAddress() throws IOException { 151 InetAddress ia = InetAddress 152 .getByName(Support_Configuration.InetTestIP); 153 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5, 154 InetAddress.getLocalHost(), 0); 155 dp.setAddress(ia); 156 assertEquals("Incorrect address returned", ia, dp.getAddress()); 157 } 158 159 /** 160 * @tests java.net.DatagramPacket#setData(byte[], int, int) 161 */ 162 public void test_setData$BII() { 163 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5); 164 dp.setData("Wagga Wagga".getBytes(), 2, 3); 165 assertEquals("Incorrect data set", "Wagga Wagga", new String(dp 166 .getData())); 167 } 168 169 /** 170 * @tests java.net.DatagramPacket#setData(byte[]) 171 */ 172 public void test_setData$B() { 173 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5); 174 dp.setData("Ralph".getBytes()); 175 assertEquals("Incorrect data set", "Ralph", new String(dp.getData(), 0, 176 dp.getData().length)); 177 } 178 179 /** 180 * @tests java.net.DatagramPacket#setLength(int) 181 */ 182 public void test_setLengthI() { 183 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5); 184 dp.setLength(1); 185 assertEquals("Failed to set packet length", 1, dp.getLength()); 186 } 187 188 /** 189 * @tests java.net.DatagramPacket#setPort(int) 190 */ 191 public void test_setPortI() throws Exception { 192 DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5, 193 InetAddress.getLocalHost(), 1000); 194 dp.setPort(2000); 195 assertEquals("Port not set", 2000, dp.getPort()); 196 } 197 198 /** 199 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, 200 * java.net.SocketAddress) 201 */ 202 public void test_Constructor$BILjava_net_SocketAddress() throws IOException { 203 @SuppressWarnings("serial") 204 class UnsupportedSocketAddress extends SocketAddress { 205 206 public UnsupportedSocketAddress() { 207 } 208 } 209 210 // Unsupported SocketAddress subclass 211 byte buf[] = new byte[1]; 212 try { 213 new DatagramPacket(buf, 1, new UnsupportedSocketAddress()); 214 fail("No exception when constructing using unsupported SocketAddress subclass"); 215 } catch (IllegalArgumentException ex) { 216 // Expected 217 } 218 219 // Case were we try to pass in null 220 try { 221 new DatagramPacket(buf, 1, null); 222 fail("No exception when constructing address using null"); 223 } catch (IllegalArgumentException ex) { 224 // Expected 225 } 226 227 // Now validate we can construct 228 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 229 .getLocalHost(), 2067); 230 DatagramPacket thePacket = new DatagramPacket(buf, 1, theAddress); 231 assertEquals("Socket address not set correctly (1)", theAddress, 232 thePacket.getSocketAddress()); 233 assertEquals("Socket address not set correctly (2)", theAddress, 234 new InetSocketAddress(thePacket.getAddress(), thePacket 235 .getPort())); 236 } 237 238 /** 239 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int, 240 * java.net.SocketAddress) 241 */ 242 public void test_Constructor$BIILjava_net_SocketAddress() 243 throws IOException { 244 @SuppressWarnings("serial") 245 class UnsupportedSocketAddress extends SocketAddress { 246 247 public UnsupportedSocketAddress() { 248 } 249 } 250 251 // Unsupported SocketAddress subclass 252 byte buf[] = new byte[2]; 253 try { 254 new DatagramPacket(buf, 1, 1, new UnsupportedSocketAddress()); 255 fail("No exception when constructing using unsupported SocketAddress subclass"); 256 } catch (IllegalArgumentException ex) { 257 // Expected 258 } 259 260 // Case were we try to pass in null 261 try { 262 new DatagramPacket(buf, 1, 1, null); 263 fail("No exception when constructing address using null"); 264 } catch (IllegalArgumentException ex) { 265 // Expected 266 } 267 268 // now validate we can construct 269 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 270 .getLocalHost(), 2067); 271 DatagramPacket thePacket = new DatagramPacket(buf, 1, 1, theAddress); 272 assertEquals("Socket address not set correctly (1)", theAddress, 273 thePacket.getSocketAddress()); 274 assertEquals("Socket address not set correctly (2)", theAddress, 275 new InetSocketAddress(thePacket.getAddress(), thePacket 276 .getPort())); 277 assertEquals("Offset not set correctly", 1, thePacket.getOffset()); 278 } 279 280 /** 281 * @tests java.net.DatagramPacket#getSocketAddress() 282 */ 283 public void test_getSocketAddress() throws IOException { 284 byte buf[] = new byte[1]; 285 DatagramPacket thePacket = new DatagramPacket(buf, 1); 286 287 // Validate get returns the value we set 288 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 289 .getLocalHost(), 0); 290 thePacket = new DatagramPacket(buf, 1); 291 thePacket.setSocketAddress(theAddress); 292 assertEquals("Socket address not set correctly (1)", theAddress, 293 thePacket.getSocketAddress()); 294 } 295 296 /** 297 * @tests java.net.DatagramPacket#setSocketAddress(java.net.SocketAddress) 298 */ 299 public void test_setSocketAddressLjava_net_SocketAddress() 300 throws IOException { 301 302 @SuppressWarnings("serial") 303 class UnsupportedSocketAddress extends SocketAddress { 304 305 public UnsupportedSocketAddress() { 306 } 307 } 308 309 // Unsupported SocketAddress subclass 310 byte buf[] = new byte[1]; 311 DatagramPacket thePacket = new DatagramPacket(buf, 1); 312 try { 313 thePacket.setSocketAddress(new UnsupportedSocketAddress()); 314 fail("No exception when setting address using unsupported SocketAddress subclass"); 315 } catch (IllegalArgumentException ex) { 316 // Expected 317 } 318 319 // Case were we try to pass in null 320 thePacket = new DatagramPacket(buf, 1); 321 try { 322 thePacket.setSocketAddress(null); 323 fail("No exception when setting address using null"); 324 } catch (IllegalArgumentException ex) { 325 // Expected 326 } 327 328 // Now validate we can set it correctly 329 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 330 .getLocalHost(), 2049); 331 thePacket = new DatagramPacket(buf, 1); 332 thePacket.setSocketAddress(theAddress); 333 assertEquals("Socket address not set correctly (1)", theAddress, 334 thePacket.getSocketAddress()); 335 assertEquals("Socket address not set correctly (2)", theAddress, 336 new InetSocketAddress(thePacket.getAddress(), thePacket 337 .getPort())); 338 } 339} 340