diff --git a/services/tunnelbroker/src/Database/DeviceSessionItem.cpp b/services/tunnelbroker/src/Database/DeviceSessionItem.cpp --- a/services/tunnelbroker/src/Database/DeviceSessionItem.cpp +++ b/services/tunnelbroker/src/Database/DeviceSessionItem.cpp @@ -1,5 +1,8 @@ #include "DeviceSessionItem.h" #include "ConfigManager.h" +#include "Tools.h" + +#include namespace comm { namespace network { @@ -38,8 +41,15 @@ } void DeviceSessionItem::validate() const { - if (!this->sessionID.size()) { - throw std::runtime_error("Error: SessionID is empty."); + std::vector requiredFields{ + this->pubKey, + this->notifyToken, + this->deviceType, + this->appVersion, + this->deviceOs}; + if (!checkEmptyStringInList(requiredFields)) { + throw std::runtime_error( + "Error: One or more required parameters are empty."); } } diff --git a/services/tunnelbroker/src/Database/MessageItem.cpp b/services/tunnelbroker/src/Database/MessageItem.cpp --- a/services/tunnelbroker/src/Database/MessageItem.cpp +++ b/services/tunnelbroker/src/Database/MessageItem.cpp @@ -1,5 +1,8 @@ #include "MessageItem.h" #include "ConfigManager.h" +#include "Tools.h" + +#include namespace comm { namespace network { @@ -33,14 +36,11 @@ } void MessageItem::validate() const { - if (!this->messageID.size()) { - throw std::runtime_error("Error: messageID is empty"); - } - if (!this->fromDeviceID.size()) { - throw std::runtime_error("Error: fromDeviceID is empty"); - } - if (!this->toDeviceID.size()) { - throw std::runtime_error("Error: toDeviceID is empty"); + std::vector requiredFields{ + this->messageID, this->fromDeviceID, this->toDeviceID}; + if (!checkEmptyStringInList(requiredFields)) { + throw std::runtime_error( + "Error: One or more required parameters are empty."); } if (!this->expire == 0) { throw std::runtime_error("Error: expire field not provided"); diff --git a/services/tunnelbroker/src/Tools/Tools.h b/services/tunnelbroker/src/Tools/Tools.h --- a/services/tunnelbroker/src/Tools/Tools.h +++ b/services/tunnelbroker/src/Tools/Tools.h @@ -11,6 +11,7 @@ bool validateDeviceID(std::string deviceID); std::string generateUUID(); bool validateSessionID(std::string sessionID); +bool checkEmptyStringInList(std::vector stringList); } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Tools/Tools.cpp b/services/tunnelbroker/src/Tools/Tools.cpp --- a/services/tunnelbroker/src/Tools/Tools.cpp +++ b/services/tunnelbroker/src/Tools/Tools.cpp @@ -67,5 +67,14 @@ } } +bool checkEmptyStringInList(std::vector stringList) { + for (std::string stringToCheck : stringList) { + if (stringToCheck.empty()) { + return false; + } + } + return true; +} + } // namespace network } // namespace comm