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 2 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
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