rusty_esp_mid-core 0.1.4

MATA mID on the chip, the pure core: a P-256 device did:mata, the byte-identical DeviceSigner, kms nonce assertions, JWS ES256, the self-issued mID token, capability matching, the owner Adoption grant, a replay window and manifest signing. no_std, forbid(unsafe), no drivers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
//! **An adoption delivered with the settings** — the owner's grant written
//! on the cable alongside the network, taken into the device's identity at
//! boot, before any network starts.
//!
//! A deployer writes `adopt` (opaque bytes, at most [`ADOPT_MAX`]) into the
//! settings namespace. At boot [`apply`] reads it and, whatever happens to
//! it, removes it from the settings, so a later reflash of the settings slot
//! can neither replay it nor leave it to be read twice:
//!
//! - **Accepted**: the container is stored verbatim in the identity store
//!   (`mid.adopt`, its kind in `mid.adopt.kind`), the owner pinned
//!   (`mid.owner`, an [`OwnerPin`]), and the pin marked **provisional**
//!   (`mid.owner.prov`) when the check was structural only.
//! - **Rehomed**: a pin already exists and the adoption is from the same
//!   owner with a roster version at least the pinned one — the grant is
//!   replaced, as `Adoption::accept`'s trust on first use allows.
//! - **Refused**: malformed, for another device, or from a different owner
//!   than the one pinned. Never silently rebound; the caller logs the reason.
//!
//! The one kind accepted here is [`KIND_MATA_STATEMENT`]: a wallet's signed
//! statement, as the UTF-8 JSON container `{"content": <text>, "jws":
//! <compact JWS>}`. Its check here is **structural**: the JWS header is
//! `{"alg":"ES256","typ":"JWT"}`, its payload's `context` is
//! [`STATEMENT_CONTEXT`] and its `content_hash` is base64url(sha256(content));
//! the content is `{"v":1, "device_did", "owner_did", "roster_version",
//! "caps", "iat", "exp", "name"?}` with `device_did` this device and
//! `owner_did` equal to the payload's `iss` (the owner's genesis DID); the
//! pin is the key that DID names. The signature and roster chain are not
//! verified here — that is the statement verifier's, once a `no_std`
//! mid-verify exists; until then the pin is provisional, and the first boot
//! with the verifier re-checks the stored container and drops the pin if it
//! fails. The settings arrive only over the cable, and the hub verifies the
//! signature in full before it treats anyone as the owner.

use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;

use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use rusty_esp_core::error::{Error, Result};
use rusty_esp_core::hal::Kv;
use serde_json::Value;

use crate::adoption::{KV_ADOPTION, KV_OWNER_PIN, OwnerPin};
use crate::did::Did;
use crate::status::KIND_MATA_STATEMENT;

/// The settings key the adoption is delivered in.
pub const SETTINGS_ADOPT: &str = "adopt";

/// The largest adoption accepted from the settings, in bytes (espino's
/// `ADOPT_MAX`).
pub const ADOPT_MAX: usize = 8 * 1024;

/// The identity key holding the stored adoption's kind.
pub const KV_ADOPTION_KIND: &str = "mid.adopt.kind";

/// The identity key marking the owner pin provisional (one byte, 1): taken
/// on a structural check, to be re-verified once the signature can be.
pub const KV_PIN_PROVISIONAL: &str = "mid.owner.prov";

/// The statement context an adoption is signed under.
pub const STATEMENT_CONTEXT: &str = "janus-adoption-v1";

/// What a boot did with the settings' adoption.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
    /// No adoption in the settings.
    Nothing,
    /// The device took its first owner.
    Adopted {
        /// The owner's genesis DID.
        owner_did: String,
        /// The roster version pinned.
        roster_version: u32,
        /// Taken on a structural check only.
        provisional: bool,
    },
    /// The pinned owner's newer grant replaced the stored one.
    Rehomed {
        /// The owner's genesis DID.
        owner_did: String,
        /// The roster version now pinned.
        roster_version: u32,
        /// Taken on a structural check only.
        provisional: bool,
    },
    /// Not taken, and removed from the settings; why, for the log.
    Refused(&'static str),
}

