#!/bin/python2

import collections
import re
import subprocess
import sys

PUC = "../pamu2fcfg/pamu2fcfg"

resident = ["", "-r"]

presence = ["", "-P"]

pin = ["", "-N"]

verification = ["", "-V"]

Credential = collections.namedtuple("Credential", "keyhandle pubkey attributes oldformat")

sshformat = 0

def print_test_case(filename, sshformat, credentials):

    start = """
  cfg.auth_file = "{authfile}";
  cfg.sshformat = {ssh};
  rc = get_devices_from_authfile(&cfg, username, dev, &n_devs);
  assert(rc == 1);
  assert(n_devs == {devices});
"""

    checks = """
  assert(strcmp(dev[{i}].coseType, "es256") == 0);
  assert(strcmp(dev[{i}].keyHandle, "{kh}") == 0);
  assert(strcmp(dev[{i}].publicKey, "{pk}") == 0);
  assert(strcmp(dev[{i}].attributes, "{attr}") == 0);
  assert(dev[{i}].old_format == {old});
"""

    free = """
  free(dev[{i}].coseType);
  free(dev[{i}].attributes);
  free(dev[{i}].keyHandle);
  free(dev[{i}].publicKey);
"""
    end = """
  memset(dev, 0, sizeof(dev_t) * {devices});
"""

    code = ""
    free_block = ""

    code += start.format(authfile = filename, ssh = sshformat, devices = len(credentials))
    for c, v in enumerate(credentials):
        code += checks.format(i = c, kh = v.keyhandle, pk = v.pubkey, attr = v.attributes, old = v.oldformat)
        free_block += free.format(i = c)

    code += free_block + end.format(devices = len(credentials))

    print(code)


# Single credentials
print >> sys.stderr, "Generating single credentials"

for r in resident:
    for p in presence:
        for n in pin:
            for v in verification:
                filename = "credentials/new_" + r + p + v + n
                print >> sys.stderr, "Generating " + filename + ".templ"
                line = subprocess.check_output([PUC, "-u@USERNAME@", r, p, v, n])

                matches = re.match(r'^.*?:(.*?),(.*?),es256,(.*)', line, re.M)
                with open(filename + ".templ", "w") as outfile:
                    outfile.write(line)
                credentials = [Credential(keyhandle = matches.group(1),
                                         pubkey = matches.group(2),
                                         attributes = matches.group(3),
                                         oldformat = 0)]

                print_test_case(filename + ".cred", sshformat, credentials)


# Double credentials
print >> sys.stderr, "Generating double credentials"

for r in resident:
    for p in presence:
        for n in pin:
            for v in verification:
                filename = "credentials/new_double_" + r + p + v + n
                print >> sys.stderr, "Generating " + filename + ".templ"
                line = subprocess.check_output([PUC, "-u@USERNAME@", r, p, v, n])

                matches = re.match(r'^.*?:(.*?),(.*?),es256,(.*)', line, re.M)
                with open(filename + ".templ", "w") as outfile:
                    outfile.write(line)
                credentials = [Credential(keyhandle = matches.group(1),
                                         pubkey = matches.group(2),
                                         attributes = matches.group(3),
                                         oldformat = 0)]

                line = subprocess.check_output([PUC, "-n", r, p, v, n])

                matches = re.match(r'^.*?:(.*?),(.*?),es256,(.*)', line, re.M)
                with open(filename + ".templ", "a") as outfile:
                    outfile.write(line)
                credentials += [Credential(keyhandle = matches.group(1),
                                         pubkey = matches.group(2),
                                         attributes = matches.group(3),
                                         oldformat = 0)]

                print_test_case(filename + ".cred", sshformat, credentials)

# Mixed credentials
print >> sys.stderr, "Mixed double credentials"

options = [("", ""), ("", "-P"), ("-P", ""), ("-P", "-P")]

