From 14b7ccf9d5f5b1574e0fe5c14974973e7029a42e Mon Sep 17 00:00:00 2001 From: HissPirat Date: Sat, 5 Sep 2020 13:34:47 +0200 Subject: [PATCH] #2348 updated formatIpForAndroidPie to use pattern matching and added unit tests --- .../kiwixmobile/core/utils/ServerUtils.kt | 29 ++++----- .../kiwixmobile/core/utils/ServerUtilsTest.kt | 61 +++++++++++++++++++ 2 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 core/src/test/java/org/kiwix/kiwixmobile/core/utils/ServerUtilsTest.kt diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt index 385897e4d..b4b6a0722 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/ServerUtils.kt @@ -17,6 +17,8 @@ */ package org.kiwix.kiwixmobile.core.utils +import android.util.Log +import org.kiwix.kiwixmobile.core.main.AddNoteDialog.TAG import java.net.InetAddress import java.net.NetworkInterface import java.net.SocketException @@ -37,9 +39,12 @@ object ServerUtils { } // To remove extra characters from IP for Android Pie ip = formatIpForAndroidPie(ip) - } catch (e: SocketException) { - e.printStackTrace() - ip += "Something Wrong! $e\n" + } catch (socketException: SocketException) { + Log.e(TAG, "$socketException") + ip += "Something Wrong! $socketException\n" + } catch (invalidIpException: IllegalArgumentException) { + Log.e(TAG, "$invalidIpException") + ip += "Something Wrong! $invalidIpException\n" } return ip } @@ -48,17 +53,13 @@ object ServerUtils { (inetAddress.hostAddress + "\n").takeIf { inetAddress.isSiteLocalAddress } ?: "" @Suppress("MagicNumber") - private fun formatIpForAndroidPie(ip: String): String { - var result: String = ip - if (ip.length > 14) { - for (i in 14 until ip.length) { - if (ip[i] == '.') { - result = ip.substring(0, i - 2) - break - } - } - } - return result + fun formatIpForAndroidPie(ip: String): String { + // regex from OneCricketeer @ https://stackoverflow.com/a/15875500/14053602 + val ipRegex = + "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" + .toRegex() + val ipMatch = ipRegex.find(ip, 0) ?: throw IllegalArgumentException() + return ipMatch.value } @JvmStatic fun getSocketAddress(): String = diff --git a/core/src/test/java/org/kiwix/kiwixmobile/core/utils/ServerUtilsTest.kt b/core/src/test/java/org/kiwix/kiwixmobile/core/utils/ServerUtilsTest.kt new file mode 100644 index 000000000..1cb6d9da9 --- /dev/null +++ b/core/src/test/java/org/kiwix/kiwixmobile/core/utils/ServerUtilsTest.kt @@ -0,0 +1,61 @@ +/* + * Kiwix Android + * Copyright (c) 2020 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.core.utils + +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +internal class ServerUtilsTest { + + @Test + internal fun `formatIpForAndroidPie should return first ip occurrence of pie address`() { + assertThat( + ServerUtils.formatIpForAndroidPie( + """ + fec0::15:b2ff:fe00:0 + fec0::15d6:ece1:61fb:5b55 + 192.168.232.2 + fec0::d8d1:9ff:fe42:160c + fec0::8d6e:2327:6d9f:ce75 + 192.168.200.2 + """.trimIndent() + ) + ).isEqualTo( + "192.168.232.2" + ) + } + + @Test + internal fun `formatIpForAndroidPie should return full ip on given ip`() { + assertThat( + ServerUtils.formatIpForAndroidPie( + """ + 192.168.232.2 + """.trimIndent() + ) + ).isEqualTo( + "192.168.232.2" + ) + } + + @Test(expected = IllegalArgumentException::class) + internal fun `formatIpForAndroidPie should throw invalid argument exception on invalid ip`() { + ServerUtils.formatIpForAndroidPie("invalid ip") + } +}