<commit_before>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
#  +-----------+
#  |  BRIDGET  |
#  |        +--------------------------------------------+
#  +--------| Use a Tor process to test making a Tor     |
#           | connection to a list of bridges or relays. |
#           +--------------------------------------------+
#
# :authors: <NAME>, <NAME>
# :licence: see included LICENSE
# :version: 0.1.0-alpha

from __future__           import with_statement
from functools            import partial
from random               import randint

import os
import sys

from twisted.python       import usage
from twisted.internet     import defer, error, reactor

from ooni                 import nettest

from ooni.utils           import log, date
from ooni.utils.config    import ValueChecker

from ooni.utils.onion     import TxtorconImportError
from ooni.utils.onion     import PTNoBridgesException, PTNotFoundException


try:
    from ooni.utils.onion     import parse_data_dir
except:
    log.msg("Please go to /ooni/lib and do 'make txtorcon' to run this test!")

class MissingAssetException(Exception):
    pass

class RandomPortException(Exception):
    """Raised when using a random port conflicts with configured ports."""
    def __init__(self):
        log.msg("Unable to use random and specific ports simultaneously")
        return sys.exit()

class BridgetArgs(usage.Options):
    """Commandline options."""
    allowed = "Port to use for Tor's %s, must be between 1024 and 65535."
    sock_check = ValueChecker(allowed % "SocksPort").port_check
    ctrl_check = ValueChecker(allowed % "ControlPort").port_check

    optParameters = [
        ['bridges', 'b', None,
         'File listing bridge IP:ORPorts to test'],
        ['relays', 'f', None,
         'File listing relay IPs to test'],
        ['socks', 's', 9049, None, sock_check],
        ['control', 'c', 9052, None, ctrl_check],
        ['torpath', 'p', None,
         'Path to the Tor binary to use'],
        ['datadir', 'd', None,
         'Tor DataDirectory to use'],
        ['transport', 't', None,
         'Tor ClientTransportPlugin'],
        ['resume', 'r', 0,
         'Resume at this index']]
    optFlags = [['random', 'x', 'Use random ControlPort and SocksPort']]

    def postOptions(self):
        if not self['bridges'] and not self['relays']:
            raise MissingAssetException(
                "Bridget can't run without bridges or relays to test!")
        if self['transport']:
            ValueChecker.uid_check(
                "Can't run bridget as root with pluggable transports!")
            if not self['bridges']:
                raise PTNoBridgesException
        if self['socks'] or self['control']:
            if self['random']:
                raise RandomPortException
        if self['datadir']:
            ValueChecker.dir_check(self['datadir'])
        if self['torpath']:
            ValueChecker.file_check(self['torpath'])

class BridgetTest(nettest.TestCase):
    """
    XXX fill me in

    :ivar config:
        An :class:`ooni.lib.txtorcon.TorConfig` instance.
    :ivar relays:
        A list of all provided relays to test.
    :ivar bridges:<commit_msg>Fix typo in class used by bridget<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
#  +-----------+
#  |  BRIDGET  |
#  |        +--------------------------------------------+
#  +--------| Use a Tor process to test making a Tor     |
#           | connection to a list of bridges or relays. |
#           +--------------------------------------------+
#
# :authors: <NAME>, <NAME>
# :licence: see included LICENSE
# :version: 0.1.0-alpha

from __future__           import with_statement
from functools            import partial
from random               import randint

import os
import sys

from twisted.python       import usage
from twisted.internet     import defer, error, reactor

from ooni                 import nettest

from ooni.utils           import log, date
from ooni.utils.config    import ValueChecker

from ooni.utils.onion     import TxtorconImportError
from ooni.utils.onion     import PTNoBridgesException, PTNotFoundException


try:
    from ooni.utils.onion     import parse_data_dir
except:
    log.msg("Please go to /ooni/lib and do 'make txtorcon' to run this test!")

class MissingAssetException(Exception):
    pass

class RandomPortException(Exception):
    """Raised when using a random port conflicts with configured ports."""
    def __init__(self):
        log.msg("Unable to use random and specific ports simultaneously")
        return sys.exit()

class BridgetArgs(usage.Options):
    """Commandline options."""
    allowed = "Port to use for Tor's %s, must be between 1024 and 65535."
    sock_check = ValueChecker(allowed % "SocksPort").port_check
    ctrl_check = ValueChecker(allowed % "ControlPort").port_check

    optParameters = [
        ['bridges', 'b', None,
         'File listing bridge IP:ORPorts to test'],
        ['relays', 'f', None,
         'File listing relay IPs to test'],
        ['socks', 's', 9049, None, sock_check],
        ['control', 'c', 9052, None, ctrl_check],
        ['torpath', 'p', None,
         'Path to the Tor binary to use'],
        ['datadir', 'd', None,
         'Tor DataDirectory to use'],
        ['transport', 't', None,
         'Tor ClientTransportPlugin'],
        ['resume', 'r', 0,
         'Resume at this index']]
    optFlags = [['random', 'x', 'Use random ControlPort and SocksPort']]

    def postOptions(self):
        if not self['bridges'] and not self['relays']:
            raise MissingAssetException(
                "Bridget can't run without bridges or relays to test!")
        if self['transport']:
            ValueChecker.uid_check(
                "Can't run bridget as root with pluggable transports!")
            if not self['bridges']:
                raise PTNoBridgesException
        if self['socks'] or self['control']:
            if self['random']:
                raise RandomPortException
        if self['datadir']:
            ValueChecker.dir_check(self['datadir'])
        if self['torpath']:
            ValueChecker.file_check(self['torpath'])

class BridgetTest(nettest.NetTestCase):
    """
    XXX fill me in

    :ivar config:
        An :class:`ooni.lib.txtorcon.TorConfig` instance.
    :ivar relays:
        A list of all provided relays to test.
    :ivar bridges:
{'fraction_non_alphanumeric': 0.14118382008433547, 'fraction_numerical': 0.006871778853662346, 'mean_word_length': 2.194014962593516, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6231514', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1797, 'n_words': 575}