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

[tor-commits] [bridgedb/master] Add unittests for bridgedb.parse.blacklist.parseBridgeBlacklistFile().



commit 0b68b8223ebec20e3bd6bdff0d3752509dadb337
Author: Isis Lovecruft <isis@xxxxxxxxxxxxxx>
Date:   Thu Nov 2 01:02:41 2017 +0000

    Add unittests for bridgedb.parse.blacklist.parseBridgeBlacklistFile().
---
 bridgedb/parse/blacklist.py           | 12 +++++++-
 bridgedb/test/test_parse_blacklist.py | 56 +++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/bridgedb/parse/blacklist.py b/bridgedb/parse/blacklist.py
index 092f3e1..0b129d0 100644
--- a/bridgedb/parse/blacklist.py
+++ b/bridgedb/parse/blacklist.py
@@ -59,9 +59,19 @@ def parseBridgeBlacklistFile(filename):
         else:
             for line in fh.readlines():
                 fields = line.split(' ', 1)
-                fingerprint, reason = fields[0], fields[1] if len(fields)==2 else ''
+
+                if len(fields) == 2:
+                    fingerprint, reason = fields[0].strip(), fields[1].strip()
+                else:
+                    fingerprint, reason = fields[0].strip(), ""
 
                 if isValidFingerprint(fingerprint):
+                    logging.info("Blacklisted %s. Reason: \"%s\"" %
+                                 (fingerprint, reason))
                     blacklist[fingerprint] = reason
+                else:
+                    logging.warn(("Can't blacklist %s (for reason \"%s\"): "
+                                  "invalid fingerprint") %
+                                 (fingerprint, reason))
 
     return blacklist
diff --git a/bridgedb/test/test_parse_blacklist.py b/bridgedb/test/test_parse_blacklist.py
new file mode 100644
index 0000000..5aa569e
--- /dev/null
+++ b/bridgedb/test/test_parse_blacklist.py
@@ -0,0 +1,56 @@
+# -*- coding: utf-8 -*-
+#
+# This file is part of BridgeDB, a Tor bridge distribution system.
+#
+# :authors: Isis Lovecruft 0xA3ADB67A2CDB8B35 <isis@xxxxxxxxxxxxxx>
+# :copyright: (c) 2017, Isis Lovecruft
+#             (c) 2017, The Tor Project, Inc.
+# :license: 3-Clause BSD, see LICENSE for licensing information
+
+"""Unittests for :class:`bridgedb.parse.blacklist` module."""
+
+from __future__ import print_function
+
+import logging
+import os
+
+from bridgedb.parse import blacklist
+
+BLACKLIST_ENTRIES = """\
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA doing bad stuff
+BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB b-b-b-b-bad to the bone
+CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC invalid fingerprint
+DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
+"""
+
+from twisted.trial import unittest
+from twisted.trial.unittest import SkipTest
+
+logging.disable(50)
+
+
+class ParseBridgeBlacklistFileTests(unittest.TestCase):
+    """Unittests for :func:`bridgedb.parse.blacklist.parseBridgeBlacklistFile`."""
+
+    def setUp(self):
+        self.fh = "TEST-blacklisted-bridges"
+
+        with open(self.fh, 'w') as fh:
+            fh.write(BLACKLIST_ENTRIES)
+            fh.flush()
+
+    def test_parseBridgeBlacklistFile(self):
+        blacklisted = blacklist.parseBridgeBlacklistFile(self.fh)
+
+        self.assertIn("A"*40, blacklisted.keys())
+        self.assertIn("B"*40, blacklisted.keys())
+        self.assertNotIn("C"*40, blacklisted.keys())
+        self.assertIn("D"*40, blacklisted.keys())
+
+        self.assertEqual(blacklisted.get("A"*40), "doing bad stuff")
+        self.assertEqual(blacklisted.get("B"*40), "b-b-b-b-bad to the bone")
+        self.assertEqual(blacklisted.get("D"*40), "")
+
+    def tearDown(self):
+        if os.path.isfile(self.fh):
+            os.unlink(self.fh)



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