Path: blob/master/src/applications/diffusion/protocol/__tests__/DiffusionCommandEngineTestCase.php
12242 views
<?php12final class DiffusionCommandEngineTestCase extends PhabricatorTestCase {34public function testCommandEngine() {5$type_git = PhabricatorRepositoryType::REPOSITORY_TYPE_GIT;6$type_hg = PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL;7$type_svn = PhabricatorRepositoryType::REPOSITORY_TYPE_SVN;89$root = dirname(phutil_get_library_root('phabricator'));10$ssh_wrapper = $root.'/bin/ssh-connect';11$home = $root.'/support/empty/';121314// Plain commands.1516$this->assertCommandEngineFormat(17'git xyz',18array(19'LANG' => 'en_US.UTF-8',20'HOME' => $home,21),22array(23'vcs' => $type_git,24'argv' => 'xyz',25));2627$this->assertCommandEngineFormat(28(string)csprintf('hg --config ui.ssh=%s xyz', $ssh_wrapper),29array(30'LANG' => 'en_US.UTF-8',31'HGPLAIN' => '1',32),33array(34'vcs' => $type_hg,35'argv' => 'xyz',36));3738$this->assertCommandEngineFormat(39'svn --non-interactive xyz',40array(41'LANG' => 'en_US.UTF-8',42),43array(44'vcs' => $type_svn,45'argv' => 'xyz',46));474849// Commands with SSH.5051$this->assertCommandEngineFormat(52'git xyz',53array(54'LANG' => 'en_US.UTF-8',55'HOME' => $home,56'GIT_SSH' => $ssh_wrapper,57),58array(59'vcs' => $type_git,60'argv' => 'xyz',61'protocol' => 'ssh',62));6364$this->assertCommandEngineFormat(65(string)csprintf('hg --config ui.ssh=%s xyz', $ssh_wrapper),66array(67'LANG' => 'en_US.UTF-8',68'HGPLAIN' => '1',69),70array(71'vcs' => $type_hg,72'argv' => 'xyz',73'protocol' => 'ssh',74));7576$this->assertCommandEngineFormat(77'svn --non-interactive xyz',78array(79'LANG' => 'en_US.UTF-8',80'SVN_SSH' => $ssh_wrapper,81),82array(83'vcs' => $type_svn,84'argv' => 'xyz',85'protocol' => 'ssh',86));878889// Commands with HTTP.9091$this->assertCommandEngineFormat(92'git xyz',93array(94'LANG' => 'en_US.UTF-8',95'HOME' => $home,96),97array(98'vcs' => $type_git,99'argv' => 'xyz',100'protocol' => 'https',101));102103$this->assertCommandEngineFormat(104(string)csprintf('hg --config ui.ssh=%s xyz', $ssh_wrapper),105array(106'LANG' => 'en_US.UTF-8',107'HGPLAIN' => '1',108),109array(110'vcs' => $type_hg,111'argv' => 'xyz',112'protocol' => 'https',113));114115$this->assertCommandEngineFormat(116'svn --non-interactive --no-auth-cache --trust-server-cert xyz',117array(118'LANG' => 'en_US.UTF-8',119),120array(121'vcs' => $type_svn,122'argv' => 'xyz',123'protocol' => 'https',124));125126// Test that filtering defenses for "--config" and "--debugger" flag127// injections in Mercurial are functional. See T13012.128129$caught = null;130try {131$this->assertCommandEngineFormat(132'',133array(),134array(135'vcs' => $type_hg,136'argv' => '--debugger',137));138} catch (DiffusionMercurialFlagInjectionException $ex) {139$caught = $ex;140}141142$this->assertTrue(143($caught instanceof DiffusionMercurialFlagInjectionException),144pht('Expected "--debugger" injection in Mercurial to throw.'));145146147$caught = null;148try {149$this->assertCommandEngineFormat(150'',151array(),152array(153'vcs' => $type_hg,154'argv' => '--config=x',155));156} catch (DiffusionMercurialFlagInjectionException $ex) {157$caught = $ex;158}159160$this->assertTrue(161($caught instanceof DiffusionMercurialFlagInjectionException),162pht('Expected "--config" injection in Mercurial to throw.'));163164$caught = null;165try {166$this->assertCommandEngineFormat(167'',168array(),169array(170'vcs' => $type_hg,171'argv' => (string)csprintf('%s', '--config=x'),172));173} catch (DiffusionMercurialFlagInjectionException $ex) {174$caught = $ex;175}176177$this->assertTrue(178($caught instanceof DiffusionMercurialFlagInjectionException),179pht('Expected quoted "--config" injection in Mercurial to throw.'));180181}182183private function assertCommandEngineFormat(184$command,185array $env,186array $inputs) {187188$repository = id(new PhabricatorRepository())189->setVersionControlSystem($inputs['vcs']);190191$future = DiffusionCommandEngine::newCommandEngine($repository)192->setArgv((array)$inputs['argv'])193->setProtocol(idx($inputs, 'protocol'))194->newFuture();195196$command_string = $future->getCommand();197198$actual_command = $command_string->getUnmaskedString();199$this->assertEqual($command, $actual_command);200201$actual_environment = $future->getEnv();202203$compare_environment = array_select_keys(204$actual_environment,205array_keys($env));206207$this->assertEqual($env, $compare_environment);208}209210}211212213