Files
PypherStream/testPypherStream.py

164 lines
4.9 KiB
Python

#Python/PypherStream/testPypherStream.py
#Matthew Ellison
# Created: 03-31-19
#Modified: 03-31-19
#This is a script to test the PypherStream project
#It starts by testing the individual classes then the larger program
from Stopwatch import Stopwatch
from Caesar import Caesar
from Playfair import Playfair
#Test the Caesar cipher
def caesarTest():
cipher = Caesar()
passed = True
inputString = "" #Holds the string to be input into the cipher
outputString = "" #Holds the string that the cipher is expected to output
cipherString = "" #Holds the string the the cipher actually output
#Test all lowercase
inputString = "abcdefghijklmnopqrstuvwxyz"
outputString = "defghijklmnopqrstuvwxyzabc"
#Try the encoding
cipherString = cipher.encode(3, inputString)
if(cipherString != outputString):
print("Caesar failed the first encoding test")
passed = False
#Try it decoding
cipherString = cipher.decode(3, outputString)
if(cipherString != inputString):
print("Caesar failed the first decoding test")
passed = False
#Test all uppercase
inputString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
outputString = "XYZABCDEFGHIJKLMNOPQRSTUVW"
#Try encoding
cipherString = cipher.encode(-3, inputString)
if(cipherString != outputString):
print("Caesar failed the second encoding test")
passed = False
#Try decoding
cipherString = cipher.decode(-3, outputString)
if(cipherString != inputString):
print("Caesar failed the second decoding test")
passed = False
#Test both cases
inputString = "AbCdEfGhIjKlMnOpQrStUvWxYz"
outputString = "DeFgHiJkLmNoPqRsTuVwXyZaBc"
#Try encoding
cipherString = cipher.encode(3, inputString)
if(cipherString != outputString):
print("Caesar failed the thrid encoding test")
passed = False
#Try decoding
cipherString = cipher.decode(3, outputString)
if(cipherString != inputString):
print("Caesar failed the third decoding test")
passed = False
#Punctuation
inputString = "abc,def. rst; wxyz"
outputString = "def,ghi. uvw; zabc"
#Try encoding
cipherString = cipher.encode(3, inputString)
if(cipherString != outputString):
print("Caesar failed the fourth encoding test")
passed = False
#Try decoding
cipherString = cipher.decode(3, outputString)
if(cipherString != inputString):
print("Caesar failed the fourth decoding test")
passed = False
#Something different
inputString = "you"
outputString = "iye"
#Try encoding
cipherString = cipher.encode(10, inputString)
if(cipherString != outputString):
print("Caesar failed the fifth encoding test")
passed = False
#Try decoding
cipherString = cipher.decode(10, outputString)
if(cipherString != inputString):
print("Caesar failed the fifth decoding test")
passed = False
#Print a message if there were no errors
if(passed):
print("Caesar passed all tests")
#Test the Playfair cipher
def playfairTest():
#Setup the variables
passed = True #Holds whether all tests have been passed
keyword = "" #Holds the keyword for the cipher
inputString = "" #Holds the string that will be encoded
outputString = "" #Holds the string that will be decoded
cipherString = "" #Holds the string that is returned from the cipher
cipher = Playfair() #The cipher itself
#Test from wikipedia
keyword = "Playfair Example"
inputString = "Hide the gold in the tree stump"
outputString = "BMODZBXDNABEKUDMUIXMMOUVIF"
#Try encoding
cipherString = cipher.encode(keyword, inputString)
if(cipherString != outputString):
print("Playfair failed the first encoding test")
passed = False
inputString = cipher.getInputString() #This makes sure input is a valid input for comparison later
#Try decoding
cipherString = cipher.decode(keyword, outputString)
if(cipherString != inputString):
print("Playfair failed the first decoding test")
passed = False
#Slight alteration to test from wikipedia
keyword = "Playfair Example"
inputString = "Hide the gold in the tree stum"
outputString = "BMODZBXDNABEKUDMUIXMMOUVIM"
#Try encoding
cipherString = cipher.encode(keyword, inputString)
if(cipherString != outputString):
print("Playfair failed the second encoding test")
passed = False
inputString = cipher.getInputString() #This makes sure input is a valid input for comparison later
#Try decoding
cipherString = cipher.decode(keyword, outputString)
if(cipherString != inputString):
print("Playfair failed the second decoding test")
passed = False
#Print a message if there were no errors
if(passed):
print("Playfair passed all tests")
#Run all the appropriate tests in the right order
if __name__ == "__main__":
#Create a timer to check the time of the tests
timer = Stopwatch()
#Test the Caesar cipher
timer.start()
caesarTest()
timer.stop()
print("It took " + timer.getString() + " to run this test\n")
#Test the Playfair cipher
timer.start()
playfairTest()
timer.stop()
print("It took " + timer.getString() + " to run this test")
""" Results:
Caesar passed all tests
It took 550.300 microseconds to run this test
"""