torproject / stem

Python controller library for Tor
https://stem.torproject.org/
GNU Lesser General Public License v3.0
257 stars 75 forks source link

Descriptor from_str() lacks type detection #80

Open atagar opened 3 years ago

atagar commented 3 years ago

Our Descriptor class' from_str function converts strings to descriptors. When used from a subclass we should (and are documented as) providing a descriptor of that type. However, this fails with...

from stem.descriptor.networkstatus import NetworkStatusDocumentV3

with open('/home/atagar/Desktop/stem/test/unit/descriptor/data/cached-consensus') as consensus_file:
  consensus = NetworkStatusDocumentV3.from_str(consensus_file.read())
Traceback (most recent call last):
  File "broken_demo.py", line 4, in <module>
    consensus = NetworkStatusDocumentV3.from_str(consensus_file.read())
  File "/home/atagar/Desktop/stem/stem/descriptor/__init__.py", line 870, in from_str
    results = list(parse_file(io.BytesIO(stem.util.str_tools._to_bytes(content)), **kwargs))
  File "/home/atagar/Desktop/stem/stem/descriptor/__init__.py", line 448, in parse_file
    for desc in parse(descriptor_file):  # type: ignore
  File "/home/atagar/Desktop/stem/stem/descriptor/__init__.py", line 446, in parse
    raise TypeError("Unable to determine the descriptor's type. filename: '%s', first line: '%s'" % (filename, stem.util.str_tools._to_unicode(first_line)))
TypeError: Unable to determine the descriptor's type. filename: '<undefined>', first line: 'network-status-version 3'

We only have from_str unit tests for server and extrainfo descriptors, which coincidentally work because our parse method can infer their type from the content. This ticket is for two things...

  1. Every descriptor type should have a from_str unit test.
  2. Using from_str with a subclass should populate the descriptor type.