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