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

[tor-commits] [stem/master] Tests for descriptor tutorial examples



commit 7d4efa2102727a1135bd19204e7d35f7b8900ec4
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Mar 4 00:10:07 2013 -0800

    Tests for descriptor tutorial examples
    
    Unit tests for our new 'mirror, mirror' tutorial examples, and writing our
    prior tests to conform with the way-less-sucky style that separates the mocking
    from the example.
---
 docs/tutorial/mirror_mirror_on_the_wall.rst |    4 +-
 test/unit/tutorial.py                       |  152 ++++++++++++++++++++-------
 2 files changed, 117 insertions(+), 39 deletions(-)

diff --git a/docs/tutorial/mirror_mirror_on_the_wall.rst b/docs/tutorial/mirror_mirror_on_the_wall.rst
index 3679ec1..617d898 100644
--- a/docs/tutorial/mirror_mirror_on_the_wall.rst
+++ b/docs/tutorial/mirror_mirror_on_the_wall.rst
@@ -87,7 +87,7 @@ through tor's control socket...
 
   from stem.descriptor import parse_file
 
-  for desc in parse_file("/home/atagar/.tor/cached-consensus"):
+  for desc in parse_file(open("/home/atagar/.tor/cached-consensus")):
     print "found relay %s (%s)" % (desc.nickname, desc.fingerprint)
 
 .. _where-can-i-get-past-descriptors:
@@ -128,7 +128,7 @@ this to work.
 
   import sys
 
-  from stem.contorl import Controller
+  from stem.control import Controller
   from stem.util import str_tools
 
   # provides a mapping of observed bandwidth to the relay nicknames
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 7d79eb2..0aec3a3 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -9,8 +9,16 @@ import sys
 import unittest
 
 from stem.control import Controller
+from stem.descriptor.reader import DescriptorReader
+from stem.descriptor.server_descriptor import RelayDescriptor
 from test import mocking
 
+MIRROR_MIRROR_OUTPUT = """\
+1. speedyexit (102.13 KB/s)
+2. speedyexit (102.13 KB/s)
+3. speedyexit (102.13 KB/s)
+"""
+
 
 class TestTutorial(unittest.TestCase):
   stdout, stdout_real = None, None
@@ -53,53 +61,123 @@ class TestTutorial(unittest.TestCase):
     tutorial_example()
     self.assertEqual("My Tor relay has read 33406 bytes and written 29649.\n", self.stdout.getvalue())
 
-  def test_mirror_mirror_on_the_wall(self):
-    from stem.descriptor.server_descriptor import RelayDescriptor
-    from stem.descriptor.reader import DescriptorReader
-    from stem.util import str_tools
+  def test_mirror_mirror_on_the_wall_1(self):
+    def tutorial_example():
+      from stem.control import Controller
 
-    exit_descriptor = mocking.get_relay_server_descriptor({
-     'router': 'speedyexit 149.255.97.109 9001 0 0'
-    }, content = True).replace('reject *:*', 'accept *:*')
-    exit_descriptor = mocking.sign_descriptor_content(exit_descriptor)
-    exit_descriptor = RelayDescriptor(exit_descriptor)
+      with Controller.from_port(control_port = 9051) as controller:
+        controller.authenticate()
 
