Пример #1
0
 def test_parse_nickname_isValidRouterNickname_invalid_None(self):
     """isValidRouterNickname(None) should return False."""
     name = None
     self.assertFalse(isValidRouterNickname(name))
Пример #2
0
 def test_parse_nickname_isValidRouterNickname_invalid_spaces(self):
     """isValidRouterNickname() should return False for an invalid nickname
     (contains spaces).
     """
     name = 'As you wish'
     self.assertFalse(isValidRouterNickname(name))
Пример #3
0
 def test_parse_nickname_isValidRouterNickname_invalid_too_long(self):
     """isValidRouterNickname() should return False for an invalid nickname
     (too long).
     """
     name = 'ThisIsReallyMoreOfANovellaRatherThanAnOnionRouterNickname'
     self.assertFalse(isValidRouterNickname(name))
Пример #4
0
 def test_parse_nickname_isValidRouterNickname_invalid_too_short(self):
     """isValidRouterNickname() should return False for an invalid nickname
     (empty string).
     """
     name = ''
     self.assertFalse(isValidRouterNickname(name))
Пример #5
0
 def test_parse_nickname_isValidRouterNickname_invalid_symbols(self):
     """isValidRouterNickname() should return False for an invalid nickname
     (with symbols in it).
     """
     name = 'what_the_bl#@p?!'
     self.assertFalse(isValidRouterNickname(name))
Пример #6
0
 def test_parse_nickname_isValidRouterNickname_valid_1(self):
     """isValidRouterNickname() should return True for a valid nickname."""
     name = 'maketotaldestroy'
     self.assertTrue(isValidRouterNickname(name))
Пример #7
0
 def test_parse_nickname_isValidRouterNickname_valid(self):
     """isValidRouterNickname() should return True for a valid nickname."""
     name = 'Unmentionable'
     self.assertTrue(isValidRouterNickname(name))
def parseRLine(line):
    """Parse an 'r'-line from a networkstatus document.

    From torspec.git/dir-spec.txt, commit 36761c7d553d L1499-1512:
      | "r" SP nickname SP identity SP digest SP publication SP IP SP ORPort
      |     SP DirPort NL
      |
      |     [At start, exactly once.]
      |
      |     "Nickname" is the OR's nickname.  "Identity" is a hash of its
      |     identity key, encoded in base64, with trailing equals sign(s)
      |     removed.  "Digest" is a hash of its most recent descriptor as
      |     signed (that is, not including the signature), encoded in base64.
      |     "Publication" is the
      |     publication time of its most recent descriptor, in the form
      |     YYYY-MM-DD HH:MM:SS, in UTC.  "IP" is its current IP address;
      |     ORPort is its current OR port, "DirPort" is its current directory
      |     port, or "0" for "none".

    :param string line: An 'r'-line from an bridge-network-status descriptor.
    :returns:
        A 7-tuple of::
            (nickname, identityDigest, descriptorDigest, timestamp,
             orAddress, orPort, dirport)
        where each value is set according to the data parsed from the
        **line**, or ``None`` if nothing suitable could be parsed.
    """
    (nickname, ID, descDigest, timestamp,
     ORaddr, ORport, dirport) = (None for x in xrange(7))

    try:
        if not line.startswith('r '):
            raise NetworkstatusParsingError(
                "Networkstatus parser received non 'r'-line: %r" % line)

        line = line[2:] # Chop off the 'r '
        fields = line.split()

        if len(fields) != 8:
            raise NetworkstatusParsingError(
                "Wrong number of fields in networkstatus 'r'-line: %r" % line)

        nickname, ID = fields[:2]

        try:
            ID = parseUnpaddedBase64(ID)
        except InvalidBase64 as error:
            raise InvalidNetworkstatusRouterIdentity(error)

        # Check the nickname validity after parsing the ID, otherwise, if the
        # nickname is invalid, we end up with the nickname being ``None`` and
        # the ID being unparsed, unpadded (meaning it is technically invalid)
        # base64.
        isValidRouterNickname(nickname)

    except NetworkstatusParsingError as error:
        logging.error(error)
        nickname, ID = None, None
    except InvalidRouterNickname as error:
        logging.error(error)
        # Assume that we mostly care about obtaining the OR's ID, then it
        # should be okay to set the nickname to ``None``, if it was invalid.
        nickname = None
    except InvalidNetworkstatusRouterIdentity as error:
        logging.error(error)
        ID = None
    else:
        try:
            descDigest = parseUnpaddedBase64(fields[2])
            timestamp = time.mktime(time.strptime(" ".join(fields[3:5]),
                                                  "%Y-%m-%d %H:%M:%S"))
            ORaddr = fields[5]
            ORport = int(fields[6])
            dirport = fields[7]
        except InvalidBase64 as error:
            logging.error(error)
            descDigest = None
        except (AttributeError, ValueError, IndexError) as error:
            logging.error(error)
            timestamp = None
    finally:
        return (nickname, ID, descDigest, timestamp, ORaddr, ORport, dirport)
