Optimize code of dcm-client for higher code coverage 51/316451/2
authorTomasz Swierczek <t.swierczek@samsung.com>
Wed, 21 Aug 2024 14:45:08 +0000 (16:45 +0200)
committerTomasz Swierczek <t.swierczek@samsung.com>
Thu, 29 Aug 2024 07:45:09 +0000 (09:45 +0200)
Change-Id: I4a8aeda049ab7a9cbadbc0251f82354fe75f1849

src/dcm-client/dcm_client.cpp

index 40eada9b3de72b48c5b86836c29113acd441bb2f..97bfd3f3255a9a4bcb34e23aaa5600de52fee62b 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************
  *
- * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2024 Samsung Electronics All Rights Reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "protobuf_asio.h"
 #include "log.h"
 
+#define LOGE_return(error, str...)\
+{\
+       LOGE(str);\
+       return error;\
+}
+
+
 size_t get_digest_size(dcm_digest_algorithm_e md)
 {
        switch(md) {
-               case DCM_DIGEST_NONE:
-                       return 0;
                case DCM_DIGEST_MD2:
                case DCM_DIGEST_MD4:
                case DCM_DIGEST_MD5:
@@ -49,6 +54,7 @@ size_t get_digest_size(dcm_digest_algorithm_e md)
                        return 64;
                case DCM_DIGEST_RIPEMD160:
                        return 20;
+               case DCM_DIGEST_NONE:
                default:
                        return 0;
        }
@@ -71,9 +77,7 @@ int dcm_client_connection::create_context(
        std::lock_guard<std::mutex> locker(fLock);
 
        if(fCookie) {
-               LOGE("Cookie has already been requested for session %p", this);
-               // Already created
-               return DCM_ERROR_SOCKET;
+               LOGE_return(DCM_ERROR_SOCKET, "Cookie has already been requested for session %p", this);
        }
 
        if(!fSocket) {
@@ -82,11 +86,13 @@ int dcm_client_connection::create_context(
                        boost::asio::local::stream_protocol::endpoint endpoint(DCM_UNIX_SOCKET_PATH);
                        fSocket->connect(endpoint);
                } catch(std::exception& ex) {
-                       LOGE("Caught exception \"%s\" when connecting socket for session %p", ex.what(), this);
-                       return DCM_ERROR_SOCKET;
+                       LOGE_return(DCM_ERROR_SOCKET,
+                                               "Caught exception \"%s\" when connecting socket for session %p",
+                                               ex.what(), this);
                } catch(...) {
-                       LOGE("Caught unknown exception when connecting socket for session %p", this);
-                       return DCM_ERROR_SOCKET;
+                       LOGE_return(DCM_ERROR_SOCKET,
+                                               "Caught unknown exception when connecting socket for session %p",
+                                               this);
                }
        }
 
@@ -101,27 +107,33 @@ int dcm_client_connection::create_context(
 
                send_receive(request, response);
                if(!response.has_associate_context()) {
-                       LOGE("Received response is not context association message in context %p", this);
-                       return DCM_ERROR_NO_DATA;
+                       LOGE_return(DCM_ERROR_NO_DATA,
+                                               "Received response is not context association message in context %p",
+                                               this);
                }
 
                auto& assoc_message(response.associate_context());
                if(assoc_message.result() != 0) {
-                       LOGE("Received context association message with error %d in %p", assoc_message.result(), this);
-                       return DCM_ERROR_NO_DATA;
+                       LOGE_return(DCM_ERROR_NO_DATA,
+                                               "Received context association message with error %d in %p",
+                                               assoc_message.result(),
+                                               this);
                }
 
                fCookie = assoc_message.context_cookie();
                fKeyType = assoc_message.key_type();
                fKeyLength = assoc_message.key_length();
        } catch(std::exception& ex) {
-               LOGE("Caught exception \"%s\" when establishing cookie for session %p", ex.what(), this);
                fSocket.reset();
-               return DCM_ERROR_SOCKET;
+               LOGE_return(DCM_ERROR_SOCKET,
+                                       "Caught exception \"%s\" when establishing cookie for session %p",
+                                       ex.what(),
+                                       this);
        } catch(...) {
-               LOGE("Caught unknown exception when establishing cookie for session %p", this);
                fSocket.reset();
-               return DCM_ERROR_SOCKET;
+               LOGE_return(DCM_ERROR_SOCKET,
+                                       "Caught unknown exception when establishing cookie for session %p",
+                                       this);
        }
 
        return DCM_ERROR_NONE;
@@ -132,8 +144,9 @@ int dcm_client_connection::close_context() noexcept
        std::lock_guard<std::mutex> locker(fLock);
 
        if(!fCookie) {
-               LOGE("Trying to close key context in session %p without connection", this);
-               return DCM_ERROR_SOCKET;
+               LOGE_return(DCM_ERROR_SOCKET,
+                                       "Trying to close key context in session %p without connection",
+                                       this);
        }
 
        try {
@@ -146,18 +159,25 @@ int dcm_client_connection::close_context() noexcept
                send_receive(request, response);
                auto& close_resp(response.close_context());
                if(close_resp.result() != 0) {
-                       LOGE("Close key context for session %p received error %d", this, close_resp.result());
-                       return DCM_ERROR_INVALID_PARAMETER;
+                       LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+                                               "Close key context for session %p received error %d",
+                                               this,
+                                               close_resp.result());
                }
        } catch(std::bad_alloc&) {
-               LOGE("Out of memory when requesting close key context for session %p", this);
-               return DCM_ERROR_OUT_OF_MEMORY;
+               LOGE_return(DCM_ERROR_OUT_OF_MEMORY,
+                                       "Out of memory when requesting close key context for session %p",
+                                       this);
        } catch(std::exception& ex) {
-               LOGE("When requesting close key context for session %p received exception : %s", this, ex.what());
-               return DCM_ERROR_UNKNOWN;
+               LOGE_return(DCM_ERROR_UNKNOWN,
+                                       "When requesting close key context for session %p received exception : %s",
+                                       this,
+                                       ex.what());
        } catch(...) {
-               LOGE("When requesting close key context for session %p received exception : %s", this, "Unknown error");
-               return DCM_ERROR_UNKNOWN;
+               LOGE_return(DCM_ERROR_UNKNOWN,
+                                       "When requesting close key context for session %p received exception : %s",
+                                       this,
+                                       "Unknown error");
        }
 
        return DCM_ERROR_NONE;
@@ -168,8 +188,9 @@ int dcm_client_connection::get_certificate_chain(std::vector<uint8_t>& chain) no
        std::lock_guard<std::mutex> locker(fLock);
 
        if(!fCookie) {
-               LOGE("Trying to request certificate in session %p without connection", this);
-               return DCM_ERROR_SOCKET;
+               LOGE_return(DCM_ERROR_SOCKET,
+                                       "Trying to request certificate in session %p without connection",
+                                       this);
        }
 
        try {
@@ -181,19 +202,22 @@ int dcm_client_connection::get_certificate_chain(std::vector<uint8_t>& chain) no
 
                send_receive(request, response);
                if(!response.has_request_chain()) {
-                       LOGE("Response from server is not certificate chain response on session %p", this);
-                       return DCM_ERROR_NO_DATA;
+                       LOGE_return(DCM_ERROR_NO_DATA,
+                                               "Response from server is not certificate chain response on session %p",
+                                               this);
                }
 
                auto& cert_resp(response.request_chain());
                if(cert_resp.result() != 0) {
-                       LOGE("Server can't respond with certificate chain: error %d in session %p", cert_resp.result(), this);
-                       return DCM_ERROR_NO_DATA;
+                       LOGE_return(DCM_ERROR_NO_DATA,
+                                               "Server can't respond with certificate chain: error %d in session %p",
+                                               cert_resp.result(),
+                                               this);
                }
 
                if(cert_resp.cert_chain().size() == 0) {
-                       LOGE("Server can't respond with certificate chain: certificate empty");
-                       return DCM_ERROR_NO_DATA;
+                       LOGE_return(DCM_ERROR_NO_DATA,
+                                               "Server can't respond with certificate chain: certificate empty");
                }
 
                // Add space for last zero byte if needed
@@ -206,17 +230,23 @@ int dcm_client_connection::get_certificate_chain(std::vector<uint8_t>& chain) no
                        chain.push_back(0);
                }
        } catch(std::bad_alloc&) {
-               LOGE("Out of memory when requesting certificate for session %p", this);
-               return DCM_ERROR_OUT_OF_MEMORY;
+               LOGE_return(DCM_ERROR_OUT_OF_MEMORY,
+                                       "Out of memory when requesting certificate for session %p",
+                                       this);
        } catch(std::invalid_argument&) {
-               LOGE("Invalid argument passed for certificate request for session %p", this);
-               return DCM_ERROR_INVALID_PARAMETER;
+               LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+                                       "Invalid argument passed for certificate request for session %p",
+                                       this);
        } catch(std::exception& ex) {
-               LOGE("When requesting certificate for session %p received exception : %s", this, ex.what());
-               return DCM_ERROR_UNKNOWN;
+               LOGE_return(DCM_ERROR_UNKNOWN,
+                                       "When requesting certificate for session %p received exception : %s",
+                                       this,
+                                       ex.what());
        } catch(...) {
-               LOGE("When requesting certificate for session %p received exception : %s", this, "Unknown error");
-               return DCM_ERROR_UNKNOWN;
+               LOGE_return(DCM_ERROR_UNKNOWN,
+                                       "When requesting certificate for session %p received exception : %s",
+                                       this,
+                                       "Unknown error");
        }
 
        return DCM_ERROR_NONE;
