1 /*
2  * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /* @test
25  * @library /test/lib
26  * @bug 6435300
27  * @summary Check using IPv6 address does not crash the VM
28  * @run main/othervm UseDGWithIPv6
29  * @run main/othervm -Djava.net.preferIPv4Stack=true UseDGWithIPv6
30  */
31 
32 import java.io.IOException;
33 import java.net.InetSocketAddress;
34 import java.net.SocketAddress;
35 import java.nio.ByteBuffer;
36 import java.nio.channels.DatagramChannel;
37 import java.nio.channels.UnsupportedAddressTypeException;
38 
39 import jdk.test.lib.net.IPSupport;
40 
41 public class UseDGWithIPv6 {
42     static String[] targets = {
43         "3ffe:e00:811:b::21:5",
44         "15.70.186.80"
45     };
46     static int BUFFER_LEN = 10;
47     static int port = 12345;
48 
main(String[] args)49     public static void main(String[] args) throws IOException
50     {
51         IPSupport.throwSkippedExceptionIfNonOperational();
52 
53         ByteBuffer data = ByteBuffer.wrap("TESTING DATA".getBytes());
54         DatagramChannel dgChannel = DatagramChannel.open();
55 
56         for(int i = 0; i < targets.length; i++){
57             data.rewind();
58             SocketAddress sa = new InetSocketAddress(targets[i], port);
59             System.out.println("-------------\nDG_Sending data:" +
60                                "\n    remaining:" + data.remaining() +
61                                "\n     position:" + data.position() +
62                                "\n        limit:" + data.limit() +
63                                "\n     capacity:" + data.capacity() +
64                                " bytes on DG channel to " + sa);
65             try {
66                 int n = dgChannel.send(data, sa);
67                 System.out.println("DG_Sent " + n + " bytes");
68             } catch (UnsupportedAddressTypeException e) {
69                 System.out.println("Ignoring unsupported address type");
70             } catch (IOException e) {
71                 //This regression test is to check vm crash only, so ioe is OK.
72                 e.printStackTrace();
73             }
74         }
75         dgChannel.close();
76     }
77 }
78