[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [sbws/master] stem: return a new Tor configuration dictionary



commit a44ff55d4d6adc8161ecaf3dc74989422ce06309
Author: juga0 <juga@xxxxxxxxxx>
Date:   Thu Dec 6 18:28:41 2018 +0000

    stem: return a new Tor configuration dictionary
    
    when parsing user configuration, instead of modifying the one
    passed to the function.
    
    Fixes bug #28738. Bugfix v0.1.1
---
 sbws/util/stem.py            | 14 +++++++-------
 tests/unit/util/test_stem.py | 13 +++++++++++++
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/sbws/util/stem.py b/sbws/util/stem.py
index bd8f7d0..d2fa5d8 100644
--- a/sbws/util/stem.py
+++ b/sbws/util/stem.py
@@ -119,14 +119,15 @@ def _init_controller_socket(socket):
 
 def parse_user_torrc_config(torrc, torrc_text):
     """Parse the user configuration torrc text call `extra_lines`
-    to a dictionary suitable to use with stem and update the existing torrc.
+    to a dictionary suitable to use with stem and return a new torrc
+    dictionary that merges that dictionary with the existing torrc.
     Example:
         [tor]
         extra_lines =
             Log debug file /tmp/tor-debug.log
             NumCPUs 1
     """
-
+    torrc_dict = torrc.copy()
     for line in torrc_text.split('\n'):
         # Remove leading and trailing whitespace, if any
         line = line.strip()
@@ -144,7 +145,7 @@ def parse_user_torrc_config(torrc, torrc_text):
                  key, value)
         # It's really easy to add to the torrc if the key doesn't exist
         if key not in torrc:
-            torrc.update({key: value})
+            torrc_dict.update({key: value})
         # But if it does, we have to make a list of values. For example, say
         # the user wants to add a SocksPort and we already have
         # 'SocksPort auto' in the torrc. We'll go from
@@ -154,12 +155,12 @@ def parse_user_torrc_config(torrc, torrc_text):
         else:
             existing_val = torrc[key]
             if isinstance(existing_val, str):
-                torrc.update({key: [existing_val, value]})
+                torrc_dict.update({key: [existing_val, value]})
             else:
                 assert isinstance(existing_val, list)
                 existing_val.append(value)
-                torrc.update({key: existing_val})
-    return torrc
+                torrc_dict.update({key: existing_val})
+    return torrc_dict
 
 
 def launch_tor(conf):
@@ -186,7 +187,6 @@ def launch_tor(conf):
     })
 
     torrc = parse_user_torrc_config(torrc, conf['tor']['extra_lines'])
-
     # Finally launch Tor
     try:
         stem.process.launch_tor_with_config(
diff --git a/tests/unit/util/test_stem.py b/tests/unit/util/test_stem.py
index 888f2db..8edf789 100644
--- a/tests/unit/util/test_stem.py
+++ b/tests/unit/util/test_stem.py
@@ -11,3 +11,16 @@ def test_parse_user_torrc_config_new_keyvalue_options_success():
     torrc_dict = parse_user_torrc_config({}, config_torrc_extra_lines)
     assert torrc_dict == \
         {'Log': 'debug file /tmp/tor-debug.log', 'NumCPUs': '1'}
+
+
+def test_parse_user_torrc_config_existing_keyvalue_options_fail(caplog):
+    torrc_dict = {'SocksPort': 'auto'}
+    config_torrc_extra_lines = """
+    SocksPort 9050
+    """
+    torrc_dict_new = parse_user_torrc_config(
+        torrc_dict, config_torrc_extra_lines)
+    # the new dictionary contains the existing key option and a list with both
+    # the existing value and the new value
+    assert torrc_dict_new != torrc_dict
+    assert torrc_dict_new == {'SocksPort': ['auto', '9050']}



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits