1+ package me .chanjar .weixin .common .util .http .apache ;
2+
3+ import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
4+ import org .apache .http .impl .client .CloseableHttpClient ;
5+ import org .testng .Assert ;
6+ import org .testng .annotations .Test ;
7+
8+ import javax .net .ssl .SSLContext ;
9+ import javax .net .ssl .SSLSocket ;
10+ import javax .net .ssl .SSLSocketFactory ;
11+ import java .lang .reflect .Constructor ;
12+ import java .lang .reflect .Field ;
13+ import java .util .Arrays ;
14+ import java .util .List ;
15+
16+ /**
17+ * 测试SSL配置,特别是TLS协议版本配置
18+ * Test SSL configuration, especially TLS protocol version configuration
19+ */
20+ public class SSLConfigurationTest {
21+
22+ @ Test
23+ public void testDefaultTLSProtocols () throws Exception {
24+ // Create a new instance to check the default configuration
25+ Class <?> builderClass = DefaultApacheHttpClientBuilder .class ;
26+ Object builder = builderClass .getDeclaredMethod ("get" ).invoke (null );
27+
28+ // 验证默认支持的TLS协议版本包含现代版本
29+ Field supportedProtocolsField = builderClass .getDeclaredField ("supportedProtocols" );
30+ supportedProtocolsField .setAccessible (true );
31+ String [] supportedProtocols = (String []) supportedProtocolsField .get (builder );
32+
33+ List <String > protocolList = Arrays .asList (supportedProtocols );
34+
35+ System .out .println ("Default supported TLS protocols: " + Arrays .toString (supportedProtocols ));
36+
37+ // 主要验证:应该支持TLS 1.2和/或1.3 (现代安全版本)
38+ // Main validation: Should support TLS 1.2 and/or 1.3 (modern secure versions)
39+ Assert .assertTrue (protocolList .contains ("TLSv1.2" ), "Should support TLS 1.2" );
40+ Assert .assertTrue (protocolList .contains ("TLSv1.3" ), "Should support TLS 1.3" );
41+
42+ // 验证不再是只有TLS 1.0 (这是导致原问题的根本原因)
43+ // Verify it's no longer just TLS 1.0 (which was the root cause of the original issue)
44+ Assert .assertTrue (protocolList .size () > 0 , "Should support at least one TLS version" );
45+ boolean hasModernTLS = protocolList .contains ("TLSv1.2" ) || protocolList .contains ("TLSv1.3" );
46+ Assert .assertTrue (hasModernTLS , "Should support at least one modern TLS version (1.2 or 1.3)" );
47+
48+ // 验证不是原来的老旧配置 (只有 "TLSv1")
49+ // Verify it's not the old configuration (only "TLSv1")
50+ boolean isOldConfig = protocolList .size () == 1 && protocolList .contains ("TLSv1" );
51+ Assert .assertFalse (isOldConfig , "Should not be the old configuration that only supported TLS 1.0" );
52+ }
53+
54+ @ Test
55+ public void testCustomTLSProtocols () throws Exception {
56+ // Test that we can set custom TLS protocols
57+ String [] customProtocols = {"TLSv1.2" , "TLSv1.3" };
58+
59+ // Create a new builder instance using reflection to avoid singleton issues in testing
60+ Class <?> builderClass = DefaultApacheHttpClientBuilder .class ;
61+ Constructor <?> constructor = builderClass .getDeclaredConstructor ();
62+ constructor .setAccessible (true );
63+ Object builder = constructor .newInstance ();
64+
65+ // Set custom protocols
66+ builderClass .getMethod ("supportedProtocols" , String [].class ).invoke (builder , (Object ) customProtocols );
67+
68+ Field supportedProtocolsField = builderClass .getDeclaredField ("supportedProtocols" );
69+ supportedProtocolsField .setAccessible (true );
70+ String [] actualProtocols = (String []) supportedProtocolsField .get (builder );
71+
72+ Assert .assertEquals (actualProtocols , customProtocols , "Custom protocols should be set correctly" );
73+
74+ System .out .println ("Custom supported TLS protocols: " + Arrays .toString (actualProtocols ));
75+ }
76+
77+ @ Test
78+ public void testSSLContextCreation () throws Exception {
79+ DefaultApacheHttpClientBuilder builder = DefaultApacheHttpClientBuilder .get ();
80+
81+ // 构建HTTP客户端以验证SSL工厂是否正确创建
82+ CloseableHttpClient client = builder .build ();
83+ Assert .assertNotNull (client , "HTTP client should be created successfully" );
84+
85+ // 验证SSL上下文支持现代TLS协议
86+ SSLContext sslContext = SSLContext .getDefault ();
87+ SSLSocketFactory socketFactory = sslContext .getSocketFactory ();
88+
89+ // 创建一个SSL socket来检查支持的协议
90+ try (SSLSocket socket = (SSLSocket ) socketFactory .createSocket ()) {
91+ String [] supportedProtocols = socket .getSupportedProtocols ();
92+ List <String > supportedList = Arrays .asList (supportedProtocols );
93+
94+ // JVM应该支持TLS 1.2(在JDK 8+中默认可用)
95+ Assert .assertTrue (supportedList .contains ("TLSv1.2" ),
96+ "JVM should support TLS 1.2. Supported protocols: " + Arrays .toString (supportedProtocols ));
97+
98+ System .out .println ("JVM supported TLS protocols: " + Arrays .toString (supportedProtocols ));
99+ }
100+
101+ client .close ();
102+ }
103+
104+ @ Test
105+ public void testBuilderChaining () {
106+ DefaultApacheHttpClientBuilder builder = DefaultApacheHttpClientBuilder .get ();
107+
108+ // 测试方法链调用
109+ ApacheHttpClientBuilder result = builder
110+ .supportedProtocols (new String []{"TLSv1.2" , "TLSv1.3" })
111+ .httpProxyHost ("proxy.example.com" )
112+ .httpProxyPort (8080 );
113+
114+ Assert .assertSame (result , builder , "Builder methods should return the same instance for method chaining" );
115+ }
116+ }
0 commit comments