|
| 1 | +package io.github.easy4j.hermes.security; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.net.InetAddress; |
| 6 | +import java.net.UnknownHostException; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 11 | + |
| 12 | +/** |
| 13 | + * EndpointPolicy 边界分支的确定性覆盖测试。 |
| 14 | + * |
| 15 | + * <p>全部用例不依赖真实 DNS:要么在解析前即被拦截(畸形 URL、空 host、端口失配), |
| 16 | + * 要么通过包级 HostResolver 注入固定解析结果(仅 strictPublic 提供注入点)。</p> |
| 17 | + */ |
| 18 | +class EndpointPolicyEdgeCoverageTest { |
| 19 | + |
| 20 | + private static InetAddress addr(String literal) { |
| 21 | + try { |
| 22 | + return InetAddress.getByName(literal); |
| 23 | + } catch (UnknownHostException e) { |
| 24 | + throw new IllegalStateException(e); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void shouldRejectMalformedUrlBeforeSchemeCheck() { |
| 30 | + EndpointPolicy policy = EndpointPolicy.strictPublic(); |
| 31 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
| 32 | + () -> policy.require("https://[::1")); |
| 33 | + assertTrue(ex.getMessage().contains("Malformed URL")); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void shouldRejectUrlWithoutHost() { |
| 38 | + EndpointPolicy policy = EndpointPolicy.strictPublic(); |
| 39 | + assertThrows(IllegalArgumentException.class, () -> policy.require("https:///path")); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void shouldTreatPortlessHttpsAsPort443AndRejectOriginMismatch() { |
| 44 | + EndpointPolicy policy = EndpointPolicy.trustedLocal("127.0.0.1", 8642); |
| 45 | + assertThrows(IllegalArgumentException.class, () -> policy.require("https://127.0.0.1")); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void shouldRejectInvalidTrustedPort() { |
| 50 | + assertThrows(IllegalArgumentException.class, () -> EndpointPolicy.trustedLocal("127.0.0.1", 0)); |
| 51 | + assertThrows(IllegalArgumentException.class, |
| 52 | + () -> EndpointPolicy.trustedPrivate("10.0.0.5", 70000)); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void shouldRejectLoopbackTrustedPrivateHostAtFactoryTime() { |
| 57 | + // 127.0.0.1 是字面量,SYSTEM_RESOLVER 不经过网络;工厂阶段即拒绝环回地址。 |
| 58 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
| 59 | + () -> EndpointPolicy.trustedPrivate("127.0.0.1", 8643)); |
| 60 | + assertTrue(ex.getMessage().contains("private addresses")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void shouldAcceptSiteLocalInsideTrustedPrivate() { |
| 65 | + EndpointPolicy policy = EndpointPolicy.trustedPrivate("192.168.1.10", 8644); |
| 66 | + assertDoesNotThrow(() -> policy.require("http://192.168.1.10:8644")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void strictPublicShouldRejectEmptyResolution() { |
| 71 | + EndpointPolicy policy = EndpointPolicy.strictPublic(host -> new InetAddress[0]); |
| 72 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
| 73 | + () -> policy.require("https://multi-nic.gateway.example.com")); |
| 74 | + assertTrue(ex.getMessage().contains("no addresses")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void strictPublicShouldRejectWhenAnyResolvedAddressIsPrivate() { |
| 79 | + EndpointPolicy policy = EndpointPolicy.strictPublic( |
| 80 | + host -> new InetAddress[] {addr("93.184.216.34"), addr("10.0.0.7")}); |
| 81 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
| 82 | + () -> policy.require("https://multi-nic.gateway.example.com")); |
| 83 | + assertTrue(ex.getMessage().contains("non-public address")); |
| 84 | + assertTrue(ex.getMessage().contains("10.0.0.7")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void strictPublicShouldRejectUnresolvableHost() { |
| 89 | + EndpointPolicy policy = EndpointPolicy.strictPublic(host -> { |
| 90 | + throw new UnknownHostException("injected"); |
| 91 | + }); |
| 92 | + assertThrows(IllegalArgumentException.class, |
| 93 | + () -> policy.require("https://injected-failure.gateway.example.com")); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void strictPublicShouldRejectLoopbackResolution() { |
| 98 | + EndpointPolicy policy = EndpointPolicy.strictPublic( |
| 99 | + host -> new InetAddress[] {addr("127.0.0.1")}); |
| 100 | + assertThrows(IllegalArgumentException.class, |
| 101 | + () -> policy.require("https://rebind.gateway.example.com")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void strictPublicShouldAcceptPublicIpv6AndStripBrackets() { |
| 106 | + EndpointPolicy policy = EndpointPolicy.strictPublic( |
| 107 | + host -> new InetAddress[] {addr("2001:4860:4860::8888")}); |
| 108 | + assertDoesNotThrow(() -> policy.require("https://[2001:4860:4860::8888]/v1")); |
| 109 | + } |
| 110 | +} |
0 commit comments