[tor-commits] [stem/master] Test 'tor --hash-password'

atagar at torproject.org atagar at torproject.org
Mon Feb 9 04:30:53 UTC 2015


commit 186b09101f828b7075c6b3aea096a5cccb20dc71
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Feb 5 08:56:02 2015 -0800

    Test 'tor --hash-password'
    
    Checking both in the happy case and when we don't provide an argument.
---
 test/integ/process.py |   35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/test/integ/process.py b/test/integ/process.py
index ef4182c..e1a1432 100644
--- a/test/integ/process.py
+++ b/test/integ/process.py
@@ -2,6 +2,9 @@
 Tests the stem.process functions with various use cases.
 """
 
+import binascii
+import hashlib
+import re
 import shutil
 import subprocess
 import tempfile
@@ -71,6 +74,38 @@ class TestProcess(unittest.TestCase):
     self.assertTrue("[warn] Failed to parse/validate config: Unknown option 'invalid_argument'.  Failing." in output)
     self.assertTrue("[err] Reading config failed--see warnings above." in output)
 
+  def test_hash_password(self):
+    """
+    Hash a controller password. It's salted so can't assert that we get a
+    particular value. Also, tor's output is unnecessarily verbose so including
+    hush to cut it down.
+    """
+
+    output = self.run_tor('--hush', '--hash-password', 'my_password')
+    self.assertTrue(re.match("^16:[0-9A-F]{58}\n$", output))
+
+    # I'm not gonna even pretend to understand the following. Ported directly
+    # from tor's test_cmdline_args.py.
+
+    output_hex = binascii.a2b_hex(output.strip()[3:])
+    salt, how, hashed = output_hex[:8], ord(output_hex[8]), output_hex[9:]
+    count = (16 + (how & 15)) << ((how >> 4) + 6)
+    stuff = salt + 'my_password'
+    repetitions = count // len(stuff) + 1
+    inp = (stuff * repetitions)[:count]
+
+    self.assertEqual(hashlib.sha1(inp).digest(), hashed)
+
+  def test_hash_password_requires_argument(self):
+    """
+    Check that 'tor --hash-password' balks if not provided with something to
+    hash.
+    """
+
+    output = self.run_tor('--hash-password', expect_failure = True)
+    self.assertTrue("[warn] Command-line option '--hash-password' with no value. Failing." in output)
+    self.assertTrue("[err] Reading config failed--see warnings above." in output)
+
   def test_launch_tor_with_config(self):
     """
     Exercises launch_tor_with_config.





More information about the tor-commits mailing list