Added unit test

Fixed invalid argument
This commit is contained in:
Alexandre Possebom 2014-09-26 16:41:33 -03:00
parent 93c84f7584
commit 1e4f079789
6 changed files with 149 additions and 46 deletions

View File

@ -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'],
},
)

0
tests/__init__.py Normal file
View File

74
tests/test.py Normal file
View File

@ -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()

69
twik/run.py Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
"""
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

View File

@ -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

View File

@ -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()