Skip to content

Commit

Permalink
Implement unittests for InputFile
Browse files Browse the repository at this point in the history
  • Loading branch information
hellais committed Jan 28, 2014
1 parent a09bbab commit b3ce859
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
12 changes: 8 additions & 4 deletions ooni/deck.py
Expand Up @@ -14,9 +14,9 @@
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'

Expand Down Expand Up @@ -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):
Expand Down
52 changes: 52 additions & 0 deletions 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

0 comments on commit b3ce859

Please sign in to comment.