Skip to content

Commit

Permalink
capsules: update test to provide error
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Feb 20, 2024
1 parent 9744072 commit 579d381
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
46 changes: 36 additions & 10 deletions capsules/extra/src/test/hmac_sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use crate::hmac_sha256::HmacSha256Software;
use crate::sha256::Sha256Software;
use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient};
use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient, CapsuleTestError};
use kernel::hil::digest;
use kernel::hil::digest::HmacSha256;
use kernel::hil::digest::{DigestData, DigestHash};
Expand Down Expand Up @@ -66,28 +66,54 @@ impl digest::ClientData<32> for TestHmacSha256 {
unimplemented!()
}

fn add_mut_data_done(&self, _result: Result<(), ErrorCode>, data: SubSliceMut<'static, u8>) {
fn add_mut_data_done(&self, result: Result<(), ErrorCode>, data: SubSliceMut<'static, u8>) {
self.data.replace(data.take());

match result {
Ok(()) => {}
Err(e) => {
kernel::debug!("HmacSha256Test: failed to add data: {:?}", e);
self.client.map(|client| {
client.done(Err(CapsuleTestError::ErrorCode(e)));
});
return;
}
}

let r = self.hmac.run(self.digest.take().unwrap());
if r.is_err() {
panic!("HmacSha256Test: failed to run HMAC: {:?}", r);
match r {
Ok(()) => {}
Err((e, d)) => {
kernel::debug!("HmacSha256Test: failed to run HMAC: {:?}", e);

self.digest.replace(d);
self.client.map(|client| {
client.done(Err(CapsuleTestError::ErrorCode(e)));
});
}
}
}
}

impl digest::ClientHash<32> for TestHmacSha256 {
fn hash_done(&self, _result: Result<(), ErrorCode>, digest: &'static mut [u8; 32]) {
let mut error = false;
for i in 0..32 {
if self.correct[i] != digest[i] {
panic!("HmacSha256Test: incorrect HMAC output!");
error = true;
}
}
kernel::debug!("HMAC-SHA256 matches!");

self.client.map(|client| {
client.done(Ok(()));
});
if !error {
kernel::debug!("HMAC-SHA256 matches!");
self.client.map(|client| {
client.done(Ok(()));
});
} else {
kernel::debug!("HmacSha256Test: incorrect HMAC output!");
self.client.map(|client| {
client.done(Err(CapsuleTestError::IncorrectResult));
});
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions capsules/extra/src/test/siphash24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! Digest trait.

use crate::sip_hash::SipHasher24;
use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient};
use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient, CapsuleTestError};
use kernel::hil::hasher::{Client, Hasher};
use kernel::utilities::cells::OptionalCell;
use kernel::utilities::cells::TakeCell;
Expand Down Expand Up @@ -74,7 +74,11 @@ impl Client<8> for TestSipHash24 {
self.hasher.clear_data();

self.client.map(|client| {
let res = if matches { Ok(()) } else { Err(()) };
let res = if matches {
Ok(())
} else {
Err(CapsuleTestError::IncorrectResult)
};
client.done(res);
});
}
Expand Down

0 comments on commit 579d381

Please sign in to comment.