From 8f0222328f91f2c6e2593aaa6a48307aee21587b Mon Sep 17 00:00:00 2001 From: Noor Date: Fri, 4 Apr 2025 13:13:01 +0000 Subject: [PATCH] feat: decompression add bincode support for serialization and deserialization implement directory decompression tests for decompression --- Cargo.lock | 33 +++++++++++++++++++++++++++++++ Cargo.toml | 1 + build.rs | 4 ++++ proto/sgx/dtpm.proto | 2 +- src/sgx/types/dtpm.rs | 11 +++++++++-- tests/dtpm-config_test.rs | 35 +++++++++++++++++++++++---------- tests/fixtures/dtpm_config.yaml | 2 +- 7 files changed, 74 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 58ca45d..7db0592 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -145,6 +145,26 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "bincode" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36eaf5d7b090263e8150820482d5d93cd964a81e4019913c972f4edcc6edb740" +dependencies = [ + "bincode_derive", + "serde", + "unty", +] + +[[package]] +name = "bincode_derive" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf95709a440f45e986983918d0e8a1f30a9b1df04918fc828670606804ac3c09" +dependencies = [ + "virtue", +] + [[package]] name = "bitflags" version = "2.6.0" @@ -185,6 +205,7 @@ name = "detee-shared" version = "0.1.0" dependencies = [ "base64", + "bincode", "prost", "serde", "serde_yaml", @@ -1147,6 +1168,18 @@ version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" +[[package]] +name = "unty" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d49784317cd0d1ee7ec5c716dd598ec5b4483ea832a2dced265471cc0f690ae" + +[[package]] +name = "virtue" +version = "0.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "051eb1abcf10076295e815102942cc58f9d5e3b4560e46e53c21e8ff6f3af7b1" + [[package]] name = "want" version = "0.3.1" diff --git a/Cargo.toml b/Cargo.toml index 1b4a8d9..379a588 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ thiserror = "2.0.11" tonic = "0.12.3" tar = "0.4.44" zstd = "0.13.3" +bincode = "2.0.1" [build-dependencies] tonic-build = "0.12.3" diff --git a/build.rs b/build.rs index 16bb3cd..d9e8a0c 100644 --- a/build.rs +++ b/build.rs @@ -34,6 +34,10 @@ fn main() -> Result<(), Box> { ".vm_proto.VmNodeListResp", "#[derive(serde::Serialize, serde::Deserialize)]", ) + .type_attribute( + ".dtpm_proto.FileEntry", + "#[derive(serde::Serialize, serde::Deserialize, bincode::Encode, bincode::Decode)]", + ) .compile_protos( &[ "proto/sgx/app.proto", diff --git a/proto/sgx/dtpm.proto b/proto/sgx/dtpm.proto index 200b5d2..084a90f 100644 --- a/proto/sgx/dtpm.proto +++ b/proto/sgx/dtpm.proto @@ -53,7 +53,7 @@ message DtpmGetConfigRes { } service DtpmConfigManager { - rpc SetConfig(DtpmSetConfigReq) returns (DtpmSetConfigRes) {} rpc UploadFiles(stream FileEntry) returns (common_proto.Empty) {} + rpc SetConfig(DtpmSetConfigReq) returns (DtpmSetConfigRes) {} rpc GetConfig(common_proto.Empty) returns (DtpmGetConfigRes) {} } \ No newline at end of file diff --git a/src/sgx/types/dtpm.rs b/src/sgx/types/dtpm.rs index c3d7212..961d117 100644 --- a/src/sgx/types/dtpm.rs +++ b/src/sgx/types/dtpm.rs @@ -2,8 +2,8 @@ use crate::sgx::pb::dtpm_proto; use base64::{engine::general_purpose::STANDARD as BASE64, Engine}; use serde::{Deserialize, Serialize}; use std::path::Path; -use tar::Builder; -use zstd::Encoder; +use tar::{Archive, Builder}; +use zstd::{Decoder, Encoder}; #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct DtpmConfig { @@ -234,3 +234,10 @@ pub fn compress_directory(input_dir: &str) -> Result> { Ok(compressed_data) } + +pub fn decompress_directory(save_path: &str, archive_buff: Vec) -> Result<()> { + let mut archive = Archive::new(Decoder::new(archive_buff.as_slice())?); + + archive.unpack(save_path)?; + Ok(()) +} diff --git a/tests/dtpm-config_test.rs b/tests/dtpm-config_test.rs index 44bfd91..3cbca31 100644 --- a/tests/dtpm-config_test.rs +++ b/tests/dtpm-config_test.rs @@ -1,4 +1,14 @@ -use detee_shared::sgx::types::dtpm::{compress_directory, DtpmConfig}; +use detee_shared::sgx::types::dtpm::{compress_directory, decompress_directory, DtpmConfig}; + +use std::sync::LazyLock; + +static TEMP_DIR: LazyLock = LazyLock::new(|| { + tempfile::tempdir() + .unwrap() + .into_path() + .to_string_lossy() + .to_string() +}); #[test] fn dtpm_config_dir_support_test() { @@ -12,21 +22,26 @@ fn dtpm_config_dir_support_test() { #[test] fn test_compression() { - // let file_path = "/Users/user/.cache/hunter/toolchain"; let file_path = "./tests"; let compressed_buff = compress_directory(file_path).unwrap(); - let tmp_dir = tempfile::tempdir() - .unwrap() - .into_path() - .to_string_lossy() - .to_string(); - - dbg!(&tmp_dir); + dbg!(&TEMP_DIR); std::fs::write( - format!("{}/{}", tmp_dir, "archive.tar.zst"), + format!("{}/{}", *TEMP_DIR, "archive.tar.zst"), &compressed_buff, ) .unwrap(); } + +#[test] +fn test_decompression_02() { + let file_path = "./tests"; + let archive_buff = compress_directory(file_path).unwrap(); + + decompress_directory(&TEMP_DIR, archive_buff).unwrap(); + + let path = format!("{}/{}", *TEMP_DIR, "fixtures/dtpm_config.yaml"); + dbg!(&path); + assert!(std::path::Path::new(&path).exists()); +} diff --git a/tests/fixtures/dtpm_config.yaml b/tests/fixtures/dtpm_config.yaml index 6594cee..39b0607 100644 --- a/tests/fixtures/dtpm_config.yaml +++ b/tests/fixtures/dtpm_config.yaml @@ -12,4 +12,4 @@ child_processes: policy: !OnNonZeroExit true filesystems: - path: /bin/actix-app-info - content: !path "/Users/user/tmp/actix-app-info/target/x86_64-unknown-linux-musl/release/actix-app-info" + content: !path "./tests"