def parseRLine(line):
    """Parse an 'r'-line from a networkstatus document.

    From torspec.git/dir-spec.txt, commit 36761c7d553d L1499-1512:
      | "r" SP nickname SP identity SP digest SP publication SP IP SP ORPort
      |     SP DirPort NL
      |
      |     [At start, exactly once.]
      |
      |     "Nickname" is the OR's nickname.  "Identity" is a hash of its
      |     identity key, encoded in base64, with trailing equals sign(s)
      |     removed.  "Digest" is a hash of its most recent descriptor as
      |     signed (that is, not including the signature), encoded in base64.
      |     "Publication" is the
      |     publication time of its most recent descriptor, in the form
      |     YYYY-MM-DD HH:MM:SS, in UTC.  "IP" is its current IP address;
      |     ORPort is its current OR port, "DirPort" is its current directory
      |     port, or "0" for "none".

    :param string line: An 'r'-line from an bridge-network-status descriptor.
    :returns:
        A 7-tuple of::
            (nickname, identityDigest, descriptorDigest, timestamp,
             orAddress, orPort, dirport)
        where each value is set according to the data parsed from the
        **line**, or ``None`` if nothing suitable could be parsed.
    """
    (nickname, ID, descDigest, timestamp, ORaddr, ORport,
     dirport) = (None for x in xrange(7))

    try:
        if not line.startswith('r '):
            raise NetworkstatusParsingError(
                "Networkstatus parser received non 'r'-line: %r" % line)

        line = line[2:]  # Chop off the 'r '
        fields = line.split()

        if len(fields) != 8:
            raise NetworkstatusParsingError(
                "Wrong number of fields in networkstatus 'r'-line: %r" % line)

        nickname, ID = fields[:2]

        try:
            ID = parseUnpaddedBase64(ID)
        except InvalidBase64 as error:
            raise InvalidNetworkstatusRouterIdentity(error)

        # Check the nickname validity after parsing the ID, otherwise, if the
        # nickname is invalid, we end up with the nickname being ``None`` and
        # the ID being unparsed, unpadded (meaning it is technically invalid)
        # base64.
        isValidRouterNickname(nickname)

    except NetworkstatusParsingError as error:
        logging.error(error)
        nickname, ID = None, None
    except InvalidRouterNickname as error:
        logging.error(error)
        # Assume that we mostly care about obtaining the OR's ID, then it
        # should be okay to set the nickname to ``None``, if it was invalid.
        nickname = None
    except InvalidNetworkstatusRouterIdentity as error:
        logging.error(error)
        ID = None
    else:
        try:
            descDigest = parseUnpaddedBase64(fields[2])
            timestamp = time.mktime(
                time.strptime(" ".join(fields[3:5]), "%Y-%m-%d %H:%M:%S"))
            ORaddr = fields[5]
            ORport = int(fields[6])
            dirport = fields[7]
        except InvalidBase64 as error:
            logging.error(error)
            descDigest = None
        except (AttributeError, ValueError, IndexError) as error:
            logging.error(error)
            timestamp = None
    finally:
        return (nickname, ID, descDigest, timestamp, ORaddr, ORport, dirport)