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