-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_hg.py
More file actions
94 lines (71 loc) · 2.42 KB
/
test_hg.py
File metadata and controls
94 lines (71 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""Tests for libvcs hg repos."""
from __future__ import annotations
import pathlib
import shutil
import pytest
from libvcs import exc
from libvcs._internal.run import run
from libvcs._internal.shortcuts import create_project
from libvcs.sync.hg import HgSync
if not shutil.which("hg"):
pytestmark = pytest.mark.skip(reason="hg is not available")
def test_hg_sync(
tmp_path: pathlib.Path,
projects_path: pathlib.Path,
hg_remote_repo: pathlib.Path,
) -> None:
"""Test HgSync."""
repo_name = "my_mercurial_project"
mercurial_repo = HgSync(
url=f"file://{hg_remote_repo}",
path=projects_path / repo_name,
)
run(["hg", "init", mercurial_repo.repo_name], cwd=tmp_path)
mercurial_repo.update_repo()
test_repo_revision = run(
["hg", "parents", "--template={rev}"],
cwd=projects_path / repo_name,
)
assert mercurial_repo.get_revision() == test_repo_revision
def test_repo_mercurial_via_create_project(
tmp_path: pathlib.Path,
projects_path: pathlib.Path,
hg_remote_repo: pathlib.Path,
) -> None:
"""Test HgSync via create_project()."""
repo_name = "my_mercurial_project"
mercurial_repo = create_project(
url=f"file://{hg_remote_repo}",
path=projects_path / repo_name,
vcs="hg",
)
run(["hg", "init", mercurial_repo.repo_name], cwd=tmp_path)
mercurial_repo.update_repo()
test_repo_revision = run(
["hg", "parents", "--template={rev}"],
cwd=projects_path / repo_name,
)
assert mercurial_repo.get_revision() == test_repo_revision
def test_vulnerability_2022_03_12_command_injection(
monkeypatch: pytest.MonkeyPatch,
user_path: pathlib.Path,
tmp_path: pathlib.Path,
hg_remote_repo: pathlib.Path,
) -> None:
"""Prevent hg aliases from executed arbitrary commands via URLs.
As of 0.11 this code path is/was only executed via .obtain(), so this only would
effect explicit invocation of .object() or update_repo() of uncloned destination.
"""
random_dir = tmp_path / "random"
random_dir.mkdir()
monkeypatch.chdir(str(random_dir))
mercurial_repo = create_project(
url="--config=alias.clone=!touch ./HELLO",
vcs="hg",
path="./",
)
with pytest.raises(exc.CommandError):
mercurial_repo.update_repo()
assert not pathlib.Path(
random_dir / "HELLO",
).exists(), "Prevent command injection in hg aliases"