tor-browser

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

test_mitm_addons.py (2709B)


      1 #!/usr/bin/env python
      2 import json
      3 import os
      4 from unittest import mock
      5 
      6 import mozunit
      7 
      8 here = os.path.dirname(__file__)
      9 os.environ["MOZPROXY_DIR"] = os.path.join(here, "files")
     10 
     11 protocol = {
     12    "http_protocol": {"aax-us-iad.amazon.com": "HTTP/1.1"},
     13    "recorded_requests": 4,
     14    "recorded_requests_unique": 1,
     15 }
     16 
     17 
     18 @mock.patch(
     19    "mozproxy.backends.mitm.scripts.http_protocol_extractor.HttpProtocolExtractor.get_ctx"
     20 )
     21 def test_http_protocol_generate_json_file(ctx_mock):
     22    ctx_mock.return_value.options.save_stream_file = os.path.join(
     23        os.environ["MOZPROXY_DIR"], "http_protocol_recording_done.mp"
     24    )
     25 
     26    from mozproxy.backends.mitm.scripts.http_protocol_extractor import (
     27        HttpProtocolExtractor,
     28    )
     29 
     30    test_http_protocol = HttpProtocolExtractor()
     31    test_http_protocol.ctx = test_http_protocol.get_ctx()
     32 
     33    # test data
     34    test_http_protocol.request_protocol = protocol["http_protocol"]
     35    test_http_protocol.hashes = ["Hash string"]
     36    test_http_protocol.request_count = protocol["recorded_requests"]
     37 
     38    test_http_protocol.done()
     39 
     40    json_path = os.path.join(
     41        os.environ["MOZPROXY_DIR"], "http_protocol_recording_done.json"
     42    )
     43    assert os.path.exists(json_path)
     44    with open(json_path) as json_file:
     45        output_data = json.load(json_file)
     46 
     47        assert output_data["recorded_requests"] == protocol["recorded_requests"]
     48        assert (
     49            output_data["recorded_requests_unique"]
     50            == protocol["recorded_requests_unique"]
     51        )
     52        assert output_data["http_protocol"] == protocol["http_protocol"]
     53 
     54 
     55 @mock.patch(
     56    "mozproxy.backends.mitm.scripts.http_protocol_extractor.HttpProtocolExtractor.get_ctx"
     57 )
     58 def test_http_protocol_response(ctx_mock):
     59    ctx_mock.return_value.options.save_stream_file = os.path.join(
     60        os.environ["MOZPROXY_DIR"], "http_protocol_recording_done.mp"
     61    )
     62 
     63    from mozproxy.backends.mitm.scripts.http_protocol_extractor import (
     64        HttpProtocolExtractor,
     65    )
     66 
     67    test_http_protocol = HttpProtocolExtractor()
     68    test_http_protocol.ctx = test_http_protocol.get_ctx()
     69 
     70    # test data
     71    flow = mock.MagicMock()
     72    flow.type = "http"
     73    flow.request.url = "https://www.google.com/complete/search"
     74    flow.request.port = 33
     75    flow.response.data.http_version = b"HTTP/1.1"
     76 
     77    test_http_protocol.request_protocol = {}
     78    test_http_protocol.hashes = []
     79    test_http_protocol.request_count = 0
     80 
     81    test_http_protocol.response(flow)
     82 
     83    assert test_http_protocol.request_count == 1
     84    assert len(test_http_protocol.hashes) == 1
     85    assert test_http_protocol.request_protocol["www.google.com"] == "HTTP/1.1"
     86 
     87 
     88 if __name__ == "__main__":
     89    mozunit.main(runwith="pytest")