package com.ruoyi.system.rental; import org.junit.Test; import static org.junit.Assert.*; /** 诊断不能只留下笼统失败,也不得依赖可能包含凭证的异常原文。 */ public class RentalWebhookDiagnosticsTest { @Test public void privateTargetIsRejectedBeforeConnectionAndClassified() throws Exception { try { RentalHttp.callbackClient().dns().lookup("192.168.3.103"); fail("内网目标必须被现有回调策略拒绝"); } catch (RentalHttp.CallbackTargetRejectedException ex) { assertEquals("TARGET_NOT_PUBLIC", RentalWorkers.callbackFailure(ex, "HTTP_EXECUTE")); } } @Test public void enabledPrivateCallbacksAllowBusinessNetworksButKeepSpecialAddressesBlocked() throws Exception { okhttp3.OkHttpClient client = RentalHttp.callbackClient(true); for (String host : new String[] {"192.168.3.103", "10.0.0.1", "172.16.0.1", "fd00::1"}) { assertFalse(client.dns().lookup(host).isEmpty()); } for (String host : new String[] {"127.0.0.1", "::1", "169.254.169.254", "0.0.0.0", "224.0.0.1"}) { try { client.dns().lookup(host); fail("特殊用途目标仍应拒绝:" + host); } catch (RentalHttp.CallbackTargetRejectedException expected) { // 开启内网只扩大业务私网范围,不关闭所有地址检查。 } } assertFalse(client.followRedirects()); assertFalse(client.followSslRedirects()); } @Test public void httpClientAllowsOnlyResolvedPrivateTargetsAndValidPorts() throws Exception { RentalHttp.checkUrl("http://192.168.3.103/api/api/webhook/debug"); RentalHttp.checkUrl("http://192.168.3.103:2020/api/webhook/debug"); okhttp3.OkHttpClient client = RentalHttp.callbackClient(true, true); assertFalse(client.dns().lookup("192.168.3.103").isEmpty()); assertEquals(java.net.Proxy.NO_PROXY, client.proxy()); for (String host : new String[] {"8.8.8.8", "127.0.0.1", "169.254.169.254"}) { try { client.dns().lookup(host); fail("HTTP不能连接公网或特殊地址"); } catch (RentalHttp.CallbackTargetRejectedException expected) { } } try { RentalHttp.checkUrl("http://192.168.3.103:65536/path"); fail("端口越界应拒绝"); } catch (IllegalArgumentException expected) { } try { RentalHttp.callbackClient(false, true).dns().lookup("192.168.3.103"); fail("关闭内网配置应拒绝HTTP私网目标"); } catch (RentalHttp.CallbackTargetRejectedException expected) { } } @Test public void networkAndCredentialErrorsHaveSafeDistinctReasons() { assertEquals("TLS_CERTIFICATE_OR_HANDSHAKE_FAILED", RentalWorkers.callbackFailure( new javax.net.ssl.SSLHandshakeException("secret must not appear"), "HTTP_EXECUTE")); assertEquals("DNS_RESOLUTION_FAILED", RentalWorkers.callbackFailure( new java.net.UnknownHostException("secret must not appear"), "HTTP_EXECUTE")); assertEquals("TIMEOUT_OR_INTERRUPTED", RentalWorkers.callbackFailure( new java.net.SocketTimeoutException("secret must not appear"), "HTTP_EXECUTE")); assertEquals("NO_VALID_CREDENTIAL_OR_LOOKUP_FAILED", RentalWorkers.callbackFailure( new IllegalArgumentException("secret must not appear"), "CREDENTIAL_LOOKUP")); } }