mimaison commented on code in PR #22501:
URL: https://github.com/apache/kafka/pull/22501#discussion_r3527773552


##########
metadata/src/test/java/org/apache/kafka/metadata/authorizer/CidrUtilsTest.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.metadata.authorizer;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import java.net.InetAddress;
+
+import static org.apache.kafka.metadata.authorizer.CidrUtils.isInRange;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+
+@Timeout(value = 40)
+public class CidrUtilsTest {
+
+    @Test
+    public void testIsInRangeNullOrNonCidr() {
+        assertFalse(isInRange("192.168.1.1", null));
+        assertFalse(isInRange("192.168.1.1", "192.168.1.1"));
+    }
+
+    @Test
+    public void testIpv4MappedAddress() throws Exception {
+        // JVM normalizes ::ffff:x.x.x.x to plain IPv4 Inet4Address,
+        // so CidrUtils dispatches to SubnetUtils (IPv4 path), not 
SubnetUtils6.
+        String normalizedHost = 
InetAddress.getByName("::ffff:192.168.1.5").getHostAddress();
+        assertEquals("192.168.1.5", normalizedHost);
+        assertTrue(isInRange(normalizedHost, "192.168.1.0/24"));
+        assertFalse(isInRange(normalizedHost, "10.0.0.0/8"));
+    }
+
+    @Test
+    public void testIpv4MappedAddressWithSubnet() {
+        // ::ffff:x.x.x.x/N: JVM normalizes ::ffff:x.x.x.x to Inet4Address, 
isIpv6() returns false,

Review Comment:
   With the latest commit `isIpv6()` now actually returns `true`



##########
metadata/src/test/java/org/apache/kafka/metadata/authorizer/CidrUtilsTest.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.metadata.authorizer;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import java.net.InetAddress;
+
+import static org.apache.kafka.metadata.authorizer.CidrUtils.isInRange;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+
+@Timeout(value = 40)
+public class CidrUtilsTest {

Review Comment:
   Yes it's cover indirectly but but I'd rather have the tests that check the 
inner logic of `CidrUtils.validate()` in `CidrUtilsTest` and only have tests 
that check `validateHostPattern()` in `AclControlManagerTest`. For example 
we're missing test with a hostname (example.com) and hostname and a path 
(example.com/test)



##########
metadata/src/test/java/org/apache/kafka/controller/AclControlManagerTest.java:
##########
@@ -440,4 +442,133 @@ public void testDeleteExceedsMaxRecords() {
         assertEquals(BoundedListTooLongException.class, 
exception.getCause().getClass());
         assertEquals("Cannot remove more than " + MAX_RECORDS_PER_USER_OP + " 
acls in a single delete operation.", exception.getCause().getMessage());
     }
+
+    @Test
+    public void testValidateHostPatternValid() {
+        // Wildcard, this works with or without CIDR support
+        AclControlManager.validateHostPattern("*", false);
+        AclControlManager.validateHostPattern("*", true);
+
+        // Regular IPv4 addresses this works with or without CIDR support
+        AclControlManager.validateHostPattern("192.168.1.1", false);
+        AclControlManager.validateHostPattern("10.0.0.1", false);
+        AclControlManager.validateHostPattern("127.0.0.1", true);
+
+        // Regular IPv6 addresses, this works with or without CIDR support
+        AclControlManager.validateHostPattern("2001:db8::1", false);
+        AclControlManager.validateHostPattern("::1", false);
+        AclControlManager.validateHostPattern("fe80::1", true);
+
+        // Valid IPv4 CIDR notations (require CIDR support)
+        AclControlManager.validateHostPattern("192.168.0.0/24", true);
+        AclControlManager.validateHostPattern("10.0.0.0/8", true);
+        AclControlManager.validateHostPattern("172.16.0.0/16", true);
+        AclControlManager.validateHostPattern("192.168.1.1/32", true);
+        AclControlManager.validateHostPattern("0.0.0.0/0", true);
+
+        // Valid IPv6 CIDR notations (require CIDR support)
+        AclControlManager.validateHostPattern("2001:db8::/32", true);
+        AclControlManager.validateHostPattern("2001:db8:abcd::/48", true);
+        AclControlManager.validateHostPattern("::1/128", true);
+        AclControlManager.validateHostPattern("::/0", true);
+    }
+
+    @Test
+    public void testValidateHostPatternInvalid() {
+        // Null or empty
+        assertThrows(InvalidRequestException.class, () ->
+            AclControlManager.validateHostPattern(null, true));
+        assertThrows(InvalidRequestException.class, () ->
+            AclControlManager.validateHostPattern("", true));
+
+        // Invalid IPv4 CIDR, prefix too large
+        InvalidRequestException e = 
assertThrows(InvalidRequestException.class, () ->
+            AclControlManager.validateHostPattern("192.168.0.0/33", true));
+        assertTrue(e.getMessage().contains("Invalid CIDR notation"));
+
+        // Invalid IPv4 CIDR, negative prefix
+        e = assertThrows(InvalidRequestException.class, () ->
+            AclControlManager.validateHostPattern("192.168.0.0/-1", true));
+        assertTrue(e.getMessage().contains("Invalid CIDR notation"));
+
+        // Invalid IPv4 CIDR, malformed address
+        e = assertThrows(InvalidRequestException.class, () ->
+            AclControlManager.validateHostPattern("192.168.0.256/24", true));
+        assertTrue(e.getMessage().contains("Invalid CIDR notation"));
+
+        // Invalid IPv4 CIDR, non-numeric prefix
+        e = assertThrows(InvalidRequestException.class, () ->
+            AclControlManager.validateHostPattern("192.168.0.0/abc", true));
+        assertTrue(e.getMessage().contains("Invalid CIDR notation"));
+
+        // Invalid IPv6 CIDR, prefix too large
+        e = assertThrows(InvalidRequestException.class, () ->
+            AclControlManager.validateHostPattern("2001:db8::/129", true));
+        assertTrue(e.getMessage().contains("Invalid CIDR notation"));
+
+        // Invalid, just a slash with no prefix
+        e = assertThrows(InvalidRequestException.class, () ->
+            AclControlManager.validateHostPattern("192.168.0.0/", true));
+        assertTrue(e.getMessage().contains("Invalid CIDR notation"));
+
+        // ::ffff:x.x.x.x/N is rejected: the JVM normalizes ::ffff:x.x.x.x to 
Inet4Address,
+        // so isIpv6() returns false and SubnetUtils rejects the ::ffff: 
prefix.

Review Comment:
   With the latest commit `isIpv6()` now actually returns `true`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to