@@ -244,23 +274,26 @@ int dcm_client_connection::sign_data(
        std::lock_guard<std::mutex> locker(fLock);
 
        if(!fCookie) {
-               LOGE("Trying to request data signing in object %p but there is no connection", this);
-               return DCM_ERROR_SOCKET;
+               LOGE_return(DCM_ERROR_SOCKET,
+                                       "Trying to request data signing in object %p but there is no connection",
+                                       this);
        }
 
        // If hash_size == 0 then hash type must be known
        if(hash_size == 0) {
                if(md == DCM_DIGEST_NONE) {
-                       LOGE("Digest type is NONE and hash size is 0");
-                       return DCM_ERROR_INVALID_PARAMETER;
+                       LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+                                               "Digest type is NONE and hash size is 0");
                }
 
                hash_size = get_digest_size(md);
        } else if(hash_size != 0 && md != DCM_DIGEST_NONE) {
                // If hash_size != 0 then hash type can be specified
                if(hash_size != get_digest_size(md)) {
-                       LOGE("Hash size mismatch. Expected %zd but got %zd", hash_size, get_digest_size(md));
-                       return DCM_ERROR_INVALID_PARAMETER;
+                       LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+                                               "Hash size mismatch. Expected %zd but got %zd",
+                                               hash_size,
+                                               get_digest_size(md));
                }
        }
 
