import os

import pysatl
from pysatl import CAPDU


if __name__ == "__main__":

    def check(hexstr, expected):
        capdu = CAPDU.from_hexstr(hexstr)
        if capdu != expected:
            raise Exception("Mismatch for input '"+hexstr+"'\nActual:   "+str(capdu)+"\nExpected: "+str(expected))


    def gencase(* ,LC ,LE):
        assert(LC <  0x10000)
        assert(LE <= 0x10000)
        data = os.getrandom(LC)
        hexstr = "00112233"
        case4 = LC>0 and LE>0
        case4e = case4 and (LC>0xFF or LE>0x100)
        if LC>0:
            if LC>0xFF or case4e:
                hexstr += "00%04X"%LC
            else:
                hexstr += "%02X" % LC
            hexstr += pysatl.Utils.hexstr(data, separator="")
        if LE>0:
            if case4e:
                if LE == 0x10000:
                    hexstr += "0000"
                else:
                    hexstr += "%04X"%LE
            elif LE == 0x10000:
                hexstr += "000000"
            elif LE>0x100:
                hexstr += "00%04X"%LE
            elif LE == 0x100:
                hexstr += "00"
            else:
                hexstr += "%02X" % LE
        expected = hexstr
        capdu = CAPDU(CLA=0x00, INS=0x11, P1=0x22, P2=0x33, DATA=data, LE=LE)
        hexstr = capdu.to_hexstr()
        if hexstr != expected:
            raise Exception("Mismatch for LC=%d, LE=%d"%(LC,LE)+"\nActual:   "+hexstr+"\nExpected: "+expected)

        b = capdu.to_bytes()
        assert(type(b) is bytes)
        return (hexstr, capdu)

    #check __repr__
    expected = "pysatl.CAPDU.from_hexstr('00112233015502')"
    capdu=None
    exec("capdu="+expected)
    assert(expected==repr(capdu))

    #check well formed inputs
    check("00112233", CAPDU(CLA=0x00, INS=0x11, P1=0x22, P2=0x33))
    check("00 11 22 33", CAPDU(CLA=0x00, INS=0x11, P1=0x22, P2=0x33))
    check("0x00,0x11,0x22,0x33", CAPDU(CLA=0x00, INS=0x11, P1=0x22, P2=0x33))

    #check we tolerate less well formed inputs
    check("00-11,22_33", CAPDU(CLA=0x00, INS=0x11, P1=0x22, P2=0x33))
    check("""0x00 0x11    0x22
          0x33""", CAPDU(CLA=0x00, INS=0x11, P1=0x22, P2=0x33))
    check("1 2 304", CAPDU(CLA=0x01, INS=0x02, P1=0x03, P2=0x04))

    LC_cases = [0,1,2,254,255,256,257,65534,65535]
    LE_cases = LC_cases + [65536]
    for LC in LC_cases:
        for LE in LE_cases:
            print(LC,LE)
            check(*gencase(LC=LC, LE=LE))

[((129, 154), 'pysatl.CAPDU.from_hexstr', 'CAPDU.from_hexstr', (['hexstr'], {}), '(hexstr)\n', (146, 154), False, 'from pysatl import CAPDU\n'), ((405, 421), 'os.getrandom', 'os.getrandom', (['LC'], {}), '(LC)\n', (417, 421), False, 'import os\n'), ((1200, 1252), 'pysatl.CAPDU', 'CAPDU', ([], {'CLA': '(0)', 'INS': '(17)', 'P1': '(34)', 'P2': '(51)', 'DATA': 'data', 'LE': 'LE'}), '(CLA=0, INS=17, P1=34, P2=51, DATA=data, LE=LE)\n', (1205, 1252), False, 'from pysatl import CAPDU\n'), ((1744, 1778), 'pysatl.CAPDU', 'CAPDU', ([], {'CLA': '(0)', 'INS': '(17)', 'P1': '(34)', 'P2': '(51)'}), '(CLA=0, INS=17, P1=34, P2=51)\n', (1749, 1778), False, 'from pysatl import CAPDU\n'), ((1814, 1848), 'pysatl.CAPDU', 'CAPDU', ([], {'CLA': '(0)', 'INS': '(17)', 'P1': '(34)', 'P2': '(51)'}), '(CLA=0, INS=17, P1=34, P2=51)\n', (1819, 1848), False, 'from pysatl import CAPDU\n'), ((1892, 1926), 'pysatl.CAPDU', 'CAPDU', ([], {'CLA': '(0)', 'INS': '(17)', 'P1': '(34)', 'P2': '(51)'}), '(CLA=0, INS=17, P1=34, P2=51)\n', (1897, 1926), False, 'from pysatl import CAPDU\n'), ((2010, 2044), 'pysatl.CAPDU', 'CAPDU', ([], {'CLA': '(0)', 'INS': '(17)', 'P1': '(34)', 'P2': '(51)'}), '(CLA=0, INS=17, P1=34, P2=51)\n', (2015, 2044), False, 'from pysatl import CAPDU\n'), ((2105, 2139), 'pysatl.CAPDU', 'CAPDU', ([], {'CLA': '(0)', 'INS': '(17)', 'P1': '(34)', 'P2': '(51)'}), '(CLA=0, INS=17, P1=34, P2=51)\n', (2110, 2139), False, 'from pysatl import CAPDU\n'), ((2171, 2202), 'pysatl.CAPDU', 'CAPDU', ([], {'CLA': '(1)', 'INS': '(2)', 'P1': '(3)', 'P2': '(4)'}), '(CLA=1, INS=2, P1=3, P2=4)\n', (2176, 2202), False, 'from pysatl import CAPDU\n'), ((696, 735), 'pysatl.Utils.hexstr', 'pysatl.Utils.hexstr', (['data'], {'separator': '""""""'}), "(data, separator='')\n", (715, 735), False, 'import pysatl\n')]