1/*
2 * Copyright (c) 2010, 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.security.Provider;
25import java.security.Security;
26
27public class TestJSSE {
28
29    private static final String LOCAL_IP = "127.0.0.1";
30
31    public static void main(String... args) throws Exception {
32        // reset the security property to make sure that the algorithms
33        // and keys used in this test are not disabled.
34        Security.setProperty("jdk.tls.disabledAlgorithms", "");
35
36        // enable debug output
37        System.setProperty("javax.net.debug", "ssl,record");
38
39        String srvProtocol = System.getProperty("SERVER_PROTOCOL");
40        String clnProtocol = System.getProperty("CLIENT_PROTOCOL");
41        String cipher = System.getProperty("CIPHER");
42        if (srvProtocol == null || clnProtocol == null || cipher == null) {
43            throw new IllegalArgumentException("Incorrect parameters");
44        }
45
46        System.out.println("ServerProtocol = " + srvProtocol);
47        System.out.println("ClientProtocol = " + clnProtocol);
48        System.out.println("Cipher         = " + cipher);
49
50        try (CipherTestUtils.Server srv = server(srvProtocol, cipher, args)) {
51            client(srv.getPort(), clnProtocol, cipher, args);
52        }
53    }
54
55    public static void client(int port, String protocols, String cipher,
56            String... exceptions) throws Exception {
57
58        String expectedExcp = exceptions.length >= 1 ? exceptions[0] : null;
59
60        System.out.println("This is client");
61        System.out.println("Testing protocol: " + protocols);
62        System.out.println("Testing cipher  : " + cipher);
63
64        CipherTestUtils.mainClient(
65            new JSSEFactory(LOCAL_IP, protocols, cipher, "Client JSSE"),
66            port, expectedExcp);
67    }
68
69    public static CipherTestUtils.Server server(String protocol,
70                String cipher, String... exceptions) throws Exception {
71
72        String expectedExcp = exceptions.length >= 1 ? exceptions[0] : null;
73
74        System.out.println("This is server");
75        System.out.println("Testing protocol: " + protocol);
76        System.out.println("Testing cipher  : " + cipher);
77
78        return CipherTestUtils.mainServer(
79            new JSSEFactory(null, protocol, cipher, "Server JSSE"),
80            expectedExcp);
81    }
82
83    private static class JSSEFactory extends CipherTestUtils.PeerFactory {
84
85        private final String cipher;
86        private final String protocol;
87        private final String host;
88        private final String name;
89
90        JSSEFactory(String host, String protocol, String cipher, String name) {
91            this.cipher = cipher;
92            this.protocol = protocol;
93            this.host = host;
94            this.name = name;
95        }
96
97        @Override
98        String getName() {
99            return name;
100        }
101
102        @Override
103        String getTestedCipher() {
104            return cipher;
105        }
106
107        @Override
108        String getTestedProtocol() {
109            return protocol;
110        }
111
112        @Override
113        CipherTestUtils.Client newClient(CipherTestUtils cipherTest, int port)
114                throws Exception {
115            return new JSSEClient(cipherTest, host, port, protocol, cipher);
116        }
117
118        @Override
119        CipherTestUtils.Server newServer(CipherTestUtils cipherTest, int port)
120                throws Exception {
121            return new JSSEServer(cipherTest, port, protocol, cipher);
122        }
123    }
124}
125