• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.conscrypt;
17 
18 import static org.conscrypt.TestUtils.UTF_8;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21 
22 import javax.net.ssl.SSLSession;
23 
24 /**
25  * Utility class for constructing mock sessions.
26  */
27 final class MockSessionBuilder {
28     static final String DEFAULT_CIPHER_SUITE = "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256";
29     static final int DEFAULT_PORT = 443;
30 
31     private byte[] id;
32     private boolean valid = true;
33     private boolean singleUse = false;
34     private String host;
35     private int port = DEFAULT_PORT;
36     private String cipherSuite = DEFAULT_CIPHER_SUITE;
37     private byte[] encodedBytes = EmptyArray.BYTE;
38 
id(byte[] id)39     MockSessionBuilder id(byte[] id) {
40         this.id = id;
41         return this;
42     }
43 
host(String host)44     MockSessionBuilder host(String host) {
45         this.host = host;
46         return this;
47     }
48 
port(int port)49     MockSessionBuilder port(int port) {
50         this.port = port;
51         return this;
52     }
53 
valid(boolean valid)54     MockSessionBuilder valid(boolean valid) {
55         this.valid = valid;
56         return this;
57     }
58 
cipherSuite(String cipherSuite)59     MockSessionBuilder cipherSuite(String cipherSuite) {
60         this.cipherSuite = cipherSuite;
61         return this;
62     }
63 
encodedBytes(byte[] encodedBytes)64     MockSessionBuilder encodedBytes(byte[] encodedBytes) {
65         this.encodedBytes = encodedBytes;
66         return this;
67     }
68 
singleUse(boolean singleUse)69     MockSessionBuilder singleUse(boolean singleUse) {
70         this.singleUse = singleUse;
71         return this;
72     }
73 
build()74     NativeSslSession build() {
75         NativeSslSession session = mock(NativeSslSession.class);
76         byte[] id = this.id == null ? host.getBytes(UTF_8) : this.id;
77         when(session.getId()).thenReturn(id);
78         when(session.isValid()).thenReturn(valid);
79         when(session.isSingleUse()).thenReturn(singleUse);
80         when(session.getProtocol()).thenReturn(TestUtils.highestCommonProtocol());
81         when(session.getPeerHost()).thenReturn(host);
82         when(session.getPeerPort()).thenReturn(port);
83         when(session.getCipherSuite()).thenReturn(cipherSuite);
84         when(session.toBytes()).thenReturn(encodedBytes);
85         when(session.toSSLSession()).thenReturn(mock(SSLSession.class));
86         return session;
87     }
88 }
89