#!/usr/bin/python

import random
import string
import subprocess
import base64
import sys

sample = string.lowercase + string.digits + string.uppercase + string.punctuation

def random_word(digits):
    return (''.join(random.choice(sample) for i in range(digits)))

def main():
    for i in range(int(sys.argv[1])):
    	i = random_word(random.randrange(513) + 1)
    	p = subprocess.Popen(["valgrind",
                              "--suppressions=b64_test_valgrind_suppressions",
                              "--leak-check=full",
                              "--error-exitcode=1",
                              "./b64_test", i], stdout=subprocess.PIPE)
        o = p.communicate()[0]
        l = o.split('\n')
        b64 = base64.b64encode(i)
        if l[0] != "Base64: " + b64:
            print "Failed Encode for " + i
            print l[0] + " vs " + b64
            exit(1)
        else:
            print "Success Encode for " + i
            print "Success Encode " + l[0] + " vs " + b64
        if l[1] != "Decode: " + i:
            print "Failed Decode for " + i
            exit(1)
        if l[2] != "UUID: Pass":
            print("Failed UUID")
            exit(1)
        if p.returncode != 0:
            print "Return code: " + str(p.returncode)
            exit(1)

if __name__ == "__main__":
   main()