@@ -275,30 +308,36 @@ int dcm_client_connection::sign_data(
 
                send_receive(request, response);
                if(!response.has_sign_data()) {
-                       LOGE("Response for hash signature has no signature data");
-                       return DCM_ERROR_NO_DATA;
+                       LOGE_return(DCM_ERROR_NO_DATA,
+                                               "Response for hash signature has no signature data");
                }
 
                auto& sign_resp(response.sign_data());
                if(sign_resp.result() != 0) {
-                       LOGE("Signature request for session %p received error %d", this, sign_resp.result());
-                       return DCM_ERROR_INVALID_PARAMETER;
+                       LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+                                               "Signature request for session %p received error %d",
+                                               this,
+                                               sign_resp.result());
                }
 
                const auto& signature = sign_resp.signature();
                if(signature.empty()) {
-                       LOGE("Received signature object is empty for session %p", this);
-                       return DCM_ERROR_NO_DATA;
+                       LOGE_return(DCM_ERROR_NO_DATA,
+                                               "Received signature object is empty for session %p",
+                                               this);
                }
 
                digest.resize(signature.size());
                memcpy(&digest[0], signature.c_str(), signature.size());
        } catch(std::bad_alloc&) {
-               LOGE("Out of memory when processing sign request for session %p", this);
-               return DCM_ERROR_OUT_OF_MEMORY;
+               LOGE_return(DCM_ERROR_OUT_OF_MEMORY,
+                                       "Out of memory when processing sign request for session %p",
+                                       this);
        } catch(...) {
-               LOGE("When processing signature for session %p got exception : %s", this, "Unknown error");
-               return DCM_ERROR_UNKNOWN;
+               LOGE_return(DCM_ERROR_UNKNOWN,
+                                       "When processing signature for session %p got exception : %s",
+                                       this,
+                                       "Unknown error");
        }
 
        return DCM_ERROR_NONE;