-    reader_wrapper = mocking.get_object(DescriptorReader, {
-      '__enter__': lambda x: x,
-      '__exit__': mocking.no_op(),
-      '__iter__': mocking.return_value(iter((
-        exit_descriptor,
-        mocking.get_relay_server_descriptor(),  # non-exit
-        exit_descriptor,
-        exit_descriptor,
-      )))
+        for desc in controller.get_network_statuses():
+          print "found relay %s (%s)" % (desc.nickname, desc.fingerprint)
+
+    controller = mocking.get_object(Controller, {
+      'authenticate': mocking.no_op(),
+      'close': mocking.no_op(),
+      'get_network_statuses': mocking.return_value(
+        [mocking.get_router_status_entry_v2()],
+      ),
     })
 
-    # provides a mapping of observed bandwidth to the relay nicknames
-    def get_bw_to_relay():
-      bw_to_relay = {}
+    mocking.mock(
+      Controller.from_port, mocking.return_value(controller),
+      target_module = Controller,
+      is_static = True,
+    )
+
+    tutorial_example()
+    self.assertEqual("found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n", self.stdout.getvalue())
+
+  def test_mirror_mirror_on_the_wall_2(self):
+    def tutorial_example():
+      from stem.descriptor import parse_file
+
+      for desc in parse_file(open("/home/atagar/.tor/cached-consensus")):
+        print "found relay %s (%s)" % (desc.nickname, desc.fingerprint)
+
+    test_file = StringIO.StringIO(mocking.get_network_status_document_v3(
+      routers = [mocking.get_router_status_entry_v3()],
+      content = True,
+    ))
+
+    mocking.support_with(test_file)
+    test_file.name = "/home/atagar/.tor/cached-consensus"
+    mocking.mock(open, mocking.return_value(test_file))
+
+    tutorial_example()
+    self.assertEqual("found relay caerSidi (A7569A83B5706AB1B1A9CB52EFF7D2D32E4553EB)\n", self.stdout.getvalue())
+
+  def test_mirror_mirror_on_the_wall_3(self):
+    def tutorial_example():
+      from stem.descriptor.reader import DescriptorReader
 
-      with reader_wrapper as reader:
+      with DescriptorReader(["/home/atagar/server-descriptors-2013-03.tar"]) as reader:
         for desc in reader:
-          if desc.exit_policy.is_exiting_allowed():
-            bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
+          print "found relay %s (%s)" % (desc.nickname, desc.fingerprint)
 
-      return bw_to_relay
+    mocking.mock(
+      DescriptorReader.__iter__,
+      mocking.return_value(iter([mocking.get_relay_server_descriptor()])),
+      target_module = DescriptorReader
+    )
+
+    tutorial_example()
+    self.assertEqual("found relay caerSidi (None)\n", self.stdout.getvalue())
+
+  def test_mirror_mirror_on_the_wall_4(self):
+    def tutorial_example():
+      from stem.control import Controller
+      from stem.util import str_tools
+
+      # provides a mapping of observed bandwidth to the relay nicknames
+      def get_bw_to_relay():
+        bw_to_relay = {}
 
-    # prints the top fifteen relays
+        with Controller.from_port(control_port = 9051) as controller:
+          controller.authenticate()
 
-    bw_to_relay = get_bw_to_relay()
-    count = 1
+          for desc in controller.get_server_descriptors():
+            if desc.exit_policy.is_exiting_allowed():
+              bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
+
+        return bw_to_relay
+
+      # prints the top fifteen relays
+
+      bw_to_relay = get_bw_to_relay()
+      count = 1
+
+      for bw_value in sorted(bw_to_relay.keys(), reverse = True):
+        for nickname in bw_to_relay[bw_value]:
+          print "%i. %s (%s/s)" % (count, nickname, str_tools.get_size_label(bw_value, 2))
+          count += 1
+
+          if count > 15:
+            return
+
+    exit_descriptor = mocking.get_relay_server_descriptor({
+      'router': 'speedyexit 149.255.97.109 9001 0 0'
+    }, content = True).replace('reject *:*', 'accept *:*')
 
-    for bw_value in sorted(bw_to_relay.keys(), reverse = True):
-      for nickname in bw_to_relay[bw_value]:
-        print "%i. %s (%s/s)" % (count, nickname, str_tools.get_size_label(bw_value, 2))
+    exit_descriptor = mocking.sign_descriptor_content(exit_descriptor)
+    exit_descriptor = RelayDescriptor(exit_descriptor)
 
-        count += 1
+    controller = mocking.get_object(Controller, {
+      'authenticate': mocking.no_op(),
+      'close': mocking.no_op(),
+      'get_server_descriptors': mocking.return_value([
+        exit_descriptor,
+        mocking.get_relay_server_descriptor(),  # non-exit
+        exit_descriptor,
+        exit_descriptor,
+      ])
+    })
 
-        if count > 15:
-          return
+    mocking.mock(
+      Controller.from_port, mocking.return_value(controller),
+      target_module = Controller,
+      is_static = True,
+    )
 
-    expected_stdout = "".join(["%i. speedyexit (102.13 KB/s)\n" % i for i in xrange(1, 4)])
-    self.assertEqual(expected_stdout, self.stdout.getvalue())
-    self.assertEqual(4, count)
+    tutorial_example()
+    self.assertEqual(MIRROR_MIRROR_OUTPUT, self.stdout.getvalue())



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