mirror of
https://github.com/pezkuwichain/pezkuwi-wallet-android.git
synced 2026-11-20 01:26:11 +00:00
Open .kurd sub-pages and a site's own 404; show a suspended name as suspended (#40)
* Open a .kurd page address typed with a path Tapping the address bar puts the page's full address in the search box, for example https://dks-news.kurd/kategori/ekonomi. The search only knew a bare name, and a .kurd address is never a web address or a search, so such input produced no result at all: the reader could not open, edit or reload any page below the home page. KurdName.parseAddress takes the name and the path, query and fragment after it (printable ASCII, no port or user info), and the .kurd row opens that page. The look-alike check still runs on the name; choosing the official name opens its home page, since the typed path belongs to the other site. * Show a .kurd site's own 404 page for a page it does not have A missing page was answered with 404 and an empty body. WebView replaces such a response with its own "Webpage not available" screen (ERR_HTTP_RESPONSE_CODE_FAILURE), as if the site could not be reached, although the gateway had proven the page absent from a site that loads. For the page's own document the server now serves the site's /404.html with status 404, as a web server does (dks.news ships one), and a line of text when the site has none. Missing images and scripts are answered as before. The fixture is a Kubo-built directory with index.html and 404.html. * Show a suspended .kurd name as suspended, and open nothing of it The register can now stop a name: the court for up to 30 days, the emergency council for 72 hours. The lookup reads Navnisan::Suspensions beside Names, at the same finalized block and through the same proof path, and applies the pallet's rule: a suspension of the name, or of a name it sits under as a subname, for the registration it was made against and not yet run out. A suspended name resolves to a new Suspended result however it was read, since a refusal needs no proof. The site server serves nothing (451) and the browser shows the existing notice with a pause icon, who stopped the name, until when, and the hash of the written reasons. Strings in English, Kurmanji and Turkish. A chain without the Suspensions item stores nothing there, so on today's runtime nothing changes.
This commit is contained in:
1 parent
cb74f8cde0
commit
f50ca479e0
24 files changed
+476
-21
No files matched your search
@@ -78,6 +78,28 @@ class KurdName private constructor(
|
||||
return KurdName(labels)
|
||||
}
|
||||
|
||||
/**
|
||||
* A `.kurd` page address -- the name and the path, query and fragment after it, as the address
|
||||
* bar shows and the reader edits it -- or null when the name is invalid ([parse]) or what follows
|
||||
* it is not a plain URL path. The path is `/` when none is given.
|
||||
*/
|
||||
fun parseAddress(input: String): Pair<KurdName, String>? {
|
||||
var text = input.trim()
|
||||
if (text.startsWith("https://", ignoreCase = true)) text = text.substring("https://".length)
|
||||
val end = text.indexOfFirst { it in "/?#" }.takeIf { it >= 0 } ?: text.length
|
||||
val name = parse(text.substring(0, end)) ?: return null
|
||||
|
||||
val rest = text.substring(end)
|
||||
if (rest.isEmpty()) return name to "/"
|
||||
val path = if (rest.startsWith("/")) rest else "/$rest"
|
||||
// Printable ASCII only: the page is fetched by path, so nothing may hide in it
|
||||
if (path.any { it.code <= 0x20 || it.code >= 0x7f }) return null
|
||||
val uri = runCatching { java.net.URI("https://${name.name}$path") }.getOrNull() ?: return null
|
||||
if (uri.host != name.name || uri.port != -1 || uri.userInfo != null) return null
|
||||
|
||||
return name to path
|
||||
}
|
||||
|
||||
/** `a-z`, `0-9` and `-`, 1 to 63 bytes, no hyphen at either end and no `--` anywhere. */
|
||||
fun isValidLabel(label: String): Boolean {
|
||||
if (label.isEmpty() || label.length > MAX_LABEL) return false
|
||||
|
||||
@@ -131,12 +131,14 @@ class RealKurdNameResolver(
|
||||
companion object {
|
||||
|
||||
/**
|
||||
* D23: a suspended name opens nothing, read either way -- a refusal needs no proof.
|
||||
* D18: a proven answer stands; an unproven one opens only for a name nobody could take for an
|
||||
* institution's, and with a warning.
|
||||
*/
|
||||
internal fun judge(name: KurdName, lookup: NameLookup, verified: Boolean): KurdResolution = when (lookup) {
|
||||
NameLookup.NotFound, NameLookup.Lapsed -> KurdResolution.NotFound(name, verified)
|
||||
is NameLookup.Found -> when {
|
||||
lookup.suspension != null -> KurdResolution.Suspended(name, lookup.suspension, verified)
|
||||
verified -> KurdResolution.Resolved(name, lookup.record, lookup.status, verified = true, block = lookup.block)
|
||||
mustBeVerified(name, lookup) -> KurdResolution.Refused(name)
|
||||
else -> KurdResolution.Resolved(name, lookup.record, lookup.status, verified = false, block = lookup.block)
|
||||
|
||||
@@ -22,6 +22,12 @@ sealed interface KurdResolution {
|
||||
val official: Boolean get() = record.isOfficial
|
||||
}
|
||||
|
||||
/**
|
||||
* Stopped by the court or the emergency council (decision D23): the name and everything under
|
||||
* it open nothing until the suspension ends or is lifted, however it was read.
|
||||
*/
|
||||
class Suspended(override val name: KurdName, val suspension: NameSuspension, val verified: Boolean) : KurdResolution
|
||||
|
||||
/** Not registered, expired, or orphaned by its parent's re-registration. */
|
||||
class NotFound(override val name: KurdName, val verified: Boolean) : KurdResolution
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ sealed interface NameLookup {
|
||||
|
||||
/** Two sources agree when they return the same records byte for byte and judge them the same. */
|
||||
fun agreesWith(other: NameLookup): Boolean = when (this) {
|
||||
is Found -> other is Found && rawChain == other.rawChain && status == other.status && locked == other.locked
|
||||
is Found -> other is Found && rawChain == other.rawChain && status == other.status && locked == other.locked && suspension == other.suspension
|
||||
else -> this === other
|
||||
}
|
||||
|
||||
@@ -46,12 +46,15 @@ sealed interface NameLookup {
|
||||
val locked: Boolean,
|
||||
/** The finalized block it was read at, when the source said; not part of agreement. */
|
||||
val block: Long? = null,
|
||||
/** The suspension that stops the name now: its own, or a name's it sits under. */
|
||||
val suspension: NameSuspension? = null,
|
||||
) : NameLookup
|
||||
|
||||
companion object {
|
||||
|
||||
private val NAMES_PREFIX = "Navnisan".toByteArray().xxHash128() + "Names".toByteArray().xxHash128()
|
||||
private val LOCKED_PREFIX = "Navnisan".toByteArray().xxHash128() + "Locked".toByteArray().xxHash128()
|
||||
private val SUSPENSIONS_PREFIX = "Navnisan".toByteArray().xxHash128() + "Suspensions".toByteArray().xxHash128()
|
||||
private val TIMESTAMP_NOW = "Timestamp".toByteArray().xxHash128() + "Now".toByteArray().xxHash128()
|
||||
|
||||
/** `NavnisanGracePeriod`: 30 days after a lease ends the name still resolves (decision D11). */
|
||||
@@ -61,6 +64,8 @@ sealed interface NameLookup {
|
||||
|
||||
fun lockedKey(node: ByteArray) = LOCKED_PREFIX + node
|
||||
|
||||
fun suspensionsKey(node: ByteArray) = SUSPENSIONS_PREFIX + node
|
||||
|
||||
val timestampNowKey: ByteArray get() = TIMESTAMP_NOW.copyOf()
|
||||
|
||||
/**
|
||||
@@ -87,10 +92,30 @@ sealed interface NameLookup {
|
||||
|
||||
val status = effectiveStatus(records.map { it.first }, now) ?: return Lapsed
|
||||
val locked = nodes.any { reader.storage(lockedKey(it), at) != null }
|
||||
val suspension = suspension(reader, at, nodes, records.map { it.first }, now)
|
||||
|
||||
val block = runCatching { reader.blockNumber(at) }.getOrNull()
|
||||
|
||||
return Found(records.first().first, status, records.map { it.second }, locked, block)
|
||||
return Found(records.first().first, status, records.map { it.second }, locked, block, suspension)
|
||||
}
|
||||
|
||||
/**
|
||||
* `Pezpallet::is_suspended`: the name's own suspension, then each ancestor's while the name
|
||||
* below is a subname. A chain without `Suspensions` stores nothing there, and nothing is stopped.
|
||||
*/
|
||||
private suspend fun suspension(
|
||||
reader: NameStorageReader,
|
||||
at: String,
|
||||
nodes: List<ByteArray>,
|
||||
chain: List<NameRecord>,
|
||||
now: Long,
|
||||
): NameSuspension? {
|
||||
for (level in chain.indices) {
|
||||
val suspension = reader.storage(suspensionsKey(nodes[level]), at)?.let(NameSuspension::decode)
|
||||
if (suspension != null && suspension.holdsFor(chain[level], now)) return suspension
|
||||
if (chain[level].kind != NameRecord.Kind.SUB) return null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.novafoundation.nova.runtime.names
|
||||
|
||||
import io.novasama.substrate_sdk_android.extensions.toHexString
|
||||
|
||||
/**
|
||||
* `pezpallet_navnisan::Suspension`, decoded from the raw value of `Navnisan::Suspensions[node]`:
|
||||
* the court, or the emergency council, has stopped a name until [until] (decision D23). While it
|
||||
* holds the name and every name under it resolve to nothing.
|
||||
*/
|
||||
data class NameSuspension(
|
||||
/** Milliseconds, chain time; the suspension ends by itself then. */
|
||||
val until: Long,
|
||||
/** The registration suspended; a name registered anew is not. */
|
||||
val reg: Long,
|
||||
/** Hash of the written reasons, hex with `0x`. */
|
||||
val reason: String,
|
||||
val by: By,
|
||||
) {
|
||||
|
||||
enum class By { COURT, EMERGENCY }
|
||||
|
||||
/** `Pezpallet::suspension_of`: for this registration, and not yet run out. */
|
||||
fun holdsFor(record: NameRecord, now: Long): Boolean = reg == record.reg && now < until
|
||||
|
||||
companion object {
|
||||
|
||||
fun decode(bytes: ByteArray): NameSuspension {
|
||||
val reader = ScaleReader(bytes)
|
||||
val suspension = with(reader) {
|
||||
NameSuspension(
|
||||
until = u64().toLong(),
|
||||
reg = u64().toLong(),
|
||||
reason = bytes(32).toHexString(withPrefix = true),
|
||||
by = when (val index = u8()) {
|
||||
0 -> By.COURT
|
||||
1 -> By.EMERGENCY
|
||||
else -> error("Unknown suspension variant $index")
|
||||
},
|
||||
)
|
||||
}
|
||||
require(reader.isExhausted) { "Trailing bytes after a suspension" }
|
||||
|
||||
return suspension
|
||||
}
|
||||
}
|
||||
}
|
||||
+22
-3
@@ -4,6 +4,7 @@ import io.novafoundation.nova.runtime.names.KurdName
|
||||
import io.novafoundation.nova.runtime.names.KurdNameResolver
|
||||
import io.novafoundation.nova.runtime.names.KurdResolution
|
||||
import io.novafoundation.nova.runtime.names.NameRecord
|
||||
import io.novafoundation.nova.runtime.names.NameSuspension
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
@@ -50,6 +51,9 @@ class KurdSiteServer(
|
||||
|
||||
object NotRegistered : PageState
|
||||
|
||||
/** Stopped by the court or the emergency council; nothing of it is served. */
|
||||
class Suspended(val suspension: NameSuspension) : PageState
|
||||
|
||||
/** Registered, but no site: no content, or content of a kind this wallet does not serve. */
|
||||
object NoSite : PageState
|
||||
|
||||
@@ -81,15 +85,16 @@ class KurdSiteServer(
|
||||
val name = KurdName.parse(host) ?: return status(404, "Not Found")
|
||||
val segments = decodePath(encodedPath) ?: return status(400, "Bad Request")
|
||||
|
||||
val (state, response) = handle(name, segments)
|
||||
val (state, response) = handle(name, segments, mainFrame)
|
||||
if (mainFrame) _pageStates.update { it + (name to state) }
|
||||
return response
|
||||
}
|
||||
|
||||
private suspend fun handle(name: KurdName, segments: List<String>): Pair<PageState, Response> {
|
||||
private suspend fun handle(name: KurdName, segments: List<String>, mainFrame: Boolean): Pair<PageState, Response> {
|
||||
val resolution = when (val resolved = resolve(name)) {
|
||||
is KurdResolution.Resolved -> resolved
|
||||
is KurdResolution.NotFound -> return PageState.NotRegistered to status(404, "Not Found")
|
||||
is KurdResolution.Suspended -> return PageState.Suspended(resolved.suspension) to status(451, "Unavailable For Legal Reasons")
|
||||
is KurdResolution.Refused -> return PageState.Refused to status(403, "Forbidden")
|
||||
is KurdResolution.Unavailable -> return PageState.Unavailable to status(502, "Bad Gateway")
|
||||
}
|
||||
@@ -112,11 +117,23 @@ class KurdSiteServer(
|
||||
val open = PageState.Open(resolution, root, onChainVersion)
|
||||
return when (val result = fetcher.fetch(root, segments)) {
|
||||
is KurdContentFetcher.Result.File -> open to Response(200, "OK", mimeTypeOf(result.path.lastOrNull()), result.bytes)
|
||||
KurdContentFetcher.Result.NotFound -> open to status(404, "Not Found")
|
||||
KurdContentFetcher.Result.NotFound -> open to if (mainFrame) notFoundPage(root) else status(404, "Not Found")
|
||||
KurdContentFetcher.Result.Unavailable -> PageState.Unavailable to status(502, "Bad Gateway")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A page the site does not have: its own `/404.html` when it ships one, as a web server would
|
||||
* answer, and otherwise a line of text. Never an empty body, which WebView replaces with its
|
||||
* own error screen as if the site could not be reached.
|
||||
*/
|
||||
private suspend fun notFoundPage(root: Cid): Response {
|
||||
val page = fetcher.fetch(root, listOf(NOT_FOUND_PAGE)) as? KurdContentFetcher.Result.File
|
||||
?: return Response(404, "Not Found", "text/plain", "404 Not Found".toByteArray())
|
||||
|
||||
return Response(404, "Not Found", mimeTypeOf(NOT_FOUND_PAGE), page.bytes)
|
||||
}
|
||||
|
||||
private suspend fun resolve(name: KurdName): KurdResolution {
|
||||
val now = clock()
|
||||
resolutions[name]?.let { (at, resolution) -> if (now - at < resolutionTtlMillis) return resolution }
|
||||
@@ -127,6 +144,8 @@ class KurdSiteServer(
|
||||
|
||||
companion object {
|
||||
|
||||
private const val NOT_FOUND_PAGE = "404.html"
|
||||
|
||||
const val CONTENT_SECURITY_POLICY = "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; " +
|
||||
"img-src 'self' data: blob:; media-src 'self' blob:; font-src 'self' data:; connect-src 'self'; " +
|
||||
"worker-src 'none'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'"
|
||||
|
||||
@@ -32,6 +32,28 @@ class KurdNameTest {
|
||||
assertEquals("gov", KurdName.parse("serok.gov.kurd")!!.top)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a page address keeps its path, query and fragment`() {
|
||||
assertEquals("dks-news.kurd" to "/kategori/ekonomi", address("https://DKS-News.kurd/kategori/ekonomi"))
|
||||
assertEquals("dks-news.kurd" to "/a/x?lang=kmr#top", address("dks-news.kurd/a/x?lang=kmr#top"))
|
||||
assertEquals("dks-news.kurd" to "/?q=1", address("dks-news.kurd?q=1"))
|
||||
assertEquals("dks.kurd" to "/", address("dks.kurd"))
|
||||
assertEquals("dks.kurd" to "/", address("https://dks.kurd/"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a page address with an invalid name, port, user or unsafe path is refused`() {
|
||||
listOf(
|
||||
"dks_news.kurd/a", // the name itself
|
||||
"dks.kurd:8080/a",
|
||||
"user@dks.kurd/a",
|
||||
"http://dks.kurd/a", // only https, as for a bare name
|
||||
"dks.kurd/a b",
|
||||
"dks.kurd/\u00e7",
|
||||
"dks.kurd/a%zz",
|
||||
).forEach { assertNull(it, KurdName.parseAddress(it)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `names the pallet refuses are never parsed`() {
|
||||
listOf(
|
||||
@@ -84,4 +106,6 @@ class KurdNameTest {
|
||||
}
|
||||
|
||||
private fun node(name: String) = KurdName.parse(name)!!.node.toHexString()
|
||||
|
||||
private fun address(input: String) = KurdName.parseAddress(input)?.let { (name, path) -> name.name to path }
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package io.novafoundation.nova.runtime.names
|
||||
import io.novafoundation.nova.runtime.names.RealKurdNameResolver.Companion.judge
|
||||
import io.novafoundation.nova.runtime.names.RealKurdNameResolver.Companion.refuseOrGiveUp
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
@@ -35,6 +36,18 @@ class KurdResolutionJudgeTest {
|
||||
assertFalse(resolution.official)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a suspended name is suspended however it was read, even one that must be verified`() {
|
||||
val suspension = NameSuspension(until = 1, reg = 9, reason = "0x00", by = NameSuspension.By.COURT)
|
||||
val stopped = { raw: NameRecord -> NameLookup.Found(raw, NameStatus.ACTIVE, listOf("raw"), locked = false, suspension = suspension) }
|
||||
|
||||
val proven = judge(ahmet, stopped(leased()), verified = true) as KurdResolution.Suspended
|
||||
assertTrue(proven.verified)
|
||||
assertSame(suspension, proven.suspension)
|
||||
assertTrue(judge(ahmet, stopped(leased()), verified = false) is KurdResolution.Suspended)
|
||||
assertTrue(judge(dks, stopped(NameRecord.decode(NameFixtures.DKS)), verified = false) is KurdResolution.Suspended)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `with no answer a zone name is refused and any other is unavailable`() {
|
||||
assertTrue(refuseOrGiveUp(KurdName.parse("serok.gov.kurd")!!) is KurdResolution.Refused)
|
||||
|
||||
@@ -107,6 +107,76 @@ class NameLookupTest {
|
||||
assertFalse(first.agreesWith(NameLookup.NotFound))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a suspension of the name or of the name above it stops it`() = runBlocking {
|
||||
val govReg = NameRecord.decode(NameFixtures.GOV).reg
|
||||
val serokReg = NameRecord.decode(NameFixtures.SEROK_GOV).reg
|
||||
val live = { FakeReader().put(gov, NameFixtures.GOV).put(serok, NameFixtures.SEROK_GOV) }
|
||||
|
||||
assertEquals(null, (NameLookup.read(live(), serok) as NameLookup.Found).suspension)
|
||||
|
||||
val own = NameLookup.read(live().suspend(serok, serokReg, NameSuspension.By.EMERGENCY), serok) as NameLookup.Found
|
||||
assertEquals(NameSuspension.By.EMERGENCY, own.suspension?.by)
|
||||
|
||||
val above = NameLookup.read(live().suspend(gov, govReg, NameSuspension.By.COURT), serok) as NameLookup.Found
|
||||
assertEquals(NameSuspension.By.COURT, above.suspension?.by)
|
||||
assertEquals(NameFixtures.NOW + 1, above.suspension?.until)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a suspension that ran out or names another registration stops nothing`() = runBlocking {
|
||||
val serokReg = NameRecord.decode(NameFixtures.SEROK_GOV).reg
|
||||
val live = { FakeReader().put(gov, NameFixtures.GOV).put(serok, NameFixtures.SEROK_GOV) }
|
||||
|
||||
val ranOut = live().suspend(serok, serokReg, NameSuspension.By.COURT, until = NameFixtures.NOW)
|
||||
assertEquals(null, (NameLookup.read(ranOut, serok) as NameLookup.Found).suspension)
|
||||
|
||||
val earlier = live().suspend(serok, serokReg - 1, NameSuspension.By.COURT)
|
||||
assertEquals(null, (NameLookup.read(earlier, serok) as NameLookup.Found).suspension)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a suspension above a name that is not a subname does not reach it`() = runBlocking {
|
||||
// serok.gov.kurd as if Parliament had granted it outright rather than set it under gov.kurd
|
||||
val granted = NameFixtures.SEROK_GOV.copyOf().also { it[SEROK_KIND_OFFSET] = 1 }
|
||||
check(NameRecord.decode(granted).kind == NameRecord.Kind.GRANTED)
|
||||
val reader = FakeReader().put(gov, NameFixtures.GOV).put(serok, granted)
|
||||
.suspend(gov, NameRecord.decode(NameFixtures.GOV).reg, NameSuspension.By.COURT)
|
||||
|
||||
assertEquals(null, (NameLookup.read(reader, serok) as NameLookup.Found).suspension)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sources that disagree on a suspension do not agree`() = runBlocking {
|
||||
val serokReg = NameRecord.decode(NameFixtures.SEROK_GOV).reg
|
||||
val live = { FakeReader().put(gov, NameFixtures.GOV).put(serok, NameFixtures.SEROK_GOV) }
|
||||
|
||||
val plain = NameLookup.read(live(), serok)
|
||||
val stopped = NameLookup.read(live().suspend(serok, serokReg, NameSuspension.By.COURT), serok)
|
||||
assertFalse(plain.agreesWith(stopped))
|
||||
assertTrue(stopped.agreesWith(NameLookup.read(live().suspend(serok, serokReg, NameSuspension.By.COURT), serok)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `suspensions are read from the pallet's own storage item`() {
|
||||
// twox128("Navnisan") ++ twox128("Suspensions"), computed apart from this code
|
||||
val prefix = "8dada8db66ab8573e0db54b9f7d276265dd5f3679e83b14ce6d7b388fae08646"
|
||||
assertEquals(prefix + "00".repeat(32), NameLookup.suspensionsKey(ByteArray(32)).toHexString())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a suspension decodes field by field and refuses trailing bytes`() {
|
||||
val raw = suspensionBytes(until = 0x0102, reg = 7, by = NameSuspension.By.EMERGENCY)
|
||||
val suspension = NameSuspension.decode(raw)
|
||||
|
||||
assertEquals(0x0102L, suspension.until)
|
||||
assertEquals(7L, suspension.reg)
|
||||
assertEquals("0x" + "ab".repeat(32), suspension.reason)
|
||||
assertEquals(NameSuspension.By.EMERGENCY, suspension.by)
|
||||
assertTrue(runCatching { NameSuspension.decode(raw + 0) }.isFailure)
|
||||
assertTrue(runCatching { NameSuspension.decode(raw.copyOf(raw.size - 1) + 2) }.isFailure)
|
||||
}
|
||||
|
||||
private fun record(kind: NameRecord.Kind, expiry: Long?, reg: Long = 1, parentReg: Long = 1) = NameRecord(
|
||||
label = "x", parent = ByteArray(32), parentReg = parentReg, reg = reg, owner = NameRecord.Owner.Governance, kind = kind,
|
||||
lockedTransfer = false, expiry = expiry, account = null, content = null,
|
||||
@@ -125,6 +195,10 @@ class NameLookupTest {
|
||||
|
||||
fun lock(name: KurdName) = apply { values[NameLookup.lockedKey(name.node).toHexString()] = byteArrayOf(0) }
|
||||
|
||||
fun suspend(name: KurdName, reg: Long, by: NameSuspension.By, until: Long = NameFixtures.NOW + 1) = apply {
|
||||
values[NameLookup.suspensionsKey(name.node).toHexString()] = suspensionBytes(until, reg, by)
|
||||
}
|
||||
|
||||
override suspend fun finalizedHead() = HEAD
|
||||
|
||||
override suspend fun blockNumber(at: String): Long = 1_284_551
|
||||
@@ -138,4 +212,16 @@ class NameLookupTest {
|
||||
const val HEAD = "0xfinalized"
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
/** Label (1 + 5), parent, parent_reg, reg, owner `Office(3)`: the kind byte follows. */
|
||||
private const val SEROK_KIND_OFFSET = 6 + 32 + 8 + 8 + 2
|
||||
|
||||
/** `Suspension { until, reg, reason, by }` as the pallet encodes it. */
|
||||
fun suspensionBytes(until: Long, reg: Long, by: NameSuspension.By): ByteArray =
|
||||
le64(until) + le64(reg) + ByteArray(32) { 0xab.toByte() } + byteArrayOf(by.ordinal.toByte())
|
||||
|
||||
private fun le64(value: Long) = ByteArray(8) { (value ushr (8 * it)).toByte() }
|
||||
}
|
||||
}
|
||||
+34
@@ -5,6 +5,7 @@ import io.novafoundation.nova.runtime.names.KurdNameResolver
|
||||
import io.novafoundation.nova.runtime.names.KurdResolution
|
||||
import io.novafoundation.nova.runtime.names.NameRecord
|
||||
import io.novafoundation.nova.runtime.names.NameStatus
|
||||
import io.novafoundation.nova.runtime.names.NameSuspension
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
@@ -37,6 +38,26 @@ class KurdSiteServerTest {
|
||||
assertEquals(404, server(resolved(content = null)).serve(name.name, "/").status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a page the site lacks is its own 404 page, or a line of text, never an empty error`() = runBlocking {
|
||||
val withPage = KurdSiteServer(
|
||||
resolverOf(resolved(ipfs(cid("bafybeiezwrv6dr544fbj4xtthdg63jblfzqj5j6cqfkslbf7hkbevgpkui")))),
|
||||
fetcher(CarSource { _, _, _ -> resource("site-with-404.car") }),
|
||||
)
|
||||
val ownPage = withPage.serve("dks.kurd", "/missing", mainFrame = true)
|
||||
assertEquals(404, ownPage.status)
|
||||
assertEquals("text/html", ownPage.mimeType)
|
||||
assertTrue(String(ownPage.body).contains("<title>404 page</title>"))
|
||||
|
||||
val withoutPage = server(resolved(ipfs(siteRoot)))
|
||||
val text = withoutPage.serve("dks.kurd", "/missing", mainFrame = true)
|
||||
assertEquals(404, text.status)
|
||||
assertTrue(text.body.isNotEmpty())
|
||||
|
||||
// A missing image or script is answered as before: the page decides what to show
|
||||
assertEquals(0, withPage.serve("dks.kurd", "/missing.png").body.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `content the server cannot verify is not served`() = runBlocking {
|
||||
val ipns = NameRecord.Content.Ipfs(byteArrayOf(0xe5.toByte(), 0x01) + siteRoot.bytes)
|
||||
@@ -115,6 +136,19 @@ class KurdSiteServerTest {
|
||||
assertSame(KurdSiteServer.PageState.NotRegistered, stateFor { KurdResolution.NotFound(it, verified = true) })
|
||||
assertSame(KurdSiteServer.PageState.Unavailable, stateFor { KurdResolution.Unavailable(it) })
|
||||
assertSame(KurdSiteServer.PageState.NoSite, stateFor(resolved(content = null)))
|
||||
|
||||
val suspension = NameSuspension(until = 1, reg = 1, reason = "0x00", by = NameSuspension.By.EMERGENCY)
|
||||
val suspended = stateFor { KurdResolution.Suspended(it, suspension, verified = true) }
|
||||
assertSame(suspension, (suspended as KurdSiteServer.PageState.Suspended).suspension)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a suspended name serves nothing, not even its content when verified`() = runBlocking {
|
||||
val suspension = NameSuspension(until = 1, reg = 1, reason = "0x00", by = NameSuspension.By.COURT)
|
||||
val response = server { KurdResolution.Suspended(it, suspension, verified = true) }.serve("dks.kurd", "/")
|
||||
|
||||
assertEquals(451, response.status)
|
||||
assertTrue(response.body.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -2,3 +2,7 @@ CAR fixtures from [ipfs/gateway-conformance](https://github.com/ipfs/gateway-con
|
||||
at commit `eaca278518dc977db10bfec4acd79c92c4ee11ac` (`fixtures/trustless_gateway_car/` and
|
||||
`fixtures/path_gateway_unixfs/`), produced by Kubo. Dual licensed MIT / Apache-2.0 by their authors.
|
||||
Unmodified; used to check the wallet's CAR and UnixFS verifier against the reference implementation.
|
||||
|
||||
`site-with-404.car` is the wallet's own: a directory holding `index.html` and `404.html`, added with
|
||||
Kubo 0.43.1 (`ipfs add -r --cid-version 1 --raw-leaves`, root
|
||||
`bafybeiezwrv6dr544fbj4xtthdg63jblfzqj5j6cqfkslbf7hkbevgpkui`) and exported with `ipfs dag export`.
|
||||
Binary file not shown.
Reference in new issue
Block a user