<commit_before>import pytest


def test_tor_mirror_present(File):
    """
    Ensure the FPF mirror of the Tor apt repo, tor-apt.freedom.press,
    is configured. This repository required manual updating with current
    tor releases, to avoid breakage of untested updates.
    """
    f = File('/etc/apt/sources.list.d/tor_apt_freedom_press.list')

    regex = ('^deb https:\/\/tor-apt\.freedom\.press trusty main$')
    assert f.contains(regex)


def test_tor_keyring_absent(Command):
    """
    Tor packages are installed via the FPF apt mirror, and signed with the
    SecureDrop Release Signing Key. As such, the official Tor public key
    should *not* be present, since we don't want to install packages
    from that source.
    """
    # Can't use the TestInfra Package module to check state=absent,
    # so let's check by shelling out to `dpkg -l`. Dpkg will automatically
    # honor simple regex in package names.
    package = "deb.torproject.org-keyring"
    c = Command("dpkg -l {}".format(package))
    assert c.rc == 1
    error_text = "dpkg-query: no packages found matching {}".format(package)
    assert c.stderr == error_text


@pytest.mark.parametrize('tor_key_info', [
    "pub   2048R/886DDD89 2009-09-04 [expires: 2020-08-29]",
    "Key fingerprint = <KEY>  F512 EE8C BC9E 886D DD89",
    "deb.torproject.org archive signing key",
])
def test_tor_mirror_fingerprint(Command, tor_key_info):
    """
    Legacy test. The Tor Project key was added to SecureDrop servers
    via the `deb.torproject.org-keyring` package. Since FPF started mirroring
    the official Tor apt repository, we no longer need the key to be present.

    Since the `deb.torproject.org-keyring` package is installed on already
    running instances, the public key will still be present. We'll need
    to remove those packages separately.<commit_msg>Fix stderr str comparison on tor keyring test<commit_after>import pytest


def test_tor_mirror_present(File):
    """
    Ensure the FPF mirror of the Tor apt repo, tor-apt.freedom.press,
    is configured. This repository required manual updating with current
    tor releases, to avoid breakage of untested updates.
    """
    f = File('/etc/apt/sources.list.d/tor_apt_freedom_press.list')

    regex = ('^deb https:\/\/tor-apt\.freedom\.press trusty main$')
    assert f.contains(regex)


def test_tor_keyring_absent(host):
    """
    Tor packages are installed via the FPF apt mirror, and signed with the
    SecureDrop Release Signing Key. As such, the official Tor public key
    should *not* be present, since we don't want to install packages
    from that source.
    """
    # Can't use the TestInfra Package module to check state=absent,
    # so let's check by shelling out to `dpkg -l`. Dpkg will automatically
    # honor simple regex in package names.
    package = "deb.torproject.org-keyring"
    c = host.run("dpkg -l {}".format(package))
    assert c.rc == 1
    error_text = "dpkg-query: no packages found matching {}".format(package)
    assert c.stderr.rstrip() == error_text


@pytest.mark.parametrize('tor_key_info', [
    "pub   2048R/886DDD89 2009-09-04 [expires: 2020-08-29]",
    "Key fingerprint = <KEY>  F512 EE8C BC9E 886D DD89",
    "deb.torproject.org archive signing key",
])
def test_tor_mirror_fingerprint(Command, tor_key_info):
    """
    Legacy test. The Tor Project key was added to SecureDrop servers
    via the `deb.torproject.org-keyring` package. Since FPF started mirroring
    the official Tor apt repository, we no longer need the key to be present.

    Since the `deb.torproject.org-keyring` package is installed on already
    running instances, the public key will still be present. We'll need
    to remove those packages separately.
{'fraction_non_alphanumeric': 0.09253811179459749, 'fraction_numerical': 0.019256485691361326, 'mean_word_length': 3.441805225653207, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3293754', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1126, 'n_words': 414}