Path: blob/master/test/jdk/java/net/Socket/UdpSocket.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @run testng/othervm -Djava.security.manager=allow -Dsun.net.maxDatagramSockets=32 UdpSocket26* @summary Basic test for a Socket to a UDP socket27*/2829import java.io.IOException;30import java.lang.ref.WeakReference;31import java.net.InetAddress;32import java.net.InetSocketAddress;33import java.net.Socket;34import java.net.SocketAddress;35import java.nio.ByteBuffer;36import java.nio.channels.DatagramChannel;37import java.security.Permission;38import java.util.Arrays;39import java.util.ArrayDeque;40import java.util.Deque;41import java.net.BindException;4243import org.testng.annotations.Test;44import static org.testng.Assert.*;4546@Test47public class UdpSocket {4849/**50* Test using the Socket API to send/receive datagrams51*/52public void testSendReceive() throws IOException {53final String MESSAGE = "hello";5455try (DatagramChannel dc = DatagramChannel.open()) {56var loopback = InetAddress.getLoopbackAddress();57dc.bind(new InetSocketAddress(loopback, 0));5859int port = ((InetSocketAddress) dc.getLocalAddress()).getPort();60try (Socket s = new Socket(loopback, port, false)) {61// send datagram with socket output stream62byte[] array1 = MESSAGE.getBytes("UTF-8");63s.getOutputStream().write(array1);6465// receive the datagram66var buf = ByteBuffer.allocate(100);67SocketAddress remote = dc.receive(buf);68buf.flip();69assertTrue(buf.remaining() == MESSAGE.length(), "Unexpected size");7071// echo the datagram72dc.send(buf, remote);7374// receive datagram with the socket input stream75byte[] array2 = new byte[100];76int n = s.getInputStream().read(array2);77assertTrue(n == MESSAGE.length(), "Unexpected size");78assertEquals(Arrays.copyOf(array1, n), Arrays.copyOf(array2, n),79"Unexpected contents");80}81}82}8384/**85* Test that the number of UDP sockets is limited when running with a86* security manager.87*/88public void testMaxSockets() throws Exception {89int limit = Integer.getInteger("sun.net.maxDatagramSockets");9091// security manager grants all permissions92var securityManager = new SecurityManager() {93@Override public void checkPermission(Permission perm) { }94};9596System.setSecurityManager(securityManager);97Deque<Socket> sockets = new ArrayDeque<>();98try {99// create the maximum number of sockets100for (int i=0; i<limit; i++) {101sockets.offer(newUdpSocket());102}103104// try to create another socket - should fail105try {106Socket s = newUdpSocket();107s.close();108assertTrue(false);109} catch (IOException expected) { }110111// close one socket112sockets.pop().close();113114// try to create another socket - should succeed115Socket s = newUdpSocket();116117// unreference the socket and wait for it to be closed by the cleaner118var ref = new WeakReference<>(s);119s = null;120while (ref.get() != null) {121System.gc();122Thread.sleep(100);123}124125// try to create another socket - should succeed126s = newUdpSocket();127s.close();128} finally {129closeAll(sockets);130System.setSecurityManager(null);131}132}133134135private Socket newUdpSocket() throws IOException {136Socket s = null;137138try {139s = new Socket(InetAddress.getLoopbackAddress(), 8000, false);140} catch (BindException unexpected) {141System.out.println("BindException caught retry Socket creation");142s = new Socket(InetAddress.getLoopbackAddress(), 8000, false);143}144return s;145}146147private void closeAll(Deque<Socket> sockets) throws IOException {148Socket s;149while ((s = sockets.poll()) != null) {150s.close();151}152}153}154155156