Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/env/__tests__/PhabricatorEnvTestCase.php
13441 views
1
<?php
2
3
final class PhabricatorEnvTestCase extends PhabricatorTestCase {
4
5
public function testLocalURIForLink() {
6
$map = array(
7
'/' => true,
8
'/D123' => true,
9
'/path/to/something/' => true,
10
"/path/to/\nHeader: x" => false,
11
'http://evil.com/' => false,
12
'//evil.com/evil/' => false,
13
'javascript:lol' => false,
14
'' => false,
15
null => false,
16
'/\\evil.com' => false,
17
);
18
19
foreach ($map as $uri => $expect) {
20
$this->assertEqual(
21
$expect,
22
PhabricatorEnv::isValidLocalURIForLink($uri),
23
pht('Valid local resource: %s', $uri));
24
}
25
}
26
27
public function testRemoteURIForLink() {
28
$map = array(
29
'http://example.com/' => true,
30
'derp://example.com/' => false,
31
'javascript:alert(1)' => false,
32
'http://127.0.0.1/' => true,
33
'http://169.254.169.254/latest/meta-data/hostname' => true,
34
);
35
36
foreach ($map as $uri => $expect) {
37
$this->assertEqual(
38
$expect,
39
PhabricatorEnv::isValidRemoteURIForLink($uri),
40
pht('Valid linkable remote URI: %s', $uri));
41
}
42
}
43
44
public function testRemoteURIForFetch() {
45
$map = array(
46
'http://example.com/' => true,
47
48
// No domain or protocol.
49
'' => false,
50
51
// No domain.
52
'http://' => false,
53
54
// No protocol.
55
'evil.com' => false,
56
57
// No protocol.
58
'//evil.com' => false,
59
60
// Bad protocol.
61
'javascript://evil.com/' => false,
62
'file:///etc/shadow' => false,
63
64
// Unresolvable hostname.
65
'http://u1VcxwUp368SIFzl7rkWWg23KM5JPB2kTHHngxjXCQc.zzz/' => false,
66
67
// Domains explicitly in blacklisted IP space.
68
'http://127.0.0.1/' => false,
69
'http://169.254.169.254/latest/meta-data/hostname' => false,
70
71
// Domain resolves into blacklisted IP space.
72
'http://localhost/' => false,
73
);
74
75
$protocols = array('http', 'https');
76
77
foreach ($map as $uri => $expect) {
78
$this->assertEqual(
79
$expect,
80
PhabricatorEnv::isValidRemoteURIForFetch($uri, $protocols),
81
pht('Valid fetchable remote URI: %s', $uri));
82
}
83
}
84
85
public function testDictionarySource() {
86
$source = new PhabricatorConfigDictionarySource(array('x' => 1));
87
88
$this->assertEqual(
89
array(
90
'x' => 1,
91
),
92
$source->getKeys(array('x', 'z')));
93
94
$source->setKeys(array('z' => 2));
95
96
$this->assertEqual(
97
array(
98
'x' => 1,
99
'z' => 2,
100
),
101
$source->getKeys(array('x', 'z')));
102
103
$source->setKeys(array('x' => 3));
104
105
$this->assertEqual(
106
array(
107
'x' => 3,
108
'z' => 2,
109
),
110
$source->getKeys(array('x', 'z')));
111
112
$source->deleteKeys(array('x'));
113
114
$this->assertEqual(
115
array(
116
'z' => 2,
117
),
118
$source->getKeys(array('x', 'z')));
119
}
120
121
public function testStackSource() {
122
$s1 = new PhabricatorConfigDictionarySource(array('x' => 1));
123
$s2 = new PhabricatorConfigDictionarySource(array('x' => 2));
124
125
$stack = new PhabricatorConfigStackSource();
126
127
$this->assertEqual(array(), $stack->getKeys(array('x')));
128
129
$stack->pushSource($s1);
130
$this->assertEqual(array('x' => 1), $stack->getKeys(array('x')));
131
132
$stack->pushSource($s2);
133
$this->assertEqual(array('x' => 2), $stack->getKeys(array('x')));
134
135
$stack->setKeys(array('x' => 3));
136
$this->assertEqual(array('x' => 3), $stack->getKeys(array('x')));
137
138
$stack->popSource();
139
$this->assertEqual(array('x' => 1), $stack->getKeys(array('x')));
140
141
$stack->popSource();
142
$this->assertEqual(array(), $stack->getKeys(array('x')));
143
144
$caught = null;
145
try {
146
$stack->popSource();
147
} catch (Exception $ex) {
148
$caught = $ex;
149
}
150
151
$this->assertTrue($caught instanceof Exception);
152
}
153
154
public function testOverrides() {
155
$outer = PhabricatorEnv::beginScopedEnv();
156
157
$outer->overrideEnvConfig('test.value', 1);
158
$this->assertEqual(1, PhabricatorEnv::getEnvConfig('test.value'));
159
160
$inner = PhabricatorEnv::beginScopedEnv();
161
$inner->overrideEnvConfig('test.value', 2);
162
$this->assertEqual(2, PhabricatorEnv::getEnvConfig('test.value'));
163
if (phutil_is_hiphop_runtime()) {
164
$inner->__destruct();
165
}
166
unset($inner);
167
168
$this->assertEqual(1, PhabricatorEnv::getEnvConfig('test.value'));
169
if (phutil_is_hiphop_runtime()) {
170
$outer->__destruct();
171
}
172
unset($outer);
173
}
174
175
public function testOverrideOrder() {
176
$outer = PhabricatorEnv::beginScopedEnv();
177
$inner = PhabricatorEnv::beginScopedEnv();
178
179
$caught = null;
180
try {
181
$outer->__destruct();
182
} catch (Exception $ex) {
183
$caught = $ex;
184
}
185
186
$this->assertTrue(
187
$caught instanceof Exception,
188
pht(
189
'Destroying a scoped environment which is not on the top of the '.
190
'stack should throw.'));
191
192
if (phutil_is_hiphop_runtime()) {
193
$inner->__destruct();
194
}
195
unset($inner);
196
197
if (phutil_is_hiphop_runtime()) {
198
$outer->__destruct();
199
}
200
unset($outer);
201
}
202
203
public function testGetEnvExceptions() {
204
$caught = null;
205
try {
206
PhabricatorEnv::getEnvConfig('not.a.real.config.option');
207
} catch (Exception $ex) {
208
$caught = $ex;
209
}
210
$this->assertTrue($caught instanceof Exception);
211
212
$caught = null;
213
try {
214
PhabricatorEnv::getEnvConfig('test.value');
215
} catch (Exception $ex) {
216
$caught = $ex;
217
}
218
$this->assertFalse($caught instanceof Exception);
219
}
220
221
public function testSelfURI() {
222
$base_uri = 'https://allowed.example.com/';
223
224
$allowed_uris = array(
225
'https://old.example.com/',
226
);
227
228
$env = PhabricatorEnv::beginScopedEnv();
229
$env->overrideEnvConfig('phabricator.base-uri', $base_uri);
230
$env->overrideEnvConfig('phabricator.allowed-uris', $allowed_uris);
231
232
$map = array(
233
'https://allowed.example.com/' => true,
234
'https://allowed.example.com' => true,
235
'https://allowed.EXAMPLE.com' => true,
236
'http://allowed.example.com/' => true,
237
'https://allowed.example.com/path/to/resource.png' => true,
238
239
'https://old.example.com/' => true,
240
'https://old.example.com' => true,
241
'https://old.EXAMPLE.com' => true,
242
'http://old.example.com/' => true,
243
'https://old.example.com/path/to/resource.png' => true,
244
245
'https://other.example.com/' => false,
246
);
247
248
foreach ($map as $input => $expect) {
249
$this->assertEqual(
250
$expect,
251
PhabricatorEnv::isSelfURI($input),
252
pht('Is self URI? %s', $input));
253
}
254
}
255
256
}
257
258