# Path: keepkeylib/client.py
# class PinException(CallException):
#     pass
# 
# class CallException(Exception):
#     def __init__(self, code, message):
#         super(CallException, self).__init__()
#         self.args = [code, message]


import time
import unittest
import common
from keepkeylib import messages_pb2 as proto
from keepkeylib import types_pb2 as types
from keepkeylib.client import PinException, CallException

    def test_cancelled_pin(self):
        self.setup_mnemonic_pin_passphrase()
        self.client.clear_session()
        self.client.setup_debuglink(button=True, pin_correct=False)  # PIN cancel
        self.assertRaises(PinException, self._some_protected_call, False, True, False)

    def test_exponential_backoff_with_reboot(self):
        if self.client.features.firmware_variant == "Emulator":
            self.skipTest("Due to a known defect in the emulator, pin timeouts don't work.")
            return

        self.setup_mnemonic_pin_passphrase()
        self.client.clear_session()

        self.client.setup_debuglink(button=True, pin_correct=False)

        def test_backoff(attempts, start):
            if attempts <= 2 :
                expected = 0
            else:
                expected = (2 ** attempts) - 1
            got = time.time() - start

            msg = "Pin delay expected to be at least %s seconds, got %s" % (expected, got)
            print(msg)
            self.assertLessEqual(expected, got, msg)

        for attempt in range(0, 5):
            start = time.time()

# Path: keepkeylib/client.py
# class PinException(CallException):
#     pass
# 
# class CallException(Exception):
#     def __init__(self, code, message):
#         super(CallException, self).__init__()
#         self.args = [code, message]


# Path: tests/test_protect_call.py
import time
import unittest
import common
from keepkeylib import messages_pb2 as proto
from keepkeylib import types_pb2 as types
from keepkeylib.client import PinException, CallException


    def test_cancelled_pin(self):
        self.setup_mnemonic_pin_passphrase()
        self.client.clear_session()
        self.client.setup_debuglink(button=True, pin_correct=False)  # PIN cancel
        self.assertRaises(PinException, self._some_protected_call, False, True, False)

    def test_exponential_backoff_with_reboot(self):
        if self.client.features.firmware_variant == "Emulator":
            self.skipTest("Due to a known defect in the emulator, pin timeouts don't work.")
            return

        self.setup_mnemonic_pin_passphrase()
        self.client.clear_session()

        self.client.setup_debuglink(button=True, pin_correct=False)

        def test_backoff(attempts, start):
            if attempts <= 2 :
                expected = 0
            else:
                expected = (2 ** attempts) - 1
            got = time.time() - start

            msg = "Pin delay expected to be at least %s seconds, got %s" % (expected, got)
            print(msg)
            self.assertLessEqual(expected, got, msg)

        for attempt in range(0, 5):
            start = time.time()

            self.assertRaises(PinException, self._some_protected_call, False, True, False)