lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

stock_obtained_tests.rs (6849B)


      1 //! Conversion refusal must retain actual verified original bytes without IO.
      2 use super::*;
      3 use crate::session::commerce::{AcceptanceClaim, AcceptanceSelection};
      4 use alloc::vec;
      5 use futures::executor::block_on;
      6 use radroots_domain::{
      7     ActorRef, ActorRole, AuthorityRef, CheckpointDigest, CheckpointRef, CommandDigest, Coverage,
      8     ExecutorId, Generation, GrantId, OperationId, PoolId, RouteDigest, Sequence, SigningAttemptId,
      9     UnixSeconds, WorkspaceId,
     10     stock::{OptionMappingKey, OptionPoolMapping, PoolView},
     11 };
     12 use radroots_protocol::{
     13     command::CapturedOrderAcceptance,
     14     commerce::{self, CommerceSigningPlan, EncodedCommerceRumor},
     15 };
     16 use radroots_signing::{
     17     issuance::{OutcomeKind, SigningIntent, SigningOutcome},
     18     provider::ProviderCall,
     19     request::CancellationSignal,
     20 };
     21 use radroots_storage::{
     22     event::BoxFuture,
     23     inventory::{InventoryCheckpointRef, InventoryOutcome, InventoryReceipt, InventoryRequest},
     24     workspace::{
     25         AttachmentInput, WorkspaceBinding, WorkspaceError, WorkspaceScope, WorkspaceStore,
     26     },
     27 };
     28 use std::num::NonZeroU64;
     29 #[path = "stock_unissued_tests.rs"]
     30 mod unissued;
     31 
     32 struct NoIo;
     33 impl WorkspaceStore for NoIo {
     34     fn authenticate(
     35         &self,
     36         _: AttachmentInput,
     37         _: AttachmentProof,
     38     ) -> BoxFuture<'_, Result<WorkspaceBinding, WorkspaceError>> {
     39         panic!("mapping refusal must execute no host IO")
     40     }
     41     fn close_workspace(&self) -> BoxFuture<'_, Result<(), WorkspaceError>> {
     42         panic!("mapping refusal must execute no host IO")
     43     }
     44 }
     45 impl InventoryStore for NoIo {
     46     fn option_mapping(
     47         &self,
     48         _: WorkspaceBinding,
     49         _: InventoryCheckpointRef,
     50         _: OptionMappingKey,
     51         _: AttachmentProof,
     52     ) -> BoxFuture<'_, Result<Option<OptionPoolMapping>, OperationError>> {
     53         panic!("unexpected IO")
     54     }
     55     fn execute_inventory(
     56         &self,
     57         _: InventoryRequest,
     58         _: AttachmentProof,
     59     ) -> BoxFuture<'_, Result<InventoryOutcome, OperationError>> {
     60         panic!("unexpected IO")
     61     }
     62     fn inventory_operation(
     63         &self,
     64         _: WorkspaceBinding,
     65         _: InventoryCheckpointRef,
     66         _: OperationId,
     67         _: Option<CommandDigest>,
     68         _: AttachmentProof,
     69     ) -> BoxFuture<'_, Result<Option<InventoryReceipt>, OperationError>> {
     70         panic!("unexpected IO")
     71     }
     72     fn pool(
     73         &self,
     74         _: WorkspaceBinding,
     75         _: InventoryCheckpointRef,
     76         _: PoolId,
     77         _: AttachmentProof,
     78     ) -> BoxFuture<'_, Result<Option<PoolView>, OperationError>> {
     79         panic!("unexpected IO")
     80     }
     81 }
     82 fn fixture() -> (
     83     ObtainedContext,
     84     radroots_signing::issuance::SignedArtifact,
     85     std::sync::Weak<()>,
     86 ) {
     87     let command = CapturedOrderAcceptance::parse(include_bytes!(
     88         "../../tests/fixtures/commerce_verification/command_wire.json"
     89     ))
     90     .unwrap();
     91     let record = commerce::decode(include_bytes!(
     92         "../../tests/fixtures/commerce_verification/record_wire.json"
     93     ))
     94     .unwrap();
     95     let rumor = EncodedCommerceRumor::capture(record.content()).unwrap();
     96     let plan = CommerceSigningPlan::parse(
     97         command.context().operation_id(),
     98         command.digest(),
     99         &rumor,
    100         include_bytes!("../../tests/fixtures/commerce_verification/plan_wire.bin"),
    101     )
    102     .unwrap();
    103     let seal = &plan.seals()[0];
    104     let intent = SigningIntent::from_commerce_seal(
    105         seal,
    106         ProviderCall::new(
    107             rumor.author(),
    108             NonZeroU64::new(600000).unwrap(),
    109             CancellationSignal::new(),
    110         ),
    111     )
    112     .unwrap();
    113     let wire = include_str!("../../tests/fixtures/commerce_verification/seal_signed.json");
    114     let (_, OutcomeKind::Obtained(artifact)) =
    115         SigningOutcome::capture(&intent, wire.into(), None).into_parts()
    116     else {
    117         panic!("fixture must verify as the exact original signed artifact")
    118     };
    119     // A deliberately wrong non-stock descriptor forces conversion to fail.
    120     // Neither the fake host nor this descriptor has transaction authority.
    121     let authority = AuthorityRef::new(ExecutorId::from_bytes([2; 32]), Generation::new(1).unwrap());
    122     let input = AttachmentInput::new(
    123         WorkspaceId::from_bytes([1; 32]),
    124         authority,
    125         rumor.author(),
    126         "local://model",
    127         rumor.author(),
    128         GrantId::from_bytes([3; 32]),
    129         CheckpointRef::new(
    130             CheckpointDigest::from_bytes([4; 32]),
    131             Sequence::new(0).unwrap(),
    132             Coverage::Partial,
    133             vec![],
    134         )
    135         .unwrap(),
    136         RouteDigest::from_bytes([5; 32]),
    137     )
    138     .unwrap();
    139     let binding = WorkspaceBinding::from_authenticated_host(
    140         input.clone(),
    141         WorkspaceScope::NewSellerOutbox,
    142         ActorRef::new(
    143             rumor.author(),
    144             ActorRole::Seller,
    145             None,
    146             Some(input.grant_id()),
    147         ),
    148         "device",
    149         Sequence::new(1).unwrap(),
    150         UnixSeconds::new(200).unwrap(),
    151         UnixSeconds::new(100).unwrap(),
    152     )
    153     .unwrap();
    154     let lease = Shared::new(());
    155     let weak = Shared::downgrade(&lease);
    156     let context = ObtainedContext {
    157         selection: AcceptanceSelection {
    158             binding,
    159             minimum: InventoryCheckpointRef::from_host_metadata(
    160                 authority,
    161                 Sequence::new(0).unwrap(),
    162                 None,
    163             )
    164             .unwrap(),
    165             operation: command.context().operation_id(),
    166             digest: command.digest(),
    167         },
    168         claim: AcceptanceClaim {
    169             id: SigningAttemptId::from_bytes([6; 32]),
    170             binding: intent.binding(),
    171         },
    172         recipient: seal.recipient(),
    173         store: Shared::new(NoIo),
    174         proof: AttachmentProof::from_protected_input(&[7; 32]).unwrap(),
    175         lease: lease.clone(),
    176     };
    177     drop(lease);
    178     (context, artifact, weak)
    179 }
    180 #[test]
    181 fn mapping_refusal_keeps_verified_artifact_credentials_and_lease_before_any_storage_request() {
    182     let (context, artifact, weak) = fixture();
    183     let wire = include_str!("../../tests/fixtures/commerce_verification/seal_signed.json");
    184     let owner = Owner::default();
    185     let entry = owner.retain_obtained(context, artifact);
    186     assert_eq!(
    187         block_on(owner.capture_obtained(&entry)).unwrap_err().code(),
    188         crate::Code::Unauthorized
    189     );
    190     drop(entry);
    191     assert!(weak.upgrade().is_some());
    192     assert!(owner.ensure_ready().is_err());
    193     assert_eq!(
    194         block_on(owner.drain()).unwrap_err().code(),
    195         crate::Code::Unauthorized
    196     );
    197     let retained = owner.unmapped.lock().unwrap();
    198     assert_eq!(retained.len(), 1);
    199     assert_eq!(retained[0].context.proof.with_secret(|b| *b), [7; 32]);
    200     retained[0].artifact.with_json(|raw| assert_eq!(raw, wire));
    201     assert!(owner.pending.lock().unwrap().is_empty());
    202 }