Skip to content

Commit

Permalink
Change the output formation of ToString method of Uuid
Browse files Browse the repository at this point in the history
1) change the realization of Display trait of Uuid, make its formation
   const of 36 bytes.
2) introduce an env(SYS_BUILD_TYPE) in optee-utee-sys and optee-teec-sys
   so that developers can run unit tests on host machine(even x86)
3) add unit tests to ci
  • Loading branch information
ivila committed Nov 29, 2024
1 parent eb1ea70 commit c4fa2b8
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ jobs:
export OPTEE_DIR=$HOME
source environment

# Run unit tests
(cd optee-utee && SYS_BUILD_TYPE=unit_test cargo test --lib --features no_panic_handler -vv)
(cd optee-teec && SYS_BUILD_TYPE=unit_test cargo test --lib -vv)

# Build Rust optee-utee and optee-teec
(cd optee-utee && cargo build --target aarch64-unknown-linux-gnu -vv)
(cd optee-teec && cargo build --target aarch64-unknown-linux-gnu -vv)
Expand Down
15 changes: 15 additions & 0 deletions optee-teec/optee-teec-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ use std::env;
use std::path::Path;

fn main() {
const ENV_SYS_BUILD_TYPE: &str = "SYS_BUILD_TYPE";
println!("cargo:rerun-if-env-changed={}", ENV_SYS_BUILD_TYPE);

let build_type = env::var(ENV_SYS_BUILD_TYPE).unwrap_or(String::from("")).to_lowercase();
match build_type.as_str() {
"unit_test" => unit_test_build(),
_ => production_build(),
}
}

// this allow developers to run unit tests in host machine(even x86)
fn unit_test_build() {
}

fn production_build() {
let optee_client_dir = env::var("OPTEE_CLIENT_EXPORT").expect("OPTEE_CLIENT_EXPORT is not set");
let search_path = Path::new(&optee_client_dir).join("usr/lib");
println!("cargo:rustc-link-search={}", search_path.display());
Expand Down
25 changes: 23 additions & 2 deletions optee-teec/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,32 @@ impl fmt::Display for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{:x}-{:x}-{:x}-{}",
"{:08x}-{:04x}-{:04x}-{}-{}",
self.raw.timeLow,
self.raw.timeMid,
self.raw.timeHiAndVersion,
hex::encode(self.raw.clockSeqAndNode)
hex::encode(&self.raw.clockSeqAndNode[0..2]),
hex::encode(&self.raw.clockSeqAndNode[2..8]),
)
}
}

#[cfg(test)]
mod tests {
use super::Uuid;

#[test]
fn test_to_string() {
let uuids = [
"00173366-2aca-49bc-beb7-10c975e6131e", // uuid with timeLow leading zeros
"11173366-0aca-49bc-beb7-10c975e6131e", // uuid with timeMid leading zeros
"11173366-2aca-09bc-beb7-10c975e6131e", // uuid with timeHiAndVersion leading zeros
"11173366-2aca-19bc-beb7-10c975e6131e", // random uuid
];
for origin in uuids.iter() {
let uuid = Uuid::parse_str(origin).unwrap();
let formatted = uuid.to_string();
assert_eq!(origin, &formatted);
}
}
}
16 changes: 16 additions & 0 deletions optee-utee/optee-utee-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ use std::env;
use std::path::Path;

fn main() {
const ENV_SYS_BUILD_TYPE: &str = "SYS_BUILD_TYPE";
println!("cargo:rerun-if-env-changed={}", ENV_SYS_BUILD_TYPE);

let build_type = env::var(ENV_SYS_BUILD_TYPE).unwrap_or(String::from("")).to_lowercase();
match build_type.as_str() {
"unit_test" => unit_test_build(),
_ => production_build(),
}
}

// this allow developers to run unit tests in host machine by
// SYS_BUILD_TYPE=unit_test cargo test --lib --features no_panic_handler
fn unit_test_build() {
}

fn production_build() {
let optee_os_dir = env::var("TA_DEV_KIT_DIR").unwrap();
let search_path = Path::new(&optee_os_dir).join("lib");

Expand Down
27 changes: 25 additions & 2 deletions optee-utee/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,34 @@ impl fmt::Display for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{:x}-{:x}-{:x}-{}",
"{:08x}-{:04x}-{:04x}-{}-{}",
self.raw.timeLow,
self.raw.timeMid,
self.raw.timeHiAndVersion,
hex::encode(self.raw.clockSeqAndNode)
hex::encode(&self.raw.clockSeqAndNode[0..2]),
hex::encode(&self.raw.clockSeqAndNode[2..8]),
)
}
}

#[cfg(test)]
mod tests {
extern crate alloc;
use super::*;
use alloc::string::ToString;

#[test]
fn test_to_string() {
let uuids = [
"00173366-2aca-49bc-beb7-10c975e6131e", // uuid with timeLow leading zeros
"11173366-0aca-49bc-beb7-10c975e6131e", // uuid with timeMid leading zeros
"11173366-2aca-09bc-beb7-10c975e6131e", // uuid with timeHiAndVersion leading zeros
"11173366-2aca-19bc-beb7-10c975e6131e", // random uuid
];
for origin in uuids.iter() {
let uuid = Uuid::parse_str(origin).unwrap();
let formatted = uuid.to_string();
assert_eq!(origin, &formatted);
}
}
}

0 comments on commit c4fa2b8

Please sign in to comment.