/// A statement container that passed the structural check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Checked {
    /// The owner's genesis DID (`content.owner_did` = the payload's `iss`).
    pub owner_did: String,
    /// The pin to store.
    pub pin: OwnerPin,
}

fn field<'v>(v: &'v Value, key: &str) -> Option<&'v Value> {
    v.as_object()?.get(key)
}

fn text<'v>(v: &'v Value, key: &str) -> Option<&'v str> {
    field(v, key)?.as_str()
}

fn b64(part: &str) -> Result<Vec<u8>> {
    URL_SAFE_NO_PAD
        .decode(part)
        .map_err(|_| Error::InvalidFormat)
}

/// The structural check of a `mata-statement-v1` container for `my_did`.
pub fn check_statement(
    container: &[u8],
    my_did: &str,
) -> core::result::Result<Checked, &'static str> {
    let outer: Value =
        serde_json::from_slice(container).map_err(|_| "the container is not JSON")?;
    let content = text(&outer, "content").ok_or("the container has no content text")?;
    let jws = text(&outer, "jws").ok_or("the container has no jws text")?;

    let mut parts = jws.split('.');
    let (Some(h), Some(p), Some(s), None) =
        (parts.next(), parts.next(), parts.next(), parts.next())
    else {
        return Err("the jws is not three parts");
    };
    let header: Value =
        serde_json::from_slice(&b64(h).map_err(|_| "the jws header is not base64url")?)
            .map_err(|_| "the jws header is not JSON")?;
    if text(&header, "alg") != Some("ES256") || text(&header, "typ") != Some("JWT") {
        return Err("the jws header is not ES256/JWT");
    }
    if b64(s).map(|sig| sig.len()) != Ok(64) {
        return Err("the jws signature is not 64 bytes");
    }
    let payload: Value =
        serde_json::from_slice(&b64(p).map_err(|_| "the jws payload is not base64url")?)
            .map_err(|_| "the jws payload is not JSON")?;
    if text(&payload, "context") != Some(STATEMENT_CONTEXT) {
        return Err("the statement's context is not janus-adoption-v1");
    }
    let iss = text(&payload, "iss").ok_or("the statement has no iss")?;
    let declared = text(&payload, "content_hash").ok_or("the statement has no content_hash")?;
    if declared != URL_SAFE_NO_PAD.encode(crate::sha256(content.as_bytes())) {
        return Err("the content_hash does not match the content");
    }

    let body: Value = serde_json::from_str(content).map_err(|_| "the content is not JSON")?;
    if field(&body, "v").and_then(Value::as_u64) != Some(1) {
        return Err("the content is not version 1");
    }
    if text(&body, "device_did") != Some(my_did) {
        return Err("the adoption is for another device");
    }
    let owner_did = text(&body, "owner_did").ok_or("the content has no owner_did")?;
    if owner_did != iss {
        return Err("the content's owner is not the statement's issuer");
    }
    let roster_version = field(&body, "roster_version")
        .and_then(Value::as_u64)
        .and_then(|v| u32::try_from(v).ok())
        .ok_or("the content has no roster_version")?;
    let owner = Did::parse(owner_did).map_err(|_| "the owner is not a key did:mata")?;
    Ok(Checked {
        owner_did: owner_did.into(),
        pin: OwnerPin {
            genesis_pubkey: *owner.pubkey(),
            roster_version,
        },
    })
}

fn stored_pin<I: Kv + ?Sized>(identity: &I) -> Result<Option<OwnerPin>> {
    let mut buf = [0u8; OwnerPin::LEN];
    match identity.get(KV_OWNER_PIN, &mut buf)? {
        Some(n) if n == OwnerPin::LEN => Ok(Some(OwnerPin::decode(&buf)?)),
        Some(_) => Err(Error::Corrupt),
        None => Ok(None),
    }
}

