def test_json_enc_int(self):
        enc = json_encdec()
        ev = enc.encode(1234)
        self.assertEqual(ev, 1234)

        ev = enc.encode(0x100000002)
        self.assertEqual(ev, [1, 2])
Beispiel #2
0
    def test_json_enc_friendly_int(self):
        enc = json_encdec()
        ev = enc.encode(TPM2_RH.OWNER)
        self.assertEqual(ev, "owner")

        ev = enc.encode(TPM2_ALG.LAST + 1)
        self.assertIsInstance(ev, int)
        self.assertEqual(ev, int(TPM2_ALG.LAST + 1))
    def test_json_dec_simple_tpm2b(self):
        dec = json_encdec()
        dv = dec.decode(TPM2B_DIGEST(), "01020304")
        self.assertEqual(dv, b"\x01\x02\x03\x04")

        dv = dec.decode(TPM2B_DIGEST(), "0xff")
        self.assertEqual(dv, b"\xFF")

        dv = dec.decode(TPM2B_DIGEST(), [1, 2, 3, 4])
        self.assertEqual(dv, b"\x01\x02\x03\x04")
    def test_json_dec_friendly_int(self):
        dec = json_encdec()
        dv = dec.decode(TPM2_ALG(), "sha1")
        self.assertEqual(dv, TPM2_ALG.SHA1)

        dv = dec.decode(TPM2_ALG(), "0x4")
        self.assertEqual(dv, TPM2_ALG.SHA1)

        dv = dec.decode(TPM2_ALG(), int(TPM2_ALG.SHA1))
        self.assertEqual(dv, TPM2_ALG.SHA1)
    def test_json_dec_int(self):
        dec = json_encdec()
        dv = dec.decode(int(), 1234)
        self.assertEqual(dv, 1234)

        dv = dec.decode(int(), "0xAA")
        self.assertEqual(dv, 0xAA)

        dv = dec.decode(int(), [1, 2])
        self.assertEqual(dv, 0x100000002)
    def test_json_enc_friendly_intlist(self):
        enc = json_encdec()
        ev = enc.encode(TPMA_OBJECT.USERWITHAUTH | TPMA_OBJECT.RESTRICTED)
        self.assertEqual(ev, {"userwithauth": 1, "restricted": 1})

        ev = enc.encode(TPMA_NV.AUTHREAD
                        | TPM2_NT.COUNTER << TPMA_NV.TPM2_NT_SHIFT)
        self.assertEqual(ev, {"authread": 1, "nt": "counter"})

        ev = enc.encode(TPMA_CC.NV | 5 << TPMA_CC.CHANDLES_SHIFT | 1234)
        self.assertEqual(ev, {"nv": 1, "chandles": 5, "commandindex": 1234})

        ev = enc.encode(TPMA_LOCALITY.ZERO
                        | TPMA_LOCALITY(5 << TPMA_LOCALITY.EXTENDED_SHIFT))
        self.assertEqual(ev, {"zero": 1, "extended": 5})
    def test_json_dec_friendly_intlist(self):
        dec = json_encdec()
        dv = dec.decode(
            TPMA_OBJECT.RESTRICTED,
            {
                "userwithauth": 1,
                "restricted": 0,
                "sign_encrypt": 1
            },
        )
        self.assertEqual(dv,
                         TPMA_OBJECT.USERWITHAUTH | TPMA_OBJECT.SIGN_ENCRYPT)

        dv = dec.decode(TPMA_OBJECT(),
                        ["TPMA_OBJECT_RESTRICTED", "ADMINWITHPOLICY"])
        self.assertEqual(dv,
                         TPMA_OBJECT.RESTRICTED | TPMA_OBJECT.ADMINWITHPOLICY)

        dv = dec.decode(
            TPMA_OBJECT(),
            f"0x{TPMA_OBJECT.USERWITHAUTH | TPMA_OBJECT.SIGN_ENCRYPT:x}")
        self.assertEqual(dv,
                         TPMA_OBJECT.USERWITHAUTH | TPMA_OBJECT.SIGN_ENCRYPT)

        dv = dec.decode(TPMA_NV(), {"written": "set", "nt": "counter"})
        self.assertEqual(
            dv, TPMA_NV.WRITTEN | TPM2_NT.COUNTER << TPMA_NV.TPM2_NT_SHIFT)

        dv = dec.decode(TPMA_CC(), {
            "v": 1,
            "commandindex": 1234,
            "chandles": 5
        })
        self.assertEqual(dv, TPMA_CC.V | 1234 | 5 << TPMA_CC.CHANDLES_SHIFT)

        dv = dec.decode(TPMA_LOCALITY(), {"one": 1, "extended": 5})
        self.assertEqual(dv, TPMA_LOCALITY(0xA0) | TPMA_LOCALITY.ONE)