Path: blob/master/src/infrastructure/env/__tests__/PhabricatorEnvTestCase.php
13441 views
<?php12final class PhabricatorEnvTestCase extends PhabricatorTestCase {34public function testLocalURIForLink() {5$map = array(6'/' => true,7'/D123' => true,8'/path/to/something/' => true,9"/path/to/\nHeader: x" => false,10'http://evil.com/' => false,11'//evil.com/evil/' => false,12'javascript:lol' => false,13'' => false,14null => false,15'/\\evil.com' => false,16);1718foreach ($map as $uri => $expect) {19$this->assertEqual(20$expect,21PhabricatorEnv::isValidLocalURIForLink($uri),22pht('Valid local resource: %s', $uri));23}24}2526public function testRemoteURIForLink() {27$map = array(28'http://example.com/' => true,29'derp://example.com/' => false,30'javascript:alert(1)' => false,31'http://127.0.0.1/' => true,32'http://169.254.169.254/latest/meta-data/hostname' => true,33);3435foreach ($map as $uri => $expect) {36$this->assertEqual(37$expect,38PhabricatorEnv::isValidRemoteURIForLink($uri),39pht('Valid linkable remote URI: %s', $uri));40}41}4243public function testRemoteURIForFetch() {44$map = array(45'http://example.com/' => true,4647// No domain or protocol.48'' => false,4950// No domain.51'http://' => false,5253// No protocol.54'evil.com' => false,5556// No protocol.57'//evil.com' => false,5859// Bad protocol.60'javascript://evil.com/' => false,61'file:///etc/shadow' => false,6263// Unresolvable hostname.64'http://u1VcxwUp368SIFzl7rkWWg23KM5JPB2kTHHngxjXCQc.zzz/' => false,6566// Domains explicitly in blacklisted IP space.67'http://127.0.0.1/' => false,68'http://169.254.169.254/latest/meta-data/hostname' => false,6970// Domain resolves into blacklisted IP space.71'http://localhost/' => false,72);7374$protocols = array('http', 'https');7576foreach ($map as $uri => $expect) {77$this->assertEqual(78$expect,79PhabricatorEnv::isValidRemoteURIForFetch($uri, $protocols),80pht('Valid fetchable remote URI: %s', $uri));81}82}8384public function testDictionarySource() {85$source = new PhabricatorConfigDictionarySource(array('x' => 1));8687$this->assertEqual(88array(89'x' => 1,90),91$source->getKeys(array('x', 'z')));9293$source->setKeys(array('z' => 2));9495$this->assertEqual(96array(97'x' => 1,98'z' => 2,99),100$source->getKeys(array('x', 'z')));101102$source->setKeys(array('x' => 3));103104$this->assertEqual(105array(106'x' => 3,107'z' => 2,108),109$source->getKeys(array('x', 'z')));110111$source->deleteKeys(array('x'));112113$this->assertEqual(114array(115'z' => 2,116),117$source->getKeys(array('x', 'z')));118}119120public function testStackSource() {121$s1 = new PhabricatorConfigDictionarySource(array('x' => 1));122$s2 = new PhabricatorConfigDictionarySource(array('x' => 2));123124$stack = new PhabricatorConfigStackSource();125126$this->assertEqual(array(), $stack->getKeys(array('x')));127128$stack->pushSource($s1);129$this->assertEqual(array('x' => 1), $stack->getKeys(array('x')));130131$stack->pushSource($s2);132$this->assertEqual(array('x' => 2), $stack->getKeys(array('x')));133134$stack->setKeys(array('x' => 3));135$this->assertEqual(array('x' => 3), $stack->getKeys(array('x')));136137$stack->popSource();138$this->assertEqual(array('x' => 1), $stack->getKeys(array('x')));139140$stack->popSource();141$this->assertEqual(array(), $stack->getKeys(array('x')));142143$caught = null;144try {145$stack->popSource();146} catch (Exception $ex) {147$caught = $ex;148}149150$this->assertTrue($caught instanceof Exception);151}152153public function testOverrides() {154$outer = PhabricatorEnv::beginScopedEnv();155156$outer->overrideEnvConfig('test.value', 1);157$this->assertEqual(1, PhabricatorEnv::getEnvConfig('test.value'));158159$inner = PhabricatorEnv::beginScopedEnv();160$inner->overrideEnvConfig('test.value', 2);161$this->assertEqual(2, PhabricatorEnv::getEnvConfig('test.value'));162if (phutil_is_hiphop_runtime()) {163$inner->__destruct();164}165unset($inner);166167$this->assertEqual(1, PhabricatorEnv::getEnvConfig('test.value'));168if (phutil_is_hiphop_runtime()) {169$outer->__destruct();170}171unset($outer);172}173174public function testOverrideOrder() {175$outer = PhabricatorEnv::beginScopedEnv();176$inner = PhabricatorEnv::beginScopedEnv();177178$caught = null;179try {180$outer->__destruct();181} catch (Exception $ex) {182$caught = $ex;183}184185$this->assertTrue(186$caught instanceof Exception,187pht(188'Destroying a scoped environment which is not on the top of the '.189'stack should throw.'));190191if (phutil_is_hiphop_runtime()) {192$inner->__destruct();193}194unset($inner);195196if (phutil_is_hiphop_runtime()) {197$outer->__destruct();198}199unset($outer);200}201202public function testGetEnvExceptions() {203$caught = null;204try {205PhabricatorEnv::getEnvConfig('not.a.real.config.option');206} catch (Exception $ex) {207$caught = $ex;208}209$this->assertTrue($caught instanceof Exception);210211$caught = null;212try {213PhabricatorEnv::getEnvConfig('test.value');214} catch (Exception $ex) {215$caught = $ex;216}217$this->assertFalse($caught instanceof Exception);218}219220public function testSelfURI() {221$base_uri = 'https://allowed.example.com/';222223$allowed_uris = array(224'https://old.example.com/',225);226227$env = PhabricatorEnv::beginScopedEnv();228$env->overrideEnvConfig('phabricator.base-uri', $base_uri);229$env->overrideEnvConfig('phabricator.allowed-uris', $allowed_uris);230231$map = array(232'https://allowed.example.com/' => true,233'https://allowed.example.com' => true,234'https://allowed.EXAMPLE.com' => true,235'http://allowed.example.com/' => true,236'https://allowed.example.com/path/to/resource.png' => true,237238'https://old.example.com/' => true,239'https://old.example.com' => true,240'https://old.EXAMPLE.com' => true,241'http://old.example.com/' => true,242'https://old.example.com/path/to/resource.png' => true,243244'https://other.example.com/' => false,245);246247foreach ($map as $input => $expect) {248$this->assertEqual(249$expect,250PhabricatorEnv::isSelfURI($input),251pht('Is self URI? %s', $input));252}253}254255}256257258