|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.web.server.util.matcher; |
| 18 | + |
| 19 | +import java.net.InetAddress; |
| 20 | +import java.net.InetSocketAddress; |
| 21 | +import java.net.UnknownHostException; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 26 | + |
| 27 | +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; |
| 28 | +import org.springframework.mock.web.server.MockServerWebExchange; |
| 29 | +import org.springframework.web.server.ServerWebExchange; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for {@link IpAddressServerWebExchangeMatcher} |
| 36 | + * |
| 37 | + * @author Guirong Hu |
| 38 | + */ |
| 39 | +@ExtendWith(MockitoExtension.class) |
| 40 | +public class IpAddressServerWebExchangeMatcherTests { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void matchesWhenIpv6RangeAndIpv6AddressThenTrue() throws UnknownHostException { |
| 44 | + ServerWebExchange ipv6Exchange = exchange("fe80::21f:5bff:fe33:bd68"); |
| 45 | + ServerWebExchangeMatcher.MatchResult matches = new IpAddressServerWebExchangeMatcher("fe80::21f:5bff:fe33:bd68") |
| 46 | + .matches(ipv6Exchange).block(); |
| 47 | + assertThat(matches.isMatch()).isTrue(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void matchesWhenIpv6RangeAndIpv4AddressThenFalse() throws UnknownHostException { |
| 52 | + ServerWebExchange ipv4Exchange = exchange("192.168.1.104"); |
| 53 | + ServerWebExchangeMatcher.MatchResult matches = new IpAddressServerWebExchangeMatcher("fe80::21f:5bff:fe33:bd68") |
| 54 | + .matches(ipv4Exchange).block(); |
| 55 | + assertThat(matches.isMatch()).isFalse(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void matchesWhenIpv4RangeAndIpv4AddressThenTrue() throws UnknownHostException { |
| 60 | + ServerWebExchange ipv4Exchange = exchange("192.168.1.104"); |
| 61 | + ServerWebExchangeMatcher.MatchResult matches = new IpAddressServerWebExchangeMatcher("192.168.1.104") |
| 62 | + .matches(ipv4Exchange).block(); |
| 63 | + assertThat(matches.isMatch()).isTrue(); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void matchesWhenIpv4SubnetAndIpv4AddressThenTrue() throws UnknownHostException { |
| 68 | + ServerWebExchange ipv4Exchange = exchange("192.168.1.104"); |
| 69 | + IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("192.168.1.0/24"); |
| 70 | + assertThat(matcher.matches(ipv4Exchange).block().isMatch()).isTrue(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void matchesWhenIpv4SubnetAndIpv4AddressThenFalse() throws UnknownHostException { |
| 75 | + ServerWebExchange ipv4Exchange = exchange("192.168.1.104"); |
| 76 | + IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("192.168.1.128/25"); |
| 77 | + assertThat(matcher.matches(ipv4Exchange).block().isMatch()).isFalse(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void matchesWhenIpv6SubnetAndIpv6AddressThenTrue() throws UnknownHostException { |
| 82 | + ServerWebExchange ipv6Exchange = exchange("2001:DB8:0:FFFF:FFFF:FFFF:FFFF:FFFF"); |
| 83 | + IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("2001:DB8::/48"); |
| 84 | + assertThat(matcher.matches(ipv6Exchange).block().isMatch()).isTrue(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void matchesWhenIpv6SubnetAndIpv6AddressThenFalse() throws UnknownHostException { |
| 89 | + ServerWebExchange ipv6Exchange = exchange("2001:DB8:1:0:0:0:0:0"); |
| 90 | + IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("2001:DB8::/48"); |
| 91 | + assertThat(matcher.matches(ipv6Exchange).block().isMatch()).isFalse(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void matchesWhenZeroMaskAndAnythingThenTrue() throws UnknownHostException { |
| 96 | + IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("0.0.0.0/0"); |
| 97 | + assertThat(matcher.matches(exchange("123.4.5.6")).block().isMatch()).isTrue(); |
| 98 | + assertThat(matcher.matches(exchange("192.168.0.159")).block().isMatch()).isTrue(); |
| 99 | + matcher = new IpAddressServerWebExchangeMatcher("192.168.0.159/0"); |
| 100 | + assertThat(matcher.matches(exchange("123.4.5.6")).block().isMatch()).isTrue(); |
| 101 | + assertThat(matcher.matches(exchange("192.168.0.159")).block().isMatch()).isTrue(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void constructorWhenIpv4AddressMaskTooLongThenIllegalArgumentException() { |
| 106 | + String ipv4AddressWithTooLongMask = "192.168.1.104/33"; |
| 107 | + assertThatIllegalArgumentException() |
| 108 | + .isThrownBy(() -> new IpAddressServerWebExchangeMatcher(ipv4AddressWithTooLongMask)) |
| 109 | + .withMessage(String.format("IP address %s is too short for bitmask of length %d", "192.168.1.104", 33)); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void constructorWhenIpv6AddressMaskTooLongThenIllegalArgumentException() { |
| 114 | + String ipv6AddressWithTooLongMask = "fe80::21f:5bff:fe33:bd68/129"; |
| 115 | + assertThatIllegalArgumentException() |
| 116 | + .isThrownBy(() -> new IpAddressServerWebExchangeMatcher(ipv6AddressWithTooLongMask)) |
| 117 | + .withMessage(String.format("IP address %s is too short for bitmask of length %d", |
| 118 | + "fe80::21f:5bff:fe33:bd68", 129)); |
| 119 | + } |
| 120 | + |
| 121 | + private static ServerWebExchange exchange(String ipAddress) throws UnknownHostException { |
| 122 | + return MockServerWebExchange.builder(MockServerHttpRequest.get("/") |
| 123 | + .remoteAddress(new InetSocketAddress(InetAddress.getByName(ipAddress), 8080))).build(); |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments