Esempio n. 1
0
    def test_compute_local_mean(self):
        with self.assertRaises(TypeError) as context:
            local_mean = WaitTimer.compute_local_mean(None)

        with self.assertRaises(TypeError) as context:
            local_mean = WaitTimer.compute_local_mean("")
        with self.assertRaises(TypeError) as context:
            local_mean = WaitTimer.compute_local_mean("XYZZY")
        with self.assertRaises(TypeError) as context:
            local_mean = WaitTimer.compute_local_mean(555)

        certs = []
        local_mean = WaitTimer.compute_local_mean(certs)
        self.assertEqual(local_mean, 30)
Esempio n. 2
0
    def test_is_valid_wait_certificate_2(self):
        addr = random_name(20)
        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        txn_ids, block_hash = generate_txn_ids(10)
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertIsNotNone(wait_timer)
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)

        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer, block_hash)
        self.assertIsNotNone(wait_cert)

        # Verify class changes don't affect validity
        d = wait_cert.duration
        wait_cert.duration = 0
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        wait_cert.duration = d

        lm = wait_cert.local_mean
        wait_cert.local_mean = wait_cert.local_mean - 1
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        wait_cert.local_mean = lm

        pc = wait_cert.previous_certificate_id
        wait_cert.previous_certificate_id = random_name(
            pe_sim.IDENTIFIER_LENGTH)
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        wait_cert.previous_certificate_id = pc

        self.check_enclave_timer_tampering(wait_cert, addr, certs, txn_ids)
    def test_wait_certificate(self):
        addr = random_name(20)
        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        txn_ids, block_hash = generate_txn_ids(10)

        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertIsNotNone(wait_timer)
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)

        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer,
            block_hash)
        self.assertIsNotNone(wait_cert)

        ewc = wait_cert.enclave_wait_certificate
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)
        str(wait_cert)

        swc = WaitCertificate.deserialize_wait_certificate(
            wait_cert.serialized_cert,
            wait_cert.signature
        )
        self.assertEqual(wait_cert.previous_certificate_id,
                         swc.previous_certificate_id)
        self.assertEqual(wait_cert.local_mean, swc.local_mean)
        self.assertEqual(wait_cert.request_time, swc.request_time)
        self.assertEqual(wait_cert.duration, swc.duration)
        self.assertEqual(wait_cert.signature, swc.signature)
        self.assertEqual(wait_cert.identifier, swc.identifier)
        self.assertEqual(wait_cert.validator_address, swc.validator_address)
        self.assertEqual(wait_cert.block_hash, swc.block_hash)

        swc.is_valid_wait_certificate(addr, certs, txn_ids)
        dwc = wait_cert.dump()
        swc = WaitCertificate.deserialize_wait_certificate(
            dwc["SerializedCert"],
            dwc["Signature"]
        )
        swc.is_valid_wait_certificate(addr, certs, txn_ids)

        self.assertEqual(wait_cert.previous_certificate_id,
                         swc.previous_certificate_id)
        self.assertEqual(wait_cert.local_mean, swc.local_mean)
        self.assertEqual(wait_cert.request_time, swc.request_time)
        self.assertEqual(wait_cert.duration, swc.duration)
        self.assertEqual(wait_cert.signature, swc.signature)
        self.assertEqual(wait_cert.identifier, swc.identifier)
        self.assertEqual(wait_cert.validator_address, swc.validator_address)
        self.assertEqual(wait_cert.block_hash, swc.block_hash)
Esempio n. 4
0
    def test_is_expired(self):
        default_target_wait_time = WaitTimer.target_wait_time
        WaitTimer.target_wait_time = 1

        # normal case
        addr = random_name(20)
        certs = generate_certs(WaitTimer.certificate_sample_length)
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        start = time.time()
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)
        end = time.time()

        self.assertLessEqual(wait_timer.duration, end - start)
        # tamper with duration
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        d = wait_timer.duration
        wait_timer.duration = 1

        start = time.time()
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)
        end = time.time()

        self.assertLessEqual(d, end - start)

        # tamper with request_time
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        wait_timer.request_time = time.time() - wait_timer.duration
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)
        end = time.time()

        self.assertLessEqual(wait_timer.duration, end - start)
        # restore default target_wait_time
        WaitTimer.target_wait_time = default_target_wait_time
    def test_is_valid_wait_certificate(self):
        addr = random_name(20)
        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        txn_ids, block_hash = generate_txn_ids(10)
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertIsNotNone(wait_timer)
        while not wait_timer.is_expired(time.time()):
            time.sleep(1)

        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer,
            block_hash)
        self.assertIsNotNone(wait_cert)
        r = wait_cert.is_valid_wait_certificate(addr, certs, txn_ids)
        self.assertTrue(r)

        # invalid list
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(None, certs, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate({}, certs, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate([], certs, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(555, certs, txn_ids)

        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, None, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, "test", txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, {}, txn_ids)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, 545, txn_ids)

        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, None)
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, {})
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, "test")
        with self.assertRaises(TypeError) as context:
            wait_cert.is_valid_wait_certificate(addr, certs, 333)
    def check_enclave_timer_tampering(self, wait_cert, oid, certs, txn_ids):
        # now we are going to tamper with the members of the enclave wait
        # timer object
        swc = wait_cert.serialized_cert
        ewt = wait_cert.enclave_wait_certificate

        d = ewt.duration
        ewt.duration = 0
        wait_cert.serialized_cert = ewt.serialize()
        r = wait_cert.is_valid_wait_certificate(oid, certs, txn_ids)
        self.assertFalse(r)
        ewt.duration = d

        lm = ewt.local_mean
        ewt.local_mean = wait_cert.local_mean - 1
        wait_cert.serialized_cert = ewt.serialize()
        r = wait_cert.is_valid_wait_certificate(oid, certs, txn_ids)
        self.assertFalse(r)
        ewt.local_mean = lm

        pc = wait_cert.previous_certificate_id
        ewt.previous_certificate_id = random_name(pe_sim.IDENTIFIER_LENGTH)
        ewt.serialized_cert = ewt.serialize()
        r = wait_cert.is_valid_wait_certificate(oid, certs, txn_ids)
        self.assertFalse(r)
        ewt.previous_certificate_id = pc

        # start up case, no previous certs and NULL_IDENTIFIER
        addr = random_name(20)
        block_hash = random_name(32)
        wait_timer = WaitTimer.create_wait_timer(addr, [])
        self.assertIsNotNone(wait_timer)
        wait_cert = WaitCertificate.create_wait_certificate(
            wait_timer,
            block_hash)
        self.assertIsNotNone(wait_cert)
        r = wait_cert.is_valid_wait_certificate(oid, [], txn_ids)
        self.assertTrue(r)
