|
5 | 5 | from pyhttpd.backport import skip_not_backported |
6 | 6 | from pyhttpd.conf import HttpdConf |
7 | 7 |
|
8 | | -pytestmark = skip_not_backported("r1938506", "SetEnvFromFile") |
| 8 | +pytestmark = skip_not_backported("r1938506", "SetEnvFromFile", |
| 9 | + backported_in="2.4.72") |
9 | 10 |
|
10 | 11 |
|
11 | | -def _write_ssi_echo(path, names): |
| 12 | +def _write_ssi_echo(path, names, encoding="entity"): |
12 | 13 | """An SSI page echoing each variable as NAME=[value]. An unset |
13 | 14 | variable echoes the default "(none)", so a set-but-empty variable |
14 | 15 | ("[]") is distinguishable from one that never made it into the table. |
15 | 16 | """ |
16 | 17 | with open(path, "w") as f: |
17 | 18 | for name in names: |
18 | | - f.write(f'{name}=[<!--#echo var="{name}" -->]\n') |
| 19 | + f.write(f'{name}=[<!--#echo encoding="{encoding}" var="{name}" -->]\n') |
19 | 20 |
|
20 | 21 |
|
21 | 22 | class TestSetEnvFromFile: |
@@ -204,3 +205,167 @@ def test_metadata_002_04_missing_file(self, env): |
204 | 205 | good.add_vhost_test1() |
205 | 206 | good.install() |
206 | 207 | assert env.apache_restart() == 0 |
| 208 | + |
| 209 | + |
| 210 | +class TestSetEnvFromFileConfig: |
| 211 | + |
| 212 | + def check_config(self, env, contents): |
| 213 | + env_file = os.path.join(env.gen_dir, "setenv-read.env") |
| 214 | + with open(env_file, "w", newline="") as f: |
| 215 | + f.write(contents) |
| 216 | + |
| 217 | + conf = HttpdConf(env, extras={ |
| 218 | + 'base': f'SetEnvFromFile "{env_file}"', |
| 219 | + }) |
| 220 | + conf.add_vhost_test1() |
| 221 | + conf.install() |
| 222 | + return env.run([os.path.join(env.bin_dir, "httpd"), |
| 223 | + "-t", "-d", env.server_dir, |
| 224 | + "-f", os.path.join(env.server_dir, "conf", "httpd.conf")]) |
| 225 | + |
| 226 | + @pytest.mark.parametrize("line_length", [8191, 8192, 16384]) |
| 227 | + @pytest.mark.parametrize("ending", ["\n", ""], ids=["newline", "eof"]) |
| 228 | + def test_metadata_002_05_long_line(self, env, line_length, ending): |
| 229 | + line = "ENV_LONG=" + "x" * (line_length - len("ENV_LONG=")) |
| 230 | + contents = "ENV_BEFORE=before\n" + line + ending |
| 231 | + if ending: |
| 232 | + contents += "ENV_AFTER=after\n" |
| 233 | + r = self.check_config(env, contents) |
| 234 | + assert r.exit_code != 0, "oversized line was accepted" |
| 235 | + assert "setenv-read.env at line 2: Line too long" in r.stderr |
| 236 | + |
| 237 | + def test_metadata_002_06_long_continuation(self, env): |
| 238 | + r = self.check_config( |
| 239 | + env, "ENV_BEFORE=before\nENV_LONG=" + "x" * 4096 |
| 240 | + + "\\\n" + "y" * 4096 + "\nENV_AFTER=after\n") |
| 241 | + assert r.exit_code != 0, "oversized continued line was accepted" |
| 242 | + assert "setenv-read.env at line 3: Line too long" in r.stderr |
| 243 | + |
| 244 | + # The 8192-byte buffer needs room for a newline and the terminating NUL. |
| 245 | + @pytest.mark.parametrize("contents", [ |
| 246 | + pytest.param("", id="empty-file"), |
| 247 | + pytest.param("ENV_OK=present", id="no-final-newline"), |
| 248 | + pytest.param("ENV_OK=present\n", id="final-newline"), |
| 249 | + pytest.param("ENV_LONG=" + "x" * (8190 - len("ENV_LONG=")), |
| 250 | + id="max-line-no-newline"), |
| 251 | + pytest.param("ENV_LONG=" + "x" * (8190 - len("ENV_LONG=")) + "\n", |
| 252 | + id="max-line-with-newline"), |
| 253 | + pytest.param("ENV_LONG=" + "x" * 4096 + "\\\n" |
| 254 | + + "y" * (8190 - len("ENV_LONG=") - 4096) + "\n", |
| 255 | + id="max-continued-line"), |
| 256 | + ]) |
| 257 | + def test_metadata_002_07_normal_eof(self, env, contents): |
| 258 | + r = self.check_config(env, contents) |
| 259 | + assert r.exit_code == 0, r.stderr |
| 260 | + |
| 261 | + @pytest.mark.parametrize("value, message", [ |
| 262 | + pytest.param('"unfinished', "Unterminated quoted value", |
| 263 | + id="unclosed-double"), |
| 264 | + pytest.param("'unfinished", "Unterminated quoted value", |
| 265 | + id="unclosed-single"), |
| 266 | + pytest.param('"', "Unterminated quoted value", id="double-quote-only"), |
| 267 | + pytest.param("'", "Unterminated quoted value", id="single-quote-only"), |
| 268 | + pytest.param(r'"escaped\"', "Unterminated quoted value", |
| 269 | + id="escaped-double-closer"), |
| 270 | + pytest.param(r"'escaped\'", "Unterminated quoted value", |
| 271 | + id="escaped-single-closer"), |
| 272 | + pytest.param('"closed"extra', "Unexpected text after quoted value", |
| 273 | + id="adjacent-text"), |
| 274 | + pytest.param("'closed' extra", "Unexpected text after quoted value", |
| 275 | + id="separate-text"), |
| 276 | + pytest.param('"" "extra"', "Unexpected text after quoted value", |
| 277 | + id="extra-quoted-token"), |
| 278 | + pytest.param(' \t"unfinished', "Unterminated quoted value", |
| 279 | + id="unclosed-after-whitespace"), |
| 280 | + ]) |
| 281 | + def test_metadata_002_11_malformed_quotes(self, env, value, message): |
| 282 | + r = self.check_config( |
| 283 | + env, f"ENV_BEFORE=before\nENV_BAD={value}\nENV_AFTER=after\n") |
| 284 | + assert r.exit_code != 0, "malformed quoted value was accepted" |
| 285 | + assert f"setenv-read.env at line 2: {message}" in r.stderr |
| 286 | + |
| 287 | + |
| 288 | +class TestSetEnvFromFileQuoted: |
| 289 | + |
| 290 | + def check_value(self, env, file_value, expected): |
| 291 | + env_file = os.path.join(env.gen_dir, "setenv-quoted.env") |
| 292 | + with open(env_file, "w", newline="") as f: |
| 293 | + f.write(f"ENV_FILE={file_value}\nENV_AFTER=present\n") |
| 294 | + |
| 295 | + doc_dir = os.path.join(env.server_dir, "htdocs", "test1") |
| 296 | + os.makedirs(doc_dir, exist_ok=True) |
| 297 | + _write_ssi_echo(os.path.join(doc_dir, "quoted.shtml"), |
| 298 | + ["ENV_FILE", "ENV_DIRECT", "ENV_AFTER"], encoding="none") |
| 299 | + |
| 300 | + direct_value = expected.replace("\\", "\\\\").replace('"', '\\"') |
| 301 | + conf = HttpdConf(env, extras={ |
| 302 | + 'base': f""" |
| 303 | + SetEnvFromFile "{env_file}" |
| 304 | + SetEnv ENV_DIRECT "{direct_value}" |
| 305 | + <Directory "{doc_dir}"> |
| 306 | + Options +Includes |
| 307 | + AddType text/html .shtml |
| 308 | + AddOutputFilter INCLUDES .shtml |
| 309 | + </Directory> |
| 310 | + """, |
| 311 | + }) |
| 312 | + conf.add_vhost_test1() |
| 313 | + conf.install() |
| 314 | + assert env.apache_restart() == 0 |
| 315 | + |
| 316 | + r = env.curl_get(env.mkurl("http", "test1", "/quoted.shtml")) |
| 317 | + assert r.response, "no response: server may have crashed" |
| 318 | + assert r.response["status"] == 200 |
| 319 | + assert r.response["body"].decode("utf-8").splitlines() == [ |
| 320 | + f"ENV_FILE=[{expected}]", |
| 321 | + f"ENV_DIRECT=[{expected}]", |
| 322 | + "ENV_AFTER=[present]", |
| 323 | + ] |
| 324 | + |
| 325 | + @pytest.mark.parametrize("quote", ['"', "'"], ids=["double", "single"]) |
| 326 | + @pytest.mark.parametrize("expected", [ |
| 327 | + pytest.param("hello", id="plain"), |
| 328 | + pytest.param("hello ", id="trailing-space"), |
| 329 | + pytest.param(" hello", id="leading-space"), |
| 330 | + pytest.param(" hello ", id="surrounding-spaces"), |
| 331 | + pytest.param("", id="empty"), |
| 332 | + pytest.param(" \t ", id="whitespace-only"), |
| 333 | + pytest.param("hello world", id="embedded-space"), |
| 334 | + pytest.param('say "hello" and \'bye\'', id="quote-escaping"), |
| 335 | + pytest.param('"unfinished', id="literal-double-quote-prefix"), |
| 336 | + pytest.param("'unfinished", id="literal-single-quote-prefix"), |
| 337 | + pytest.param('"hello"', id="literal-double-quotes"), |
| 338 | + pytest.param("'hello'", id="literal-single-quotes"), |
| 339 | + pytest.param(r"C:\app\file", id="backslash-escaping"), |
| 340 | + pytest.param("C:\\app\\", id="trailing-backslash"), |
| 341 | + pytest.param(r"\n\t", id="literal-escapes"), |
| 342 | + pytest.param("key=value # literal", id="equals-and-hash"), |
| 343 | + ]) |
| 344 | + def test_metadata_002_08_quoted(self, env, quote, expected): |
| 345 | + value = expected.replace("\\", "\\\\").replace(quote, "\\" + quote) |
| 346 | + self.check_value(env, quote + value + quote, expected) |
| 347 | + |
| 348 | + @pytest.mark.parametrize("quote", ['"', "'"], ids=["double", "single"]) |
| 349 | + def test_metadata_002_09_quoted_whitespace(self, env, quote): |
| 350 | + self.check_value(env, f" \t{quote} hello {quote} \t", " hello ") |
| 351 | + |
| 352 | + @pytest.mark.parametrize("quote", ['"', "'"], ids=["double", "single"]) |
| 353 | + def test_metadata_002_10_quoted_continuation(self, env, quote): |
| 354 | + self.check_value(env, f"{quote}first \\\nsecond {quote}", "first second ") |
| 355 | + |
| 356 | + @pytest.mark.parametrize("file_value, expected", [ |
| 357 | + pytest.param("hello world", "hello world", id="embedded-space"), |
| 358 | + pytest.param(" hello world ", " hello world", id="whitespace"), |
| 359 | + pytest.param(r"C:\app\file", r"C:\app\file", id="literal-backslashes"), |
| 360 | + pytest.param(r"a\\b", r"a\\b", id="doubled-backslash"), |
| 361 | + pytest.param('hello "world"', 'hello "world"', id="embedded-quotes"), |
| 362 | + pytest.param("", "", id="empty"), |
| 363 | + pytest.param(" ", "", id="whitespace-only"), |
| 364 | + ]) |
| 365 | + def test_metadata_002_12_unquoted(self, env, file_value, expected): |
| 366 | + self.check_value(env, file_value, expected) |
| 367 | + |
| 368 | + @pytest.mark.parametrize("quote", ['"', "'"], ids=["double", "single"]) |
| 369 | + def test_metadata_002_13_literal_escapes(self, env, quote): |
| 370 | + value = r"C:\app\n\t" |
| 371 | + self.check_value(env, quote + value + quote, value) |
0 commit comments