tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

filelisting.py (1794B)


      1 #!/usr/bin/env python
      2 
      3 # This Source Code Form is subject to the terms of the Mozilla Public
      4 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 # You can obtain one at http://mozilla.org/MPL/2.0/.
      6 
      7 import os
      8 import re
      9 from urllib.request import urlopen
     10 
     11 import mozhttpd
     12 import mozunit
     13 import pytest
     14 
     15 
     16 @pytest.fixture(name="docroot")
     17 def fixture_docroot():
     18    """Returns a docroot path."""
     19    return os.path.dirname(os.path.abspath(__file__))
     20 
     21 
     22 @pytest.fixture(name="httpd")
     23 def fixture_httpd(docroot):
     24    """Yields a started MozHttpd server."""
     25    httpd = mozhttpd.MozHttpd(port=0, docroot=docroot)
     26    httpd.start(block=False)
     27    yield httpd
     28    httpd.stop()
     29 
     30 
     31 @pytest.mark.parametrize(
     32    "path",
     33    [
     34        pytest.param("", id="no_params"),
     35        pytest.param("?foo=bar&fleem=&foo=fleem", id="with_params"),
     36    ],
     37 )
     38 def test_filelist(httpd, docroot, path):
     39    f = urlopen(
     40        "http://{host}:{port}/{path}".format(
     41            host="127.0.0.1", port=httpd.httpd.server_port, path=path
     42        )
     43    )
     44 
     45    filelist = os.listdir(docroot)
     46 
     47    pattern = r"""\<[a-zA-Z0-9\-\_\.\="'\/\\%\!\@\#\$\^\&\*\(\) :;]*\>"""
     48 
     49    for line in f.readlines():
     50        subbed_lined = re.sub(pattern, "", line.decode().strip("\n"))
     51        webline = subbed_lined.strip("/").strip().strip("@")
     52 
     53        if (
     54            webline
     55            and not webline.startswith("Directory listing for")
     56            and not webline.startswith("<!DOCTYPE")
     57        ):
     58            msg = f"File {webline} in dir listing corresponds to a file"
     59            assert webline in filelist, msg
     60            filelist.remove(webline)
     61 
     62    msg = f"Should have no items in filelist ({filelist}) unaccounted for"
     63    assert len(filelist) == 0, msg
     64 
     65 
     66 if __name__ == "__main__":
     67    mozunit.main()