Esempio n. 7
0
    def test_population_estimate(self):
        with self.assertRaises(TypeError) as context:
            pop_est = WaitTimer.population_estimate(None)
        with self.assertRaises(TypeError) as context:
            pop_est = WaitTimer.population_estimate("")
        with self.assertRaises(TypeError) as context:
            pop_est = WaitTimer.population_estimate("XYZZY")
        with self.assertRaises(TypeError) as context:
            pop_est = WaitTimer.population_estimate(555)

        certs = []
        with self.assertRaises(ValueError) as context:
            pop_est = WaitTimer.population_estimate(certs)

        # ultra simple test for expected value
        certs = generate_certs(WaitTimer.certificate_sample_length)
        pop_est = WaitTimer.population_estimate(certs)
        self.assertEqual(pop_est, 1)
Esempio n. 8
0
    def test_create_wait_timer(self):
        certs = []
        # invalid list
        with self.assertRaises(TypeError) as context:
            WaitTimer.create_wait_timer(None, certs)
        with self.assertRaises(TypeError) as context:
            WaitTimer.create_wait_timer([], certs)
        with self.assertRaises(TypeError) as context:
            WaitTimer.create_wait_timer({}, certs)
        with self.assertRaises(TypeError) as context:
            WaitTimer.create_wait_timer(555, certs)

        addr = random_name(20)
        # invalid list
        with self.assertRaises(TypeError) as context:
            WaitTimer.create_wait_timer(addr, None)
        with self.assertRaises(TypeError) as context:
            WaitTimer.create_wait_timer(addr, "")
        with self.assertRaises(TypeError) as context:
            WaitTimer.create_wait_timer(addr, "XYZZY")
        with self.assertRaises(TypeError) as context:
            WaitTimer.create_wait_timer(addr, 555)

        certs = []  # empty list
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        # this will only catch changes in behavior of local mean calc
        self.assertEqual(wait_timer.local_mean, 30)

        certs = generate_certs(WaitTimer.fixed_duration_blocks - 1)
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        # this will only catch changes in behavior of local mean calc
        # based on the existing defaults...
        self.assertEqual(wait_timer.local_mean, 2882.388)

        certs = generate_certs(WaitTimer.fixed_duration_blocks)
        wait_timer = WaitTimer.create_wait_timer(addr, certs)
        self.assertEqual(wait_timer.local_mean, 30)
Esempio n. 9
0
    def is_valid_wait_certificate(self, originator_id, certs, transactions):
        """Determines whether the wait certificate is valid.

        Args:
            certs (list): A list of historical certs.

        Returns:
            bool: Whether or not the wait certificate is valid.
        """

        if not isinstance(originator_id, basestring):
            raise TypeError

        if not isinstance(certs, list):
            raise TypeError

        if not isinstance(transactions, list):
            raise TypeError

        cert = self.enclave_wait_certificate
        if not cert:
            return False

        if cert.duration < self.poet_enclave.MINIMUM_WAIT_TIME:
            LOGGER.warn('Wait time less then minimum: %s != %s',
                        cert.duration, self.poet_enclave.MINIMUM_WAIT_TIME)
            return False

        expected_mean = WaitTimer.compute_local_mean(certs)
        if not is_close(cert.local_mean, expected_mean, abs_tol=0.001):
            LOGGER.warn('mismatch local mean: %s != %s', cert.local_mean,
                        expected_mean)
            return False

        if cert.previous_certificate_id == self.poet_enclave.NULL_IDENTIFIER:
            if len(certs) == 0:
                return True

        if cert.previous_certificate_id != certs[-1].identifier:
            LOGGER.warn('mismatch previous identifier: %s != %s',
                        cert.previous_certificate_id, certs[-1].identifier)
            return False

        hasher = hashlib.sha256()
        for tid in transactions:
            hasher.update(tid)
        block_hash = hasher.hexdigest()

        if block_hash != self.block_hash:
            LOGGER.warn('Transaction hash mismatch : %s != %s',
                        self.block_hash, block_hash)
            return False

        if self.validator_address != originator_id:
            LOGGER.warn('Originator Id mismatch: %s != %s',
                        self.validator_address, originator_id)
            return False

        try:
            return self.poet_enclave.verify_wait_certificate(cert)
        except Timeout:
            raise NotAvailableException
        except ConnectionError:
            raise NotAvailableException