mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-14 09:57:31 -04:00
#2348 updated formatIpForAndroidPie to use pattern matching and added unit tests
This commit is contained in:
parent
b8d712a2d4
commit
14b7ccf9d5
@ -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 =
|
||||
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Kiwix Android
|
||||
* Copyright (c) 2020 Kiwix <android.kiwix.org>
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user