forked from signalapp/Signal-Android
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimise and test IP2Country (#1684)
* Move ipv4Int to top level * Remove redundant fun calls in ipv4ToCountry * Add null safety to loadFile * Close streams on failure * Simplify cacheCountryForIP * Add IP2CountryTest * Generate binary * Simplify ipv4Int * Fix companion object visibility * Use array instead of Treemap * Synchronize OnionApi#paths * Move csv * Deduplicate locations csv * Move ipToCode to gradle * Use std lib binarySearch --------- Co-authored-by: bemusementpark <bemusementpark>
- Loading branch information
1 parent
4917548
commit 3e17ab2
Showing
6 changed files
with
148 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import java.io.File | ||
import java.io.DataOutputStream | ||
import java.io.FileOutputStream | ||
|
||
task("ipToCode") { | ||
val inputFile = File("${projectDir}/geolite2_country_blocks_ipv4.csv") | ||
|
||
val outputDir = "${buildDir}/generated/binary" | ||
val outputFile = File(outputDir, "geolite2_country_blocks_ipv4.bin").apply { parentFile.mkdirs() } | ||
|
||
outputs.file(outputFile) | ||
|
||
doLast { | ||
|
||
// Ensure the input file exists | ||
if (!inputFile.exists()) { | ||
throw IllegalArgumentException("Input file does not exist: ${inputFile.absolutePath}") | ||
} | ||
|
||
// Create a DataOutputStream to write binary data | ||
DataOutputStream(FileOutputStream(outputFile)).use { out -> | ||
inputFile.useLines { lines -> | ||
var prevCode = -1 | ||
lines.drop(1).forEach { line -> | ||
runCatching { | ||
val ints = line.split(".", "/", ",") | ||
val code = ints[5].toInt().also { if (it == prevCode) return@forEach } | ||
val ip = ints.take(4).fold(0) { acc, s -> acc shl 8 or s.toInt() } | ||
|
||
out.writeInt(ip) | ||
out.writeInt(code) | ||
|
||
prevCode = code | ||
} | ||
} | ||
} | ||
} | ||
|
||
println("Processed data written to: ${outputFile.absolutePath}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/src/test/java/org/thoughtcrime/securesms/util/IP2CountryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.thoughtcrime.securesms.util | ||
|
||
import android.content.Context | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
import org.mockito.Mockito.mock | ||
|
||
@RunWith(Parameterized::class) | ||
class IP2CountryTest( | ||
private val ip: String, | ||
private val country: String | ||
) { | ||
private val context: Context = mock(Context::class.java) | ||
private val ip2Country = IP2Country(context, this::class.java.classLoader!!::getResourceAsStream) | ||
|
||
@Test | ||
fun getCountryNamesCache() { | ||
assertEquals(country, ip2Country.cacheCountryForIP(ip)) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
@Parameterized.Parameters | ||
fun data(): Collection<Array<Any>> = listOf( | ||
arrayOf("223.121.64.0", "Hong Kong"), | ||
arrayOf("223.121.64.1", "Hong Kong"), | ||
arrayOf("223.121.127.0", "Hong Kong"), | ||
arrayOf("223.121.128.0", "China"), | ||
arrayOf("223.121.129.0", "China"), | ||
arrayOf("223.122.0.0", "Hong Kong"), | ||
arrayOf("223.123.0.0", "Pakistan"), | ||
arrayOf("223.123.128.0", "China"), | ||
arrayOf("223.124.0.0", "China"), | ||
arrayOf("223.128.0.0", "China"), | ||
arrayOf("223.130.0.0", "Singapore") | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters