Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests #47

Merged
merged 9 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
qrcode
Cryptography
googletrans==3.0.0
googletrans==3.1.0a0
colorama
pillow
numpy
Expand Down
4 changes: 4 additions & 0 deletions src/cipher/cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ def print_options():
print('''
Not implemented.
''')

def test(args):
raise Exception('Not implemented')
return {'status': False, 'msg': 'Not Implemented'}
34 changes: 21 additions & 13 deletions src/cipher/ciphers/L33T.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,16 @@ class L33T(Cipher): #make sure you change this from text to your cipher
'c': '(',
'e': '3',
'g': '6',
'h': '#',
'i': '¡',
'h': 'H',
'i': '1',
'k': 'X',
'l': '1',
'l': 'l',
'o': '0',
'p': '9',
's': '5',
't': '7',
'x': '*',
'z': '2',
'0': 'O',
'1': 'L',
'2': 'Z',
'3': 'E',
'4': 'A',
'5': 'S',
'6': 'G',
'7': 'T',
'8': 'B',
'9': 'P',
}
inverse_leet_speak = dict((v, k) for k,v in leet_speak.items())

Expand Down Expand Up @@ -76,3 +66,21 @@ def print_options():
python main.py l33t -e -t 'hello'
python main.py l33t -d -t 'h3llo'
''')

def test(args):
total = 2

args.text = 'hello'
expect = 'H3ll0'
out = L33T.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

args.text, expect = expect, args.text
out = L33T.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

return {'status': True, 'msg': f'Ran {total} tests'}
18 changes: 18 additions & 0 deletions src/cipher/ciphers/b64.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ def print_options():
python main.py text -e -t 'hello'
python main.py text -d -t 'hello'
''')

def test(args):
total = 2

args.text = 'hello'
expect = 'aGVsbG8='
out = B64.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode {args.text}
expected "{args.text}" got "{out['text']}"'''}

args.text, expect = expect, args.text
out = B64.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode {args.text}
expected "{args.text}" got "{out['text']}"'''}

return {'status': True, 'msg': f'Ran {total} tests'}
20 changes: 20 additions & 0 deletions src/cipher/ciphers/bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,23 @@ def print_options():
python main.py text -e -t 'hello'
python main.py text -d -t 'hello'
''')

def test(args):
total = 2

args.text = 'hello'
expect = '1101000 1100101 1101100 1101100 1101111'
# NOTE (marvhus): Should the binary output have a byte length of 7 or 8?
out = bin.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

args.text, expect = expect, args.text
out = bin.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

return {'status': True, 'msg': f'Ran {total} tests'}

59 changes: 59 additions & 0 deletions src/cipher/ciphers/cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,62 @@ def print_options():
python main.py cc -e -t "hello" -k 10 -ex '123456789'
python main.py cc -d -t "hello" -k 10 -ex '123456789'
''')

def test(args):
total = 0
expect = [
'hello',
'ifmmp',
'jgnnq',
'khoor',
'lipps',
'mjqqt',
'nkrru',
'olssv',
'pmttw',
'qnuux',
'rovvy',
'spwwz',
'tqxxa',
'uryyb',
'vszzc',
'wtaad',
'xubbe',
'yvccf',
'zwddg',
'axeeh',
'byffi',
'czggj',
'dahhk',
'ebiil',
'fcjjm',
'gdkkn',
'hello',
]
for i in range(1, 26):
total += 1
args.text = 'hello'
args.key = i
out = CC.encode(args)
if not out['success']:
return {'status': False, 'msg': f'''
Encoding failed: "{out['text']}"'''}

if out['text'] not in expect[i]:
return {'status': False, 'msg': f'''Failed to encode "hello"
expected "{expect[i]}" with key {i} got {out['text']}'''}

for i in range(1, 26):
total += 1
args.text = expect[i]
args.key = i
out = CC.decode(args)
if not out['success']:
return {'status': False, 'msg': f'''
Decoding failed: "{out['text']}"'''}

if out['text'] not in 'hello':
return {'status': False, 'msg': f'''Failed to decode "{args.text}"
expected "hello" with key {i} got {out['text']}'''}

return {'status': True, 'msg': f'Ran {total} tests'}
25 changes: 21 additions & 4 deletions src/cipher/ciphers/hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Hex(Cipher): #make sure you change this from text to your cipher

def encode(args):
text = args.text
filename = args.output

if not text:
return {'text': "No input text", 'success': False}
Expand All @@ -24,7 +23,6 @@ def encode(args):

def decode(args):
text = args.text
filename = args.output

if not text:
return {'text': "No input text", 'success': False}
Expand All @@ -42,9 +40,28 @@ def print_options():

### Input
-t / --text ------ input text
-o / -output ----- output to file

