Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / diffusion / protocol / __tests__ / DiffusionCommandEngineTestCase.php
blob50b7d4a5e2d1ca825e55ac8bc0f85401eb55871c
1 <?php
3 final class DiffusionCommandEngineTestCase extends PhabricatorTestCase {
5 public function testCommandEngine() {
6 $type_git = PhabricatorRepositoryType::REPOSITORY_TYPE_GIT;
7 $type_hg = PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL;
8 $type_svn = PhabricatorRepositoryType::REPOSITORY_TYPE_SVN;
10 $root = dirname(phutil_get_library_root('phabricator'));
11 $ssh_wrapper = $root.'/bin/ssh-connect';
12 $home = $root.'/support/empty/';
15 // Plain commands.
17 $this->assertCommandEngineFormat(
18 'git xyz',
19 array(
20 'LANG' => 'en_US.UTF-8',
21 'HOME' => $home,
23 array(
24 'vcs' => $type_git,
25 'argv' => 'xyz',
26 ));
28 $this->assertCommandEngineFormat(
29 (string)csprintf('hg --config ui.ssh=%s xyz', $ssh_wrapper),
30 array(
31 'LANG' => 'en_US.UTF-8',
32 'HGPLAIN' => '1',
34 array(
35 'vcs' => $type_hg,
36 'argv' => 'xyz',
37 ));
39 $this->assertCommandEngineFormat(
40 'svn --non-interactive xyz',
41 array(
42 'LANG' => 'en_US.UTF-8',
44 array(
45 'vcs' => $type_svn,
46 'argv' => 'xyz',
47 ));
50 // Commands with SSH.
52 $this->assertCommandEngineFormat(
53 'git xyz',
54 array(
55 'LANG' => 'en_US.UTF-8',
56 'HOME' => $home,
57 'GIT_SSH' => $ssh_wrapper,
59 array(
60 'vcs' => $type_git,
61 'argv' => 'xyz',
62 'protocol' => 'ssh',
63 ));
65 $this->assertCommandEngineFormat(
66 (string)csprintf('hg --config ui.ssh=%s xyz', $ssh_wrapper),
67 array(
68 'LANG' => 'en_US.UTF-8',
69 'HGPLAIN' => '1',
71 array(
72 'vcs' => $type_hg,
73 'argv' => 'xyz',
74 'protocol' => 'ssh',
75 ));
77 $this->assertCommandEngineFormat(
78 'svn --non-interactive xyz',
79 array(
80 'LANG' => 'en_US.UTF-8',
81 'SVN_SSH' => $ssh_wrapper,
83 array(
84 'vcs' => $type_svn,
85 'argv' => 'xyz',
86 'protocol' => 'ssh',
87 ));
90 // Commands with HTTP.
92 $this->assertCommandEngineFormat(
93 'git xyz',
94 array(
95 'LANG' => 'en_US.UTF-8',
96 'HOME' => $home,
98 array(
99 'vcs' => $type_git,
100 'argv' => 'xyz',
101 'protocol' => 'https',
104 $this->assertCommandEngineFormat(
105 (string)csprintf('hg --config ui.ssh=%s xyz', $ssh_wrapper),
106 array(
107 'LANG' => 'en_US.UTF-8',
108 'HGPLAIN' => '1',
110 array(
111 'vcs' => $type_hg,
112 'argv' => 'xyz',
113 'protocol' => 'https',
116 $this->assertCommandEngineFormat(
117 'svn --non-interactive --no-auth-cache --trust-server-cert xyz',
118 array(
119 'LANG' => 'en_US.UTF-8',
121 array(
122 'vcs' => $type_svn,
123 'argv' => 'xyz',
124 'protocol' => 'https',
127 // Test that filtering defenses for "--config" and "--debugger" flag
128 // injections in Mercurial are functional. See T13012.
130 $caught = null;
131 try {
132 $this->assertCommandEngineFormat(
134 array(),
135 array(
136 'vcs' => $type_hg,
137 'argv' => '--debugger',
139 } catch (DiffusionMercurialFlagInjectionException $ex) {
140 $caught = $ex;
143 $this->assertTrue(
144 ($caught instanceof DiffusionMercurialFlagInjectionException),
145 pht('Expected "--debugger" injection in Mercurial to throw.'));
148 $caught = null;
149 try {
150 $this->assertCommandEngineFormat(
152 array(),
153 array(
154 'vcs' => $type_hg,
155 'argv' => '--config=x',
157 } catch (DiffusionMercurialFlagInjectionException $ex) {
158 $caught = $ex;
161 $this->assertTrue(
162 ($caught instanceof DiffusionMercurialFlagInjectionException),
163 pht('Expected "--config" injection in Mercurial to throw.'));
165 $caught = null;
166 try {
167 $this->assertCommandEngineFormat(
169 array(),
170 array(
171 'vcs' => $type_hg,
172 'argv' => (string)csprintf('%s', '--config=x'),
174 } catch (DiffusionMercurialFlagInjectionException $ex) {
175 $caught = $ex;
178 $this->assertTrue(
179 ($caught instanceof DiffusionMercurialFlagInjectionException),
180 pht('Expected quoted "--config" injection in Mercurial to throw.'));
184 private function assertCommandEngineFormat(
185 $command,
186 array $env,
187 array $inputs) {
189 $repository = id(new PhabricatorRepository())
190 ->setVersionControlSystem($inputs['vcs']);
192 $future = DiffusionCommandEngine::newCommandEngine($repository)
193 ->setArgv((array)$inputs['argv'])
194 ->setProtocol(idx($inputs, 'protocol'))
195 ->newFuture();
197 $command_string = $future->getCommand();
199 $actual_command = $command_string->getUnmaskedString();
200 $this->assertEqual($command, $actual_command);
202 $actual_environment = $future->getEnv();
204 $compare_environment = array_select_keys(
205 $actual_environment,
206 array_keys($env));
208 $this->assertEqual($env, $compare_environment);