def test_get_buffer(self):
     self.assertEqual(
         t2pc.PolicyLocality([0, 2, 4]).get_buffer_for_digest(),
         bytearray.fromhex("0000016F" + "15"))
     self.assertEqual(
         t2pc.PolicyLocality([34]).get_buffer_for_digest(),
         bytearray.fromhex("0000016F" + "22"))
 def test_create_with_invalid_localites(self):
     with self.assertRaises(ValueError):
         t2pc.PolicyLocality([5])
     with self.assertRaises(ValueError):
         t2pc.PolicyLocality([12])
     with self.assertRaises(ValueError):
         t2pc.PolicyLocality([31])
     with self.assertRaises(ValueError):
         t2pc.PolicyLocality([256])
 def test_create_with_base_localities(self):
     policy = t2pc.PolicyLocality([0, 2, 4])
     self.assertEqual(policy.get_bitfield(), 0b00010101)
     policy2 = t2pc.PolicyLocality([1, 2])
     self.assertEqual(policy2.get_bitfield(), 0b00000110)
     policy3 = t2pc.PolicyLocality([3])
     self.assertEqual(policy3.get_bitfield(), 0b00001000)
     policy4 = t2pc.PolicyLocality([57])
     self.assertEqual(policy4.get_bitfield(), 57)
    def test_policy_locality(self):
        expected_result = bytearray.fromhex(
            "07039B45BAF2CC169B0D84AF7C53FD1622B033DF0A5DCDA66360AA99E54947CD")

        test = t2pc.PolicyTreeSolo(t2pc.PolicyLocality([3, 4]))

        phash = t2pc.PolicyHasher('sha256')
        self.assertEqual(test.get_policy(phash), expected_result)
    def test_complex_policy_1(self):
        expected_result = bytearray.fromhex(
            "DFFDB6C8EAFCBE691E358882B18703121EAB40DE2386F7A8E7B4A06591E1F0EE")

        # Computation details:
        #   A = TPM2_PolicyLocality(3 & 4)
        #   B = TPM2_PolicyCommandCode(TPM_CC_NV_UndefineSpaceSpecial)
        #   C = TPM2_PolicyCommandCode(TPM_CC_NV_Write)
        #   policy = {{A} AND {C}} OR {{A} AND {B}}

        a = t2pc.PolicyLocality([3, 4])
        b = t2pc.PolicyCommandCode('TPM_CC_NV_UndefineSpaceSpecial')
        c = t2pc.PolicyCommandCode('TPM_CC_NV_Write')

        leg1 = t2pc.PolicyTreeAnd([a, c])
        leg2 = t2pc.PolicyTreeAnd([a, b])
        final = t2pc.PolicyTreeOr([leg1, leg2])

        phash = t2pc.PolicyHasher('sha256')
        self.assertEqual(final.get_policy(phash), expected_result)
 def test_create_with_mixed_lower_and_upper(self):
     with self.assertRaises(ValueError):
         t2pc.PolicyLocality([1, 4, 35])
     with self.assertRaises(ValueError):
         t2pc.PolicyLocality([36, 128])
 def test_create_with_empty_list(self):
     policy = t2pc.PolicyLocality(None)
     self.assertEqual(policy.get_bitfield(), 0)
     policy2 = t2pc.PolicyLocality(())
     self.assertEqual(policy2.get_bitfield(), 0)