1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.java.net; 18 19import java.io.IOException; 20import java.net.DatagramPacket; 21import java.net.DatagramSocket; 22import java.net.InetAddress; 23import java.net.InetSocketAddress; 24import java.net.SocketTimeoutException; 25import java.nio.channels.DatagramChannel; 26import junit.framework.TestCase; 27 28/** 29 * Implements some simple tests for datagrams. Not as excessive as the core 30 * tests, but good enough for the harness. 31 */ 32public class OldAndroidDatagramTest extends TestCase { 33 34 /** 35 * Helper class that listens to incoming datagrams and reflects them to the 36 * sender. Incoming datagram is interpreted as a String. It is uppercased 37 * before being sent back. 38 */ 39 40 class Reflector extends Thread { 41 // Helper class for reflecting incoming datagrams. 42 DatagramSocket socket; 43 44 boolean alive = true; 45 46 byte[] buffer = new byte[256]; 47 48 DatagramPacket packet; 49 50 /** 51 * Main loop. Receives datagrams and reflects them. 52 */ 53 @Override 54 public void run() { 55 try { 56 while (alive) { 57 try { 58 packet.setLength(buffer.length); 59 socket.receive(packet); 60 String s = stringFromPacket(packet); 61 // System.out.println(s + " (from " + packet.getAddress() + ":" + packet.getPort() + ")"); 62 63 try { 64 Thread.sleep(100); 65 } catch (InterruptedException ex) { 66 // Ignore. 67 } 68 69 stringToPacket(s.toUpperCase(), packet); 70 71 packet.setAddress(InetAddress.getLocalHost()); 72 packet.setPort(2345); 73 74 socket.send(packet); 75 } catch (java.io.InterruptedIOException e) { 76 } 77 } 78 } catch (java.io.IOException ex) { 79 ex.printStackTrace(); 80 } finally { 81 socket.close(); 82 } 83 } 84 85 /** 86 * Creates a new Relfector object for the given local address and port. 87 */ 88 public Reflector(int port, InetAddress address) { 89 try { 90 packet = new DatagramPacket(buffer, buffer.length); 91 socket = new DatagramSocket(port, address); 92 } catch (IOException ex) { 93 throw new RuntimeException( 94 "Creating datagram reflector failed", ex); 95 } 96 } 97 } 98 99 /** 100 * Converts a given datagram packet's contents to a String. 101 */ 102 static String stringFromPacket(DatagramPacket packet) { 103 return new String(packet.getData(), 0, packet.getLength()); 104 } 105 106 /** 107 * Converts a given String into a datagram packet. 108 */ 109 static void stringToPacket(String s, DatagramPacket packet) { 110 byte[] bytes = s.getBytes(); 111 System.arraycopy(bytes, 0, packet.getData(), 0, bytes.length); 112 packet.setLength(bytes.length); 113 } 114 115 /** 116 * Implements the main part of the Datagram test. 117 */ 118 public void testDatagram() throws Exception { 119 120 Reflector reflector = null; 121 DatagramSocket socket = null; 122 123 try { 124 // Setup the reflector, so we have a partner to send to 125 reflector = new Reflector(1234, InetAddress.getLocalHost()); 126 reflector.start(); 127 128 byte[] buffer = new byte[256]; 129 130 DatagramPacket packet = new DatagramPacket(buffer, buffer.length); 131 socket = new DatagramSocket(2345, InetAddress.getLocalHost()); 132 133 // Send ten simple packets and check for the expected responses. 134 for (int i = 1; i <= 10; i++) { 135 String s = "Hello, Android world #" + i + "!"; 136 stringToPacket(s, packet); 137 138 packet.setAddress(InetAddress.getLocalHost()); 139 packet.setPort(1234); 140 141 socket.send(packet); 142 143 try { 144 Thread.sleep(100); 145 } catch (InterruptedException ex) { 146 // Ignore. 147 } 148 149 packet.setLength(buffer.length); 150 socket.receive(packet); 151 String t = stringFromPacket(packet); 152 // System.out.println(t + " (from " + packet.getAddress() + ":" + packet.getPort() + ")"); 153 154 assertEquals(s.toUpperCase(), t); 155 } 156 } finally { 157 if (reflector != null) { 158 reflector.alive = false; 159 } 160 161 if (socket != null) { 162 socket.close(); 163 } 164 } 165 } 166 167 public void test_54072_DatagramSocket() throws Exception { 168 DatagramSocket s = new DatagramSocket(null); 169 assertTrue(s.getLocalAddress().isAnyLocalAddress()); 170 s.bind(new InetSocketAddress(8888)); 171 assertTrue(s.getLocalAddress().isAnyLocalAddress()); 172 s.close(); 173 assertNull(s.getLocalAddress()); 174 } 175 176 public void test_54072_DatagramChannel() throws Exception { 177 DatagramChannel ch = DatagramChannel.open(); 178 DatagramSocket s = ch.socket(); 179 assertTrue(s.getLocalAddress().isAnyLocalAddress()); 180 s.bind(new InetSocketAddress(8888)); 181 assertTrue(s.getLocalAddress().isAnyLocalAddress()); 182 s.close(); 183 assertNull(s.getLocalAddress()); 184 } 185} 186