Home | History | Annotate | Download | only in conscrypt
      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 /**
     23  * Utility class for constructing mock sessions.
     24  */
     25 final class MockSessionBuilder {
     26     static final String DEFAULT_CIPHER_SUITE = "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256";
     27     static final String DEFAULT_PROTOCOL = TestUtils.PROTOCOL_TLS_V1_2;
     28     static final int DEFAULT_PORT = 443;
     29 
     30     private byte[] id;
     31     private boolean valid = true;
     32     private String host;
     33     private int port = DEFAULT_PORT;
     34     private String cipherSuite = DEFAULT_CIPHER_SUITE;
     35     private String protocol = DEFAULT_PROTOCOL;
     36     private byte[] encodedBytes = EmptyArray.BYTE;
     37 
     38     MockSessionBuilder id(byte[] id) {
     39         this.id = id;
     40         return this;
     41     }
     42 
     43     MockSessionBuilder protocol(String protocol) {
     44         this.protocol = protocol;
     45         return this;
     46     }
     47 
     48     MockSessionBuilder host(String host) {
     49         this.host = host;
     50         return this;
     51     }
     52 
     53     MockSessionBuilder port(int port) {
     54         this.port = port;
     55         return this;
     56     }
     57 
     58     MockSessionBuilder valid(boolean valid) {
     59         this.valid = valid;
     60         return this;
     61     }
     62 
     63     MockSessionBuilder cipherSuite(String cipherSuite) {
     64         this.cipherSuite = cipherSuite;
     65         return this;
     66     }
     67 
     68     MockSessionBuilder encodedBytes(byte[] encodedBytes) {
     69         this.encodedBytes = encodedBytes;
     70         return this;
     71     }
     72 
     73     SslSessionWrapper build() {
     74         SslSessionWrapper session = mock(SslSessionWrapper.class);
     75         byte[] id = this.id == null ? host.getBytes(UTF_8) : this.id;
     76         when(session.getId()).thenReturn(id);
     77         when(session.isValid()).thenReturn(valid);
     78         when(session.getProtocol()).thenReturn(protocol);
     79         when(session.getPeerHost()).thenReturn(host);
     80         when(session.getPeerPort()).thenReturn(port);
     81         when(session.getCipherSuite()).thenReturn(cipherSuite);
     82         when(session.toBytes()).thenReturn(encodedBytes);
     83         return session;
     84     }
     85 }
     86