code tidy

This commit is contained in:
Tiago Batista Cardoso
2026-01-25 14:22:20 +01:00
parent 55a0eb21bb
commit 15bfbcd0d4
12 changed files with 124 additions and 537 deletions

View File

@@ -1,14 +1,9 @@
use rand::{Rng, rng};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::fs::{File, OpenOptions, create_dir};
use std::io::{self, Write};
use std::env;
use crate::data;
use std::io::Write;
// --- Constants ---
pub const MAX_CHUNK_DATA_SIZE: usize = 1024;
@@ -345,9 +340,9 @@ pub fn generate_base_tree() -> MerkleTree {
let oscar_content = "oscar is the opponent".to_string().into_bytes();
let mut children_nodes = Vec::new();
for i in 0..10 {
for _ in 0..10 {
let mut i_nodes = Vec::new();
for j in 0..10 {
for _ in 0..10 {
let node1 = MerkleNode::Chunk(ChunkNode::new(bob_content.clone()).unwrap());
let hash = hash(&node1.serialize());
i_nodes.push(hash);
@@ -368,14 +363,14 @@ pub fn generate_base_tree() -> MerkleTree {
let node2 = MerkleNode::Chunk(ChunkNode::new(alice_content).unwrap());
let hash2 = hash(&node2.serialize());
//res.insert(hash1, node1);
//res.insert(hash2, node2);
res.insert(hash1, node1);
res.insert(hash2, node2);
res.insert(hashbig, bignode);
let node3 = MerkleNode::Chunk(ChunkNode::new(oscar_content).unwrap());
let hash3 = hash(&node3.serialize());
//res.insert(hash3, node3);
res.insert(hash3, node3);
let dir1 = MerkleNode::Directory(DirectoryNode {
entries: [DirectoryEntry {
@@ -386,7 +381,7 @@ pub fn generate_base_tree() -> MerkleTree {
});
let hash_dir1 = hash(&dir1.serialize());
//res.insert(hash_dir1, dir1);
res.insert(hash_dir1, dir1);
let root = MerkleNode::Directory(DirectoryNode {
entries: [
@@ -394,14 +389,14 @@ pub fn generate_base_tree() -> MerkleTree {
filename: generate_random_filename(),
content_hash: hashbig,
},
/*DirectoryEntry {
DirectoryEntry {
filename: generate_random_filename(),
content_hash: hash2,
},
DirectoryEntry {
filename: generate_random_filename(),
content_hash: hash_dir1,
},*/
},
]
.to_vec(),
});
@@ -486,42 +481,3 @@ pub fn big_or_chunk_to_file(tree: &MerkleTree, node: &MerkleNode, file: &mut Fil
}
}
}
#[cfg(test)]
mod tests {
use super::*;
///
/// creates a cryptographic signature
///
#[test]
fn test_saving_tree() {
if let Ok(current_dir) = env::current_dir() {
println!("Current working directory: {:?}", current_dir);
}
println!("--------- tree test starts ------------");
match create_dir("../Download/") {
Ok(_) => println!("Directory created successfully!"),
Err(e) => println!("Failed to create directory: {}", e),
}
let tree = generate_base_tree();
println!("--------- test tree created ------------");
if let Some(root_node) = tree.data.get(&tree.root) {
node_to_file(&tree, root_node, "../Download/".to_string(), 0);
}
}
/*#[test]
fn signing_message() {
let username = String::from("gamixtreize");
let crypto_pair = CryptographicSignature::new(username.clone());
let handshake = HandshakeMessage::hello(0, 12, username);
let ser = handshake.serialize();
let signed_message = sign_message(&crypto_pair, &ser);
println!("unsigned_message: {:?}", ser);
println!("signed_message: {:?}", signed_message);
}*/
}