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