Skip to content

Commit

Permalink
imp: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omarabid committed Aug 31, 2023
1 parent e53996b commit 713843d
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn convert_uint256_to_cid_v1(uint256: String) -> Result<String> {
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())
}
236 changes: 230 additions & 6 deletions tests/unit.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}

0 comments on commit 713843d

Please sign in to comment.