示例#1
0
def get_suciprofile(home_net_public_key_id: int):
    """
    get_suciprofile
    """
    channel = grpc.insecure_channel('localhost:50051')
    stub = SuciProfileDBStub(channel)
    response = stub.ListSuciProfile(Void())
    if not response.suci_profiles:
        print("SuciProfileList is empty")
    else:
        for x in response.suci_profiles:
            if home_net_public_key_id == x.home_net_public_key_id:
                return x.home_net_public_key
        return None
示例#2
0
    def setUp(self):
        # Create an in-memory store
        self._tmpfile = tempfile.TemporaryDirectory()
        store = SqliteStore(self._tmpfile.name + '/')

        # Bind the rpc server to a free port
        self._rpc_server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=10), )
        port = self._rpc_server.add_insecure_port('0.0.0.0:0')

        suciprofile_db = {}
        # Add the servicer
        self._servicer = SuciProfileDBRpcServicer(
            store,
            suciprofile_db,
        )

        self._servicer.add_to_server(self._rpc_server)
        self._rpc_server.start()

        # Create a rpc stub
        channel = grpc.insecure_channel('0.0.0.0:{}'.format(port))
        self._stub = SuciProfileDBStub(channel)
示例#3
0
class RpcTestsSuciExt(unittest.TestCase):
    """
    Tests for the SubscriberDB rpc servicer and stub
    """
    def setUp(self):
        # Create an in-memory store
        self._tmpfile = tempfile.TemporaryDirectory()
        store = SqliteStore(self._tmpfile.name + '/')

        # Bind the rpc server to a free port
        self._rpc_server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=10), )
        port = self._rpc_server.add_insecure_port('0.0.0.0:0')

        suciprofile_db = {}
        # Add the servicer
        self._servicer = SuciProfileDBRpcServicer(
            store,
            suciprofile_db,
        )

        self._servicer.add_to_server(self._rpc_server)
        self._rpc_server.start()

        # Create a rpc stub
        channel = grpc.insecure_channel('0.0.0.0:{}'.format(port))
        self._stub = SuciProfileDBStub(channel)

    def tearDown(self):
        self._tmpfile.cleanup()
        self._rpc_server.stop(0)

    def test_add_delete_suciprofile(self):
        """
        Test if AddSuciProfile and DeleteSuciProfile rpc call works
        """
        home_net_public_key_id = 2
        protection_scheme = SuciProfile.ProfileA
        home_net_public_key = bytes(
            b'\t\xd4(\x93O7\x15\x13\xa4\x1c\xf6\xef\x96\x01bwH\xf3wO\x1ds\x99\xd7\x8d{dc\x0c\x94Q\x08'
        )
        home_net_private_key = bytes(
            b'\xc8\x0b\x16v\xa0\xc9\x83PI\x0f\xf1\xc3\x13\x08-\xedE\xbcY\xe9\xe7)\xb8x\x8e\xba\xc44\xb3\x8a\xb5m'
        )

        request = SuciProfile(
            home_net_public_key_id=home_net_public_key_id,
            protection_scheme=protection_scheme,
            home_net_public_key=home_net_public_key,
            home_net_private_key=home_net_private_key,
        )
        # Add subscriber
        self._stub.AddSuciProfile(request)

        # Add subscriber again
        with self.assertRaises(grpc.RpcError) as err:
            self._stub.AddSuciProfile(request)
        self.assertEqual(err.exception.code(), grpc.StatusCode.ALREADY_EXISTS)

        # See if we can get the data for the subscriber
        self.assertEqual(len(self._stub.ListSuciProfile(Void()).suci_profiles),
                         1)
        self.assertEqual(
            self._stub.ListSuciProfile(
                Void()).suci_profiles[0].home_net_public_key_id, 2)
        self.assertEqual(
            self._stub.ListSuciProfile(
                Void()).suci_profiles[0].protection_scheme,
            SuciProfile.ProfileA)

        # Delete the subscriber
        request = SuciProfile(
            home_net_public_key_id=int(home_net_public_key_id))
        self._stub.DeleteSuciProfile(request)
        self.assertEqual(len(self._stub.ListSuciProfile(Void()).suci_profiles),
                         0)

        # Delete the subscriber
        with self.assertRaises(grpc.RpcError) as err:
            request = SuciProfile(
                home_net_public_key_id=int(home_net_public_key_id))
            self._stub.DeleteSuciProfile(request)
        self.assertEqual(err.exception.code(), grpc.StatusCode.NOT_FOUND)