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."); - } + checkEmptyField("pubKey", this->pubKey); + checkEmptyField("notifyToken", this->notifyToken); + checkEmptyField("deviceType", this->deviceType); + checkEmptyField("appVersion", this->appVersion); + checkEmptyField("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"); - } + checkEmptyField("messageID", this->messageID); + checkEmptyField("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"); - } + checkEmptyField("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"); - } + checkEmptyField("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 @@ -11,6 +11,8 @@ bool validateDeviceID(std::string deviceID); std::string generateUUID(); bool validateSessionID(std::string sessionID); +void checkEmptyField(std::string fieldName, std::string stringToCheck); +void checkEmptyField(std::string fieldName, uint64_t numberToCheck); } // 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,19 @@ } } +void checkEmptyField(std::string fieldName, std::string stringToCheck) { + if (stringToCheck.empty()) { + throw std::runtime_error( + "Error: Required text field " + fieldName + " is empty."); + } +} + +void checkEmptyField(std::string fieldName, uint64_t numberToCheck) { + if (numberToCheck == 0) { + throw std::runtime_error( + "Error: Required number " + fieldName + " is zero."); + } +} + } // namespace network } // namespace comm