Home | History | Annotate | Download | only in conscrypt
      1 /*
      2  * Copyright 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 
     17 package org.conscrypt;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.mockito.Matchers.same;
     21 import static org.mockito.Mockito.when;
     22 
     23 import java.nio.charset.Charset;
     24 import javax.net.ssl.SSLEngine;
     25 import org.junit.Before;
     26 import org.junit.Test;
     27 import org.mockito.Matchers;
     28 import org.mockito.Mock;
     29 import org.mockito.MockitoAnnotations;
     30 
     31 public class ApplicationProtocolSelectorAdapterTest {
     32     private static Charset US_ASCII = Charset.forName("US-ASCII");
     33     private static final String[] PROTOCOLS = new String[] {"a", "b", "c"};
     34     private static final byte[] PROTOCOL_BYTES = SSLUtils.encodeProtocols(PROTOCOLS);
     35 
     36     @Mock private ApplicationProtocolSelector selector;
     37 
     38     @Mock private SSLEngine engine;
     39 
     40     private ApplicationProtocolSelectorAdapter adapter;
     41 
     42     @Before
     43     public void setup() {
     44         MockitoAnnotations.initMocks(this);
     45 
     46         adapter = new ApplicationProtocolSelectorAdapter(engine, selector);
     47     }
     48 
     49     @Test
     50     public void nullProtocolsShouldNotSelect() {
     51         mockSelection("a");
     52         assertEquals(-1, select(null));
     53     }
     54 
     55     @Test
     56     public void emptyProtocolsShouldNotSelect() {
     57         mockSelection("a");
     58         assertEquals(-1, select(EmptyArray.BYTE));
     59     }
     60 
     61     @Test
     62     public void selectCorrectProtocol() {
     63         for (String protocol : PROTOCOLS) {
     64             mockSelection(protocol);
     65             assertEquals(protocol, getProtocolAt(select(PROTOCOL_BYTES)));
     66         }
     67     }
     68 
     69     @Test
     70     public void invalidProtocolShouldNotSelect() {
     71         mockSelection("d");
     72         assertEquals(-1, select(PROTOCOL_BYTES));
     73     }
     74 
     75     private int select(byte[] protocols) {
     76         return adapter.selectApplicationProtocol(protocols);
     77     }
     78 
     79     private void mockSelection(String returnValue) {
     80         when(selector.selectApplicationProtocol(same(engine), Matchers.anyListOf(String.class)))
     81                 .thenReturn(returnValue);
     82     }
     83 
     84     private String getProtocolAt(int index) {
     85         int len = PROTOCOL_BYTES[index];
     86         return new String(PROTOCOL_BYTES, index + 1, len, US_ASCII);
     87     }
     88 }
     89