Something went wrong. Try again.
@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
Something went wrong. Try again.
5.1 kB · 211 lines
PHP
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212<?php
final class DiffusionCommandEngineTestCase extends PhabricatorTestCase {
public function testCommandEngine() { $type_git = PhabricatorRepositoryType::REPOSITORY_TYPE_GIT; $type_hg = PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL; $type_svn = PhabricatorRepositoryType::REPOSITORY_TYPE_SVN;
$root = dirname(phutil_get_library_root('phabricator')); $ssh_wrapper = $root.'/bin/ssh-connect'; $home = $root.'/support/empty/';
// Plain commands.
$this->assertCommandEngineFormat( 'git xyz', array( 'LANG' => 'en_US.UTF-8', 'HOME' => $home, ), array( 'vcs' => $type_git, 'argv' => 'xyz', ));
$this->assertCommandEngineFormat( (string)csprintf('hg --config ui.ssh=%s xyz', $ssh_wrapper), array( 'LANG' => 'en_US.UTF-8', 'HGPLAIN' => '1', ), array( 'vcs' => $type_hg, 'argv' => 'xyz', ));
$this->assertCommandEngineFormat( 'svn --non-interactive xyz', array( 'LANG' => 'en_US.UTF-8', ), array( 'vcs' => $type_svn, 'argv' => 'xyz', ));
// Commands with SSH.
$this->assertCommandEngineFormat( 'git xyz', array( 'LANG' => 'en_US.UTF-8', 'HOME' => $home, 'GIT_SSH' => $ssh_wrapper, ), array( 'vcs' => $type_git, 'argv' => 'xyz', 'protocol' => 'ssh', ));
$this->assertCommandEngineFormat( (string)csprintf('hg --config ui.ssh=%s xyz', $ssh_wrapper), array( 'LANG' => 'en_US.UTF-8', 'HGPLAIN' => '1', ), array( 'vcs' => $type_hg, 'argv' => 'xyz', 'protocol' => 'ssh', ));
$this->assertCommandEngineFormat( 'svn --non-interactive xyz', array( 'LANG' => 'en_US.UTF-8', 'SVN_SSH' => $ssh_wrapper, ), array( 'vcs' => $type_svn, 'argv' => 'xyz', 'protocol' => 'ssh', ));
// Commands with HTTP.
$this->assertCommandEngineFormat( 'git xyz', array( 'LANG' => 'en_US.UTF-8', 'HOME' => $home, ), array( 'vcs' => $type_git, 'argv' => 'xyz', 'protocol' => 'https', ));
$this->assertCommandEngineFormat( (string)csprintf('hg --config ui.ssh=%s xyz', $ssh_wrapper), array( 'LANG' => 'en_US.UTF-8', 'HGPLAIN' => '1', ), array( 'vcs' => $type_hg, 'argv' => 'xyz', 'protocol' => 'https', ));
$this->assertCommandEngineFormat( 'svn --non-interactive --no-auth-cache --trust-server-cert xyz', array( 'LANG' => 'en_US.UTF-8', ), array( 'vcs' => $type_svn, 'argv' => 'xyz', 'protocol' => 'https', ));
// Test that filtering defenses for "--config" and "--debugger" flag // injections in Mercurial are functional. See T13012.
$caught = null; try { $this->assertCommandEngineFormat( '', array(), array( 'vcs' => $type_hg, 'argv' => '--debugger', )); } catch (DiffusionMercurialFlagInjectionException $ex) { $caught = $ex; }
$this->assertTrue( ($caught instanceof DiffusionMercurialFlagInjectionException), pht('Expected "--debugger" injection in Mercurial to throw.'));
$caught = null; try { $this->assertCommandEngineFormat( '', array(), array( 'vcs' => $type_hg, 'argv' => '--config=x', )); } catch (DiffusionMercurialFlagInjectionException $ex) { $caught = $ex; }
$this->assertTrue( ($caught instanceof DiffusionMercurialFlagInjectionException), pht('Expected "--config" injection in Mercurial to throw.'));
$caught = null; try { $this->assertCommandEngineFormat( '', array(), array( 'vcs' => $type_hg, 'argv' => (string)csprintf('%s', '--config=x'), )); } catch (DiffusionMercurialFlagInjectionException $ex) { $caught = $ex; }
$this->assertTrue( ($caught instanceof DiffusionMercurialFlagInjectionException), pht('Expected quoted "--config" injection in Mercurial to throw.'));
}
private function assertCommandEngineFormat( $command, array $env, array $inputs) {
$repository = id(new PhabricatorRepository()) ->setVersionControlSystem($inputs['vcs']);
$future = DiffusionCommandEngine::newCommandEngine($repository) ->setArgv((array)$inputs['argv']) ->setProtocol(idx($inputs, 'protocol')) ->newFuture();
$command_string = $future->getCommand();
$actual_command = $command_string->getUnmaskedString(); $this->assertEqual($command, $actual_command);
$actual_environment = $future->getEnv();
$compare_environment = array_select_keys( $actual_environment, array_keys($env));
$this->assertEqual($env, $compare_environment); }
}