lib

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

tests.rs (8378B)


      1 //! Framing-only tests: these deliberately supply synthetic body data and do
      2 //! not claim business admission or an accepted stock operation.
      3 use super::*;
      4 use crate::workspace::tests::{CryptoFixture, participant};
      5 use radroots_domain::{ActorRole, OrderId, UnixSeconds};
      6 
      7 fn selection() -> AttachmentInput {
      8     let (input, _, _) = SellerStockConfig::new(
      9         participant(),
     10         participant(),
     11         "frame-fixture",
     12         UnixSeconds::new(200).unwrap(),
     13     )
     14     .unwrap()
     15     .enroll(
     16         UnixSeconds::new(100).unwrap(),
     17         WorkspaceId::from_bytes([1; 32]),
     18         ExecutorId::from_bytes([2; 32]),
     19         GrantId::from_bytes([3; 32]),
     20         AttachmentProof::from_protected_input(&[4; 32]).unwrap(),
     21     )
     22     .unwrap();
     23     input
     24 }
     25 fn header(input: &AttachmentInput) -> Header {
     26     Header {
     27         version: 5,
     28         family: Family::Acceptance,
     29         namespace: SellerAuthoritySelection::from_attachment(input),
     30         actor: ActorRef::new(
     31             participant(),
     32             ActorRole::Seller,
     33             None,
     34             Some(input.grant_id()),
     35         ),
     36         operation: OperationId::from_bytes([5; 32]),
     37         digest: CommandDigest::from_bytes([6; 32]),
     38         transaction: TransactionId::from_bytes([7; 32]),
     39         revision: Sequence::new(1).unwrap(),
     40         previous: None,
     41         head: CheckpointDigest::from_bytes([8; 32]),
     42         pool: None,
     43         order: Some(OrderId::from_bytes([9; 32])),
     44         related_operation: None,
     45         subject_grant: None,
     46         bytes: 0,
     47         parts: 0,
     48         body_digest: [0; 32],
     49     }
     50 }
     51 async fn database(input: &AttachmentInput) -> SqliteConnection {
     52     let mut connection = SqliteConnection::connect_with(
     53         &sqlx::sqlite::SqliteConnectOptions::new()
     54             .in_memory(true)
     55             .foreign_keys(true),
     56     )
     57     .await
     58     .unwrap();
     59     for (_, _, _, sql) in super::super::schema::objects(7).unwrap() {
     60         sqlx::raw_sql(sql).execute(&mut connection).await.unwrap();
     61     }
     62     // Only the foreign-key metadata needed by framing is installed. This is
     63     // not an enrolled NativeWorkspace and supplies no authenticated grant.
     64     sqlx::query("INSERT INTO stock_grants(grant_id,protected) VALUES(?,?)")
     65         .bind(input.grant_id().as_bytes().as_slice())
     66         .bind([1_u8].as_slice())
     67         .execute(&mut connection)
     68         .await
     69         .unwrap();
     70     connection
     71 }
     72 async fn insert(connection: &mut SqliteConnection, input: &AttachmentInput, prepared: &Prepared) {
     73     let value = header(input);
     74     let mut tx = connection.begin_with("BEGIN IMMEDIATE").await.unwrap();
     75     sqlx::query("INSERT INTO stock_operations(actor,grant_id,operation_id,command_digest,transaction_id,pool_id,revision,chain_head,protected,family,order_id,related_operation) VALUES(?,?,?,?,?,NULL,?,?,?,2,?,NULL)")
     76         .bind(input.participant().as_bytes().as_slice()).bind(input.grant_id().as_bytes().as_slice())
     77         .bind(value.operation.as_bytes().as_slice()).bind(value.digest.as_bytes().as_slice()).bind(value.transaction.as_bytes().as_slice())
     78         .bind(value.revision.get() as i64).bind(value.head.as_bytes().as_slice()).bind(&prepared.protected)
     79         .bind(value.order.unwrap().as_bytes().as_slice()).execute(&mut *tx).await.unwrap();
     80     prepared
     81         .insert_parts(&mut tx, value.actor, value.operation)
     82         .await
     83         .unwrap();
     84     tx.commit().await.unwrap();
     85 }
     86 
     87 #[tokio::test]
     88 async fn actual_protected_multipart_body_roundtrips_exact_boundaries_and_sql_witnesses() {
     89     let fixture = CryptoFixture::new();
     90     let input = selection();
     91     let crypto = fixture.crypto();
     92     for length in [1, PART_BYTES, PART_BYTES + 1, BODY_MAX_BYTES] {
     93         let mut connection = database(&input).await;
     94         let body = vec![0x51; length];
     95         let prepared = prepare(&crypto, &input, header(&input), &body)
     96             .await
     97             .unwrap();
     98         assert_eq!(prepared.parts.len(), length.div_ceil(PART_BYTES));
     99         insert(&mut connection, &input, &prepared).await;
    100         let value = read(&mut connection, &input, &crypto, header(&input).operation)
    101             .await
    102             .unwrap()
    103             .unwrap();
    104         assert_eq!(value.body.as_slice(), body.as_slice());
    105         assert_eq!(value.protected, prepared.protected);
    106         confirm_parts(
    107             &mut connection,
    108             value.header.actor,
    109             value.header.operation,
    110             value.parts_witness,
    111             value.header.parts,
    112         )
    113         .await
    114         .unwrap();
    115         sqlx::query("PRAGMA foreign_keys=OFF")
    116             .execute(&mut connection)
    117             .await
    118             .unwrap();
    119         sqlx::query("UPDATE stock_operation_parts SET grant_id=zeroblob(32) WHERE part=0")
    120             .execute(&mut connection)
    121             .await
    122             .unwrap();
    123         assert_eq!(
    124             confirm_parts(
    125                 &mut connection,
    126                 value.header.actor,
    127                 value.header.operation,
    128                 value.parts_witness,
    129                 value.header.parts
    130             )
    131             .await,
    132             Err(OperationError::RecoveryRequired)
    133         );
    134         assert!(
    135             read(&mut connection, &input, &crypto, value.header.operation)
    136                 .await
    137                 .is_err()
    138         );
    139         sqlx::query("UPDATE stock_operation_parts SET grant_id=? WHERE part=0")
    140             .bind(input.grant_id().as_bytes().as_slice())
    141             .execute(&mut connection)
    142             .await
    143             .unwrap();
    144         sqlx::query("PRAGMA foreign_keys=ON")
    145             .execute(&mut connection)
    146             .await
    147             .unwrap();
    148         sqlx::query(
    149             "UPDATE stock_operation_parts SET protected=randomblob(length(protected)) WHERE part=0",
    150         )
    151         .execute(&mut connection)
    152         .await
    153         .unwrap();
    154         assert_eq!(
    155             confirm_parts(
    156                 &mut connection,
    157                 value.header.actor,
    158                 value.header.operation,
    159                 value.parts_witness,
    160                 value.header.parts
    161             )
    162             .await,
    163             Err(OperationError::RecoveryRequired)
    164         );
    165         assert!(
    166             read(&mut connection, &input, &crypto, header(&input).operation)
    167                 .await
    168                 .is_err()
    169         );
    170         connection.close().await.unwrap();
    171     }
    172     assert!(matches!(
    173         prepare(&crypto, &input, header(&input), &[]).await,
    174         Err(OperationError::Workspace(WorkspaceError::Capacity))
    175     ));
    176     assert!(matches!(
    177         prepare(
    178             &crypto,
    179             &input,
    180             header(&input),
    181             &vec![0x51; BODY_MAX_BYTES + 1]
    182         )
    183         .await,
    184         Err(OperationError::Workspace(WorkspaceError::Capacity))
    185     ));
    186 }
    187 
    188 #[tokio::test]
    189 async fn substituted_family_scope_and_missing_or_reordered_body_parts_refuse() {
    190     let fixture = CryptoFixture::new();
    191     let input = selection();
    192     let crypto = fixture.crypto();
    193     for statement in [
    194         "DELETE FROM stock_operation_parts WHERE part=1",
    195         "UPDATE stock_operation_parts SET protected=(SELECT protected FROM stock_operation_parts WHERE part=0) WHERE part=1",
    196         "UPDATE stock_operations SET command_digest=zeroblob(32)",
    197         "UPDATE stock_operations SET transaction_id=zeroblob(32)",
    198         "UPDATE stock_operations SET order_id=zeroblob(32)",
    199     ] {
    200         let mut connection = database(&input).await;
    201         let prepared = prepare(&crypto, &input, header(&input), &vec![0x51; PART_BYTES + 1])
    202             .await
    203             .unwrap();
    204         insert(&mut connection, &input, &prepared).await;
    205         sqlx::raw_sql(statement)
    206             .execute(&mut connection)
    207             .await
    208             .unwrap();
    209         assert!(
    210             read(&mut connection, &input, &crypto, header(&input).operation)
    211                 .await
    212                 .is_err()
    213         );
    214         connection.close().await.unwrap();
    215     }
    216     let mut bad = header(&input);
    217     bad.family = Family::Inventory;
    218     assert!(matches!(
    219         prepare(&crypto, &input, bad, b"frame").await,
    220         Err(OperationError::RecoveryRequired)
    221     ));
    222     let mut bad = header(&input);
    223     bad.related_operation = Some(OperationId::from_bytes([0xa1; 32]));
    224     assert!(matches!(
    225         prepare(&crypto, &input, bad, b"frame").await,
    226         Err(OperationError::RecoveryRequired)
    227     ));
    228 }