Browse Source

Merge remote-tracking branch 'tor-github/pr/18'

teor 5 years ago
parent
commit
cdd4389230
6 changed files with 102 additions and 7 deletions
  1. 1 0
      .travis.yml
  2. 17 1
      lib/chutney/Debug.py
  3. 1 1
      lib/chutney/Templating.py
  4. 1 1
      lib/chutney/TorNet.py
  5. 4 4
      lib/chutney/Traffic.py
  6. 78 0
      tests/unit-tests.sh

+ 1 - 0
.travis.yml

@@ -163,6 +163,7 @@ install:
   - tor --version
 
 script:
+  - tests/unit-tests.sh
   - tools/test-network.sh --allow-failures 1
 
 after_failure:

+ 17 - 1
lib/chutney/Debug.py

@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # Copyright 2011 Nick Mathewson, Michael Stone
 # Copyright 2013 The Tor Project
@@ -11,6 +11,7 @@ from __future__ import print_function
 
 import cgitb
 import os
+import sys
 
 # Get verbose tracebacks, so we can diagnose better.
 cgitb.enable(format="plain")
@@ -24,3 +25,18 @@ def debug(s):
     "Print a debug message on stdout if debug_flag is True."
     if debug_flag:
         print("DEBUG: %s" % s)
+
+
+def main():
+    global debug_flag
+    debug("This message should appear if $CHUTNEY_DEBUG is true.")
+    debug_flag = True
+    debug("This message should always appear.")
+    debug_flag = False
+    debug("This message should never appear.")
+    # We don't test tracebacks, because it's hard to know what to expect
+    # (and they make python exit with a non-zero exit status)
+    return 0
+
+if __name__ == '__main__':
+    sys.exit(main())

+ 1 - 1
lib/chutney/Templating.py

@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # Copyright 2011 Nick Mathewson, Michael Stone
 #

+ 1 - 1
lib/chutney/TorNet.py

@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # Copyright 2011 Nick Mathewson, Michael Stone
 # Copyright 2013 The Tor Project

+ 4 - 4
lib/chutney/Traffic.py

@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 #
 # Copyright 2013 The Tor Project
 #
@@ -430,12 +430,12 @@ class TrafficTester():
 
 def main():
     """Test the TrafficTester by sending and receiving some data."""
-    DATA = "a foo is a bar" * 1000
-    proxy = ('localhost', 9008)
+    DATA = b"a foo is a bar" * 1000
     bind_to = ('localhost', int(sys.argv[1]))
 
     tt = TrafficTester(bind_to, DATA)
-    tt.add(Source(tt, bind_to, DATA, proxy))
+    # Don't use a proxy for self-testing, so that we avoid tor entirely
+    tt.add(Source(tt, bind_to, DATA))
     success = tt.run()
 
     if success:

+ 78 - 0
tests/unit-tests.sh

@@ -0,0 +1,78 @@
+#!/bin/sh
+
+# Exit on errors
+set -e
+# Verbose mode
+set -v
+
+
+# Output is prefixed with the name of the script
+myname=$(basename "$0")
+
+echo "$myname: finding chutney directory"
+TEST_DIR=$(dirname "$0")
+CHUTNEY_DIR=$(dirname "$TEST_DIR")
+
+echo "$myname: changing to chutney directory"
+cd "$CHUTNEY_DIR"
+
+
+echo "$myname: running Debug.py tests"
+
+LOG_FILE=$(mktemp)
+export LOG_FILE
+test -n "$LOG_FILE"
+
+unset CHUTNEY_DEBUG
+export CHUTNEY_DEBUG
+lib/chutney/Debug.py | tee "$LOG_FILE"
+LOG_FILE_LINES=$(wc -l < "$LOG_FILE")
+test "$LOG_FILE_LINES" -eq 1
+
+LOG_FILE=$(mktemp)
+export LOG_FILE
+test -n "$LOG_FILE"
+
+export CHUTNEY_DEBUG=1
+lib/chutney/Debug.py | tee "$LOG_FILE"
+LOG_FILE_LINES=$(wc -l < "$LOG_FILE")
+test "$LOG_FILE_LINES" -eq 2
+
+unset CHUTNEY_DEBUG
+export CHUTNEY_DEBUG
+
+
+echo "$myname: running Templating.py tests"
+
+LOG_FILE=$(mktemp)
+export LOG_FILE
+test -n "$LOG_FILE"
+
+lib/chutney/Templating.py torrc_templates/common.i | tee "$LOG_FILE"
+grep -q owning_controller_process "$LOG_FILE"
+grep -q connlimit "$LOG_FILE"
+grep -q controlport "$LOG_FILE"
+grep -q nick "$LOG_FILE"
+grep -q authorities "$LOG_FILE"
+grep -q dir "$LOG_FILE"
+
+
+echo "$myname: running Traffic.py tests"
+
+LOG_FILE=$(mktemp)
+export LOG_FILE
+test -n "$LOG_FILE"
+
+# Choose an arbitrary port
+PYTHONPATH=$PYTHONPATH:lib lib/chutney/Traffic.py 9999 | tee "$LOG_FILE"
+
+# Traffic.py produces output with a single newline. But we don't want to get
+# too picky about the details: allow an extra line and a few extra chars.
+LOG_FILE_LINES=$(wc -l < "$LOG_FILE")
+test "$LOG_FILE_LINES" -le 2
+LOG_FILE_CHARS=$(wc -c < "$LOG_FILE")
+test "$LOG_FILE_CHARS" -le 4
+
+
+# We don't test TorNet.py: it's integration tested with tor using the
+# chutney/chutney script