diff options
| author | Badr Bouslikhin <[email protected]> | 2024-02-11 20:17:15 +0100 |
|---|---|---|
| committer | Badr Bouslikhin <[email protected]> | 2024-02-11 20:17:15 +0100 |
| commit | 333b2afe6d5319317b2679e634c3cf242bab0e73 (patch) | |
| tree | 40b71a570ba626c0eed949428ec542a512702aa9 /embassy-boot | |
| parent | eb3bd39b068ae34892520ec38f111704ea357355 (diff) | |
test(boot): add various write firmware test configurations
Diffstat (limited to 'embassy-boot')
| -rw-r--r-- | embassy-boot/src/firmware_updater/asynch.rs | 72 | ||||
| -rw-r--r-- | embassy-boot/src/firmware_updater/blocking.rs | 78 |
2 files changed, 150 insertions, 0 deletions
diff --git a/embassy-boot/src/firmware_updater/asynch.rs b/embassy-boot/src/firmware_updater/asynch.rs index 99a3aa246..d31eff005 100644 --- a/embassy-boot/src/firmware_updater/asynch.rs +++ b/embassy-boot/src/firmware_updater/asynch.rs | |||
| @@ -382,4 +382,76 @@ mod tests { | |||
| 382 | 382 | ||
| 383 | assert_eq!(Sha1::digest(update).as_slice(), hash); | 383 | assert_eq!(Sha1::digest(update).as_slice(), hash); |
| 384 | } | 384 | } |
| 385 | |||
| 386 | #[test] | ||
| 387 | fn can_verify_sha1_page_bigger_than_chunk() { | ||
| 388 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 4096, 8>::default()); | ||
| 389 | let state = Partition::new(&flash, 0, 4096); | ||
| 390 | let dfu = Partition::new(&flash, 65536, 65536); | ||
| 391 | let mut aligned = [0; 8]; | ||
| 392 | |||
| 393 | let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; | ||
| 394 | let mut to_write = [0; 4096]; | ||
| 395 | to_write[..7].copy_from_slice(update.as_slice()); | ||
| 396 | |||
| 397 | let mut updater = FirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }, &mut aligned); | ||
| 398 | let mut offset = 0; | ||
| 399 | for chunk in to_write.chunks(1024) { | ||
| 400 | block_on(updater.write_firmware(offset, chunk)).unwrap(); | ||
| 401 | offset += chunk.len(); | ||
| 402 | } | ||
| 403 | let mut chunk_buf = [0; 2]; | ||
| 404 | let mut hash = [0; 20]; | ||
| 405 | block_on(updater.hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash)).unwrap(); | ||
| 406 | |||
| 407 | assert_eq!(Sha1::digest(update).as_slice(), hash); | ||
| 408 | } | ||
| 409 | |||
| 410 | #[test] | ||
| 411 | fn can_verify_sha1_page_smaller_than_chunk() { | ||
| 412 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default()); | ||
| 413 | let state = Partition::new(&flash, 0, 4096); | ||
| 414 | let dfu = Partition::new(&flash, 65536, 65536); | ||
| 415 | let mut aligned = [0; 8]; | ||
| 416 | |||
| 417 | let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; | ||
| 418 | let mut to_write = [0; 4096]; | ||
| 419 | to_write[..7].copy_from_slice(update.as_slice()); | ||
| 420 | |||
| 421 | let mut updater = FirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }, &mut aligned); | ||
| 422 | let mut offset = 0; | ||
| 423 | for chunk in to_write.chunks(2048) { | ||
| 424 | block_on(updater.write_firmware(offset, chunk)).unwrap(); | ||
| 425 | offset += chunk.len(); | ||
| 426 | } | ||
| 427 | let mut chunk_buf = [0; 2]; | ||
| 428 | let mut hash = [0; 20]; | ||
| 429 | block_on(updater.hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash)).unwrap(); | ||
| 430 | |||
| 431 | assert_eq!(Sha1::digest(update).as_slice(), hash); | ||
| 432 | } | ||
| 433 | |||
| 434 | #[test] | ||
| 435 | fn can_verify_sha1_cross_page_boundary() { | ||
| 436 | let flash = Mutex::<NoopRawMutex, _>::new(MemFlash::<131072, 1024, 8>::default()); | ||
| 437 | let state = Partition::new(&flash, 0, 4096); | ||
| 438 | let dfu = Partition::new(&flash, 65536, 65536); | ||
| 439 | let mut aligned = [0; 8]; | ||
| 440 | |||
| 441 | let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; | ||
| 442 | let mut to_write = [0; 4096]; | ||
| 443 | to_write[..7].copy_from_slice(update.as_slice()); | ||
| 444 | |||
| 445 | let mut updater = FirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }, &mut aligned); | ||
| 446 | let mut offset = 0; | ||
| 447 | for chunk in to_write.chunks(896) { | ||
| 448 | block_on(updater.write_firmware(offset, chunk)).unwrap(); | ||
| 449 | offset += chunk.len(); | ||
| 450 | } | ||
| 451 | let mut chunk_buf = [0; 2]; | ||
| 452 | let mut hash = [0; 20]; | ||
| 453 | block_on(updater.hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash)).unwrap(); | ||
| 454 | |||
| 455 | assert_eq!(Sha1::digest(update).as_slice(), hash); | ||
| 456 | } | ||
| 385 | } | 457 | } |
diff --git a/embassy-boot/src/firmware_updater/blocking.rs b/embassy-boot/src/firmware_updater/blocking.rs index 45ae966f3..5b8076f81 100644 --- a/embassy-boot/src/firmware_updater/blocking.rs +++ b/embassy-boot/src/firmware_updater/blocking.rs | |||
| @@ -422,4 +422,82 @@ mod tests { | |||
| 422 | 422 | ||
| 423 | assert_eq!(Sha1::digest(update).as_slice(), hash); | 423 | assert_eq!(Sha1::digest(update).as_slice(), hash); |
| 424 | } | 424 | } |
| 425 | |||
| 426 | #[test] | ||
| 427 | fn can_verify_sha1_page_bigger_than_chunk() { | ||
| 428 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 4096, 8>::default())); | ||
| 429 | let state = BlockingPartition::new(&flash, 0, 4096); | ||
| 430 | let dfu = BlockingPartition::new(&flash, 65536, 65536); | ||
| 431 | let mut aligned = [0; 8]; | ||
| 432 | |||
| 433 | let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; | ||
| 434 | let mut to_write = [0; 4096]; | ||
| 435 | to_write[..7].copy_from_slice(update.as_slice()); | ||
| 436 | |||
| 437 | let mut updater = BlockingFirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }, &mut aligned); | ||
| 438 | let mut offset = 0; | ||
| 439 | for chunk in to_write.chunks(1024) { | ||
| 440 | updater.write_firmware(offset, chunk).unwrap(); | ||
| 441 | offset += chunk.len(); | ||
| 442 | } | ||
| 443 | let mut chunk_buf = [0; 2]; | ||
| 444 | let mut hash = [0; 20]; | ||
| 445 | updater | ||
| 446 | .hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash) | ||
| 447 | .unwrap(); | ||
| 448 | |||
| 449 | assert_eq!(Sha1::digest(update).as_slice(), hash); | ||
| 450 | } | ||
| 451 | |||
| 452 | #[test] | ||
| 453 | fn can_verify_sha1_page_smaller_than_chunk() { | ||
| 454 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default())); | ||
| 455 | let state = BlockingPartition::new(&flash, 0, 4096); | ||
| 456 | let dfu = BlockingPartition::new(&flash, 65536, 65536); | ||
| 457 | let mut aligned = [0; 8]; | ||
| 458 | |||
| 459 | let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; | ||
| 460 | let mut to_write = [0; 4096]; | ||
| 461 | to_write[..7].copy_from_slice(update.as_slice()); | ||
| 462 | |||
| 463 | let mut updater = BlockingFirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }, &mut aligned); | ||
| 464 | let mut offset = 0; | ||
| 465 | for chunk in to_write.chunks(2048) { | ||
| 466 | updater.write_firmware(offset, chunk).unwrap(); | ||
| 467 | offset += chunk.len(); | ||
| 468 | } | ||
| 469 | let mut chunk_buf = [0; 2]; | ||
| 470 | let mut hash = [0; 20]; | ||
| 471 | updater | ||
| 472 | .hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash) | ||
| 473 | .unwrap(); | ||
| 474 | |||
| 475 | assert_eq!(Sha1::digest(update).as_slice(), hash); | ||
| 476 | } | ||
| 477 | |||
| 478 | #[test] | ||
| 479 | fn can_verify_sha1_cross_page_boundary() { | ||
| 480 | let flash = Mutex::<NoopRawMutex, _>::new(RefCell::new(MemFlash::<131072, 1024, 8>::default())); | ||
| 481 | let state = BlockingPartition::new(&flash, 0, 4096); | ||
| 482 | let dfu = BlockingPartition::new(&flash, 65536, 65536); | ||
| 483 | let mut aligned = [0; 8]; | ||
| 484 | |||
| 485 | let update = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66]; | ||
| 486 | let mut to_write = [0; 4096]; | ||
| 487 | to_write[..7].copy_from_slice(update.as_slice()); | ||
| 488 | |||
| 489 | let mut updater = BlockingFirmwareUpdater::new(FirmwareUpdaterConfig { dfu, state }, &mut aligned); | ||
| 490 | let mut offset = 0; | ||
| 491 | for chunk in to_write.chunks(896) { | ||
| 492 | updater.write_firmware(offset, chunk).unwrap(); | ||
| 493 | offset += chunk.len(); | ||
| 494 | } | ||
| 495 | let mut chunk_buf = [0; 2]; | ||
| 496 | let mut hash = [0; 20]; | ||
| 497 | updater | ||
| 498 | .hash::<Sha1>(update.len() as u32, &mut chunk_buf, &mut hash) | ||
| 499 | .unwrap(); | ||
| 500 | |||
| 501 | assert_eq!(Sha1::digest(update).as_slice(), hash); | ||
| 502 | } | ||
| 425 | } | 503 | } |
