feat: decompression
add bincode support for serialization and deserialization implement directory decompression tests for decompression
This commit is contained in:
		
							parent
							
								
									cf1bc1143c
								
							
						
					
					
						commit
						8f0222328f
					
				
							
								
								
									
										33
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										33
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @ -145,6 +145,26 @@ version = "0.22.1" | |||||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
| checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" | 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]] | [[package]] | ||||||
| name = "bitflags" | name = "bitflags" | ||||||
| version = "2.6.0" | version = "2.6.0" | ||||||
| @ -185,6 +205,7 @@ name = "detee-shared" | |||||||
| version = "0.1.0" | version = "0.1.0" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "base64", |  "base64", | ||||||
|  |  "bincode", | ||||||
|  "prost", |  "prost", | ||||||
|  "serde", |  "serde", | ||||||
|  "serde_yaml", |  "serde_yaml", | ||||||
| @ -1147,6 +1168,18 @@ version = "0.2.11" | |||||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
| checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" | 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]] | [[package]] | ||||||
| name = "want" | name = "want" | ||||||
| version = "0.3.1" | version = "0.3.1" | ||||||
|  | |||||||
| @ -12,6 +12,7 @@ thiserror = "2.0.11" | |||||||
| tonic = "0.12.3" | tonic = "0.12.3" | ||||||
| tar = "0.4.44" | tar = "0.4.44" | ||||||
| zstd = "0.13.3" | zstd = "0.13.3" | ||||||
|  | bincode = "2.0.1" | ||||||
| 
 | 
 | ||||||
| [build-dependencies] | [build-dependencies] | ||||||
| tonic-build = "0.12.3" | tonic-build = "0.12.3" | ||||||
|  | |||||||
							
								
								
									
										4
									
								
								build.rs
									
									
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										4
									
								
								build.rs
									
									
									
									
									
								
							| @ -34,6 +34,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { | |||||||
|             ".vm_proto.VmNodeListResp", |             ".vm_proto.VmNodeListResp", | ||||||
|             "#[derive(serde::Serialize, serde::Deserialize)]", |             "#[derive(serde::Serialize, serde::Deserialize)]", | ||||||
|         ) |         ) | ||||||
|  |         .type_attribute( | ||||||
|  |             ".dtpm_proto.FileEntry", | ||||||
|  |             "#[derive(serde::Serialize, serde::Deserialize, bincode::Encode, bincode::Decode)]", | ||||||
|  |         ) | ||||||
|         .compile_protos( |         .compile_protos( | ||||||
|             &[ |             &[ | ||||||
|                 "proto/sgx/app.proto", |                 "proto/sgx/app.proto", | ||||||
|  | |||||||
| @ -53,7 +53,7 @@ message DtpmGetConfigRes { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| service DtpmConfigManager { | service DtpmConfigManager { | ||||||
|   rpc SetConfig(DtpmSetConfigReq) returns (DtpmSetConfigRes) {} |  | ||||||
|   rpc UploadFiles(stream FileEntry) returns (common_proto.Empty) {} |   rpc UploadFiles(stream FileEntry) returns (common_proto.Empty) {} | ||||||
|  |   rpc SetConfig(DtpmSetConfigReq) returns (DtpmSetConfigRes) {} | ||||||
|   rpc GetConfig(common_proto.Empty) returns (DtpmGetConfigRes) {} |   rpc GetConfig(common_proto.Empty) returns (DtpmGetConfigRes) {} | ||||||
| } | } | ||||||
| @ -2,8 +2,8 @@ use crate::sgx::pb::dtpm_proto; | |||||||
| use base64::{engine::general_purpose::STANDARD as BASE64, Engine}; | use base64::{engine::general_purpose::STANDARD as BASE64, Engine}; | ||||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||||
| use std::path::Path; | use std::path::Path; | ||||||
| use tar::Builder; | use tar::{Archive, Builder}; | ||||||
| use zstd::Encoder; | use zstd::{Decoder, Encoder}; | ||||||
| 
 | 
 | ||||||
| #[derive(Debug, Clone, Serialize, Deserialize, Default)] | #[derive(Debug, Clone, Serialize, Deserialize, Default)] | ||||||
| pub struct DtpmConfig { | pub struct DtpmConfig { | ||||||
| @ -234,3 +234,10 @@ pub fn compress_directory(input_dir: &str) -> Result<Vec<u8>> { | |||||||
| 
 | 
 | ||||||
|     Ok(compressed_data) |     Ok(compressed_data) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | pub fn decompress_directory(save_path: &str, archive_buff: Vec<u8>) -> Result<()> { | ||||||
|  |     let mut archive = Archive::new(Decoder::new(archive_buff.as_slice())?); | ||||||
|  | 
 | ||||||
|  |     archive.unpack(save_path)?; | ||||||
|  |     Ok(()) | ||||||
|  | } | ||||||
|  | |||||||
| @ -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<String> = LazyLock::new(|| { | ||||||
|  |     tempfile::tempdir() | ||||||
|  |         .unwrap() | ||||||
|  |         .into_path() | ||||||
|  |         .to_string_lossy() | ||||||
|  |         .to_string() | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| #[test] | #[test] | ||||||
| fn dtpm_config_dir_support_test() { | fn dtpm_config_dir_support_test() { | ||||||
| @ -12,21 +22,26 @@ fn dtpm_config_dir_support_test() { | |||||||
| 
 | 
 | ||||||
| #[test] | #[test] | ||||||
| fn test_compression() { | fn test_compression() { | ||||||
|     // let file_path = "/Users/user/.cache/hunter/toolchain";
 |  | ||||||
|     let file_path = "./tests"; |     let file_path = "./tests"; | ||||||
|     let compressed_buff = compress_directory(file_path).unwrap(); |     let compressed_buff = compress_directory(file_path).unwrap(); | ||||||
| 
 | 
 | ||||||
|     let tmp_dir = tempfile::tempdir() |     dbg!(&TEMP_DIR); | ||||||
|         .unwrap() |  | ||||||
|         .into_path() |  | ||||||
|         .to_string_lossy() |  | ||||||
|         .to_string(); |  | ||||||
| 
 |  | ||||||
|     dbg!(&tmp_dir); |  | ||||||
| 
 | 
 | ||||||
|     std::fs::write( |     std::fs::write( | ||||||
|         format!("{}/{}", tmp_dir, "archive.tar.zst"), |         format!("{}/{}", *TEMP_DIR, "archive.tar.zst"), | ||||||
|         &compressed_buff, |         &compressed_buff, | ||||||
|     ) |     ) | ||||||
|     .unwrap(); |     .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()); | ||||||
|  | } | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								tests/fixtures/dtpm_config.yaml
									
									
									
									
										vendored
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										2
									
								
								tests/fixtures/dtpm_config.yaml
									
									
									
									
										vendored
									
									
								
							| @ -12,4 +12,4 @@ child_processes: | |||||||
|       policy: !OnNonZeroExit true |       policy: !OnNonZeroExit true | ||||||
| filesystems: | filesystems: | ||||||
|   - path: /bin/actix-app-info |   - 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" | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user