1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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 }