From 713843d7722695a05d63833f2165587acd547469 Mon Sep 17 00:00:00 2001 From: Abid Omar Date: Thu, 31 Aug 2023 17:48:08 +0700 Subject: [PATCH] imp: add unit tests --- src/lib.rs | 2 +- tests/unit.rs | 236 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 231 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 199b127..741c5cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,7 +100,7 @@ pub fn convert_uint256_to_cid_v1(uint256: String) -> Result { bytes.insert(1, 0x20); let multihash = cid::multihash::Multihash::from_bytes(&bytes)?; - let cid = cid::Cid::new_v1(64, multihash); + let cid = cid::Cid::new_v1(0x70, multihash); Ok(cid.to_string()) } diff --git a/tests/unit.rs b/tests/unit.rs index 53cb247..1a59d77 100644 --- a/tests/unit.rs +++ b/tests/unit.rs @@ -1,7 +1,11 @@ -use ipfs_cid::generate_cid_v0; +use ipfs_cid::{ + convert_cid_v0_to_uint256, convert_cid_v1_to_uint256, + convert_uint256_to_cid_v0, convert_uint256_to_cid_v1, generate_cid_v0, + generate_cid_v1, +}; #[test] -fn test_generate_from_bytes_file0() { +fn test_generate_v0_from_bytes_file0() { // Read file into bytes let bytes_vector = std::fs::read("data/file0").unwrap(); let bytes_slice = bytes_vector.as_slice(); @@ -14,7 +18,7 @@ fn test_generate_from_bytes_file0() { } #[test] -fn test_generate_from_bytes_file1() { +fn test_generate_v0_from_bytes_file1() { // Read file into bytes let bytes_vector = std::fs::read("data/file1").unwrap(); let bytes_slice = bytes_vector.as_slice(); @@ -27,7 +31,7 @@ fn test_generate_from_bytes_file1() { } #[test] -fn test_generate_from_bytes_file2() { +fn test_generate_v0_from_bytes_file2() { // Read file into bytes let bytes_vector = std::fs::read("data/file2").unwrap(); let bytes_slice = bytes_vector.as_slice(); @@ -40,7 +44,7 @@ fn test_generate_from_bytes_file2() { } #[test] -fn test_generate_from_bytes_file3() { +fn test_generate_v0_from_bytes_file3() { // Read file into bytes let bytes_vector = std::fs::read("data/file3").unwrap(); let bytes_slice = bytes_vector.as_slice(); @@ -53,7 +57,7 @@ fn test_generate_from_bytes_file3() { } #[test] -fn test_generate_from_bytes_file4() { +fn test_generate_v0_from_bytes_file4() { // Read file into bytes let bytes_vector = std::fs::read("data/file4").unwrap(); let bytes_slice = bytes_vector.as_slice(); @@ -64,3 +68,223 @@ fn test_generate_from_bytes_file4() { assert_eq!(cid, expected_cid); } + +#[test] +fn test_generate_v1_from_bytes_file0() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file0").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + let expected_cid = String::from( + "bafybeicw4bpvhscvz2a7in5bytor4bx7kmp3k2ctkwm4et4ri5dx2ypt64", + ); + + assert_eq!(cid, expected_cid); +} + +#[test] +fn test_generate_v1_from_bytes_file1() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file1").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + let expected_cid = String::from( + "bafybeif56keohn57vtbnwiboeoldxinsbmuswkpo3ekjjo6m2qj4lmxwyq", + ); + + assert_eq!(cid, expected_cid); +} + +#[test] +fn test_generate_v1_from_bytes_file2() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file2").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + let expected_cid = String::from( + "bafybeiettolckdkgpy2jzix6ocs2ut7viscalntq2jzgj4u56q4lfmgxae", + ); + + assert_eq!(cid, expected_cid); +} + +#[test] +fn test_generate_v1_from_bytes_file3() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file3").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + let expected_cid = String::from( + "bafybeicirmdtizyoxvto4layqmahze6rtmurxr6h2hh6iu2qmcuqyloyp4", + ); + + assert_eq!(cid, expected_cid); +} + +#[test] +fn test_generate_v1_from_bytes_file4() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file4").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + let expected_cid = String::from( + "bafybeihphn6mvbd3ezn6ikmdmtflnrxcybdytyrd734crkomqax3iubhxi", + ); + + assert_eq!(cid, expected_cid); +} + +#[test] +fn test_convert_v0_to_uint256_and_back_file0() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file0").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v0(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v0_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v0(uint256).unwrap(); + + assert_eq!(cid, cid_back); +} + +#[test] +fn test_convert_v0_to_uint256_and_back_file1() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file1").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v0(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v0_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v0(uint256).unwrap(); + + assert_eq!(cid, cid_back); +} + +#[test] +fn test_convert_v0_to_uint256_and_back_file2() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file2").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v0(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v0_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v0(uint256).unwrap(); + + assert_eq!(cid, cid_back); +} + +#[test] +fn test_convert_v0_to_uint256_and_back_file3() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file3").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v0(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v0_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v0(uint256).unwrap(); + + assert_eq!(cid, cid_back); +} + +#[test] +fn test_convert_v0_to_uint256_and_back_file4() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file4").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v0(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v0_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v0(uint256).unwrap(); + + assert_eq!(cid, cid_back); +} + +#[test] +fn test_convert_v1_to_uint256_and_back_file0() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file0").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v1_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v1(uint256).unwrap(); + + assert_eq!(cid, cid_back); +} + +#[test] +fn test_convert_v1_to_uint256_and_back_file1() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file1").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v1_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v1(uint256).unwrap(); + + assert_eq!(cid, cid_back); +} + +#[test] +fn test_convert_v1_to_uint256_and_back_file2() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file2").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v1_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v1(uint256).unwrap(); + + assert_eq!(cid, cid_back); +} + +#[test] +fn test_convert_v1_to_uint256_and_back_file3() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file3").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v1_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v1(uint256).unwrap(); + + assert_eq!(cid, cid_back); +} + +#[test] +fn test_convert_v1_to_uint256_and_back_file4() { + // Read file into bytes + let bytes_vector = std::fs::read("data/file4").unwrap(); + let bytes_slice = bytes_vector.as_slice(); + // Generate CID from bytes + let cid = generate_cid_v1(bytes_slice).unwrap(); + // Convert CID to uint256 + let uint256 = convert_cid_v1_to_uint256(cid.clone()).unwrap(); + // Convert uint256 back to CID + let cid_back = convert_uint256_to_cid_v1(uint256).unwrap(); + + assert_eq!(cid, cid_back); +}