From 7a6a9477eee35e9e98e1522deed72927c933db7d Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Sun, 31 Jan 2021 23:34:42 -0800 Subject: [PATCH] Use default name when empty (#774) * Disallow empty name for contact profile * Disallow empty name for signing profile * Disallow empty name for peer --- itests/tests/conftest.py | 1 + itests/tests/test_squeak_node.py | 41 +++++++++++++++++++ .../admin/squeak_admin_server_handler.py | 2 +- squeaknode/core/squeak_controller.py | 17 +++++++- 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/itests/tests/conftest.py b/itests/tests/conftest.py index c5e56adb..6b8d9f6e 100644 --- a/itests/tests/conftest.py +++ b/itests/tests/conftest.py @@ -134,6 +134,7 @@ def peer_id(server_stub, admin_stub): # Create a new peer create_peer_response = admin_stub.CreatePeer( squeak_admin_pb2.CreatePeerRequest( + peer_name="fake_peer_name", host="fake_host", port=1234, ) diff --git a/itests/tests/test_squeak_node.py b/itests/tests/test_squeak_node.py index 533558c5..38652f78 100644 --- a/itests/tests/test_squeak_node.py +++ b/itests/tests/test_squeak_node.py @@ -481,6 +481,31 @@ def test_make_contact_profile(server_stub, admin_stub): assert contact_profile_id in contact_profile_ids +def test_make_signing_profile_empty_name(server_stub, admin_stub): + # Try to create a new signing profile with an empty name + with pytest.raises(Exception) as excinfo: + admin_stub.CreateSigningProfile( + squeak_admin_pb2.CreateSigningProfileRequest( + profile_name="", + ) + ) + assert "Profile name cannot be empty." in str(excinfo.value) + + +def test_make_contact_profile_empty_name(server_stub, admin_stub): + # Try to create a new contact profile with an empty name + contact_signing_key = generate_signing_key() + contact_address = get_address(contact_signing_key) + with pytest.raises(Exception) as excinfo: + admin_stub.CreateContactProfile( + squeak_admin_pb2.CreateContactProfileRequest( + profile_name="", + address=contact_address, + ) + ) + assert "Profile name cannot be empty." in str(excinfo.value) + + def test_set_profile_following(server_stub, admin_stub, contact_profile_id): # Set the profile to be following admin_stub.SetSqueakProfileFollowing( @@ -683,6 +708,7 @@ def test_create_peer(server_stub, admin_stub): # Add a new peer create_peer_response = admin_stub.CreatePeer( squeak_admin_pb2.CreatePeerRequest( + peer_name="fake_peer_name", host="fake_host", port=1234, ) @@ -706,6 +732,19 @@ def test_create_peer(server_stub, admin_stub): assert "fake_host" in peer_hosts +def test_create_peer_empty_name(server_stub, admin_stub): + # Try to create a new signing profile with an empty name + with pytest.raises(Exception) as excinfo: + admin_stub.CreatePeer( + squeak_admin_pb2.CreatePeerRequest( + peer_name="", + host="another_fake_host", + port=1234, + ) + ) + assert "Peer name cannot be empty." in str(excinfo.value) + + def test_set_peer_downloading(server_stub, admin_stub, peer_id): # Get the peer get_peer_response = admin_stub.GetPeer( @@ -999,6 +1038,7 @@ def test_connect_other_node( # Add the main node as a peer create_peer_response = other_admin_stub.CreatePeer( squeak_admin_pb2.CreatePeerRequest( + peer_name="test_peer", host="squeaknode", port=8774, ) @@ -1200,6 +1240,7 @@ def test_download_single_squeak( # Add the main node as a peer create_peer_response = other_admin_stub.CreatePeer( squeak_admin_pb2.CreatePeerRequest( + peer_name="test_peer", host="squeaknode", port=8774, ) diff --git a/squeaknode/admin/squeak_admin_server_handler.py b/squeaknode/admin/squeak_admin_server_handler.py index 769f3520..fe6ace26 100644 --- a/squeaknode/admin/squeak_admin_server_handler.py +++ b/squeaknode/admin/squeak_admin_server_handler.py @@ -388,7 +388,7 @@ class SqueakAdminServerHandler(object): return squeak_admin_pb2.DeleteSqueakReply() def handle_create_peer(self, request): - peer_name = request.peer_name if request.peer_name else None + peer_name = request.peer_name host = request.host port = request.port logger.info( diff --git a/squeaknode/core/squeak_controller.py b/squeaknode/core/squeak_controller.py index eee29f0f..9025c6d3 100644 --- a/squeaknode/core/squeak_controller.py +++ b/squeaknode/core/squeak_controller.py @@ -120,6 +120,10 @@ class SqueakController: return sent_offer def create_signing_profile(self, profile_name: str): + if len(profile_name) == 0: + raise Exception( + "Profile name cannot be empty.", + ) signing_key = CSigningKey.generate() verifying_key = signing_key.get_verifying_key() address = CSqueakAddress.from_verifying_key(verifying_key) @@ -154,9 +158,16 @@ class SqueakController: return self.squeak_db.insert_profile(squeak_profile) def create_contact_profile(self, profile_name: str, squeak_address: str): + if len(profile_name) == 0: + raise Exception( + "Profile name cannot be empty.", + ) if not is_address_valid(squeak_address): raise Exception( - "Invalid squeak address: {}".format(squeak_address)) + "Invalid squeak address: {}".format( + squeak_address + ), + ) squeak_profile = SqueakProfile( profile_id=None, profile_name=profile_name, @@ -226,6 +237,10 @@ class SqueakController: return self.squeak_db.delete_squeak(squeak_hash) def create_peer(self, peer_name: str, host: str, port: int): + if len(peer_name) == 0: + raise Exception( + "Peer name cannot be empty.", + ) port = port or self.config.core.default_peer_rpc_port squeak_peer = SqueakPeer( peer_id=None,