1 /*
2  * Copyright (c) 2016, 2018, 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 import java.io.IOException;
25 import java.net.*;
26 import java.util.Set;
27 import static java.lang.System.out;
28 
29 /*
30  * @test
31  * @bug 8143923
32  * @summary java.net socket supportedOptions set depends on call order
33  * @run main/othervm SupportedOptionsSet first
34  * @run main/othervm SupportedOptionsSet second
35  * @run main/othervm -Djava.net.preferIPv4Stack=true SupportedOptionsSet first
36  * @run main/othervm -Djava.net.preferIPv4Stack=true SupportedOptionsSet second
37  */
38 
39 // Run with othervm as the implementation of the supported options sets, once
40 // calculated, stores them in a private static fields.
41 
42 public class SupportedOptionsSet {
43 
main(String[] args)44     public static void main(String[] args) throws IOException {
45         if (args[0].equals("first"))
46             first();
47         else if (args[0].equals("second"))
48             second();
49     }
50 
first()51     static void first() throws IOException {
52         try (Socket s = new Socket();
53              ServerSocket ss = new ServerSocket();
54              DatagramSocket ds = new DatagramSocket();
55              MulticastSocket ms = new MulticastSocket()) {
56 
57             Set<?> first = s.supportedOptions();
58             Set<?> second = ss.supportedOptions();
59             assertNotEqual(first, second,
60                  "Socket and ServerSocket should have different options.");
61 
62             first = ds.supportedOptions();
63             second = ms.supportedOptions();
64             assertNotEqual(first, second,
65                 "DatagramSocket and MulticastSocket should have different options.");
66         }
67     }
68 
69     /** Tests with the order of access to supportedOptions reversed.  */
second()70     static void second() throws IOException {
71         try (ServerSocket ss = new ServerSocket();
72              Socket s = new Socket();
73              DatagramSocket ds = new DatagramSocket();
74              MulticastSocket ms = new MulticastSocket()) {
75 
76             Set<?> first = ss.supportedOptions();
77             Set<?> second = s.supportedOptions();
78             assertNotEqual(first, second,
79                 "ServerSocket and Socket should have different options.");
80 
81             first = ms.supportedOptions();
82             second = ds.supportedOptions();
83             assertNotEqual(first, second,
84                 "MulticastSocket and DatagramSocket should have different options.");
85 
86         }
87     }
88 
assertNotEqual(Set<?> s1, Set<?> s2, String message)89     static void assertNotEqual(Set<?> s1, Set<?> s2, String message) {
90         if (s1.equals(s2)) {
91             out.println("s1: " + s1);
92             out.println("s2: " + s2);
93             throw new RuntimeException(message);
94         }
95     }
96 }
97