[tor-commits] [ooni-probe/master] Implement unittests for InputFile

art at torproject.org art at torproject.org
Fri Feb 28 14:32:20 UTC 2014


commit b3ce859ec0dab4d116fc810e3dc7884a036224a5
Author: Arturo Filastò <art at fuffa.org>
Date:   Tue Jan 28 23:16:01 2014 +0100

    Implement unittests for InputFile
---
 ooni/deck.py            |   12 +++++++----
 ooni/tests/test_deck.py |   52 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 4 deletions(-)

diff --git a/ooni/deck.py b/ooni/deck.py
index 260f8b4..ebf2a18 100644
--- a/ooni/deck.py
+++ b/ooni/deck.py
@@ -14,9 +14,9 @@ import json
 from hashlib import sha256
 
 class InputFile(object):
-    def __init__(self, input_hash):
+    def __init__(self, input_hash, base_path=config.inputs_directory):
         self.id = input_hash
-        cache_path = os.path.join(config.inputs_directory, input_hash)
+        cache_path = os.path.join(base_path, input_hash)
         self.cached_file = cache_path
         self.cached_descriptor = cache_path + '.desc'
     
@@ -81,20 +81,24 @@ def nettest_to_path(path):
         raise e.NetTestNotFound(path)
 
 class Deck(InputFile):
-    def __init__(self, deck_hash=None, deckFile=None):
+    def __init__(self, deck_hash=None, 
+                 deckFile=None,
+                 decks_directory=config.decks_directory):
         self.id = deck_hash
         self.bouncer = None
         self.netTestLoaders = []
         self.inputs = []
         self.testHelpers = {}
 
+        self.decksDirectory = decks_directory
+
         self.deckHash = deck_hash
  
         if deckFile: self.loadDeck(deckFile)
 
     @property
     def cached_file(self):
-        return os.path.join(config.decks_directory, self.deckHash)
+        return os.path.join(self.decksDirectory, self.deckHash)
    
     @property
     def cached_descriptor(self):
diff --git a/ooni/tests/test_deck.py b/ooni/tests/test_deck.py
new file mode 100644
index 0000000..627bad9
--- /dev/null
+++ b/ooni/tests/test_deck.py
@@ -0,0 +1,52 @@
+import os
+
+from twisted.trial import unittest
+
+from hashlib import sha256
+from ooni.deck import InputFile
+
+dummy_deck_content = """- options:
+    collector: null
+    help: 0
+    logfile: null
+    no-default-reporter: 0
+    parallelism: null
+    pcapfile: null
+    reportfile: null
+    resume: 0
+    subargs: []
+    test_file: some_dummy_test
+    testdeck: null
+"""
+
+class TestInputFile(unittest.TestCase):
+    def test_file_cached(self):
+        file_hash = sha256(dummy_deck_content).hexdigest()
+        input_file = InputFile(file_hash, base_path='.')
+        with open(file_hash, 'w+') as f:
+            f.write(dummy_deck_content)
+        assert input_file.fileCached
+
+    def test_file_invalid_hash(self):
+        invalid_hash = 'a'*64
+        with open(invalid_hash, 'w+') as f:
+            f.write("b"*100)
+        input_file = InputFile(invalid_hash, base_path='.')
+        self.assertRaises(AssertionError, input_file.verify)
+
+    def test_save_descriptor(self):
+        descriptor = {
+                'name': 'spam',
+                'id': 'spam',
+                'version': 'spam',
+                'author': 'spam',
+                'date': 'spam',
+                'description': 'spam'
+        }
+        file_id = 'a'*64
+        input_file = InputFile(file_id, base_path='.')
+        input_file.load(descriptor)
+        input_file.save()
+        assert os.path.isfile(file_id)
+
+        assert input_file.descriptorCached





More information about the tor-commits mailing list