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 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 }