From 64b3296442214899bbec92384bbf76421edda819 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Thu, 20 Jul 2017 07:40:34 -0600 Subject: [PATCH] Clean unittest query building slightly In two unit tests a string used to build another string, and thus read-only, is marked const. In one instance, for consistency the malloc/strcpy sequence to build the actual query is replaced with the single strdup call used elsewhere. In one instance the declaration of a query string is moved to where it is used. Change-Id: Iddc0317d446b5bdc099e0045eba4bb1c87062640 Signed-off-by: Mats Wichmann Reviewed-on: https://gerrit.iotivity.org/gerrit/21699 Tested-by: jenkins-iotivity Reviewed-by: Phil Coval --- service/notification/unittest/NSProviderTest2.cpp | 15 +++++++-------- .../src/serverBuilder/unittests/RCSResourceObjectTest.cpp | 5 ++--- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/service/notification/unittest/NSProviderTest2.cpp b/service/notification/unittest/NSProviderTest2.cpp index d9cf8ea..618a461 100644 --- a/service/notification/unittest/NSProviderTest2.cpp +++ b/service/notification/unittest/NSProviderTest2.cpp @@ -99,11 +99,10 @@ namespace request->obsInfo.action = action; request->obsInfo.obsId = id++; - std::string query = std::string(NS_QUERY_CONSUMER_ID) + const std::string query = std::string(NS_QUERY_CONSUMER_ID) + "=" + testConsumerId; - request->query = (char *)malloc(query.size() + 1); + request->query = strdup(query.c_str()); EXPECT_NE((void *)NULL, request->query); - strncpy(request->query, query.c_str(), query.size() + 1); } request->method = method; request->numRcvdVendorSpecificHeaderOptions = 0; @@ -239,7 +238,7 @@ TEST(NotificationProviderTest, ExpectFailGetRequestForNotificationWithInvalidInt { OCEntityHandlerFlag flag = OC_REQUEST_FLAG; OCEntityHandlerRequest * getRequest = getEntityRequest(OC_REST_GET, OC_OBSERVE_NO_OPTION); - std::string query = std::string("if=") + "test.invalid"; + const std::string query = std::string("if=") + "test.invalid"; getRequest->query = strdup(query.c_str()); auto ret = NSEntityHandlerNotificationCb(flag, getRequest, NULL); @@ -253,7 +252,7 @@ TEST(NotificationProviderTest, ExpectFailGetRequestForMsgWithInvalidInterface) { OCEntityHandlerFlag flag = OC_REQUEST_FLAG; OCEntityHandlerRequest * getRequest = getEntityRequest(OC_REST_GET, OC_OBSERVE_NO_OPTION); - std::string query = std::string("if=") + "test.invalid"; + const std::string query = std::string("if=") + "test.invalid"; getRequest->query = strdup(query.c_str()); auto ret = NSEntityHandlerMessageCb(flag, getRequest, NULL); @@ -267,7 +266,7 @@ TEST(NotificationProviderTest, ExpectFailGetRequestForSyncWithInvalidInterface) { OCEntityHandlerFlag flag = OC_REQUEST_FLAG; OCEntityHandlerRequest * getRequest = getEntityRequest(OC_REST_GET, OC_OBSERVE_NO_OPTION); - std::string query = std::string("if=") + "test.invalid"; + const std::string query = std::string("if=") + "test.invalid"; getRequest->query = strdup(query.c_str()); auto ret = NSEntityHandlerSyncCb(flag, getRequest, NULL); @@ -281,7 +280,7 @@ TEST(NotificationProviderTest, ExpectFailGetRequestForTopicWithInvalidInterface) { OCEntityHandlerFlag flag = OC_REQUEST_FLAG; OCEntityHandlerRequest * getRequest = getEntityRequest(OC_REST_GET, OC_OBSERVE_NO_OPTION); - std::string query = std::string("if=") + "test.invalid"; + const std::string query = std::string("if=") + "test.invalid"; getRequest->query = strdup(query.c_str()); auto ret = NSEntityHandlerTopicCb(flag, getRequest, NULL); @@ -295,7 +294,7 @@ TEST(NotificationProviderTest, ExpectSuccessGetRequestForTopicWithInvalidInterfa { OCEntityHandlerFlag flag = OC_REQUEST_FLAG; OCEntityHandlerRequest * getRequest = getEntityRequest(OC_REST_GET, OC_OBSERVE_NO_OPTION); - std::string query = std::string("if=") + NS_INTERFACE_BASELINE; + const std::string query = std::string("if=") + NS_INTERFACE_BASELINE; getRequest->query = strdup(query.c_str()); auto ret = NSEntityHandlerTopicCb(flag, getRequest, NULL); diff --git a/service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp b/service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp index e111be6..851c97c 100644 --- a/service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp +++ b/service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp @@ -374,7 +374,6 @@ public: auto request = make_shared(); OCEntityHandlerRequest ocEntityHandlerRequest; - string query; memset(&ocEntityHandlerRequest, 0, sizeof(OCEntityHandlerRequest)); OC::MessageContainer mc; @@ -387,8 +386,8 @@ public: if(!interface.empty()) { - query = string("if=" + interface); - ocEntityHandlerRequest.query = const_cast (query.c_str()); + const string query = string("if=" + interface); + ocEntityHandlerRequest.query = strdup(query.c_str()); } formResourceRequest(OC_REQUEST_FLAG, &ocEntityHandlerRequest, request); -- 2.7.4