[gnome-keysign: 39/65] bluetooth: main and test now use the new dynamic port allocation



commit 0bf3d6a3db740c9815cdb99cfd2c5a66d3b2ddfa
Author: RyuzakiKK <aasonykk gmail com>
Date:   Sun Aug 20 22:49:04 2017 +0200

    bluetooth: main and test now use the new dynamic port allocation

 keysign/bluetoothoffer.py   | 21 +++++++++++++--------
 keysign/bluetoothreceive.py |  7 ++++---
 tests/test_bluetooth.py     |  5 +++--
 3 files changed, 20 insertions(+), 13 deletions(-)
---
diff --git a/keysign/bluetoothoffer.py b/keysign/bluetoothoffer.py
index 2b5ec88..c802b0d 100644
--- a/keysign/bluetoothoffer.py
+++ b/keysign/bluetoothoffer.py
@@ -121,14 +121,19 @@ def main(args):
     file_key_data = get_public_key_data(key.fingerprint)
     hmac = mac_generate(key.fingerprint.encode('ascii'), file_key_data)
     offer = BluetoothOffer(key)
-    code, _ = offer.allocate_code()
-    offer.start().addCallback(_received)
-    print("Offering key: {}".format(key))
-    print("Discovery info: {}".format(code))
-    print("HMAC: {}".format(hmac))
-    # Wait for the user without blocking everything
-    reactor.callInThread(cancel)
-    reactor.run()
+    code, data = offer.allocate_code()
+    if data:
+        port = data.rsplit("=", 1)[1]
+        offer.start().addCallback(_received)
+        print("Offering key: {}".format(key))
+        print("Discovery info: {}".format(code))
+        print("HMAC: {}".format(hmac))
+        print("Port: {}".format(port))
+        # Wait for the user without blocking everything
+        reactor.callInThread(cancel)
+        reactor.run()
+    else:
+        print("Bluetooth not available")
 
 if __name__ == "__main__":
     import sys
diff --git a/keysign/bluetoothreceive.py b/keysign/bluetoothreceive.py
index e5483f8..cce0338 100644
--- a/keysign/bluetoothreceive.py
+++ b/keysign/bluetoothreceive.py
@@ -104,8 +104,8 @@ class BluetoothReceive:
 
 def main(args):
     log.debug('Running main with args: %s', args)
-    if not len(args) == 2:
-        raise ValueError("You must provide two arguments: bluetooth code and hmac")
+    if not len(args) == 3:
+        raise ValueError("You must provide three arguments: bluetooth code, hmac and port")
 
     def _received(result):
         key_data, success, error_message = result
@@ -119,7 +119,8 @@ def main(args):
     print("Trying to download the key, please wait")
     bt_mac = args[0]
     hmac = args[1]
-    receive = BluetoothReceive()
+    port = int(args[2])
+    receive = BluetoothReceive(port)
     d = receive.find_key(bt_mac, hmac)
     d.addCallback(_received)
     reactor.run()
diff --git a/tests/test_bluetooth.py b/tests/test_bluetooth.py
index 82ee4e4..dbe2829 100644
--- a/tests/test_bluetooth.py
+++ b/tests/test_bluetooth.py
@@ -46,9 +46,10 @@ def test_bt():
     # Start offering the key
     offer = BluetoothOffer(key)
     info = yield offer.allocate_code()
-    code, _ = info
+    code, data = info
+    port = int(data.rsplit("=", 1)[1])
     offer.start()
-    receive = BluetoothReceive()
+    receive = BluetoothReceive(port)
     msg_tuple = yield receive.find_key(code, hmac)
     downloaded_key_data, success, _ = msg_tuple
     assert_true(success)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]