tor-browser

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

test_utils.py (3350B)


      1 import os
      2 import sys
      3 import tempfile
      4 from unittest.mock import patch
      5 
      6 import mozunit
      7 import pytest
      8 import yaml
      9 
     10 # need this so raptor imports work both from /raptor and via mach
     11 here = os.path.abspath(os.path.dirname(__file__))
     12 
     13 raptor_dir = os.path.join(os.path.dirname(here), "raptor")
     14 sys.path.insert(0, raptor_dir)
     15 
     16 from utils import bool_from_str, transform_platform, write_yml_file
     17 
     18 
     19 @pytest.mark.parametrize("platform", ["win", "mac", "linux64"])
     20 def test_transform_platform(platform):
     21    transformed = transform_platform("mitmproxy-rel-bin-{platform}.manifest", platform)
     22    assert "{platform}" not in transformed
     23    if platform == "mac":
     24        assert "osx" in transformed
     25    else:
     26        assert platform in transformed
     27 
     28 
     29 def test_transform_platform_no_change():
     30    starting_string = "nothing-in-here-to-transform"
     31    assert transform_platform(starting_string, "mac") == starting_string
     32 
     33 
     34 @pytest.mark.parametrize("processor", ["x86_64", "other"])
     35 def test_transform_platform_processor(processor):
     36    transformed = transform_platform(
     37        "string-with-processor-{x64}.manifest", "win", processor
     38    )
     39    assert "{x64}" not in transformed
     40    if processor == "x86_64":
     41        assert "_x64" in transformed
     42 
     43 
     44 @pytest.mark.parametrize(
     45    "platform, processor, version",
     46    [
     47        ("mac", None, None),
     48        ("mac", "arm", None),
     49        ("mac", "arm", "11.0.0"),
     50        ("mac", None, "11.0.0"),
     51        ("mac", None, "8.1.1"),
     52    ],
     53 )
     54 def test_transform_platform_macos_arm(platform, processor, version):
     55    # For this test assume platform is mac for all
     56    transformed = transform_platform(
     57        "mitmproxy-rel-bin-{platform}.manifest", platform, processor, version
     58    )
     59    assert "{platform}" not in transformed
     60    if processor == "arm" and version != "11.0.0":
     61        assert "osx-arm64" not in transformed
     62    if processor == "arm" and version == "11.0.0":
     63        assert "osx-arm64" in transformed
     64    if not processor and not version:
     65        # include check for .manifest extension so no ambiguity
     66        assert "osx.manifest" in transformed
     67    if not processor and version == "11.0.0":
     68        # E.g. intel macs using latest mitmproxy
     69        assert "osx.manifest" in transformed
     70    if not processor and version != "11.0.0":
     71        assert "osx.manifest" in transformed
     72 
     73 
     74 @patch("logger.logger.RaptorLogger.info")
     75 def test_write_yml_file(mock_info):
     76    yml_file = os.path.join(tempfile.mkdtemp(), "utils.yaml")
     77 
     78    yml_data = dict(args=["--a", "apple", "--b", "banana"], env=dict(LOG_VERBOSE=1))
     79 
     80    assert not os.path.exists(yml_file)
     81    write_yml_file(yml_file, yml_data)
     82    assert os.path.exists(yml_file)
     83 
     84    with open(yml_file) as yml_in:
     85        yml_loaded = yaml.unsafe_load(yml_in)
     86        assert yml_loaded == yml_data
     87 
     88 
     89 @pytest.mark.parametrize(
     90    "value, expected_result",
     91    [
     92        ("true", True),
     93        ("TRUE", True),
     94        ("True", True),
     95        ("false", False),
     96        ("FALSE", False),
     97        ("False", False),
     98    ],
     99 )
    100 def test_bool_from_str(value, expected_result):
    101    assert expected_result == bool_from_str(value)
    102 
    103 
    104 @pytest.mark.parametrize("invalid_value", ["invalid_str", ""])
    105 def test_bool_from_str_with_invalid_values(invalid_value):
    106    with pytest.raises(ValueError):
    107        bool_from_str(invalid_value)
    108 
    109 
    110 if __name__ == "__main__":
    111    mozunit.main()