From 1e4f07978974a825e05bbf4d5204b191d7ded3d6 Mon Sep 17 00:00:00 2001 From: Alexandre Possebom Date: Fri, 26 Sep 2014 16:41:33 -0300 Subject: [PATCH] Added unit test Fixed invalid argument --- setup.py | 4 +-- tests/__init__.py | 0 tests/test.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ twik/run.py | 69 +++++++++++++++++++++++++++++++++++++++++++ twik/twik.py | 40 ------------------------- twik/util.py | 8 ++--- 6 files changed, 149 insertions(+), 46 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test.py create mode 100644 twik/run.py diff --git a/setup.py b/setup.py index c6499ba..a8e02b3 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages import sys, os -version = '0.3' +version = '0.4' setup(name='twik', version=version, @@ -13,6 +13,6 @@ setup(name='twik', license='GPLv3', packages=['twik'], entry_points = { - 'console_scripts': ['twik = twik.twik:main'], + 'console_scripts': ['twik = twik.run:main'], }, ) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test.py b/tests/test.py new file mode 100644 index 0000000..1804dc6 --- /dev/null +++ b/tests/test.py @@ -0,0 +1,74 @@ +import unittest +from twik import twik + +class SimplisticTest(unittest.TestCase): + def setUp(self): + self.t = twik.Twik() + + def testPasswordAphanumericAndSpecialChars(self): + for chars in range(4, 27): + password = self.t.getpassword('tag', + 'TFCY2AJI-NBPU-V01E-F7CP-PJIZNRKPF25W', 'foobar', chars, 1) + if chars == 4: + self.assertEqual(password, 'm3/I') + if chars == 8: + self.assertEqual(password, 'mb/5AsJ9') + if chars == 12: + self.assertEqual(password, 'mb/5AsJ9Uon7') + if chars == 22: + self.assertEqual(password, 'mb15As*9Uon7ZzvcsXMjpV') + if chars == 26: + self.assertEqual(password, 'mb15AsJ9&on7ZzvcsXMjpVLTqQ') + + + def testPasswordAlphanumeric(self): + for chars in range(4, 27): + password = self.t.getpassword('tag', + 'TFCY2AJI-NBPU-V01E-F7CP-PJIZNRKPF25W', 'foobar', chars, 2) + if chars == 4: + self.assertEqual(password, 'm31I') + if chars == 8: + self.assertEqual(password, 'mb15AsJ9') + if chars == 12: + self.assertEqual(password, 'mb15AsJ9Uon7') + if chars == 22: + self.assertEqual(password, 'mb15AsJ9Uon7ZzvcsXMjpV') + if chars == 26: + self.assertEqual(password, 'mb15AsJ9Uon7ZzvcsXMjpVLTqQ') + + def testPasswordNumeric(self): + for chars in range(4, 27): + password = self.t.getpassword('tag', + 'TFCY2AJI-NBPU-V01E-F7CP-PJIZNRKPF25W', 'foobar', chars, 3) + if chars == 4: + self.assertEqual(password, '4315') + if chars == 8: + self.assertEqual(password, '43154099') + if chars == 12: + self.assertEqual(password, '431540992657') + if chars == 22: + self.assertEqual(password, '4315409926570734032171') + if chars == 26: + self.assertEqual(password, '43154099265707340321711986') + + def testSize(self): + for chars in range(4, 27): + password = self.t.getpassword('tag', + 'TFCY2AJI-NBPU-V01E-F7CP-PJIZNRKPF25W', 'foobar', chars, 1) + self.assertEqual(len(password), chars) + + def testSizeWrong(self): + password = self.t.getpassword('tag', + 'TFCY2AJI-NBPU-V01E-F7CP-PJIZNRKPF25W', 'foobar', 100, 1) + self.assertEqual(password, None) + def testSize(self): + password = self.t.getpassword('tag', + 'TFCY2AJI-NBPU-V01E-F7CP-PJIZNRKPF25W', 'foobar', 0, 1) + self.assertEqual(password, None) + + def tearDown(self): + self.t = None + +if __name__ == '__main__': + unittest.main() + diff --git a/twik/run.py b/twik/run.py new file mode 100644 index 0000000..75bf4f2 --- /dev/null +++ b/twik/run.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +# -*- coding: utf-8; -*- +""" +* Copyright 2014 Alexandre Possebom +* Copyright 2014 Red Dye No. 2 +* Copyright (C) 2011-2013 TG Byte Software GmbH +* Copyright (C) 2009-2011 Thilo-Alexander Ginkel. +* Copyright (C) 2010-2014 Eric Woodruff +* Copyright (C) 2006-2010 Steve Cooper + +This file is part of twik. + +Twik is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Twik is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Twik. If not, see . +""" +from util import Util +from twik import Twik +import getpass +import argparse +import sys + +def main(): + """ + ALPHANUMERIC_AND_SPECIAL_CHARS=1 + ALPHANUMERIC=2 + NUMERIC=3 + """ + parser = argparse.ArgumentParser() + parser.add_argument("tag", type=str, + help="generate password for a specified tag") + parser.add_argument("-c", "--chars", type=int, + choices=range(4, 27), + metavar="[4-26]", + help="length of generated password [4-26]. Default: 12") + parser.add_argument("-p", "--profile", type=str, default='Profile', + help="profile to use. Default:'Profile'") + parser.add_argument("-t", "--passwordtype", type=int, choices=[1, 2, 3], + help=''' + 1 for ALPHANUMERIC_AND_SPECIAL_CHAR + 2 for ALPHANUMERIC + 3 for NUMERIC + Default: 1 + ''') + args = parser.parse_args() + + util = Util(args.tag, args.chars, args.passwordtype, args.profile) + + master_key = getpass.getpass(prompt='Master Key: ') + + twik = Twik() + password = twik.getpassword(args.tag, util.get_privatekey(), master_key, + util.get_chars(), util.get_passord_type()) + + print "Your password is %s" % password + +if __name__ == "__main__": + main() + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/twik/twik.py b/twik/twik.py index ff364db..c8bd2e9 100644 --- a/twik/twik.py +++ b/twik/twik.py @@ -99,44 +99,4 @@ class Twik(object): password = self.generatehash(mhash, master_key, length, password_type) return password -def main(): - """ - ALPHANUMERIC_AND_SPECIAL_CHARS=1 - ALPHANUMERIC=2 - NUMERIC=3 - """ - parser = argparse.ArgumentParser() - parser.add_argument("tag", type=str, - help="generate password for a specified tag") - parser.add_argument("-c", "--chars", type=int, default=-1, - help="length of generated password [4-26]. Default: 12") - parser.add_argument("-p", "--profile", type=str, default='Profile', - help="profile to use. Default:'Profile'") - parser.add_argument("-t", "--passwordtype", type=int, choices=[1, 2, 3], - default=-1, - help=''' - 1 for ALPHANUMERIC_AND_SPECIAL_CHAR - 2 for ALPHANUMERIC - 3 for NUMERIC - Default: 1 - ''') - args = parser.parse_args() - - if args.chars > 26 or args.chars < 4: - print "Invalid password length [4-26]" - sys.exit(2) - - util = Util(args.tag, args.chars, args.passwordtype, args.profile) - - master_key = getpass.getpass(prompt='Master Key: ') - - twik = Twik() - password = twik.getpassword(args.tag, util.get_privatekey(), master_key, - util.get_chars(), util.get_passord_type()) - - print "Your password is %s" % password - -if __name__ == "__main__": - main() - # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/twik/util.py b/twik/util.py index 1a79a3c..e54542f 100644 --- a/twik/util.py +++ b/twik/util.py @@ -87,10 +87,10 @@ class Util(object): def get_chars(self): config_key = '%s_chars' % self.tag - if self.config.has_option(self.profile, config_key) and self.chars == -1: + if self.config.has_option(self.profile, config_key) and self.chars == None: self.chars = self.config.getint(self.profile, config_key) else: - if self.chars == -1: + if self.chars == None: self.chars = 12 self.config.set(self.profile, config_key, self.chars) self.writeconfig() @@ -100,10 +100,10 @@ class Util(object): def get_passord_type(self): config_key = '%s_password_type' % self.tag - if self.config.has_option(self.profile, config_key) and self.pass_type == -1: + if self.config.has_option(self.profile, config_key) and self.pass_type == None: self.pass_type = self.config.getint(self.profile, config_key) else: - if self.pass_type == -1: + if self.pass_type == None: self.pass_type = 1 self.config.set(self.profile, config_key, self.pass_type) self.writeconfig()