Add a couple unit tests for StaticIpResolver and string into qa.

This commit is contained in:
Alex Hart
2023-03-01 09:39:40 -04:00
committed by Greyson Parrelli
parent a95e695a97
commit d0c858221e
4 changed files with 78 additions and 16 deletions

View File

@@ -18,4 +18,6 @@ ktlint {
dependencies { dependencies {
implementation(libs.dnsjava) implementation(libs.dnsjava)
testImplementation(testLibs.junit.junit)
testImplementation(testLibs.mockk)
} }

View File

@@ -14,7 +14,7 @@ import kotlin.streams.toList
* Feeds into our custom DNS resolver to provide a static IP fallback for our services. * Feeds into our custom DNS resolver to provide a static IP fallback for our services.
*/ */
class StaticIpResolver @JvmOverloads constructor( class StaticIpResolver @JvmOverloads constructor(
private val resolverProvider: () -> Resolver = { SimpleResolver("1.1.1.1") } private val recordFetcher: RecordFetcher = RealRecordFetcher
) { ) {
/** /**
@@ -56,13 +56,7 @@ class StaticIpResolver @JvmOverloads constructor(
private fun resolveOnce(hostName: String): List<String> { private fun resolveOnce(hostName: String): List<String> {
try { try {
val resolver = resolverProvider() val records = recordFetcher.fetchRecords(hostName)
val lookup: Lookup = doLookup(hostName)
lookup.setResolver(resolver)
val records: Array<Record>? = lookup.run()
if (records != null) { if (records != null) {
return records return records
.filter { it.type == Type.A } .filter { it.type == Type.A }
@@ -71,19 +65,34 @@ class StaticIpResolver @JvmOverloads constructor(
.map { it.hostAddress } .map { it.hostAddress }
.filterNotNull() .filterNotNull()
} else { } else {
throw IllegalStateException("Failed to resolve host! $hostName") throw IllegalStateException("Failed to resolve host! Lookup did not return any records.. $hostName")
} }
} catch (e: UnknownHostException) { } catch (e: UnknownHostException) {
throw IllegalStateException("Failed to resolve host! $hostName") throw IllegalStateException("Failed to resolve host! $hostName", e)
} }
} }
@Throws(UnknownHostException::class) interface RecordFetcher {
private fun doLookup(hostname: String): Lookup { fun fetchRecords(hostName: String): Array<Record>?
try { }
return Lookup(hostname)
} catch (e: Throwable) { private object RealRecordFetcher : RecordFetcher {
throw UnknownHostException() override fun fetchRecords(hostName: String): Array<Record>? {
val resolver = SimpleResolver("1.1.1.1")
val lookup: Lookup = doLookup(hostName)
lookup.setResolver(resolver)
return lookup.run()
}
@Throws(UnknownHostException::class)
private fun doLookup(hostname: String): Lookup {
try {
return Lookup(hostname)
} catch (e: Throwable) {
throw UnknownHostException()
}
} }
} }
} }

View File

@@ -0,0 +1,50 @@
package org.signal.buildtools
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Test
import org.xbill.DNS.ARecord
import org.xbill.DNS.DClass
import org.xbill.DNS.Name
import org.xbill.DNS.Record
import java.lang.IllegalStateException
import java.net.Inet4Address
class StaticIpResolverTest {
companion object {
const val SIGNAL_DOT_ORG = "www.signal.org"
val SIGNAL_IP = byteArrayOf(123, 45, 67, 89)
val STRINGIFIED_IP = SIGNAL_IP.joinToString(".")
}
@Test
fun `Given a hostname with records, when I resolveToBuildConfig, then I expect a matching IP`() {
val staticIpResolver = StaticIpResolver(FakeRecordFetcher())
val actual = staticIpResolver.resolveToBuildConfig(SIGNAL_DOT_ORG)
val expected = """
new String[]{"$STRINGIFIED_IP"}
""".trimIndent()
assertEquals(expected, actual)
}
@Test(expected = IllegalStateException::class)
fun `Given a hostname without records, when I resolveToBuildConfig, then I expect`() {
val staticIpResolver = StaticIpResolver(FakeRecordFetcher(emptyMap()))
staticIpResolver.resolveToBuildConfig(SIGNAL_DOT_ORG)
}
private class FakeRecordFetcher(private val recordMap: Map<String, Array<Record>?> = mapOf(
SIGNAL_DOT_ORG to arrayOf(ARecord(Name.fromString("www."), DClass.ANY, 0L, mockk<Inet4Address> {
every { address } returns SIGNAL_IP
every { hostAddress } returns STRINGIFIED_IP
}))
)) : StaticIpResolver.RecordFetcher {
override fun fetchRecords(hostName: String): Array<Record>? {
return recordMap[hostName]
}
}
}

View File

@@ -64,6 +64,7 @@ task qa {
group 'Verification' group 'Verification'
description 'Quality Assurance. Run before pushing.' description 'Quality Assurance. Run before pushing.'
dependsOn 'clean', dependsOn 'clean',
':build-logic:tools:test',
':Signal-Android:testPlayProdReleaseUnitTest', ':Signal-Android:testPlayProdReleaseUnitTest',
':Signal-Android:lintPlayProdRelease', ':Signal-Android:lintPlayProdRelease',
'Signal-Android:ktlintCheck', 'Signal-Android:ktlintCheck',