Skip to content

Commit

Permalink
chore: fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jul 14, 2024
1 parent 7e7439a commit 7985015
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 51 deletions.
45 changes: 17 additions & 28 deletions src/ic_oss_bucket/src/api_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,48 +66,37 @@ impl UpgradeArgs {

#[ic_cdk::init]
fn init(args: Option<CanisterArgs>) {
match args.expect("Init args is missing") {
CanisterArgs::Init(args) => {
match args {
Some(CanisterArgs::Init(args)) => {
store::state::with_mut(|b| {
b.name = if args.name.is_empty() {
"default".to_string()
} else {
args.name
if !args.name.is_empty() {
b.name = args.name
};
b.file_id = args.file_id;
b.max_file_size = if args.max_file_size == 0 {
MAX_FILE_SIZE
} else {
args.max_file_size
if args.max_file_size > 0 {
b.max_file_size = args.max_file_size
};
b.max_folder_depth = if args.max_folder_depth == 0 {
10
} else {
args.max_folder_depth
if args.max_folder_depth > 0 {
b.max_folder_depth = args.max_folder_depth
};
b.max_children = if args.max_children == 0 {
1000
} else {
args.max_children
if args.max_children > 0 {
b.max_children = args.max_children
};
b.visibility = if args.visibility == 0 { 0 } else { 1 };
b.max_custom_data_size = if args.max_custom_data_size == 0 {
1024 * 4
} else {
args.max_custom_data_size
if args.visibility > 0 {
b.visibility = 1
};
if args.max_custom_data_size > 0 {
b.max_custom_data_size = args.max_custom_data_size
};
b.enable_hash_index = args.enable_hash_index;

// The root folder 0 is created by default
b.folder_id = 1;
b.folder_count = 1;
});
}
CanisterArgs::Upgrade(_) => {
Some(CanisterArgs::Upgrade(_)) => {
ic_cdk::trap(
"Cannot initialize the canister with an Upgrade args. Please provide an Init args.",
);
}
None => {}
}

store::state::save();
Expand Down
63 changes: 40 additions & 23 deletions src/ic_oss_bucket/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::{

type Memory = VirtualMemory<DefaultMemoryImpl>;

#[derive(Clone, Default, Deserialize, Serialize)]
#[derive(Clone, Deserialize, Serialize)]
pub struct Bucket {
pub name: String,
pub file_count: u64,
Expand All @@ -54,6 +54,29 @@ pub struct Bucket {
pub trusted_eddsa_pub_keys: Vec<ByteN<32>>,
}

impl Default for Bucket {
fn default() -> Self {
Self {
name: "default".to_string(),
file_count: 0,
file_id: 0,
folder_count: 1, // The root folder 0 is created by default
folder_id: 1,
max_file_size: MAX_FILE_SIZE,
max_folder_depth: 10,
max_children: 100,
max_custom_data_size: 1024 * 4,
enable_hash_index: false,
status: 0,
visibility: 0,
managers: BTreeSet::new(),
auditors: BTreeSet::new(),
trusted_ecdsa_pub_keys: Vec::new(),
trusted_eddsa_pub_keys: Vec::new(),
}
}
}

impl Bucket {
pub fn read_permission(
&self,
Expand Down Expand Up @@ -1267,10 +1290,6 @@ mod test {
#[test]
fn test_fs() {
state::with_mut(|b| {
b.name = "default".to_string();
b.max_file_size = MAX_FILE_SIZE;
b.max_folder_depth = 10;
b.max_children = 1000;
b.enable_hash_index = true;
});

Expand All @@ -1284,16 +1303,14 @@ mod test {
..Default::default()
})
.unwrap();
assert_eq!(f1, 1);
assert_eq!(f1, 0);

assert!(fs::get_full_chunks(0).is_err());
let f1_data = fs::get_full_chunks(f1).unwrap();
assert!(f1_data.is_empty());

let f1_meta = fs::get_file(f1).unwrap();
assert_eq!(f1_meta.name, "f1.bin");

assert!(fs::update_chunk(0, 0, 999, [0u8; 32].to_vec(), |_| Ok(())).is_err());
let _ = fs::update_chunk(f1, 0, 999, [0u8; 32].to_vec(), |_| Ok(())).unwrap();
let _ = fs::update_chunk(f1, 1, 1000, [0u8; 32].to_vec(), |_| Ok(())).unwrap();
let f1_data = fs::get_full_chunks(f1).unwrap();
Expand All @@ -1318,7 +1335,7 @@ mod test {
..Default::default()
})
.unwrap();
assert_eq!(f2, 2);
assert_eq!(f2, 1);
fs::update_chunk(f2, 0, 999, [0u8; 16].to_vec(), |_| Ok(())).unwrap();
fs::update_chunk(f2, 1, 1000, [1u8; 16].to_vec(), |_| Ok(())).unwrap();
fs::update_chunk(f1, 3, 1000, [1u8; 16].to_vec(), |_| Ok(())).unwrap();
Expand Down Expand Up @@ -1360,7 +1377,7 @@ mod test {
.into_iter()
.map(|v| v.id)
.collect::<Vec<_>>(),
vec![2, 1]
vec![f2, f1]
);

assert_eq!(
Expand Down Expand Up @@ -1391,9 +1408,9 @@ mod test {
vec![2, 1]
);

fs::move_file(1, 0, 1, 1000).unwrap();
fs::move_file(f1, 0, 1, 1000).unwrap();
assert_eq!(
fs::get_file_ancestors(1),
fs::get_file_ancestors(f1),
vec![FolderName {
id: 1,
name: "fd1".to_string(),
Expand All @@ -1404,19 +1421,19 @@ mod test {
.into_iter()
.map(|v| v.id)
.collect::<Vec<_>>(),
vec![2]
vec![f2]
);
assert_eq!(
fs::list_files(1, 999, 999)
.into_iter()
.map(|v| v.id)
.collect::<Vec<_>>(),
vec![1]
vec![f1]
);

fs::move_file(2, 0, 2, 1000).unwrap();
fs::move_file(f2, 0, 2, 1000).unwrap();
assert_eq!(
fs::get_file_ancestors(2),
fs::get_file_ancestors(f2),
vec![FolderName {
id: 2,
name: "fd2".to_string(),
Expand All @@ -1434,7 +1451,7 @@ mod test {
.into_iter()
.map(|v| v.id)
.collect::<Vec<_>>(),
vec![2]
vec![f2]
);

fs::move_folder(2, 0, 1, 1000).unwrap();
Expand All @@ -1446,7 +1463,7 @@ mod test {
}]
);
assert_eq!(
fs::get_file_ancestors(2),
fs::get_file_ancestors(f2),
vec![
FolderName {
id: 2,
Expand All @@ -1460,15 +1477,15 @@ mod test {
);

assert_eq!(
fs::batch_delete_subfiles(0, BTreeSet::from([1, 2]), 999).unwrap(),
fs::batch_delete_subfiles(0, BTreeSet::from([f1, f2]), 999).unwrap(),
Vec::<u32>::new()
);

fs::move_file(1, 1, 0, 1000).unwrap();
fs::move_file(2, 2, 0, 1000).unwrap();
fs::move_file(f1, 1, 0, 1000).unwrap();
fs::move_file(f2, 2, 0, 1000).unwrap();
assert_eq!(
fs::batch_delete_subfiles(0, BTreeSet::from([2, 1]), 999).unwrap(),
vec![1, 2]
fs::batch_delete_subfiles(0, BTreeSet::from([f2, f1]), 999).unwrap(),
vec![f1, f2]
);
assert!(fs::delete_folder(1, 999, |_| Ok(())).is_err());
assert!(fs::delete_folder(2, 999, |_| Ok(())).unwrap());
Expand Down

0 comments on commit 7985015

Please sign in to comment.