#tests.py

from time import time
from sha256 import sha256_hash as sh

#testing if our SHA-256 hash function generates the correct hashes by asserting reference test vectors
#from https://www.cosic.esat.kuleuven.be/nessie/testvectors/hash/sha/Sha-2-256.unverified.test-vectors,
#https://www.di-mgt.com.au/sha_testvectors.html and https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program
#tests can be invoked in the shell

#test vector array
#tva[i][0] : pretty form of input message
#tva[i][1] : input message
#tva[i][3] : validated SHA-256 hash/digest for the input string
tva = (
    ('""', '',
     'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'),
    ('"a"', 'a',
     'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb'),
    ('"message digest"', 'message digest',
     'f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650'),
    ('"abc"', 'abc',
     'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'),
    ('"1234567890" * 8', '1234567890' * 8,
     'f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e'),
    ('"<KEY>"',
     'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq',
     '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1'),
    ('"<KEY>"',
     '<KEY>',
     'cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1'),
    ('"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
     'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
     'db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0'),
    ('"a" * 1000000', 'a' * 1000000,
     'cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0'),
    #(
    #    '"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno * 16777216"',
    #    'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno' *
    #    16777216,
    #    'ecbbc42cbf296603acb2c6bc0410ef4378bafb24b710357f12df607758b33e2b'
    #)
)


#read file and extract test vectors
def file2tva(filename):
    tva = []
    file = open(filename, 'r')
    lines = file.readlines()
    file.close()
    l = len(lines)
    for i in range(l):
        m = lines[i].split(' ')
        if (m[0] == 'Msg'):
            tva.append(("0x" + m[2][:-1], bytearray.fromhex(m[2][:-1]),
                        lines[i + 1].split(' ')[2][:-1]))
            i += 2

    #since length of the first test vector is 0, setting it to empty string
    if (filename == "tva\SHA256ShortMsg.rsp"):
        x = list(tva[0])
        x[1] = ""
        tva[0] = tuple(x)
    return tuple(tva)


def tests(tva):
    '''Assert test vectors with the hashes generated by our SHA-256 hash function'''
    i = len(tva)
    for j in range(i):
        h = sh(tva[j][1])
        print("Input string           : ", tva[j][0])
        print("Expected SHA-256 hash  : ", tva[j][2])
        print("Generated SHA-256 hash : ", h, end="\n\n")
        assert h == tva[j][2]
    print("\nAll tests passed successfully")


if __name__ == '__main__':
    tests(tva)