]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14682 - Explicit ipv6 case insensitivity coverage
authorBelen Pruvost <belen.pruvost@sonarsource.com>
Thu, 15 Apr 2021 12:10:56 +0000 (14:10 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 15 Apr 2021 20:03:44 +0000 (20:03 +0000)
server/sonar-server-common/src/test/java/org/sonar/server/webhook/WebhookCustomDnsTest.java

index 8c6906b9490360b00b4d6cca20480281fba7b8b5..8ebee139b631280b1c7ade4696ddd9451b8a3aa5 100644 (file)
 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<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())
@@ -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();
+  }
 }