Add unit tests for packet parsing, DNS cache, blocklist parser and server list

DnsCache takes its clock as a parameter so expiry can be tested.
This commit is contained in:
2026-10-25 12:16:46 +02:00
parent 43e57c9357
commit 8c6bb33527
8 changed files with 476 additions and 3 deletions
+7
View File
@@ -42,6 +42,11 @@ android {
includeInApk = false
includeInBundle = false
}
testOptions {
// android.util.Log and friends are no-ops in JVM unit tests
unitTests.isReturnDefaultValues = true
}
}
dependencies {
@@ -51,4 +56,6 @@ dependencies {
implementation("androidx.compose.material3:material3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
testImplementation("junit:junit:4.13.2")
}
@@ -3,7 +3,10 @@ package cc.skysparkle.sparkledns
import android.os.SystemClock
// TTL-aware LRU cache of DNS responses, keyed by the question section
class DnsCache(private val maxEntries: Int = 2000) {
class DnsCache(
private val maxEntries: Int = 2000,
private val clock: () -> Long = SystemClock::elapsedRealtime,
) {
private class Entry(
val response: ByteArray,
@@ -19,7 +22,7 @@ class DnsCache(private val maxEntries: Int = 2000) {
fun get(query: ByteArray): ByteArray? {
val key = keyOf(query) ?: return null
val now = SystemClock.elapsedRealtime()
val now = clock()
val entry = synchronized(map) {
val e = map[key] ?: return null
if (now >= e.expiresAt) {
@@ -47,7 +50,7 @@ class DnsCache(private val maxEntries: Int = 2000) {
val ttl = minOf(minTtl, MAX_TTL)
if (ttl <= 0) return
val now = SystemClock.elapsedRealtime()
val now = clock()
val entry = Entry(response.copyOf(), records.first, records.second, now, now + ttl * 1000L)
synchronized(map) { map[key] = entry }
}
@@ -0,0 +1,53 @@
package cc.skysparkle.sparkledns
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
class BlocklistParserTest {
@Test
fun understandsCommonListFormats() {
assertEquals("ads.example.com", BlocklistParser.parseLine("0.0.0.0 ads.example.com"))
assertEquals("ads.example.com", BlocklistParser.parseLine("127.0.0.1\tads.example.com # tracker"))
assertEquals("ads.example.com", BlocklistParser.parseLine("ads.example.com"))
assertEquals("tracker.example", BlocklistParser.parseLine("||tracker.example^"))
assertEquals("tracker.example", BlocklistParser.parseLine("||tracker.example^\$third-party"))
assertEquals("bad.example", BlocklistParser.parseLine("address=/bad.example/0.0.0.0"))
assertEquals("bad.example", BlocklistParser.parseLine("server=/bad.example/"))
assertEquals("bad.example", BlocklistParser.parseLine("local-zone: \"bad.example\" always_nxdomain"))
}
@Test
fun skipsCommentsRulesAndLocalNames() {
assertNull(BlocklistParser.parseLine(""))
assertNull(BlocklistParser.parseLine("# comment"))
assertNull(BlocklistParser.parseLine("! adblock comment"))
assertNull(BlocklistParser.parseLine("[Adblock Plus 2.0]"))
assertNull(BlocklistParser.parseLine("@@||allowed.example^"))
assertNull(BlocklistParser.parseLine("example.com##.banner"))
assertNull(BlocklistParser.parseLine("127.0.0.1 localhost"))
assertNull(BlocklistParser.parseLine("0.0.0.0 0.0.0.0"))
assertNull(BlocklistParser.parseLine("::1 ip6-localhost"))
}
@Test
fun normalizesUserInput() {
assertEquals("example.com", BlocklistParser.normalize(" HTTPS://Example.COM/path?q=1 "))
assertEquals("example.com", BlocklistParser.normalize("*.example.com"))
assertEquals("example.com", BlocklistParser.normalize("example.com."))
assertEquals("example.com", BlocklistParser.normalize("example.com:8080"))
assertNull(BlocklistParser.normalize("localhost"))
assertNull(BlocklistParser.normalize("bad..example"))
assertNull(BlocklistParser.normalize("-bad.example"))
assertNull(BlocklistParser.normalize("exa mple.com"))
assertNull(BlocklistParser.normalize("192.168.0.1"))
}
@Test
fun parentHashMatchesHashOfTheSuffix() {
val name = "a.b.example.com"
assertEquals(DomainHash.of("example.com"), DomainHash.of(name, name.indexOf("example")))
assertEquals(DomainHash.of(name), DomainHash.of(name, 0))
}
}
@@ -0,0 +1,115 @@
package cc.skysparkle.sparkledns
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
class DnsCacheTest {
private var now = 1_000_000L
private val cache = DnsCache(clock = { now })
@Test
fun answersWithTheNewQueryIdAndAgedTtl() {
val first = DnsMessage.buildQuery("example.com")
cache.put(first, dnsResponse(first, ttls = listOf(300)))
now += 10_000
val second = DnsMessage.buildQuery("EXAMPLE.com")
val hit = requireNotNull(cache.get(second))
assertEquals(second[0], hit[0])
assertEquals(second[1], hit[1])
assertEquals(290L, answerTtl(hit))
}
@Test
fun expiresWithTheShortestTtl() {
val query = DnsMessage.buildQuery("example.com")
cache.put(query, dnsResponse(query, ttls = listOf(300, 60)))
now += 59_000
val hit = requireNotNull(cache.get(query))
assertEquals(241L, answerTtl(hit, 0))
assertEquals(1L, answerTtl(hit, 1))
now += 1_000
assertNull(cache.get(query))
}
@Test
fun capsTtlAtOneHour() {
val query = DnsMessage.buildQuery("example.com")
cache.put(query, dnsResponse(query, ttls = listOf(86_400)))
now += 3_599_000
assertNotNull(cache.get(query))
now += 1_000
assertNull(cache.get(query))
}
@Test
fun keepsEmptyNxdomainForThirtySeconds() {
val query = DnsMessage.buildQuery("nothing.example")
cache.put(query, dnsResponse(query, rcode = 3, ttls = emptyList()))
now += 29_000
assertEquals(3, DnsMessage.rcode(requireNotNull(cache.get(query))))
now += 1_000
assertNull(cache.get(query))
}
@Test
fun skipsFailuresTruncatedAndZeroTtlAnswers() {
val failed = DnsMessage.buildQuery("a.example")
val truncated = DnsMessage.buildQuery("b.example")
val zero = DnsMessage.buildQuery("c.example")
cache.put(failed, dnsResponse(failed, rcode = 2))
cache.put(truncated, dnsResponse(truncated, truncated = true))
cache.put(zero, dnsResponse(zero, ttls = listOf(0)))
assertNull(cache.get(failed))
assertNull(cache.get(truncated))
assertNull(cache.get(zero))
}
@Test
fun leavesTheOptRecordAlone() {
val query = DnsMessage.buildQuery("example.com")
val response = dnsResponse(query, withOpt = true)
cache.put(query, response)
now += 10_000
val hit = requireNotNull(cache.get(query))
assertArrayEquals(response.copyOfRange(response.size - 11, response.size), hit.copyOfRange(hit.size - 11, hit.size))
}
@Test
fun separatesRecordTypes() {
val a = DnsMessage.buildQuery("example.com", type = 1)
val aaaa = DnsMessage.buildQuery("example.com", type = 28)
cache.put(a, dnsResponse(a))
assertNull(cache.get(aaaa))
}
@Test
fun evictsTheLeastRecentlyUsedEntry() {
val small = DnsCache(maxEntries = 2, clock = { now })
val a = DnsMessage.buildQuery("a.example")
val b = DnsMessage.buildQuery("b.example")
val c = DnsMessage.buildQuery("c.example")
small.put(a, dnsResponse(a))
small.put(b, dnsResponse(b))
small.get(a)
small.put(c, dnsResponse(c))
assertNotNull(small.get(a))
assertNull(small.get(b))
assertNotNull(small.get(c))
}
}
@@ -0,0 +1,69 @@
package cc.skysparkle.sparkledns
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test
class DnsMessageTest {
@Test
fun questionIsReadBackLowercase() {
val question = requireNotNull(DnsMessage.question(DnsMessage.buildQuery("WWW.Example.COM.", type = 28)))
assertEquals("www.example.com", question.name)
assertEquals(28, question.type)
}
@Test
fun questionRejectsCompressedOrCutNames() {
val query = DnsMessage.buildQuery("example.com")
val compressed = query.copyOf(12) + byteArrayOf(0xC0.toByte(), 0x0C, 0, 1, 0, 1)
assertNull(DnsMessage.question(compressed))
assertNull(DnsMessage.question(query.copyOf(16)))
assertNull(DnsMessage.question(query.copyOf(11)))
}
@Test
fun withIdCopiesTheQueryId() {
val query = DnsMessage.buildQuery("example.com")
val response = dnsResponse(query)
response[0] = (query[0] + 1).toByte()
val fixed = DnsMessage.withId(response, query)
assertEquals(query[0], fixed[0])
assertEquals(query[1], fixed[1])
assertSame(fixed, DnsMessage.withId(fixed, query))
}
@Test
fun errorReplyKeepsOnlyTheQuestion() {
val query = DnsMessage.buildQuery("example.com")
// An EDNS OPT record in the additional section must not be echoed back
val withOpt = query.copyOf().also { it[11] = 1 } + byteArrayOf(0, 0, 41, 0x10, 0, 0, 0, 0, 0, 0, 0)
val reply = requireNotNull(DnsMessage.errorReply(withOpt, DnsMessage.RCODE_NXDOMAIN))
assertEquals(query.size, reply.size)
assertTrue(DnsMessage.isResponse(reply))
assertEquals(DnsMessage.RCODE_NXDOMAIN, DnsMessage.rcode(reply))
assertEquals(1, u16(reply, 4))
assertArrayEquals(ByteArray(6), reply.copyOfRange(6, 12))
assertArrayEquals(query.copyOfRange(12, query.size), reply.copyOfRange(12, reply.size))
}
@Test
fun servfailAndResponseFlags() {
val query = DnsMessage.buildQuery("example.com")
assertFalse(DnsMessage.isResponse(query))
assertEquals(DnsMessage.RCODE_SERVFAIL, DnsMessage.rcode(requireNotNull(DnsMessage.servfail(query))))
assertNull(DnsMessage.servfail(ByteArray(5)))
assertEquals(-1, DnsMessage.rcode(ByteArray(2)))
}
}
@@ -0,0 +1,77 @@
package cc.skysparkle.sparkledns
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
class PacketTest {
private val client = byteArrayOf(10, 111, 222.toByte(), 1)
private val server = byteArrayOf(10, 111, 222.toByte(), 2)
@Test
fun parsesDnsQuery() {
val query = DnsMessage.buildQuery("example.com")
val packet = ipv4Udp(client, server, 40000, 53, query)
val parsed = requireNotNull(Packet.parseDnsQuery(packet, packet.size))
assertArrayEquals(query, parsed.payload)
assertArrayEquals(client, parsed.srcIp)
assertArrayEquals(server, parsed.dstIp)
assertEquals(40000, parsed.srcPort)
assertEquals(53, parsed.dstPort)
}
@Test
fun usesOnlyTheBytesThatWereRead() {
val query = DnsMessage.buildQuery("example.com")
val packet = ipv4Udp(client, server, 40000, 53, query)
val buffer = packet + ByteArray(100) { 0x55 }
val parsed = requireNotNull(Packet.parseDnsQuery(buffer, packet.size))
assertArrayEquals(query, parsed.payload)
}
@Test
fun ignoresEverythingThatIsNotADnsQuery() {
val query = DnsMessage.buildQuery("example.com")
val otherPort = ipv4Udp(client, server, 40000, 443, query)
val tcp = ipv4Udp(client, server, 40000, 53, query, protocol = 6)
val fragment = ipv4Udp(client, server, 40000, 53, query, flagsAndOffset = 0x2000)
val ipv6 = ipv4Udp(client, server, 40000, 53, query).also { it[0] = 0x60 }
assertNull(Packet.parseDnsQuery(otherPort, otherPort.size))
assertNull(Packet.parseDnsQuery(tcp, tcp.size))
assertNull(Packet.parseDnsQuery(fragment, fragment.size))
assertNull(Packet.parseDnsQuery(ipv6, ipv6.size))
assertNull(Packet.parseDnsQuery(otherPort, 27))
}
@Test
fun replySwapsEndpointsAndHasValidChecksums() {
val query = DnsMessage.buildQuery("example.com")
val packet = ipv4Udp(client, server, 40000, 53, query)
val parsed = requireNotNull(Packet.parseDnsQuery(packet, packet.size))
// Odd length exercises the checksum padding
val answer = dnsResponse(query) + byteArrayOf(7)
val reply = Packet.buildReply(parsed, answer)
assertEquals(28 + answer.size, reply.size)
assertEquals(0x45, reply[0].toInt())
assertEquals(reply.size, u16(reply, 2))
assertArrayEquals(server, reply.copyOfRange(12, 16))
assertArrayEquals(client, reply.copyOfRange(16, 20))
assertEquals(53, u16(reply, 20))
assertEquals(40000, u16(reply, 22))
assertArrayEquals(answer, reply.copyOfRange(28, reply.size))
assertEquals(0xFFFF, onesSum(reply.copyOfRange(0, 20)))
val udp = reply.copyOfRange(20, reply.size)
val pseudo = reply.copyOfRange(12, 20) + byteArrayOf(0, 17) + byteArrayOf((udp.size shr 8).toByte(), udp.size.toByte())
assertEquals(0xFFFF, onesSum(pseudo, udp))
}
}
@@ -0,0 +1,71 @@
package cc.skysparkle.sparkledns
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test
class ServerListTest {
@Test
fun readsAllColumns() {
val list = ServerList.parse(
"""
# Name | URL | policy | h3 | donate
Example DNS | https://dns.example.org/dns-query | https://example.org/privacy | h3 | https://example.org/donate
Other | https://doh.other.net/q | https://other.net/policy | https://other.net/give
""".trimIndent()
)
assertEquals(2, list.size)
val first = list[0]
assertEquals("dns.example.org", first.id)
assertEquals("Example DNS", first.name)
assertEquals("https://dns.example.org/dns-query", first.url)
assertEquals("https://example.org/privacy", first.policy)
assertEquals("https://example.org/donate", first.donate)
assertTrue(first.http3)
val second = list[1]
assertFalse(second.http3)
assertEquals("https://other.net/give", second.donate)
}
@Test
fun skipsServersWithoutHttpsOrPolicy() {
val list = ServerList.parse(
"""
Plain | http://dns.example.org/dns-query | https://example.org/privacy
No policy | https://dns.example.org/dns-query | not a url
Too short | https://dns.example.org/dns-query
""".trimIndent()
)
assertTrue(list.isEmpty())
}
@Test
fun shippedServersCannotBeRedirected() {
val quad9 = Providers.builtIn.first { it.id == "quad9" }
val list = ServerList.parse("Fake | https://dns.quad9.net/elsewhere | https://evil.example/policy")
assertSame(quad9, list.single())
}
@Test
fun dropsDuplicatesTrimsNamesAndCapsTheList() {
val long = "N".repeat(60)
val lines = (1..60).joinToString("\n") { "$long | https://dns$it.example/q | https://example.org/p" } +
"\nDuplicate | https://dns1.example/q | https://example.org/p"
val list = ServerList.parse(lines)
assertEquals(50, list.size)
assertEquals(40, list[0].name.length)
assertEquals(list.size, list.map { it.id }.toSet().size)
assertNull(list.firstOrNull { it.name == "Duplicate" })
}
}
@@ -0,0 +1,78 @@
package cc.skysparkle.sparkledns
import java.io.ByteArrayOutputStream
// Hand-built DNS messages and IPv4/UDP packets for the tests
internal fun dnsResponse(
query: ByteArray,
rcode: Int = 0,
ttls: List<Int> = listOf(300),
withOpt: Boolean = false,
truncated: Boolean = false,
): ByteArray {
val questionEnd = requireNotNull(skipName(query, 12)) + 4
val out = ByteArrayOutputStream()
out.write(query, 0, 2)
out.write(0x81 or if (truncated) 0x02 else 0) // QR + RD (+ TC)
out.write(0x80 or rcode) // RA + RCODE
out.write(byteArrayOf(0, 1, 0, ttls.size.toByte(), 0, 0, 0, (if (withOpt) 1 else 0).toByte()))
out.write(query, 12, questionEnd - 12)
for (ttl in ttls) {
out.write(byteArrayOf(0xC0.toByte(), 0x0C, 0, 1, 0, 1))
out.write(be32(ttl))
out.write(byteArrayOf(0, 4, 93, 184.toByte(), 216.toByte(), 34))
}
if (withOpt) {
// Root name, type OPT, UDP size 4096, extended flags with the DO bit, no data
out.write(byteArrayOf(0, 0, 41, 0x10, 0, 0, 0, 0x80.toByte(), 0, 0, 0))
}
return out.toByteArray()
}
internal fun ipv4Udp(
src: ByteArray,
dst: ByteArray,
srcPort: Int,
dstPort: Int,
payload: ByteArray,
protocol: Int = 17,
flagsAndOffset: Int = 0,
): ByteArray {
val p = ByteArray(28 + payload.size)
p[0] = 0x45
put16(p, 2, p.size)
put16(p, 6, flagsAndOffset)
p[8] = 64
p[9] = protocol.toByte()
src.copyInto(p, 12)
dst.copyInto(p, 16)
put16(p, 20, srcPort)
put16(p, 22, dstPort)
put16(p, 24, 8 + payload.size)
payload.copyInto(p, 28)
return p
}
// One's complement sum of 16-bit words; a correct checksum makes it 0xFFFF
internal fun onesSum(vararg parts: ByteArray): Int {
var sum = 0L
for (part in parts) {
var i = 0
while (i + 1 < part.size) {
sum += u16(part, i)
i += 2
}
if (i < part.size) sum += (part[i].toInt() and 0xFF) shl 8
}
while (sum shr 16 != 0L) sum = (sum and 0xFFFF) + (sum shr 16)
return sum.toInt()
}
internal fun be32(value: Int) = ByteArray(4).also { put32(it, 0, value) }
// TTL field of the n-th answer of a message built by dnsResponse
internal fun answerTtl(message: ByteArray, index: Int = 0): Long {
val first = requireNotNull(skipName(message, 12)) + 4
return u32(message, first + index * 16 + 6)
}