/// The adoption the identity store holds, as `/v1/status` presents it
/// ([`Stored::as_status`]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Stored {
    /// The owner the device accepted.
    pub owner_did: String,
    /// [`KIND_MATA_STATEMENT`] or [`crate::status::KIND_ADOPTION_V1`].
    pub kind: &'static str,
    /// The stored bytes: the statement container, or the v1 envelope.
    pub bytes: Vec<u8>,
    /// The pin was taken on a structural check only ([`KV_PIN_PROVISIONAL`]).
    pub provisional: bool,
}

impl Stored {
    /// As the status document takes it.
    #[must_use]
    pub fn as_status(&self) -> crate::status::Adoption<'_> {
        crate::status::Adoption {
            owner_did: &self.owner_did,
            kind: self.kind,
            bytes: &self.bytes,
        }
    }
}

/// The adoption in `identity`, if it holds one for `my_did`: a statement
/// [`apply`] stored (its kind key says so), or a v1 envelope stored by the
/// mesh's adoption (no kind key). Bytes that parse as neither, or that name
/// another device, are not presented: `None`.
pub fn stored<I: Kv + ?Sized>(identity: &I, my_did: &str) -> Result<Option<Stored>> {
    let mut bytes = vec![0u8; ADOPT_MAX];
    let n = match identity.get(KV_ADOPTION, &mut bytes) {
        Ok(Some(n)) => n,
        Ok(None) | Err(Error::BufferTooSmall { .. }) => return Ok(None),
        Err(e) => return Err(e),
    };
    bytes.truncate(n);
    let mut kind = [0u8; 32];
    let is_statement = match identity.get(KV_ADOPTION_KIND, &mut kind) {
        Ok(Some(k)) => &kind[..k] == KIND_MATA_STATEMENT.as_bytes(),
        Ok(None) => false,
        Err(Error::BufferTooSmall { .. }) => return Ok(None),
        Err(e) => return Err(e),
    };
    let mut flag = [0u8; 1];
    let provisional =
        matches!(identity.get(KV_PIN_PROVISIONAL, &mut flag), Ok(Some(1))) && flag[0] == 1;
    let (owner_did, kind) = if is_statement {
        match check_statement(&bytes, my_did) {
            Ok(c) => (c.owner_did, KIND_MATA_STATEMENT),
            Err(_) => return Ok(None),
        }
    } else {
        match crate::adoption::Adoption::decode(&bytes) {
            Ok(a) if a.fields.device_did == my_did => (
                String::from(a.fields.owner_did),
                crate::status::KIND_ADOPTION_V1,
            ),
            _ => return Ok(None),
        }
    };
    Ok(Some(Stored {
        owner_did,
        kind,
        bytes,
        provisional,
    }))
}