### Example
python3 main.py -e -t 'Hello world!' -o ~/hello.txt
python3 main.py -e -t 'Hello world!'
''')
#TODO(marvhus): Remove -o/--output and instead implement it in the Main.output() function

def test(args):
total = 2

args.text = 'hello'
expect = '68656c6c6f'
out = Hex.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

args.text = expect
expect = 'hello'
out = Hex.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

return {'status': True, 'msg': f'Ran {total} tests'}

19 changes: 19 additions & 0 deletions src/cipher/ciphers/mor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,22 @@ def print_options():
python main.py mor -e -t 'hello'
python main.py mor -d -t '.... . .-.. .-.. ---'
''')

def test(args):
total = 2

args.text = 'HELLO'
expect = '.... . .-.. .-.. ---'
out = Mor.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

args.text, expect = expect, args.text
out = Mor.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

return {'status': True, 'msg': f'Ran {total} tests'}

18 changes: 18 additions & 0 deletions src/cipher/ciphers/oct.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ def print_options():
python main.py oct -e -t 'hello'
python main.py oct -d -t '150 145 154 154 157'
''')

def test(args):
total = 2

args.text = 'hello'
expect = '150 145 154 154 157'
out = Oct.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

args.text, expect = expect, args.text
out = Oct.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

return {'status': True, 'msg': f'Ran {total} tests'}
18 changes: 18 additions & 0 deletions src/cipher/ciphers/r13.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ def print_options():
python main.py r13 -e -t 'hello'
python main.py r13 -d -t 'hello'
''')

def test(args):
total = 2

args.text = 'hello'
expect = 'uryyb'
out = R13.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

args.text, expect = expect, args.text
out = R13.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

return {'status': True, 'msg': f'Ran {total} tests'}
19 changes: 19 additions & 0 deletions src/cipher/ciphers/r47.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,22 @@ def print_options():
python main.py r47 -e -t 'hello'
python main.py r47 -d -t 'hello'
''')

def test(args):
total = 2

args.text = 'hello'
expect = '96==@'
out = R47.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode {args.text}
expected {args.text} got {out['text']}'''}

args.text, expect = expect, args.text
out = R47.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode {args.text}
expected {args.text} got {out['text']}'''}

return {'status': True, 'msg': f'Ran {total} tests'}

18 changes: 18 additions & 0 deletions src/cipher/ciphers/rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,21 @@ def print_options():
python main.py rc -e -t 'hello'
python main.py rc -d -t 'hello'
''')

def test(args):
total = 2

args.text = 'hello'
expect = 'olleh'
out = RC.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode {args.text}
expected {args.text} got {out['text']}'''}

args.text, expect = expect, args.text
out = RC.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode {args.text}
expected {args.text} got {out['text']}'''}

return {'status': True, 'msg': f'Ran {total} tests'}
17 changes: 17 additions & 0 deletions src/cipher/ciphers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,20 @@ def print_options():
### Example
python main.py text -t 'hello'
''')

def test(args):
total = 2
args.text = 'hello'
out = Text.encode(args)
if not out['success'] or args.text not in out['text']:
return {'status': False, 'msg': f'''Failed to encode {args.text}
expected {args.text} got {out['text']}'''}

args.text = 'hello'
out = Text.decode(args)
if not out['success'] or args.text != out['text']:
return {'status': False, 'msg': f'''Failed to decode {args.text}
expected {args.text} got {out['text']}'''}

return {'status': True, 'msg': f'Ran {total} tests'}

27 changes: 25 additions & 2 deletions src/cipher/ciphers/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Translate(Cipher): #make sure you change this from text to your cipher

@staticmethod
def translate(text, src_lang, dest_lang):
tranlator = Translator()
translated = tranlator.translate(text, src=src_lang, dest=dest_lang)
translator = Translator()
translated = translator.translate(text, src=src_lang, dest=dest_lang)
return translated.text

@staticmethod
Expand Down Expand Up @@ -62,3 +62,26 @@ def print_options():
python main.py translate -e -t 'hello' -src 'en' -dest 'no'
python main.py translate -d -t 'hallo' -src 'no' -dest 'en'
''')

def test(args):
total = 2

args.text = 'hello'
args.src_lang = 'en'
args.dest_lang = 'no'
expect = 'Hallo'
out = Translate.encode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to encode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

args.text = 'hallo'
args.src_lang = 'no'
args.dest_lang = 'en'
expect = 'hello'
out = Translate.decode(args)
if not out['success'] or out['text'] != expect:
return {'status': False, 'msg': f'''Failed to decode "{args.text}"
expected "{expect}" got "{out['text']}"'''}

return {'status': True, 'msg': f'Ran {total} tests'}
Loading