for p1, p2 in options:
    filename = "credentials/new_mixed_" + p1 +"1" + p2 + "2"
    print >> sys.stderr, "Generating " + filename + ".templ"
    line = subprocess.check_output([PUC, "-u@USERNAME@", p1])

    matches = re.match(r'^.*?:(.*?),(.*?),es256,(.*)', line, re.M)
    with open(filename + ".templ", "w") as outfile:
        outfile.write(line)
    credentials = [Credential(keyhandle = matches.group(1),
                              pubkey = matches.group(2),
                              attributes = matches.group(3),
                              oldformat = 0)]

    line = subprocess.check_output([PUC, "-n", p2])

    matches = re.match(r'^.*?:(.*?),(.*?),es256,(.*)', line, re.M)
    with open(filename + ".templ", "a") as outfile:
        outfile.write(line)
    credentials += [Credential(keyhandle = matches.group(1),
                               pubkey = matches.group(2),
                               attributes = matches.group(3),
                               oldformat = 0)]

    print_test_case(filename + ".cred", sshformat, credentials)

[((18, 13, 18, 90), 'collections.namedtuple', 'collections.namedtuple', ({(18, 36, 18, 48): '"""Credential"""', (18, 50, 18, 89): '"""keyhandle pubkey attributes oldformat"""'}, {}), "('Credential', 'keyhandle pubkey attributes oldformat')", False, 'import collections\n'), ((124, 11, 124, 61), 'subprocess.check_output', 'subprocess.check_output', ({(124, 35, 124, 60): "[PUC, '-u@USERNAME@', p1]"}, {}), "([PUC, '-u@USERNAME@', p1])", False, 'import subprocess\n'), ((126, 14, 126, 66), 're.match', 're.match', ({(126, 23, 126, 53): '"""^.*?:(.*?),(.*?),es256,(.*)"""', (126, 55, 126, 59): 'line', (126, 61, 126, 65): 're.M'}, {}), "('^.*?:(.*?),(.*?),es256,(.*)', line, re.M)", False, 'import re\n'), ((134, 11, 134, 51), 'subprocess.check_output', 'subprocess.check_output', ({(134, 35, 134, 50): "[PUC, '-n', p2]"}, {}), "([PUC, '-n', p2])", False, 'import subprocess\n'), ((136, 14, 136, 66), 're.match', 're.match', ({(136, 23, 136, 53): '"""^.*?:(.*?),(.*?),es256,(.*)"""', (136, 55, 136, 59): 'line', (136, 61, 136, 65): 're.M'}, {}), "('^.*?:(.*?),(.*?),es256,(.*)', line, re.M)", False, 'import re\n'), ((72, 23, 72, 81), 'subprocess.check_output', 'subprocess.check_output', ({(72, 47, 72, 80): "[PUC, '-u@USERNAME@', r, p, v, n]"}, {}), "([PUC, '-u@USERNAME@', r, p, v, n])", False, 'import subprocess\n'), ((74, 26, 74, 78), 're.match', 're.match', ({(74, 35, 74, 65): '"""^.*?:(.*?),(.*?),es256,(.*)"""', (74, 67, 74, 71): 'line', (74, 73, 74, 77): 're.M'}, {}), "('^.*?:(.*?),(.*?),es256,(.*)', line, re.M)", False, 'import re\n'), ((94, 23, 94, 81), 'subprocess.check_output', 'subprocess.check_output', ({(94, 47, 94, 80): "[PUC, '-u@USERNAME@', r, p, v, n]"}, {}), "([PUC, '-u@USERNAME@', r, p, v, n])", False, 'import subprocess\n'), ((96, 26, 96, 78), 're.match', 're.match', ({(96, 35, 96, 65): '"""^.*?:(.*?),(.*?),es256,(.*)"""', (96, 67, 96, 71): 'line', (96, 73, 96, 77): 're.M'}, {}), "('^.*?:(.*?),(.*?),es256,(.*)', line, re.M)", False, 'import re\n'), ((104, 23, 104, 71), 'subprocess.check_output', 'subprocess.check_output', ({(104, 47, 104, 70): "[PUC, '-n', r, p, v, n]"}, {}), "([PUC, '-n', r, p, v, n])", False, 'import subprocess\n'), ((106, 26, 106, 78), 're.match', 're.match', ({(106, 35, 106, 65): '"""^.*?:(.*?),(.*?),es256,(.*)"""', (106, 67, 106, 71): 'line', (106, 73, 106, 77): 're.M'}, {}), "('^.*?:(.*?),(.*?),es256,(.*)', line, re.M)", False, 'import re\n')]