Skip to content

Commit 4a358c7

Browse files
committed
fix(mock): port MockPlatform + through-core tests onto rewritten #104
Adapt the mock platform to #104's async_trait capability traits and truapi::latest types: add #[async_trait] to each capability impl, flatten feature_supported to bare v01 request/response types, update CoreStorageKey::PermissionAuthorization to { product_id, request }, and add JsonRpcConnection::close. Fix the through-core preimage + storage-fault tests to assert the Result discriminant at wire byte 1: the versioned envelope is [version_index, result_discriminant, ..inner], and V1's version index is 0, so the Ok/Err distinction lives at byte 1, not byte 0.
1 parent 7cee789 commit 4a358c7

2 files changed

Lines changed: 39 additions & 26 deletions

File tree

rust/crates/truapi-platform/src/mock.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use futures::StreamExt;
2929
use futures::stream::{self, BoxStream};
3030

3131
use truapi::v01;
32-
use truapi::versioned::system::{HostFeatureSupportedRequest, HostFeatureSupportedResponse};
3332

33+
use crate::async_trait;
3434
use crate::{
3535
AuthPresenter, AuthState, ChainProvider, CoreStorage, CoreStorageKey, Features,
3636
JsonRpcConnection, Navigation, Notifications, Permissions, PreimageHost, ProductStorage,
@@ -254,9 +254,10 @@ fn core_key(key: &CoreStorageKey) -> String {
254254
match key {
255255
CoreStorageKey::AuthSession => "core:auth-session".to_string(),
256256
CoreStorageKey::PairingDeviceIdentity => "core:pairing-device-identity".to_string(),
257-
CoreStorageKey::PermissionAuthorization { storage_key } => {
258-
format!("core:permission:{storage_key}")
259-
}
257+
CoreStorageKey::PermissionAuthorization {
258+
product_id,
259+
request,
260+
} => format!("core:permission:{product_id}:{request:?}"),
260261
}
261262
}
262263

@@ -268,6 +269,7 @@ fn preimage_key(value: &[u8]) -> Vec<u8> {
268269
hasher.finish().to_le_bytes().to_vec()
269270
}
270271

272+
#[async_trait]
271273
impl ProductStorage for MockPlatform {
272274
async fn read(&self, key: String) -> Result<Option<Vec<u8>>, v01::HostLocalStorageReadError> {
273275
if let Some(reason) = &self.config.faults.storage_error {
@@ -314,6 +316,7 @@ impl ProductStorage for MockPlatform {
314316
}
315317
}
316318

319+
#[async_trait]
317320
impl CoreStorage for MockPlatform {
318321
async fn read_core_storage(
319322
&self,
@@ -363,6 +366,7 @@ impl CoreStorage for MockPlatform {
363366
}
364367
}
365368

369+
#[async_trait]
366370
impl Navigation for MockPlatform {
367371
async fn navigate_to(&self, url: String) -> Result<(), v01::HostNavigateToError> {
368372
if let Some(reason) = &self.config.faults.navigate_error {
@@ -378,6 +382,7 @@ impl Navigation for MockPlatform {
378382
}
379383
}
380384

385+
#[async_trait]
381386
impl Notifications for MockPlatform {
382387
async fn push_notification(
383388
&self,
@@ -405,6 +410,7 @@ impl Notifications for MockPlatform {
405410
}
406411
}
407412

413+
#[async_trait]
408414
impl Permissions for MockPlatform {
409415
async fn device_permission(
410416
&self,
@@ -425,17 +431,15 @@ impl Permissions for MockPlatform {
425431
}
426432
}
427433

434+
#[async_trait]
428435
impl Features for MockPlatform {
429436
async fn feature_supported(
430437
&self,
431-
request: HostFeatureSupportedRequest,
432-
) -> Result<HostFeatureSupportedResponse, v01::GenericError> {
433-
let HostFeatureSupportedRequest::V1(_) = request;
434-
Ok(HostFeatureSupportedResponse::V1(
435-
v01::HostFeatureSupportedResponse {
436-
supported: self.config.feature_supported,
437-
},
438-
))
438+
_request: v01::HostFeatureSupportedRequest,
439+
) -> Result<v01::HostFeatureSupportedResponse, v01::GenericError> {
440+
Ok(v01::HostFeatureSupportedResponse {
441+
supported: self.config.feature_supported,
442+
})
439443
}
440444
}
441445

@@ -457,8 +461,11 @@ impl JsonRpcConnection for MockConnection {
457461
Some(frames) => Box::pin(stream::iter(frames.clone())),
458462
}
459463
}
464+
465+
fn close(&self) {}
460466
}
461467

468+
#[async_trait]
462469
impl ChainProvider for MockPlatform {
463470
async fn connect(
464471
&self,
@@ -493,6 +500,7 @@ impl AuthPresenter for MockPlatform {
493500
}
494501
}
495502

503+
#[async_trait]
496504
impl UserConfirmation for MockPlatform {
497505
async fn confirm_user_action(
498506
&self,
@@ -519,6 +527,7 @@ impl ThemeHost for MockPlatform {
519527
}
520528
}
521529

530+
#[async_trait]
522531
impl PreimageHost for MockPlatform {
523532
async fn submit_preimage(&self, value: Vec<u8>) -> Result<Vec<u8>, v01::PreimageSubmitError> {
524533
if let Some(reason) = &self.config.faults.preimage_submit_error {
@@ -610,12 +619,7 @@ mod tests {
610619
// ...and a product key must not be visible through core storage.
611620
block_on(p.write("x".into(), vec![2])).unwrap();
612621
assert_eq!(
613-
block_on(
614-
p.read_core_storage(CoreStorageKey::PermissionAuthorization {
615-
storage_key: "x".into()
616-
})
617-
)
618-
.unwrap(),
622+
block_on(p.read_core_storage(CoreStorageKey::PairingDeviceIdentity)).unwrap(),
619623
None
620624
);
621625
}
@@ -744,11 +748,11 @@ mod tests {
744748
feature_supported: false,
745749
..Default::default()
746750
});
747-
let HostFeatureSupportedResponse::V1(response) = block_on(p.feature_supported(
748-
HostFeatureSupportedRequest::V1(v01::HostFeatureSupportedRequest::Chain {
751+
let response = block_on(
752+
p.feature_supported(v01::HostFeatureSupportedRequest::Chain {
749753
genesis_hash: vec![0; 32],
750754
}),
751-
))
755+
)
752756
.unwrap();
753757
assert!(!response.supported);
754758
}

rust/crates/truapi-server/src/core.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,16 +509,22 @@ mod tests {
509509
use truapi::versioned::preimage::RemotePreimageSubmitRequest;
510510
use truapi_platform::mock::MockConfig;
511511

512-
// Default mock auto-confirms: submit succeeds (Ok disc 0x00).
512+
// Versioned envelope layout is [version_index, result_discriminant, ..].
513+
// For a V1 response the version index is 0, so the Ok/Err distinction
514+
// lives at byte index 1: 0x00 = Ok, 0x01 = Err.
515+
516+
// Default mock auto-confirms: submit succeeds (V1 index 0x00, Ok 0x00).
513517
let confirmed = make_mock_core(MockConfig::default());
514518
let ok_payload = run_request(
515519
&confirmed,
516520
"preimage_submit",
517521
RemotePreimageSubmitRequest::V1(vec![1, 2, 3]).encode(),
518522
);
519523
assert_eq!(ok_payload.first(), Some(&0x00));
524+
assert_eq!(ok_payload.get(1), Some(&0x00));
520525

521-
// confirm_user_actions = false: the core rejects before the platform (Err disc 0x01).
526+
// confirm_user_actions = false: the core rejects before the platform
527+
// (V1 index 0x00, Err 0x01).
522528
let rejected = make_mock_core(MockConfig {
523529
confirm_user_actions: false,
524530
..Default::default()
@@ -528,7 +534,8 @@ mod tests {
528534
"preimage_submit",
529535
RemotePreimageSubmitRequest::V1(vec![1, 2, 3]).encode(),
530536
);
531-
assert_eq!(err_payload.first(), Some(&0x01));
537+
assert_eq!(err_payload.first(), Some(&0x00));
538+
assert_eq!(err_payload.get(1), Some(&0x01));
532539
}
533540

534541
/// A MockPlatform storage fault surfaces through the real core as a wire
@@ -546,9 +553,11 @@ mod tests {
546553
});
547554
let read =
548555
HostLocalStorageReadRequest::V1(v01::HostLocalStorageReadRequest { key: "k".into() });
549-
// Err envelope: Result discriminant 0x01 (vs 0x00 Ok in the happy-path test).
556+
// Versioned wire layout is [version_index=0x00 (V1)][result_index][inner];
557+
// the Err discriminant is byte 1, not byte 0 (byte 0 is the V1 version index).
550558
let payload = run_request(&core, "local_storage_read", read.encode());
551-
assert_eq!(payload.first(), Some(&0x01));
559+
assert_eq!(payload.first(), Some(&0x00)); // V1 version index
560+
assert_eq!(payload.get(1), Some(&0x01)); // Result::Err discriminant
552561
}
553562

554563
#[test]

0 commit comments

Comments
 (0)