use crate::error::{Error, Result}; use crate::object::blob::{self, BlobForm}; use crate::object::chunker::{gear_table, next_chunk}; use crate::object::commit::{self, Commit, Sig}; use crate::object::id::{hash_bytes, object_id, Id}; use crate::object::tree; use crate::object::view::{self, Bookmark, Operation, TargetSet, TimeMs, View, WorkspaceRef}; use crate::object::{RawObject, TYPE_BLOB, TYPE_CHUNK, TYPE_TREE}; use crate::store::pack; pub fn run() -> Result { let empty_hash = hex(&hash_bytes(b"")); expect( empty_hash == "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262", "blake3 vector", )?; let gear = gear_table(); expect(gear[0] == 0xf8447b379cdadcd5, "gear table entry 0")?; expect(gear[1] == 0xbe63d125aec8ca6d, "gear table entry 1")?; let mut chunk_input = vec![0u8; 300_000]; for (index, byte) in chunk_input.iter_mut().enumerate() { *byte = ((index * 31 + 2 * 17) % 251) as u8; } let first = next_chunk(&chunk_input); let second = next_chunk(&chunk_input[first..]); expect(first == 262_144 && second == 37_856, "chunk boundaries")?; expect( object_id(TYPE_CHUNK, &chunk_input[..first]).to_hex() == "c83c67617ae4445631508cc4edba83ba58d9e996cea40c980ec69a8b6da86458", "first chunk id", )?; expect( object_id(TYPE_CHUNK, &chunk_input[first..]).to_hex() == "0c91665d423fd704baae9454d24697460c4898fbf16cc6ad498d3f750d073a4b", "second chunk id", )?; let build = blob::build(&chunk_input); expect( build.root.to_hex() == "937fe1a8d674aa28cb9fa074a76ea84cdc1d6c61e209f47bbdebbe4361b698cf", "manifest blob id", )?; let manifest = blob::decode(&build.objects.last().unwrap().body)?; expect(manifest.form == BlobForm::Leaf, "manifest form")?; let empty_tree_body = vec![0u8]; let empty_tree_id = object_id(TYPE_TREE, &empty_tree_body); expect( empty_tree_id.to_hex() == "8be3caa39b307b2b4a46533174cc7faa49c0ff34317302d188276c9641be35fb", "empty tree id", )?; expect(tree::decode(&empty_tree_body)?.entries.is_empty(), "empty tree decode")?; let inline = blob::encode_inline(b"hello"); expect( object_id(TYPE_BLOB, &inline).to_hex() == "f04a7f7324bbb7279471b5c656c893ee51b62ed9f9bad4c8cb8a289101f68240", "inline blob id", )?; let change_id: [u8; 16] = [ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, ]; let signature = Sig { name: b"Alice".to_vec(), email: b"alice@example.com".to_vec(), seconds: 1_700_000_000, tz_min: 0, }; let commit = Commit { version: 1, parents: Vec::new(), root_tree: empty_tree_id, change_id, author: signature.clone(), committer: signature, message: b"initial commit".to_vec(), renames_state: commit::RENAMES_RECORDED, renames: Vec::new(), extra: Vec::new(), }; let commit_body = commit::encode(&commit); let commit_id = object_id(crate::object::TYPE_COMMIT, &commit_body); expect( commit_id.to_hex() == "7f648b3621402df01985d121888c89c37107274fb8bb54661f12deca6cc72299", "commit id", )?; expect( commit_id == Id::from_hex("7f648b3621402df01985d121888c89c37107274fb8bb54661f12deca6cc72299")?, "commit round trip", )?; let view = View { heads: vec![commit_id], bookmarks: vec![Bookmark { name: b"main".to_vec(), targets: TargetSet { adds: vec![Some(commit_id)], removes: Vec::new(), }, }], tags: Vec::new(), remote_refs: Vec::new(), working_copies: vec![WorkspaceRef { workspace: b"default".to_vec(), target: commit_id, }], }; let view_body = view::encode_view(&view); let view_id = object_id(crate::object::TYPE_VIEW, &view_body); expect( view_id.to_hex() == "a2e19569bb3b5c28775bc8503c45c4edce6d51f55303e39bc02eec529324cd11", "view id", )?; let operation = Operation { parents: Vec::new(), view: view_id, start: TimeMs { millis: 1_700_000_000_000, tz_min: 0, }, end: TimeMs { millis: 1_700_000_000_000, tz_min: 0, }, hostname: b"host".to_vec(), username: b"alice".to_vec(), description: b"selftest operation".to_vec(), tags: Vec::new(), }; let operation_body = view::encode_operation(&operation); expect( object_id(crate::object::TYPE_OPERATION, &operation_body).to_hex() == "0a5494529353fb5886870a128139e07d95f40afcd831afc423617808f31dc426", "operation id", )?; let output = pack::encode(&[RawObject::known(TYPE_TREE, empty_tree_id, vec![0])]); expect( hex(&output.pack) == "56504b310100000001000000000000008be3caa39b307b2b4a46533174cc7faa49c0ff34317302d188276c9641be35fb030001010072c385a83448a5e16853735a79a212f0377d0e40be777d121271758525dbc911", "pack bytes", )?; expect(output.index.len() == 1136, "index length")?; expect( hex(&hash_bytes(&output.index)) == "d5dfb7b2ce4235f87856c12ff2e0b65d817dc29dfedad62ba34e71d74dca0cfc", "index checksum", )?; pack::decode(output.pack)?; println!("self-test: ok"); Ok(0) } fn expect(condition: bool, message: &str) -> Result<()> { if condition { Ok(()) } else { Err(Error::new(format!("self-test failed: {message}"))) } } fn hex(bytes: &[u8]) -> String { const DIGITS: &[u8; 16] = b"0123456789abcdef"; let mut text = String::with_capacity(bytes.len() * 2); for byte in bytes { text.push(DIGITS[(byte >> 4) as usize] as char); text.push(DIGITS[(byte & 0x0f) as usize] as char); } text }