def start(bo): success = False try: while not success: # server_socket.accept() is not stoppable. So with select we can call accept() # only when we are sure that there is already a waiting connection ready_to_read, ready_to_write, in_error = yield threads.deferToThread( select.select, [bo.server_socket], [], [], 0.5) if ready_to_read: # We are sure that a connection is available, so we can call # accept() without deferring it to a thread client_socket, address = bo.server_socket.accept() key_data = get_public_key_data(bo.key.fingerprint) kd_decoded = key_data.decode('utf-8') # We send only a part of the key. In this way we can simulate the case # where the connection has been lost half = len(kd_decoded) / 2 kd_corrupted = kd_decoded[:half] yield threads.deferToThread(client_socket.sendall, kd_corrupted) client_socket.shutdown(socket.SHUT_RDWR) client_socket.close() success = True except Exception as e: log.error("An error occurred: %s" % e)
def test_get_all_public_key_data(self): # Hm. The behaviour of something that matches # more than one key may change. data = get_public_key_data("", homedir=self.homedir) newkey = openpgpkey_from_data(data) # Hrm. We may be better off checking for a few things # we actually care about rather than delegating to the Key() itself. assert_equals(self.originalkey, newkey)
def test_bt_corrupted_key(): """This test requires two working Bluetooth devices""" @inlineCallbacks def start(bo): success = False try: while not success: # server_socket.accept() is not stoppable. So with select we can call accept() # only when we are sure that there is already a waiting connection ready_to_read, ready_to_write, in_error = yield threads.deferToThread( select.select, [bo.server_socket], [], [], 0.5) if ready_to_read: # We are sure that a connection is available, so we can call # accept() without deferring it to a thread client_socket, address = bo.server_socket.accept() key_data = get_public_key_data(bo.key.fingerprint) kd_decoded = key_data.decode('utf-8') # We send only a part of the key. In this way we can simulate the case # where the connection has been lost half = len(kd_decoded) / 2 kd_corrupted = kd_decoded[:half] yield threads.deferToThread(client_socket.sendall, kd_corrupted) client_socket.shutdown(socket.SHUT_RDWR) client_socket.close() success = True except Exception as e: log.error("An error occurred: %s" % e) # This should be a new, empty directory homedir = tempfile.mkdtemp() os.environ["GNUPGHOME"] = homedir key = import_key_from_file("seckey-no-pw-1.asc", homedir) log.info("Running with key %r", key) file_key_data = get_public_key_data(key.fingerprint) hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data) # Start offering the key offer = BluetoothOffer(key) data = yield offer.allocate_code() # getting the code from "BT=code;...." code = data.split("=", 1)[1] code = code.split(";", 1)[0] port = int(data.rsplit("=", 1)[1]) start(offer) receive = BluetoothReceive(port) msg_tuple = yield receive.find_key(code, hmac) downloaded_key_data, result, error = msg_tuple assert_false(result) assert_equal(type(error), ValueError)
def test_bt_corrupted_key(): """This test requires two working Bluetooth devices""" @inlineCallbacks def start(bo): success = False try: while not success: # server_socket.accept() is not stoppable. So with select we can call accept() # only when we are sure that there is already a waiting connection ready_to_read, ready_to_write, in_error = yield threads.deferToThread( select.select, [bo.server_socket], [], [], 0.5) if ready_to_read: # We are sure that a connection is available, so we can call # accept() without deferring it to a thread client_socket, address = bo.server_socket.accept() key_data = get_public_key_data(bo.key.fingerprint) kd_decoded = key_data.decode('utf-8') # We send only a part of the key. In this way we can simulate the case # where the connection has been lost half = len(kd_decoded)/2 kd_corrupted = kd_decoded[:half] yield threads.deferToThread(client_socket.sendall, kd_corrupted) client_socket.shutdown(socket.SHUT_RDWR) client_socket.close() success = True except Exception as e: log.error("An error occurred: %s" % e) # This should be a new, empty directory homedir = tempfile.mkdtemp() key = import_key_from_file("seckey-no-pw-1.asc", homedir) log.info("Running with key %r", key) file_key_data = get_public_key_data(key.fingerprint, homedir=homedir) hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data) # Start offering the key offer = BluetoothOffer(key) data = yield offer.allocate_code() # getting the code from "BT=code;...." code = data.split("=", 1)[1] code = code.split(";", 1)[0] port = int(data.rsplit("=", 1)[1]) start(offer) receive = BluetoothReceive(port) msg_tuple = yield receive.find_key(code, hmac) downloaded_key_data, result, error = msg_tuple assert_false(result) assert_equal(type(error), ValueError)
def test_wrmhl(): # This should be a new, empty directory homedir = tempfile.mkdtemp() os.environ["GNUPGHOME"] = homedir key = import_key_from_file("seckey-no-pw-1.asc", homedir) file_key_data = get_public_key_data(key.fingerprint) log.info("Running with key %r", key) # Start offering the key offer = WormholeOffer(key) info = yield offer.allocate_code() code, _ = info offer.start() receive = WormholeReceive(code) msg_tuple = yield receive.start() downloaded_key_data, success, _ = msg_tuple assert_true(success) log.info("Checking with key: %r", downloaded_key_data) assert_equal(downloaded_key_data, file_key_data)
def test_wrmhl_offline_code(): # This should be a new, empty directory homedir = tempfile.mkdtemp() os.environ["GNUPGHOME"] = homedir key = import_key_from_file("seckey-no-pw-1.asc", homedir) file_key_data = get_public_key_data(key.fingerprint) # We assume that this channel, at execution time, is free code = u"5556-penguin-paw-print" # Start offering the key offer = WormholeOffer(key) offer.allocate_code(code) offer.start() # Start receiving the key receive = WormholeReceive(code) msg_tuple = yield receive.start() downloaded_key_data, success, _ = msg_tuple assert_true(success) log.info("Checking with key: %r", downloaded_key_data) assert_equal(downloaded_key_data, file_key_data)
def test_bt(): """This test requires two working Bluetooth devices""" # This should be a new, empty directory homedir = tempfile.mkdtemp() key = import_key_from_file("seckey-no-pw-1.asc", homedir) file_key_data = get_public_key_data(key.fingerprint, homedir=homedir) log.info("Running with key %r", key) hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data) # Start offering the key offer = BluetoothOffer(key) data = yield offer.allocate_code() # getting the code from "BT=code;...." code = data.split("=", 1)[1] code = code.split(";", 1)[0] port = int(data.rsplit("=", 1)[1]) offer.start() receive = BluetoothReceive(port) msg_tuple = yield receive.find_key(code, hmac) downloaded_key_data, success, _ = msg_tuple assert_true(success) log.info("Checking with key: %r", downloaded_key_data) assert_equal(downloaded_key_data.encode("utf-8"), file_key_data)
def test_bt(): """This test requires two working Bluetooth devices""" # This should be a new, empty directory homedir = tempfile.mkdtemp() os.environ["GNUPGHOME"] = homedir key = import_key_from_file("seckey-no-pw-1.asc", homedir) file_key_data = get_public_key_data(key.fingerprint) log.info("Running with key %r", key) hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data) # Start offering the key offer = BluetoothOffer(key) data = yield offer.allocate_code() # getting the code from "BT=code;...." code = data.split("=", 1)[1] code = code.split(";", 1)[0] port = int(data.rsplit("=", 1)[1]) offer.start() receive = BluetoothReceive(port) msg_tuple = yield receive.find_key(code, hmac) downloaded_key_data, success, _ = msg_tuple assert_true(success) log.info("Checking with key: %r", downloaded_key_data) assert_equal(downloaded_key_data.encode("utf-8"), file_key_data)
def start(bo): success = False try: while not success: # server_socket.accept() is not stoppable. So with select we can call accept() # only when we are sure that there is already a waiting connection ready_to_read, ready_to_write, in_error = yield threads.deferToThread( select.select, [bo.server_socket], [], [], 0.5) if ready_to_read: # We are sure that a connection is available, so we can call # accept() without deferring it to a thread client_socket, address = bo.server_socket.accept() key_data = get_public_key_data(bo.key.fingerprint) kd_decoded = key_data.decode('utf-8') # We send only a part of the key. In this way we can simulate the case # where the connection has been lost half = len(kd_decoded)/2 kd_corrupted = kd_decoded[:half] yield threads.deferToThread(client_socket.sendall, kd_corrupted) client_socket.shutdown(socket.SHUT_RDWR) client_socket.close() success = True except Exception as e: log.error("An error occurred: %s" % e)
def test_no_match(self): data = get_public_key_data("nothing should match this", homedir=self.homedir) newkey = openpgpkey_from_data(data) assert False
def test_get_public_key_data(self): fpr = self.originalkey.fingerprint data = get_public_key_data(fpr, homedir=self.homedir) newkey = openpgpkey_from_data(data) assert_equals(fpr, newkey.fingerprint)
def test_get_public_key_no_data(): tmp = tempfile.mkdtemp() d = get_public_key_data(None, homedir=tmp) assert_equals("", d)