package org.sonar.server.webhook;
import com.google.common.collect.ImmutableList;
+import java.net.Inet6Address;
import java.net.InetAddress;
+import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
+import java.util.Collections;
import java.util.Optional;
import okhttp3.HttpUrl;
import org.assertj.core.api.Assertions;
.isInstanceOf(IllegalArgumentException.class);
}
+ @Test
+ public void lookup_fail_on_ipv6_local_case_insensitive() throws UnknownHostException, SocketException {
+ Optional<InetAddress> inet6Address = Collections.list(NetworkInterface.getNetworkInterfaces())
+ .stream()
+ .flatMap(ni -> Collections.list(ni.getInetAddresses()).stream())
+ .filter(i -> i instanceof Inet6Address).findAny();
+
+ if (!inet6Address.isPresent()) {
+ return;
+ }
+
+ String differentCaseAddress = getDifferentCaseInetAddress(inet6Address.get());
+
+ when(configuration.getBoolean(SONAR_VALIDATE_WEBHOOKS.getKey())).thenReturn(Optional.of(true));
+ when(networkInterfaceProvider.getNetworkInterfaceAddresses())
+ .thenReturn(ImmutableList.of(inet6Address.get()));
+
+ Assertions.assertThatThrownBy(() -> underTest.lookup(differentCaseAddress))
+ .hasMessageContaining(INVALID_URL)
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
@Test
public void lookup_fail_on_network_interface_throwing_socket_exception() throws SocketException {
when(networkInterfaceProvider.getNetworkInterfaceAddresses())
Assertions.assertThat(underTest.lookup("sonarsource.com").toString()).contains("sonarsource.com/");
}
+
+ private String getDifferentCaseInetAddress(InetAddress inetAddress) {
+ StringBuilder differentCaseAddress = new StringBuilder();
+ String address = inetAddress.getHostAddress();
+ int i;
+ for (i = 0; i < address.length(); i++) {
+ char c = address.charAt(i);
+ if (Character.isAlphabetic(c)) {
+ differentCaseAddress.append(Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c));
+ break;
+ } else {
+ differentCaseAddress.append(c);
+ }
+ }
+
+ if (i < address.length() - 1) {
+ differentCaseAddress.append(address.substring(i + 1));
+ }
+
+ return differentCaseAddress.toString();
+ }
}