Skip to content

Commit d8adc38

Browse files
authored
test(rust/rbac-registration): Fix test in CIP509 (#160)
* test(rbac-registration): fix test and test data Signed-off-by: bkioshn <[email protected]> * fix(rbac-registration): name txn_index Signed-off-by: bkioshn <[email protected]> * fix(rbac-registration): add more test to registration chain Signed-off-by: bkioshn <[email protected]> --------- Signed-off-by: bkioshn <[email protected]>
1 parent 3eb7126 commit d8adc38

File tree

8 files changed

+190
-95
lines changed

8 files changed

+190
-95
lines changed

rust/rbac-registration/src/cardano/cip509/cip509.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -551,18 +551,17 @@ mod tests {
551551

552552
#[test]
553553
fn new() {
554-
let block = test::block_1();
555-
let index = TxnIndex::from(3);
556-
let res = Cip509::new(&block, index, &[])
554+
let data = test::block_1();
555+
let res = Cip509::new(&data.block, data.txn_index, &[])
557556
.expect("Failed to get Cip509")
558557
.expect("There must be Cip509 in block");
559558
assert!(!res.report.is_problematic(), "{:?}", res.report);
560559
}
561560

562561
#[test]
563562
fn from_block() {
564-
let block = test::block_1();
565-
let res = Cip509::from_block(&block, &[]);
563+
let data = test::block_1();
564+
let res = Cip509::from_block(&data.block, &[]);
566565
assert_eq!(1, res.len());
567566
let cip509 = res.first().unwrap();
568567
assert!(!cip509.report.is_problematic(), "{:?}", cip509.report);

rust/rbac-registration/src/cardano/cip509/utils/cip134_uri_set.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ mod tests {
266266

267267
#[test]
268268
fn set_new() {
269-
let block = test::block_1();
270-
let cip509 = Cip509::new(&block, 3.into(), &[]).unwrap().unwrap();
269+
let data = test::block_1();
270+
let cip509 = Cip509::new(&data.block, data.txn_index, &[])
271+
.unwrap()
272+
.unwrap();
271273
assert!(
272274
!cip509.report().is_problematic(),
273275
"Failed to decode Cip509: {:?}",
@@ -288,7 +290,7 @@ mod tests {
288290
// cSpell:disable
289291
assert_eq!(
290292
uri.uri(),
291-
"web+cardano://addr/stake_test1urs8t0ssa3w9wh90ld5tprp3gurxd487rth2qlqk6ernjqcef4ugr"
293+
format!("web+cardano://addr/{}", data.stake_addr.unwrap())
292294
);
293295
// cSpell:enable
294296
let Address::Stake(address) = uri.address() else {

rust/rbac-registration/src/cardano/cip509/validation.rs

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ mod tests {
257257

258258
#[test]
259259
fn block_1() {
260-
let block = test::block_1();
260+
let data = test::block_1();
261261

262-
let mut registrations = Cip509::from_block(&block, &[]);
262+
let mut registrations = Cip509::from_block(&data.block, &[]);
263263
assert_eq!(1, registrations.len());
264264

265265
let registration = registrations.pop().unwrap();
@@ -271,30 +271,27 @@ mod tests {
271271
assert!(registration.previous_transaction().is_none());
272272

273273
let origin = registration.origin();
274-
assert_eq!(origin.txn_index(), 3.into());
275-
assert_eq!(origin.point().slot_or_default(), 77_429_134.into());
274+
assert_eq!(origin.txn_index(), data.txn_index);
275+
assert_eq!(origin.point().slot_or_default(), data.slot);
276276

277277
let (purpose, metadata, _) = registration.consume().unwrap();
278-
assert_eq!(
279-
purpose,
280-
Uuid::parse_str("ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c").unwrap()
281-
);
278+
assert_eq!(purpose, Uuid::parse_str(&data.purpose).unwrap());
282279
assert_eq!(1, metadata.role_data.len());
283280
}
284281

285282
#[test]
286283
fn block_2() {
287-
let block = test::block_2();
284+
let data = test::block_2();
288285

289-
let mut registrations = Cip509::from_block(&block, &[]);
286+
let mut registrations = Cip509::from_block(&data.block, &[]);
290287
assert_eq!(1, registrations.len());
291288

292289
let registration = registrations.pop().unwrap();
293290
assert!(registration.report().is_problematic());
294291

295292
let origin = registration.origin();
296-
assert_eq!(origin.txn_index(), 0.into());
297-
assert_eq!(origin.point().slot_or_default(), 77_171_632.into());
293+
assert_eq!(origin.txn_index(), data.txn_index);
294+
assert_eq!(origin.point().slot_or_default(), data.slot);
298295

299296
// The consume function must return the problem report contained within the registration.
300297
let report = registration.consume().unwrap_err();
@@ -305,44 +302,35 @@ mod tests {
305302

306303
#[test]
307304
fn block_3() {
308-
let block = test::block_3();
305+
let data = test::block_3();
309306

310-
let mut registrations = Cip509::from_block(&block, &[]);
307+
let mut registrations = Cip509::from_block(&data.block, &[]);
311308
assert_eq!(1, registrations.len());
312309

313310
let registration = registrations.pop().unwrap();
314-
assert!(
315-
!registration.report().is_problematic(),
316-
"{:?}",
317-
registration.report()
318-
);
311+
assert!(registration.report().is_problematic());
312+
319313
assert_eq!(
320314
registration.previous_transaction(),
321-
Some(
322-
Blake2b256Hash::from_str(
323-
"4d3f576f26db29139981a69443c2325daa812cc353a31b5a4db794a5bcbb06c2"
324-
)
325-
.unwrap()
326-
)
315+
Some(Blake2b256Hash::from_str(data.prv_hash.unwrap().as_str()).unwrap())
327316
);
328317

329318
let origin = registration.origin();
330-
assert_eq!(origin.txn_index(), 0.into());
331-
assert_eq!(origin.point().slot_or_default(), 77_170_639.into());
319+
assert_eq!(origin.txn_index(), data.txn_index);
320+
assert_eq!(origin.point().slot_or_default(), data.slot);
332321

333-
let (purpose, metadata, _) = registration.consume().unwrap();
334-
assert_eq!(
335-
purpose,
336-
Uuid::parse_str("ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c").unwrap()
337-
);
338-
assert_eq!(1, metadata.role_data.len());
322+
let report = registration.consume().unwrap_err();
323+
assert!(report.is_problematic());
324+
let report = format!("{report:?}");
325+
assert!(report
326+
.contains("Role payment key reference index (1) is not found in transaction outputs"));
339327
}
340328

341329
#[test]
342330
fn block_4() {
343-
let block = test::block_4();
331+
let data = test::block_4();
344332

345-
let mut registrations = Cip509::from_block(&block, &[]);
333+
let mut registrations = Cip509::from_block(&data.block, &[]);
346334
assert_eq!(1, registrations.len());
347335

348336
let registration = registrations.pop().unwrap();
@@ -353,30 +341,24 @@ mod tests {
353341
);
354342
assert_eq!(
355343
registration.previous_transaction(),
356-
Some(
357-
Blake2b256Hash::from_str(
358-
"6695b9cac9230af5c8ee50747b1ca3c78a854d181c7e5c6c371de01b80274d31"
359-
)
360-
.unwrap()
361-
)
344+
Some(Blake2b256Hash::from_str(data.prv_hash.unwrap().as_str()).unwrap())
362345
);
363346

364347
let origin = registration.origin();
365-
assert_eq!(origin.txn_index(), 1.into());
366-
assert_eq!(origin.point().slot_or_default(), 77_436_369.into());
348+
assert_eq!(origin.txn_index(), data.txn_index);
349+
assert_eq!(origin.point().slot_or_default(), data.slot);
367350

368351
let (purpose, metadata, _) = registration.consume().unwrap();
369-
assert_eq!(
370-
purpose,
371-
Uuid::parse_str("ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c").unwrap()
372-
);
352+
assert_eq!(purpose, Uuid::parse_str(&data.purpose).unwrap());
373353
assert_eq!(1, metadata.role_data.len());
374354
}
375355

376356
#[test]
377357
fn extract_stake_addresses_from_metadata() {
378-
let block = test::block_1();
379-
let cip509 = Cip509::new(&block, 3.into(), &[]).unwrap().unwrap();
358+
let data = test::block_1();
359+
let cip509 = Cip509::new(&data.block, data.txn_index, &[])
360+
.unwrap()
361+
.unwrap();
380362
assert!(
381363
!cip509.report().is_problematic(),
382364
"Failed to decode Cip509: {:?}",

rust/rbac-registration/src/cardano/cip509/x509_chunks.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ mod tests {
137137
let mut report = ProblemReport::new("X509Chunks");
138138
// We don't care about actual values in the context, all we want is to check the decoding
139139
// of differently compressed data.
140-
let block = test::block_3();
141-
let transactions = block.txs();
140+
let data = test::block_3();
141+
let transactions = data.block.txs();
142142
let MultiEraTx::Conway(txn) = transactions.first().unwrap() else {
143143
panic!("Unexpected transaction type");
144144
};
@@ -170,8 +170,8 @@ mod tests {
170170
let mut report = ProblemReport::new("X509Chunks");
171171
// We don't care about actual values in the context, all we want is to check the decoding
172172
// of differently compressed data.
173-
let block = test::block_3();
174-
let transactions = block.txs();
173+
let data = test::block_3();
174+
let transactions = data.block.txs();
175175
let MultiEraTx::Conway(txn) = transactions.first().unwrap() else {
176176
panic!("Unexpected transaction type");
177177
};
@@ -203,8 +203,8 @@ mod tests {
203203
let mut report = ProblemReport::new("X509Chunks");
204204
// We don't care about actual values in the context, all we want is to check the decoding
205205
// of differently compressed data.
206-
let block = test::block_3();
207-
let transactions = block.txs();
206+
let data = test::block_3();
207+
let transactions = data.block.txs();
208208
let MultiEraTx::Conway(txn) = transactions.first().unwrap() else {
209209
panic!("Unexpected transaction type");
210210
};

rust/rbac-registration/src/registration/cardano/mod.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ impl RegistrationChainInner {
202202
// Previous transaction ID in the CIP509 should equal to the current transaction ID
203203
// or else it is not a part of the chain
204204
if prv_tx_id == self.current_tx_id_hash {
205-
new_inner.current_tx_id_hash = prv_tx_id;
205+
// Update the current transaction ID hash
206+
new_inner.current_tx_id_hash = cip509.txn_hash();
206207
} else {
207208
bail!("Invalid previous transaction ID, not a part of this registration chain");
208209
}
@@ -415,8 +416,10 @@ mod test {
415416

416417
#[test]
417418
fn multiple_registrations() {
418-
let block = test::block_1();
419-
let registration = Cip509::new(&block, 3.into(), &[]).unwrap().unwrap();
419+
let data = test::block_1();
420+
let registration = Cip509::new(&data.block, data.txn_index, &[])
421+
.unwrap()
422+
.unwrap();
420423
assert!(
421424
!registration.report().is_problematic(),
422425
"{:#?}",
@@ -425,18 +428,17 @@ mod test {
425428

426429
// Create a chain with the first registration.
427430
let chain = RegistrationChain::new(registration).unwrap();
428-
assert_eq!(chain.purpose(), &[Uuid::parse_str(
429-
"ca7a1457-ef9f-4c7f-9c74-7f8c4a4cfa6c"
430-
)
431-
.unwrap()]);
431+
assert_eq!(chain.purpose(), &[Uuid::parse_str(&data.purpose).unwrap()]);
432432
assert_eq!(1, chain.x509_certs().len());
433433
let origin = &chain.x509_certs().get(&0).unwrap().0;
434-
assert_eq!(origin.point().slot_or_default(), 77_429_134.into());
435-
assert_eq!(origin.txn_index(), 3.into());
434+
assert_eq!(origin.point().slot_or_default(), data.slot);
435+
assert_eq!(origin.txn_index(), data.txn_index);
436436

437437
// Try to add an invalid registration.
438-
let block = test::block_2();
439-
let registration = Cip509::new(&block, 0.into(), &[]).unwrap().unwrap();
438+
let data = test::block_2();
439+
let registration = Cip509::new(&data.block, data.txn_index, &[])
440+
.unwrap()
441+
.unwrap();
440442
assert!(registration.report().is_problematic());
441443

442444
let error = chain.update(registration).unwrap_err();
@@ -448,13 +450,21 @@ mod test {
448450
);
449451

450452
// Add the second registration.
451-
let block = test::block_4();
452-
let registration = Cip509::new(&block, 1.into(), &[]).unwrap().unwrap();
453+
let data = test::block_4();
454+
let registration = Cip509::new(&data.block, data.txn_index, &[])
455+
.unwrap()
456+
.unwrap();
453457
assert!(
454458
!registration.report().is_problematic(),
455459
"{:#?}",
456460
registration.report()
457461
);
458-
chain.update(registration).unwrap();
462+
let update = chain.update(registration).unwrap();
463+
464+
// Current tx hash should updated to RBAC data in block 4
465+
assert_eq!(update.current_tx_id_hash().to_string(), data.tx_hash);
466+
assert!(update
467+
.role_data()
468+
.contains_key(&RoleNumber::from(data.role)));
459469
}
460470
}

0 commit comments

Comments
 (0)