Clean unittest query building slightly
authorMats Wichmann <mats@linux.com>
Thu, 20 Jul 2017 13:40:34 +0000 (07:40 -0600)
committerPhil Coval <philippe.coval@osg.samsung.com>
Thu, 10 Aug 2017 13:39:03 +0000 (13:39 +0000)
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 <mats@linux.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/21699
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
service/notification/unittest/NSProviderTest2.cpp
service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp

index d9cf8ea..618a461 100644 (file)
@@ -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);
 
index e111be6..851c97c 100644 (file)
@@ -374,7 +374,6 @@ public:
         auto request = make_shared<OCResourceRequest>();
 
         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<char *> (query.c_str());
+            const string query = string("if=" + interface);
+            ocEntityHandlerRequest.query = strdup(query.c_str());
         }
 
         formResourceRequest(OC_REQUEST_FLAG, &ocEntityHandlerRequest, request);