View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.ssl;
29  
30  import static org.hamcrest.MatcherAssert.assertThat;
31  import static org.junit.jupiter.api.Assertions.assertNull;
32  import static org.junit.jupiter.api.Assertions.assertTrue;
33  
34  import org.apache.hc.core5.http.ParseException;
35  import org.apache.hc.core5.http.ProtocolVersion;
36  import org.apache.hc.core5.util.Tokenizer;
37  import org.hamcrest.CoreMatchers;
38  import org.junit.jupiter.api.Assertions;
39  import org.junit.jupiter.api.Test;
40  
41  class TLSTest {
42  
43  
44      @Test
45      void isSame() throws ParseException {
46          assertTrue(TLS.V_1_0.isSame(TLS.parse("TLSv1")));
47      }
48  
49      @Test
50      void isComparable() throws ParseException {
51          assertTrue(TLS.V_1_0.isComparable(TLS.parse("TLSv1")));
52      }
53  
54      @Test
55      void greaterEquals() throws ParseException {
56          assertTrue(TLS.V_1_3.greaterEquals(TLS.parse("TLSv1")));
57      }
58  
59      @Test
60      void lessEquals() throws ParseException {
61          assertTrue(TLS.V_1_0.lessEquals(TLS.parse("TLSv1.3")));
62      }
63  
64      @Test
65      void parse() throws ParseException {
66          assertTrue(TLS.V_1_0.lessEquals(TLS.parse("TLSv1.3")));
67      }
68  
69      @Test
70      void parseNull() throws ParseException {
71          assertNull(TLS.parse(null));
72      }
73  
74      @Test
75      void excludeWeakNull() {
76          assertNull((TLS.excludeWeak((String[]) null)));
77      }
78  
79      @Test
80      void excludeWeak() {
81          final String[] mixProtocol = {
82                  "SSL 2.0",
83                  "TLS 1.3",
84                  "SSL 3.0",
85                  "TLS 1.2",
86                  "TLS 1.1"
87          };
88          final String[] strongProtocols = TLS.excludeWeak(mixProtocol);
89          for (final String protocol : strongProtocols) {
90              Assertions.assertTrue(TLS.isSecure(protocol));
91          }
92      }
93  
94      @Test
95      public void testParseBasic() throws Exception {
96          assertThat(TLS.parse("TLSv1"), CoreMatchers.equalTo(TLS.V_1_0.getVersion()));
97          assertThat(TLS.parse("TLSv1.1"), CoreMatchers.equalTo(TLS.V_1_1.getVersion()));
98          assertThat(TLS.parse("TLSv1.2"), CoreMatchers.equalTo(TLS.V_1_2.getVersion()));
99          assertThat(TLS.parse("TLSv1.3  "), CoreMatchers.equalTo(TLS.V_1_3.getVersion()));
100         assertThat(TLS.parse("TLSv22.356"), CoreMatchers.equalTo(new ProtocolVersion("TLS", 22, 356)));
101     }
102 
103     @Test
104     public void testParseBuffer() throws Exception {
105         final Tokenizer.Cursor cursor = new Tokenizer.Cursor(1, 13);
106         assertThat(TLS.parse(" TLSv1.2,0000", cursor, Tokenizer.delimiters(',')),
107                 CoreMatchers.equalTo(TLS.V_1_2.getVersion()));
108         assertThat(cursor.getPos(), CoreMatchers.equalTo(8));
109     }
110 
111     @Test
112     public void testParseFailure() throws Exception {
113         Assertions.assertThrows(ParseException.class, () -> TLS.parse("Tlsv1"));
114         Assertions.assertThrows(ParseException.class, () -> TLS.parse("TLSV1"));
115         Assertions.assertThrows(ParseException.class, () -> TLS.parse("TLSv"));
116         Assertions.assertThrows(ParseException.class, () -> TLS.parse("TLSv1A"));
117         Assertions.assertThrows(ParseException.class, () -> TLS.parse("TLSv1.A"));
118         Assertions.assertThrows(ParseException.class, () -> TLS.parse("TLSv1.1 huh?"));
119     }
120 
121 }