1 /*
2  * Copyright (c) 2020, 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.nio.file.Path;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Objects;
30 
31 /*
32  * Abstract peer implementation.
33  */
34 public abstract class AbstractPeer implements Peer {
35 
36     /*
37      * Peer's log path. The path could be null.
38      */
getLogPath()39     protected Path getLogPath() {
40         return null;
41     }
42 
43     /*
44      * Prints the content of log file to stdout.
45      */
printLog()46     protected void printLog() throws IOException {
47         Path logPath = getLogPath();
48         Objects.requireNonNull(logPath, "Please specify log path.");
49         System.out.println(Utilities.readFile(logPath).orElse(""));
50     }
51 
52     /*
53      * Deletes log file if exists.
54      */
deleteLog()55     protected void deleteLog() throws IOException {
56         Utilities.deleteFile(getLogPath());
57     }
58 
59     /*
60      * The negotiated application protocol.
61      */
getNegoAppProtocol()62     public String getNegoAppProtocol() throws SSLTestException {
63         throw new UnsupportedOperationException("getNegoAppProtocol");
64     }
65 
66     @Override
close()67     public void close() throws IOException { }
68 
69     public static abstract class Builder implements Peer.Builder {
70 
71         // The supported SSL/TLS protocols.
72         private Protocol[] protocols;
73 
74         // The supported cipher suites.
75         private CipherSuite[] cipherSuites;
76 
77         // The trusted CAs and certificates.
78         private CertTuple certTuple;
79 
80         // The server-acceptable SNI server name patterns;
81         // Or the client-desired SNI server names.
82         private String[] serverNames;
83 
84         // The supported application protocols.
85         private String[] appProtocols;
86 
87         // The supported named groups.
88         private NamedGroup[] namedGroups;
89 
90         private String message = "M";
91 
92         private int timeout = Utilities.TIMEOUT;
93 
94         // System properties
95         private final Map<String, String> props = new HashMap<>();
96 
getProtocols()97         public Protocol[] getProtocols() {
98             return protocols;
99         }
100 
getProtocol()101         public Protocol getProtocol() {
102             return protocols != null && protocols.length > 0
103                     ? protocols[0]
104                     : null;
105         }
106 
setProtocols(Protocol... protocols)107         public Builder setProtocols(Protocol... protocols) {
108             this.protocols = protocols;
109             return this;
110         }
111 
getCipherSuites()112         public CipherSuite[] getCipherSuites() {
113             return cipherSuites;
114         }
115 
getCipherSuite()116         public CipherSuite getCipherSuite() {
117             return cipherSuites != null && cipherSuites.length > 0
118                     ? cipherSuites[0]
119                     : null;
120         }
121 
setCipherSuites(CipherSuite... cipherSuites)122         public Builder setCipherSuites(CipherSuite... cipherSuites) {
123             this.cipherSuites = cipherSuites;
124             return this;
125         }
126 
getCertTuple()127         public CertTuple getCertTuple() {
128             return certTuple;
129         }
130 
setCertTuple(CertTuple certTuple)131         public Builder setCertTuple(CertTuple certTuple) {
132             this.certTuple = certTuple;
133             return this;
134         }
135 
getServerNames()136         public String[] getServerNames() {
137             return serverNames;
138         }
139 
getServerName()140         public String getServerName() {
141             return serverNames != null && serverNames.length > 0
142                     ? serverNames[0]
143                     : null;
144         }
145 
setServerNames(String... serverNames)146         public Builder setServerNames(String... serverNames) {
147             this.serverNames = serverNames;
148             return this;
149         }
150 
getAppProtocols()151         public String[] getAppProtocols() {
152             return appProtocols;
153         }
154 
getAppProtocol()155         public String getAppProtocol() {
156             return appProtocols != null && appProtocols.length > 0
157                     ? appProtocols[0]
158                     : null;
159         }
160 
setAppProtocols(String... appProtocols)161         public Builder setAppProtocols(String... appProtocols) {
162             this.appProtocols = appProtocols;
163             return this;
164         }
165 
getNamedGroups()166         public NamedGroup[] getNamedGroups() {
167             return namedGroups;
168         }
169 
setNamedGroups(NamedGroup... namedGroups)170         public Builder setNamedGroups(NamedGroup... namedGroups) {
171             this.namedGroups = namedGroups;
172             return this;
173         }
174 
getMessage()175         public String getMessage() {
176             return message;
177         }
178 
setMessage(String message)179         public Builder setMessage(String message) {
180             this.message = message;
181             return this;
182         }
183 
getTimeout()184         public int getTimeout() {
185             return timeout;
186         }
187 
setTimeout(int timeout)188         public Builder setTimeout(int timeout) {
189             this.timeout = timeout;
190             return this;
191         }
192 
addProp(String prop, String value)193         public Builder addProp(String prop, String value) {
194             props.put(prop, value);
195             return this;
196         }
197 
getProp(String prop)198         public String getProp(String prop) {
199             return props.get(prop);
200         }
201 
addAllProps(Map<String, String> props)202         public Builder addAllProps(Map<String, String> props) {
203             this.props.putAll(props);
204             return this;
205         }
206 
getAllProps()207         public Map<String, String> getAllProps() {
208             return Collections.unmodifiableMap(props);
209         }
210 
build()211         public abstract AbstractPeer build() throws Exception;
212     }
213 }
214