/// Take the settings' adoption into the identity store (see the module
/// docs). `my_did` is this device's `did:mata`.
pub fn apply<S: Kv + ?Sized, I: Kv + ?Sized>(
    settings: &mut S,
    identity: &mut I,
    my_did: &str,
) -> Result<Outcome> {
    let mut container = vec![0u8; ADOPT_MAX];
    let n = match settings.get(SETTINGS_ADOPT, &mut container) {
        Ok(Some(n)) => n,
        Ok(None) => return Ok(Outcome::Nothing),
        // longer than any adoption may be: refused, and cleared
        Err(Error::BufferTooSmall { .. }) => {
            settings.remove(SETTINGS_ADOPT)?;
            return Ok(Outcome::Refused("the adoption is longer than 8 KiB"));
        }
        Err(e) => return Err(e),
    };
    container.truncate(n);

    let checked = match check_statement(&container, my_did) {
        Ok(c) => c,
        Err(why) => {
            settings.remove(SETTINGS_ADOPT)?;
            return Ok(Outcome::Refused(why));
        }
    };
    let pinned = stored_pin(identity)?;
    if let Some(pin) = &pinned {
        if pin.genesis_pubkey != checked.pin.genesis_pubkey {
            settings.remove(SETTINGS_ADOPT)?;
            return Ok(Outcome::Refused("the device is pinned to another owner"));
        }
        if checked.pin.roster_version < pin.roster_version {
            settings.remove(SETTINGS_ADOPT)?;
            return Ok(Outcome::Refused(
                "the adoption's roster is older than the pinned one",
            ));
        }
    }

    // the grant, then the pin, then the mark, then the settings cleared: a
    // power cut between any two leaves a state the next boot redoes
    // harmlessly (the same owner, the same roster version)
    identity.put(KV_ADOPTION, &container)?;
    identity.put(KV_ADOPTION_KIND, KIND_MATA_STATEMENT.as_bytes())?;
    let mut pin = [0u8; OwnerPin::LEN];
    checked.pin.encode(&mut pin)?;
    identity.put(KV_OWNER_PIN, &pin)?;
    identity.put(KV_PIN_PROVISIONAL, &[1])?;
    settings.remove(SETTINGS_ADOPT)?;

    let (owner_did, roster_version) = (checked.owner_did, checked.pin.roster_version);
    Ok(if pinned.is_some() {
        Outcome::Rehomed {
            owner_did,
            roster_version,
            provisional: true,
        }
    } else {
        Outcome::Adopted {
            owner_did,
            roster_version,
            provisional: true,
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::key::DeviceKey;
    use alloc::format;
    use alloc::string::ToString;
    use rusty_esp_core::hal::host::MemoryKv;

    fn did_of(key: &DeviceKey) -> String {
        let mut buf = [0u8; 64];
        key.did().write(&mut buf).unwrap().to_string()
    }

    /// A container shaped as the wallet makes it (the signature bytes are a
    /// stand-in: the check here is structural).
    fn container(device: &str, owner: &str, iss: &str, roster: u32, context: &str) -> Vec<u8> {
        let content = format!(
            r#"{{"caps":[],"device_did":"{device}","exp":0,"iat":1760000000,"name":"porch","owner_did":"{owner}","roster_version":{roster},"v":1}}"#
        );
        let hash = URL_SAFE_NO_PAD.encode(crate::sha256(content.as_bytes()));
        let payload = format!(
            r#"{{"iss":"{iss}","iat":1760000000,"exp":null,"nonce":null,"context":"{context}","content_type":"application/json","content_hash":"{hash}","content_preview":null}}"#
        );
        let jws = format!(
            "{}.{}.{}",
            URL_SAFE_NO_PAD.encode(br#"{"alg":"ES256","typ":"JWT"}"#),
            URL_SAFE_NO_PAD.encode(payload.as_bytes()),
            URL_SAFE_NO_PAD.encode([7u8; 64])
        );
        serde_json::to_vec(&serde_json::json!({ "content": content, "jws": jws })).unwrap()
    }

    fn setup() -> (String, String, String) {
        let device = did_of(&DeviceKey::from_seed_for_tests(
            "settings-adopt-device",
            "d",
        ));
        let owner = did_of(&DeviceKey::from_seed_for_tests("settings-adopt-owner", "o"));
        let other = did_of(&DeviceKey::from_seed_for_tests("settings-adopt-other", "x"));
        (device, owner, other)
    }

    #[test]
    fn the_stored_adoption_is_read_back_for_the_status() {
        let (device, owner, other) = setup();
        let (mut s, mut id) = (MemoryKv::new(), MemoryKv::new());
        assert_eq!(stored(&id, &device), Ok(None));

        let c = container(&device, &owner, &owner, 1, STATEMENT_CONTEXT);
        s.put(SETTINGS_ADOPT, &c).unwrap();
        apply(&mut s, &mut id, &device).unwrap();
        let got = stored(&id, &device).unwrap().unwrap();
        assert_eq!(
            got,
            Stored {
                owner_did: owner.clone(),
                kind: KIND_MATA_STATEMENT,
                bytes: c,
                provisional: true,
            }
        );
        assert_eq!(got.as_status().owner_did, owner);
        // another device's store: not presented as ours
        assert_eq!(stored(&id, &other), Ok(None));

        // a v1 envelope, as the mesh stores it: no kind key, no provisional mark
        let owner_key = DeviceKey::from_seed_for_tests("settings-adopt-owner", "o");
        let pubkey = *owner_key.did().pubkey();
        let fields = crate::adoption::AdoptionFields {
            device_did: &device,
            owner_did: &owner,
            owner_genesis_pubkey: &pubkey,
            hub_endpoint_id: &[0; 32],
            hub_relay: "",
            hub_host: "",
            caps: crate::adoption::CapList::Slice(&[]),
            roster_version: 1,
            issued_at: 1_700_000_000,
            expires_at: 0,
        };
        let mut env = vec![0u8; fields.canonical_len() + 64];
        let n = fields.sign_into(&owner_key, &mut env).unwrap();
        env.truncate(n);
        let mut id = MemoryKv::new();
        id.put(KV_ADOPTION, &env).unwrap();
        let got = stored(&id, &device).unwrap().unwrap();
        assert_eq!(got.kind, crate::status::KIND_ADOPTION_V1);
        assert_eq!(got.owner_did, owner);
        assert!(!got.provisional);
        assert_eq!(got.bytes, env);
    }

    #[test]
    fn nothing_in_the_settings_is_nothing() {
        let (device, ..) = setup();
        let (mut s, mut id) = (MemoryKv::new(), MemoryKv::new());
        assert_eq!(apply(&mut s, &mut id, &device), Ok(Outcome::Nothing));
    }

    #[test]
    fn a_first_adoption_is_pinned_provisionally_stored_verbatim_and_cleared() {
        let (device, owner, _) = setup();
        let c = container(&device, &owner, &owner, 1, STATEMENT_CONTEXT);
        let (mut s, mut id) = (MemoryKv::new(), MemoryKv::new());
        s.put(SETTINGS_ADOPT, &c).unwrap();
        assert_eq!(
            apply(&mut s, &mut id, &device),
            Ok(Outcome::Adopted {
                owner_did: owner.clone(),
                roster_version: 1,
                provisional: true
            })
        );
        let mut buf = vec![0u8; ADOPT_MAX];
        let n = id.get(KV_ADOPTION, &mut buf).unwrap().unwrap();
        assert_eq!(&buf[..n], &c[..], "the container is stored byte for byte");
        let n = id.get(KV_ADOPTION_KIND, &mut buf).unwrap().unwrap();
        assert_eq!(&buf[..n], KIND_MATA_STATEMENT.as_bytes());
        let pin = stored_pin(&id).unwrap().unwrap();
        assert_eq!(pin.genesis_pubkey, *Did::parse(&owner).unwrap().pubkey());
        assert_eq!(id.get(KV_PIN_PROVISIONAL, &mut buf).unwrap(), Some(1));
        assert_eq!(s.get(SETTINGS_ADOPT, &mut buf).unwrap(), None, "cleared");
    }

    #[test]
    fn the_same_owner_rehomes_another_is_refused_and_both_are_cleared() {
        let (device, owner, other) = setup();
        let (mut s, mut id) = (MemoryKv::new(), MemoryKv::new());
        s.put(
            SETTINGS_ADOPT,
            &container(&device, &owner, &owner, 2, STATEMENT_CONTEXT),
        )
        .unwrap();
        apply(&mut s, &mut id, &device).unwrap();

        s.put(
            SETTINGS_ADOPT,
            &container(&device, &owner, &owner, 3, STATEMENT_CONTEXT),
        )
        .unwrap();
        assert!(matches!(
            apply(&mut s, &mut id, &device),
            Ok(Outcome::Rehomed {
                roster_version: 3,
                ..
            })
        ));

        s.put(
            SETTINGS_ADOPT,
            &container(&device, &owner, &owner, 2, STATEMENT_CONTEXT),
        )
        .unwrap();
        assert_eq!(
            apply(&mut s, &mut id, &device),
            Ok(Outcome::Refused(
                "the adoption's roster is older than the pinned one"
            ))
        );

        s.put(
            SETTINGS_ADOPT,
            &container(&device, &other, &other, 9, STATEMENT_CONTEXT),
        )
        .unwrap();
        assert_eq!(
            apply(&mut s, &mut id, &device),
            Ok(Outcome::Refused("the device is pinned to another owner"))
        );
        let mut buf = [0u8; 8];
        assert_eq!(
            s.get(SETTINGS_ADOPT, &mut buf).unwrap(),
            None,
            "cleared even when refused"
        );
        assert_eq!(
            stored_pin(&id).unwrap().unwrap().roster_version,
            3,
            "the owner kept"
        );
    }

    #[test]
    fn the_structural_checks_each_refuse() {
        let (device, owner, other) = setup();
        let refuse = |c: Vec<u8>| check_statement(&c, &device).unwrap_err();
        assert_eq!(
            refuse(container(&other, &owner, &owner, 1, STATEMENT_CONTEXT)),
            "the adoption is for another device"
        );
        assert_eq!(
            refuse(container(&device, &owner, &other, 1, STATEMENT_CONTEXT)),
            "the content's owner is not the statement's issuer"
        );
        assert_eq!(
            refuse(container(&device, &owner, &owner, 1, "some-other-context")),
            "the statement's context is not janus-adoption-v1"
        );
        assert_eq!(refuse(b"not json".to_vec()), "the container is not JSON");
        assert_eq!(
            refuse(container(
                &device,
                "did:mata:nope",
                "did:mata:nope",
                1,
                STATEMENT_CONTEXT
            )),
            "the owner is not a key did:mata"
        );
        // content edited after signing: its hash no longer matches
        let mut v: Value =
            serde_json::from_slice(&container(&device, &owner, &owner, 1, STATEMENT_CONTEXT))
                .unwrap();
        let edited = v["content"]
            .as_str()
            .unwrap()
            .replace("\"porch\"", "\"attic\"");
        v["content"] = Value::String(edited);
        assert_eq!(
            refuse(serde_json::to_vec(&v).unwrap()),
            "the content_hash does not match the content"
        );
    }

    #[test]
    fn an_oversize_adoption_is_refused_and_cleared() {
        let (device, ..) = setup();
        let (mut s, mut id) = (MemoryKv::new(), MemoryKv::new());
        s.put(SETTINGS_ADOPT, &vec![b'x'; ADOPT_MAX + 1]).unwrap();
        assert_eq!(
            apply(&mut s, &mut id, &device),
            Ok(Outcome::Refused("the adoption is longer than 8 KiB"))
        );
        let mut buf = [0u8; 8];
        assert_eq!(s.get(SETTINGS_ADOPT, &mut buf).unwrap(), None);
    }
}

#[cfg(test)]
mod dyn_tests {
    use super::*;
    use rusty_esp_core::hal::host::MemoryKv;

    /// The stores a firmware holds are boxed `dyn Kv`s: `apply` takes them.
    #[test]
    fn apply_takes_unsized_stores() {
        let mut s: alloc::boxed::Box<dyn Kv> = alloc::boxed::Box::new(MemoryKv::new());
        let mut id: alloc::boxed::Box<dyn Kv> = alloc::boxed::Box::new(MemoryKv::new());
        assert_eq!(apply(&mut *s, &mut *id, "did:mata:x"), Ok(Outcome::Nothing));
    }
}