ptupitsyn commented on a change in pull request #8591:
URL: https://github.com/apache/ignite/pull/8591#discussion_r545881507



##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/util/HostAndPortRangeTest.java
##########
@@ -0,0 +1,156 @@
+/*
+ * 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.ignite.internal.util;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class HostAndPortRangeTest {

Review comment:
       Javadoc is required for all classes.

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +53,89 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
+        if (addrStr.contains("[")) { // IPv6

Review comment:
       `[` and `]` are only needed when there is a port, otherwise IPv6 address 
does not have those.

##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/util/HostAndPortRangeTest.java
##########
@@ -0,0 +1,156 @@
+/*
+ * 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.ignite.internal.util;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class HostAndPortRangeTest {

Review comment:
       Looks like this test is not added to any suite, and thus won't run on CI.

##########
File path: 
modules/core/src/test/java/org/apache/ignite/internal/util/HostAndPortRangeTest.java
##########
@@ -0,0 +1,156 @@
+/*
+ * 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.ignite.internal.util;
+
+import org.apache.ignite.IgniteCheckedException;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class HostAndPortRangeTest {
+
+    /**
+     * tests correct input address with IPv4 host and port range.
+     * @throws IgniteCheckedException on incorrect host/port
+     */
+    @Test
+    public void testParseIPv4WithPortRange() throws IgniteCheckedException {
+        String addrStr = "127.0.0.1:8080..8090";
+        String errMsgPrefix = "";
+        int dfltPortFrom = 18360;
+        int dfltPortTo = 18362;
+        HostAndPortRange actual = HostAndPortRange.parse(addrStr, 
dfltPortFrom, dfltPortTo, errMsgPrefix);
+        HostAndPortRange expected = new HostAndPortRange("127.0.0.1", 8080, 
8090);
+        assertEquals(expected, actual);
+    }
+
+    /**
+     * tests correct input address with IPv4 host and single port.
+     * @throws IgniteCheckedException on incorrect host/port
+     */
+    @Test
+    public void testParseIPv4WithSinglePort() throws IgniteCheckedException {
+        String addrStr = "127.0.0.1:8080";
+        String errMsgPrefix = "";
+        int dfltPortFrom = 18360;
+        int dfltPortTo = 18362;
+        HostAndPortRange actual = HostAndPortRange.parse(addrStr, 
dfltPortFrom, dfltPortTo, errMsgPrefix);
+        HostAndPortRange expected = new HostAndPortRange("127.0.0.1", 8080, 
8080);
+        assertEquals(expected, actual);
+    }
+
+    /**
+     * ests correct input address with IPv4 host and no port.
+     * @throws IgniteCheckedException on incorrect host/port
+     */
+    @Test
+    public void testParseIPv4NoPort() throws IgniteCheckedException {
+        String addrStr = "127.0.0.1";
+        String errMsgPrefix = "";
+        int dfltPortFrom = 18360;
+        int dfltPortTo = 18362;
+        HostAndPortRange actual = HostAndPortRange.parse(addrStr, 
dfltPortFrom, dfltPortTo, errMsgPrefix);
+        HostAndPortRange expected = new HostAndPortRange("127.0.0.1", 18360, 
18362);
+        assertEquals(expected, actual);
+    }
+
+    /**
+     * tests correct input address with IPv6 host and port range.
+     * @throws IgniteCheckedException on incorrect host/port
+     */
+    @Test
+    public void testParseIPv6WithPortRange() throws IgniteCheckedException {
+        String addrStr = "[::1]:8080..8090";
+        String errMsgPrefix = "";
+        int dfltPortFrom = 18360;
+        int dfltPortTo = 18362;
+        HostAndPortRange actual = HostAndPortRange.parse(addrStr, 
dfltPortFrom, dfltPortTo, errMsgPrefix);
+        HostAndPortRange expected = new HostAndPortRange("::1", 8080, 8090);
+        assertEquals(expected, actual);
+    }
+
+    /**
+     * tests correct input address with IPv6 host and single port.
+     * @throws IgniteCheckedException on incorrect host/port
+     */
+    @Test
+    public void testParseIPv6WithSinglePort() throws IgniteCheckedException {
+        String addrStr = "[3ffe:2a00:100:7031::]:8080";
+        String errMsgPrefix = "";
+        int dfltPortFrom = 18360;
+        int dfltPortTo = 18362;
+        HostAndPortRange actual = HostAndPortRange.parse(addrStr, 
dfltPortFrom, dfltPortTo, errMsgPrefix);
+        HostAndPortRange expected = new 
HostAndPortRange("3ffe:2a00:100:7031::", 8080, 8080);
+        assertEquals(expected, actual);
+    }
+
+    /**
+     * tests correct input address with IPv6 host and no port.
+     * @throws IgniteCheckedException on incorrect host/port
+     */
+    @Test
+    public void testParseIPv6NoPort() throws IgniteCheckedException {
+        String addrStr = "[::FFFF:129.144.52.38]";
+        String errMsgPrefix = "";
+        int dfltPortFrom = 18360;
+        int dfltPortTo = 18362;
+        HostAndPortRange actual = HostAndPortRange.parse(addrStr, 
dfltPortFrom, dfltPortTo, errMsgPrefix);
+        HostAndPortRange expected = new 
HostAndPortRange("::FFFF:129.144.52.38", 18360, 18362);
+        assertEquals(expected, actual);
+    }
+
+    /**
+     * tests incorrect input address with IPv6 host (no brackets) and port.
+     * @throws IgniteCheckedException on incorrect host/port
+     */
+    @Test(expected = IgniteCheckedException.class)

Review comment:
       Please add a check for the exception message too, not only the class.

##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/util/HostAndPortRange.java
##########
@@ -53,60 +53,89 @@ public static HostAndPortRange parse(String addrStr, int 
dfltPortFrom, int dfltP
 
         String host;
 
+        String portStr;
         int portFrom;
         int portTo;
 
         if (F.isEmpty(addrStr))
             throw createParseError(addrStr, errMsgPrefix, "Address is empty");
 
-        final int colIdx = addrStr.indexOf(':');
+        if (addrStr.contains("[")) { // IPv6
+            int hostEndIdx = addrStr.indexOf(']');
+            if (hostEndIdx == -1) {
+                throw createParseError(addrStr, errMsgPrefix, "IPv6 host is 
incorrect");
+            }
+            host = addrStr.substring(1, hostEndIdx);

Review comment:
       `substring(1` assumes that `[` is at index `0`, however, the line above 
`addrStr.contains("[")` allows `[` at any index.




----------------------------------------------------------------
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.

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


Reply via email to