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,9 +41,11 @@ } void DeviceSessionItem::validate() const { - if (!this->sessionID.size()) { - throw std::runtime_error("Error: SessionID is empty."); - } + tools::checkIfNotEmpty("pubKey", this->pubKey); + tools::checkIfNotEmpty("notifyToken", this->notifyToken); + tools::checkIfNotEmpty("deviceType", this->deviceType); + tools::checkIfNotEmpty("appVersion", this->appVersion); + tools::checkIfNotEmpty("deviceOs", this->deviceOs); } void DeviceSessionItem::assignItemFromDatabase( 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,18 +36,8 @@ } 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"); - } - if (!this->expire == 0) { - throw std::runtime_error("Error: expire field not provided"); - } + tools::checkIfNotEmpty("messageID", this->messageID); + tools::checkIfNotZero("expire", this->expire); } void MessageItem::assignItemFromDatabase(const AttributeValues &itemFromDB) { diff --git a/services/tunnelbroker/src/Database/PublicKeyItem.cpp b/services/tunnelbroker/src/Database/PublicKeyItem.cpp --- a/services/tunnelbroker/src/Database/PublicKeyItem.cpp +++ b/services/tunnelbroker/src/Database/PublicKeyItem.cpp @@ -1,5 +1,6 @@ #include "PublicKeyItem.h" #include "ConfigManager.h" +#include "Tools.h" namespace comm { namespace network { @@ -20,12 +21,7 @@ } void PublicKeyItem::validate() const { - if (!this->deviceID.size()) { - throw std::runtime_error("Error: DeviceID is empty"); - } - if (!this->publicKey.size()) { - throw std::runtime_error("Error: PublicKey is empty"); - } + tools::checkIfNotEmpty("publicKey", this->publicKey); } void PublicKeyItem::assignItemFromDatabase(const AttributeValues &itemFromDB) { diff --git a/services/tunnelbroker/src/Database/SessionSignItem.cpp b/services/tunnelbroker/src/Database/SessionSignItem.cpp --- a/services/tunnelbroker/src/Database/SessionSignItem.cpp +++ b/services/tunnelbroker/src/Database/SessionSignItem.cpp @@ -1,5 +1,6 @@ #include "SessionSignItem.h" #include "ConfigManager.h" +#include "Tools.h" namespace comm { namespace network { @@ -22,12 +23,7 @@ } void SessionSignItem::validate() const { - if (!this->deviceID.size()) { - throw std::runtime_error("Error: DeviceID is empty"); - } - if (!this->sign.size()) { - throw std::runtime_error("Error: Sign is empty"); - } + tools::checkIfNotEmpty("sign", this->sign); } void SessionSignItem::assignItemFromDatabase( 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 @@ -12,6 +12,8 @@ bool validateDeviceID(std::string deviceID); std::string generateUUID(); bool validateSessionID(std::string sessionID); +void checkIfNotEmpty(std::string fieldName, std::string stringToCheck); +void checkIfNotZero(std::string fieldName, uint64_t numberToCheck); } // namespace tools } // namespace network 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 @@ -68,6 +68,20 @@ } } +void checkIfNotEmpty(std::string fieldName, std::string stringToCheck) { + if (stringToCheck.empty()) { + throw std::runtime_error( + "Error: Required text field " + fieldName + " is empty."); + } +} + +void checkIfNotZero(std::string fieldName, uint64_t numberToCheck) { + if (numberToCheck == 0) { + throw std::runtime_error( + "Error: Required number " + fieldName + " is zero."); + } +} + } // namespace tools } // namespace network } // namespace comm