@@ -9,6 +9,7 @@ import com.x8bit.bitwarden.data.platform.util.getWebHostFromAndroidUriOrNull
9
9
import com.x8bit.bitwarden.data.platform.util.hasHttpProtocol
10
10
import com.x8bit.bitwarden.data.platform.util.hasPort
11
11
import com.x8bit.bitwarden.data.platform.util.isAndroidApp
12
+ import com.x8bit.bitwarden.data.platform.util.isIpAddress
12
13
import com.x8bit.bitwarden.data.platform.util.parseDomainOrNull
13
14
import com.x8bit.bitwarden.data.platform.util.toUriOrNull
14
15
import io.mockk.every
@@ -200,6 +201,12 @@ class StringExtensionsTest {
200
201
assertTrue(uriString.hasPort())
201
202
}
202
203
204
+ @Test
205
+ fun `hasPort returns true when URI is IP address and port` () {
206
+ val uriString = " 192.168.1.1:8080"
207
+ assertTrue(uriString.hasPort())
208
+ }
209
+
203
210
@Test
204
211
fun `getHostWithPortOrNull should return host with port when present` () {
205
212
val uriString = " www.google.com:8080"
@@ -223,4 +230,48 @@ class StringExtensionsTest {
223
230
val uriString = " androidapp://www.google.com:8080"
224
231
assertEquals(" www.google.com:8080" , uriString.getHostWithPortOrNull())
225
232
}
233
+
234
+ @Suppress(" MaxLineLength" )
235
+ @Test
236
+ fun `getHostWithPortOrNull should return host with port when URI is IP address with port and scheme` () {
237
+ val uriString = " https://192.168.1.1:8080"
238
+ assertEquals(" 192.168.1.1:8080" , uriString.getHostWithPortOrNull())
239
+ }
240
+
241
+ @Test
242
+ fun `isIpAddress should return correct value for various IP addresses` () {
243
+ testIpAddresses.forEach { (ipAddress, expected) ->
244
+ assertEquals(expected, ipAddress.isIpAddress()) { " Failed for $ipAddress " }
245
+ }
246
+ }
247
+
248
+ private val testIpAddresses = listOf (
249
+ " 192.168.1.1" to true ,
250
+ " 10.0.0.5:8080" to true ,
251
+ " 255.255.255.0:9000" to true ,
252
+ " 0.0.0.0" to true ,
253
+ " 256.1.1.1" to false ,
254
+ " 10.1.1" to false ,
255
+ " 10.1.1.1:abc" to false ,
256
+ " 10.1.1.1:-1" to false ,
257
+ " 10.1.1.1:" to false ,
258
+ " 10.1.1.1:0" to true ,
259
+ " 1.1.1.1:123" to true ,
260
+ " 1.1.1.1:65535" to true ,
261
+ " 1.1.1.1:65536" to false ,
262
+ " 1.1.1.1:99999" to false ,
263
+ " :1234" to false ,
264
+ " 255.255.255.255:65535" to true ,
265
+ " 255.255.255.255:0" to true ,
266
+ " 255.255.255.255:" to false ,
267
+ " 255.255.255.255:-1" to false ,
268
+ " http://192.168.1.1" to true ,
269
+ " https://10.0.0.5:8080" to true ,
270
+ " http://255.255.255.0:9000" to true ,
271
+ " https://0.0.0.0" to true ,
272
+ " http://256.1.1.1" to false ,
273
+ " https://10.1.1" to false ,
274
+ " http://10.1.1.1:abc" to false ,
275
+ " https://10.1.1.1:-1" to false ,
276
+ )
226
277
}
0 commit comments