From 15415cb4005535e63f7bb73cee73ef7413f0c5dd Mon Sep 17 00:00:00 2001 From: Belen Pruvost Date: Thu, 15 Apr 2021 14:10:56 +0200 Subject: [PATCH] SONAR-14682 - Explicit ipv6 case insensitivity coverage --- .../server/webhook/WebhookCustomDnsTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookCustomDnsTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookCustomDnsTest.java index 8c6906b9490..8ebee139b63 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookCustomDnsTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookCustomDnsTest.java @@ -20,9 +20,12 @@ 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; @@ -72,6 +75,28 @@ public class WebhookCustomDnsTest { .isInstanceOf(IllegalArgumentException.class); } + @Test + public void lookup_fail_on_ipv6_local_case_insensitive() throws UnknownHostException, SocketException { + Optional 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()) @@ -97,4 +122,25 @@ public class WebhookCustomDnsTest { 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(); + } } -- 2.39.5