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  
32  import org.apache.hc.core5.http.ParseException;
33  import org.apache.hc.core5.http.ProtocolVersion;
34  import org.apache.hc.core5.util.Tokenizer;
35  import org.hamcrest.CoreMatchers;
36  import org.junit.jupiter.api.Assertions;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Unit tests for {@link TlsVersionParser}.
42   */
43  public class TestTlsVersionParser {
44  
45      private TlsVersionParser impl;
46  
47      @BeforeEach
48      public void setup() {
49          impl = new TlsVersionParser();
50      }
51  
52      @Test
53      public void testParseBasic() throws Exception {
54          assertThat(impl.parse("TLSv1"), CoreMatchers.equalTo(TLS.V_1_0.getVersion()));
55          assertThat(impl.parse("TLSv1.1"), CoreMatchers.equalTo(TLS.V_1_1.getVersion()));
56          assertThat(impl.parse("TLSv1.2"), CoreMatchers.equalTo(TLS.V_1_2.getVersion()));
57          assertThat(impl.parse("TLSv1.3"), CoreMatchers.equalTo(TLS.V_1_3.getVersion()));
58          assertThat(impl.parse("TLSv22.356"), CoreMatchers.equalTo(new ProtocolVersion("TLS", 22, 356)));
59      }
60  
61      @Test
62      public void testParseBuffer() throws Exception {
63          final Tokenizer.Cursor cursor = new Tokenizer.Cursor(1, 13);
64          assertThat(impl.parse(" TLSv1.2,0000", cursor, Tokenizer.INIT_BITSET(',')),
65                  CoreMatchers.equalTo(TLS.V_1_2.getVersion()));
66          assertThat(cursor.getPos(), CoreMatchers.equalTo(8));
67      }
68  
69      @Test
70      public void testParseFailure1() throws Exception {
71          Assertions.assertThrows(ParseException.class, () ->
72                  impl.parse("Tlsv1"));
73      }
74  
75      @Test
76      public void testParseFailure2() throws Exception {
77          Assertions.assertThrows(ParseException.class, () ->
78                  impl.parse("TLSV1"));
79      }
80  
81      @Test
82      public void testParseFailure3() throws Exception {
83          Assertions.assertThrows(ParseException.class, () ->
84                  impl.parse("TLSv"));
85      }
86  
87      @Test
88      public void testParseFailure4() throws Exception {
89          Assertions.assertThrows(ParseException.class, () ->
90                  impl.parse("TLSv1A"));
91      }
92  
93      @Test
94      public void testParseFailure5() throws Exception {
95          Assertions.assertThrows(ParseException.class, () ->
96                  impl.parse("TLSv1.A"));
97      }
98  
99  }