Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

FOGL-2773: Allow service name with double quotes escaped in FogLAMP C… #1541

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion C/common/include/json_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
*
* Released under the Apache 2.0 Licence
*
* Author: Stefano Simonelli
* Author: Stefano Simonelli, Massimiliano Pinto
*/

#include<vector>
#include<string>

bool JSONStringToVectorString(std::vector<std::string>& vectorString,
const std::string& JSONString,
const std::string& Key);
Expand Down
3 changes: 2 additions & 1 deletion C/common/service_record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <service_record.h>
#include <string>
#include <sstream>
#include <json_utils.h>

using namespace std;

Expand Down Expand Up @@ -63,7 +64,7 @@ void ServiceRecord::asJSON(string& json) const
ostringstream convert;

convert << "{ ";
convert << "\"name\" : \"" << m_name << "\",";
convert << "\"name\" : \"" << JSONescape(m_name) << "\",";
convert << "\"type\" : \"" << m_type << "\",";
convert << "\"protocol\" : \"" << m_protocol << "\",";
convert << "\"address\" : \"" << m_address << "\",";
Expand Down
4 changes: 3 additions & 1 deletion C/services/south/include/south_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class SouthService : public ServiceHandler {
void addConfigDefaults(DefaultConfigCategory& defaults);
bool loadPlugin();
int createTimerFd(struct timeval rate);
void createConfigCategories(DefaultConfigCategory configCategory, std::string parent_name,std::string current_name);
bool createConfigCategories(DefaultConfigCategory configCategory,
std::string parent_name,
std::string current_name);
private:
SouthPlugin *southPlugin;
const std::string& m_name;
Expand Down
16 changes: 13 additions & 3 deletions C/services/south/south.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ void SouthService::stop()
/**
* Creates config categories and sub categories recursively, along with their parent-child relations
*/
void SouthService::createConfigCategories(DefaultConfigCategory configCategory, std::string parent_name, std::string current_name)
bool SouthService::createConfigCategories(DefaultConfigCategory configCategory, std::string parent_name, std::string current_name)
{

// Deal with registering and fetching the configuration
Expand All @@ -435,7 +435,13 @@ void SouthService::createConfigCategories(DefaultConfigCategory configCategory,
defConfig.removeItemsType(ConfigCategory::ItemType::CategoryType);

// Create/Update category name (we pass keep_original_items=true)
m_mgtClient->addCategory(defConfig, true);
if (!m_mgtClient->addCategory(defConfig, true))
{
logger->fatal("Failed to add category %s for %s",
current_name.c_str(),
parent_name.c_str());
return false;
}

// Add this service under 'South' parent category
vector<string> children;
Expand All @@ -460,6 +466,7 @@ void SouthService::createConfigCategories(DefaultConfigCategory configCategory,
}
}

return true;
}

/**
Expand All @@ -484,7 +491,10 @@ bool SouthService::loadPlugin()
{
// Adds categories and sub categories to the configuration
DefaultConfigCategory defConfig(m_name, manager->getInfo(handle)->config);
createConfigCategories(defConfig, string("South"), m_name);
if (!createConfigCategories(defConfig, string("South"), m_name))
{
return false;
}

// Must now reload the configuration to obtain any items added from
// the plugin
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/C/common/test_service_record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ string expected("{ \"name\" : \"test1\",\"type\" : \"testType\",\"protocol\" : \
ASSERT_EQ(json.compare(expected), 0);
}

TEST(ServiceRecordTest, JSONQuotes)
{
ServiceRecord serviceRecord("test\"1\"", "testType", "http", "localhost", 1234, 4321);
string json;
string expected("{ \"name\" : \"test\\\"1\\\"\",\"type\" : \"testType\",\"protocol\" : \"http\",\"address\" : \"localhost\",\"management_port\" : 4321,\"service_port\" : 1234 }");

serviceRecord.asJSON(json);

ASSERT_EQ(json.compare(expected), 0);
}
25 changes: 25 additions & 0 deletions tests/unit/C/services/core/test_service_regsitery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ TEST(ServiceRegistryTest, Register)
ASSERT_EQ(registry->registerService(record), true);
}

TEST(ServiceRegistryTest, RegisterQuotes)
{
ServiceRecord *record = new ServiceRecord("test\"11\"", "south", "http", "hostname", 1234, 4321);

ServiceRegistry *registry = ServiceRegistry::getInstance();
ASSERT_EQ(registry->registerService(record), true);
}

TEST(ServiceRegistryTest, DupRegister)
{
ServiceRecord *record = new ServiceRecord("test1", "south", "http", "hostname", 1234, 4321);
Expand Down Expand Up @@ -52,6 +60,14 @@ TEST(ServiceRegistryTest, Find)
ASSERT_NE(registry->findService("findtest"), (ServiceRecord *)0);
}

TEST(ServiceRegistryTest, FindQuotes)
{
ServiceRecord *record = new ServiceRecord("find\"test\"", "south", "http", "hostname", 1234, 4321);
ServiceRegistry *registry = ServiceRegistry::getInstance();
ASSERT_EQ(registry->registerService(record), true);
ASSERT_NE(registry->findService("find\"test\""), (ServiceRecord *)0);
}

TEST(ServiceRegistryTest, NotFind)
{
ServiceRegistry *registry = ServiceRegistry::getInstance();
Expand All @@ -67,6 +83,15 @@ TEST(ServiceRegistryTest, Unregister)
ASSERT_EQ(true, registry->unRegisterService(record));
}

TEST(ServiceRegistryTest, UnregisterQuotes)
{
ServiceRecord *record = new ServiceRecord("unregister\"me\"", "south", "http", "hostname", 1234, 4321);

ServiceRegistry *registry = ServiceRegistry::getInstance();
ASSERT_EQ(true, registry->registerService(record));
ASSERT_EQ(true, registry->unRegisterService(record));
}

TEST(ServiceRegistryTest, UnregisterNoExistant)
{
ServiceRecord *record = new ServiceRecord("non-existant", "north", "http", "hostname", 1234, 4321);
Expand Down