navid/veins / e362a182625e66b06bd48dcda2992ca49b89e9b8638e55b72b043f1e7ea705b1 / veins-rust/tests/format_vectors.rs

format_vectors.rs

← veins-rust/tests · 5168 bytes · raw · download

use veins_rust::object::blob;
use veins_rust::object::chunker::{gear_table, next_chunk};
use veins_rust::object::commit::{self, Commit, Sig};
use veins_rust::object::id::{hash_bytes, object_id, Id};
use veins_rust::object::tree;
use veins_rust::object::view::{self, Bookmark, Operation, TargetSet, TimeMs, View, WorkspaceRef};
use veins_rust::object::{RawObject, TYPE_BLOB, TYPE_CHUNK, TYPE_TREE};
use veins_rust::store::pack;

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

#[test]
fn published_vectors_match() {
    assert_eq!(
        hex(&hash_bytes(b"")),
        "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262"
    );
    assert_eq!(gear_table()[0], 0xf8447b379cdadcd5);
    assert_eq!(gear_table()[1], 0xbe63d125aec8ca6d);

    let mut input = vec![0u8; 300_000];
    for (index, byte) in input.iter_mut().enumerate() {
        *byte = ((index * 31 + 34) % 251) as u8;
    }
    let first = next_chunk(&input);
    assert_eq!(first, 262_144);
    assert_eq!(next_chunk(&input[first..]), 37_856);
    assert_eq!(
        object_id(TYPE_CHUNK, &input[..first]).to_hex(),
        "c83c67617ae4445631508cc4edba83ba58d9e996cea40c980ec69a8b6da86458"
    );
    assert_eq!(
        object_id(TYPE_CHUNK, &input[first..]).to_hex(),
        "0c91665d423fd704baae9454d24697460c4898fbf16cc6ad498d3f750d073a4b"
    );
    assert_eq!(
        blob::build(&input).root.to_hex(),
        "937fe1a8d674aa28cb9fa074a76ea84cdc1d6c61e209f47bbdebbe4361b698cf"
    );

    let empty_tree = vec![0u8];
    let empty_tree_id = object_id(TYPE_TREE, &empty_tree);
    assert_eq!(
        empty_tree_id.to_hex(),
        "8be3caa39b307b2b4a46533174cc7faa49c0ff34317302d188276c9641be35fb"
    );
    assert!(tree::decode(&empty_tree).unwrap().entries.is_empty());

    assert_eq!(
        object_id(TYPE_BLOB, &blob::encode_inline(b"hello")).to_hex(),
        "f04a7f7324bbb7279471b5c656c893ee51b62ed9f9bad4c8cb8a289101f68240"
    );
}

#[test]
fn objects_round_trip_and_pack_is_canonical() {
    let change_id = [
        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 empty_tree_id = Id::from_hex(
        "8be3caa39b307b2b4a46533174cc7faa49c0ff34317302d188276c9641be35fb",
    )
    .unwrap();
    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(veins_rust::object::TYPE_COMMIT, &commit_body);
    assert_eq!(
        commit_id.to_hex(),
        "7f648b3621402df01985d121888c89c37107274fb8bb54661f12deca6cc72299"
    );
    assert_eq!(commit::decode(&commit_body).unwrap().message, b"initial commit");

    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(veins_rust::object::TYPE_VIEW, &view_body);
    assert_eq!(
        view_id.to_hex(),
        "a2e19569bb3b5c28775bc8503c45c4edce6d51f55303e39bc02eec529324cd11"
    );
    assert_eq!(view::decode_view(&view_body).unwrap().heads, vec![commit_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);
    assert_eq!(
        object_id(veins_rust::object::TYPE_OPERATION, &operation_body).to_hex(),
        "0a5494529353fb5886870a128139e07d95f40afcd831afc423617808f31dc426"
    );

    let output = pack::encode(&[RawObject::known(TYPE_TREE, empty_tree_id, vec![0])]);
    assert_eq!(
        hex(&output.pack),
        "56504b310100000001000000000000008be3caa39b307b2b4a46533174cc7faa49c0ff34317302d188276c9641be35fb030001010072c385a83448a5e16853735a79a212f0377d0e40be777d121271758525dbc911"
    );
    assert_eq!(output.index.len(), 1136);
    assert_eq!(
        hex(&hash_bytes(&output.index)),
        "d5dfb7b2ce4235f87856c12ff2e0b65d817dc29dfedad62ba34e71d74dca0cfc"
    );
    pack::decode(output.pack).unwrap();
}