def test_parent_ref_has_two_initial_commits(self, diverse_repo):
        from hangar.diagnostics.integrity import _verify_commit_tree_integrity
        from hangar.records.parsing import commit_parent_db_key_from_raw_key

        repo = diverse_repo
        repo = diverse_repo
        history = repo.log(return_contents=True)
        all_commits = history['order']
        initial_commit = all_commits[-1]
        for cmt in all_commits:
            if cmt == initial_commit:
                continue

            parentKey = commit_parent_db_key_from_raw_key(cmt)
            with repo._env.refenv.begin(write=True) as txn:
                parentVal = txn.get(parentKey)
                txn.put(parentKey, b'', overwrite=True)

            with pytest.raises(
                    RuntimeError,
                    match=
                    'Commit tree integrity compromised. Multiple "initial"'):
                _verify_commit_tree_integrity(repo._env.refenv)

            with repo._env.refenv.begin(write=True) as txn:
                txn.put(parentKey, parentVal, overwrite=True)
    def test_parent_ref_digest_of_cmt_does_not_exist(self, diverse_repo):
        from hangar.diagnostics.integrity import _verify_commit_tree_integrity
        from hangar.records.parsing import commit_parent_db_key_from_raw_key

        repo = diverse_repo
        history = repo.log(return_contents=True)
        all_commits = history['order']
        for cmt in all_commits:
            parentKey = commit_parent_db_key_from_raw_key(cmt)
            with repo._env.refenv.begin(write=True) as txn:
                parentVal = txn.get(parentKey)
                txn.delete(parentKey)

            with pytest.raises(
                    RuntimeError,
                    match='Data corruption detected for parent ref'):
                _verify_commit_tree_integrity(repo._env.refenv)

            with repo._env.refenv.begin(write=True) as txn:
                txn.put(parentKey, parentVal)
    def test_parent_ref_references_nonexisting_commits(self, diverse_repo):
        from hangar.diagnostics.integrity import _verify_commit_tree_integrity
        from hangar.records.parsing import commit_parent_db_key_from_raw_key
        from hangar.records.parsing import commit_parent_raw_val_from_db_val
        from hangar.records.parsing import commit_parent_db_val_from_raw_val

        repo = diverse_repo
        history = repo.log(return_contents=True)
        all_commits = history['order']
        for cmt in all_commits:
            parentKey = commit_parent_db_key_from_raw_key(cmt)
            with repo._env.refenv.begin(write=True) as txn:
                parentVal = txn.get(parentKey)
                parent_raw = commit_parent_raw_val_from_db_val(parentVal)
                parent = parent_raw.ancestor_spec

                if parent.dev_ancestor:
                    modifiedVal = commit_parent_db_val_from_raw_val(
                        master_ancestor=parent.master_ancestor,
                        dev_ancestor='corrupt',
                        is_merge_commit=parent.is_merge_commit)
                elif parent.master_ancestor:
                    modifiedVal = commit_parent_db_val_from_raw_val(
                        master_ancestor='corrupt',
                        dev_ancestor=parent.dev_ancestor,
                        is_merge_commit=parent.is_merge_commit)
                else:
                    continue

                txn.put(parentKey, modifiedVal.raw, overwrite=True)

            with pytest.raises(
                    RuntimeError,
                    match='Data corruption detected in commit tree'):
                _verify_commit_tree_integrity(repo._env.refenv)

            with repo._env.refenv.begin(write=True) as txn:
                txn.put(parentKey, parentVal)