1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "rd_server.h"
24 #include "rd_client.h"
25 #include "rd_database.h"
28 #include "oic_malloc.h"
29 #include "oic_string.h"
30 #include "ocpayload.h"
31 #include "payload_logging.h"
34 #include "gtest/gtest.h"
35 #include <sys/types.h>
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
51 #include "gtest_helper.h"
55 namespace itst = iotivity::test;
57 #define DEFAULT_CONTEXT_VALUE 0x99
58 #define DEFAULT_MESSAGE_TYPE "application/json"
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 static const char TAG[] = "RDDatabaseTests";
65 std::chrono::seconds const SHORT_TEST_TIMEOUT = std::chrono::seconds(5);
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 class RDDatabaseTests : public testing::Test {
79 OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER);
80 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseInit());
83 virtual void TearDown()
85 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseClose());
90 static OCRepPayload *CreateResources(const char *deviceId)
92 OCRepPayload *repPayload = OCRepPayloadCreate();
93 EXPECT_TRUE(repPayload != NULL);
94 EXPECT_TRUE(OCRepPayloadSetPropString(repPayload, OC_RSRVD_DEVICE_ID, deviceId));
95 EXPECT_TRUE(OCRepPayloadSetPropInt(repPayload, OC_RSRVD_DEVICE_TTL, 86400));
98 OICStrcpy(address.addr,MAX_ADDR_STR_SIZE, "192.168.1.1");
100 std::string resourceURI_thermostat = "/a/thermostat";
101 std::string resourceTypeName_thermostat = "core.thermostat";
102 std::string resourceURI_light = "/a/light";
103 std::string resourceTypeName_light = "core.light";
105 const OCRepPayload *linkArr[2];
106 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
108 OCRepPayload *link = OCRepPayloadCreate();
109 OCRepPayloadSetPropString(link, OC_RSRVD_HREF, resourceURI_thermostat.c_str());
110 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
111 char **rt = (char **)OICMalloc(sizeof(char *) * 1);
112 rt[0] = OICStrdup(resourceTypeName_thermostat.c_str());
113 OCRepPayloadSetStringArray(link, OC_RSRVD_RESOURCE_TYPE, (const char **)rt,
116 size_t itfDim[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
117 char **itf = (char **)OICMalloc(sizeof(char *) * 1);
118 itf[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
119 OCRepPayloadSetStringArray(link, OC_RSRVD_INTERFACE, (const char **)itf,
121 OCRepPayloadSetPropInt(link, OC_RSRVD_INS, 0);
122 size_t mtDim[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
123 char **mt = (char **)OICMalloc(sizeof(char *) * 1);
124 mt[0] = OICStrdup(DEFAULT_MESSAGE_TYPE);
125 OCRepPayloadSetStringArray(link, OC_RSRVD_MEDIA_TYPE, (const char **)mt,
127 OCRepPayload *policy = OCRepPayloadCreate();
128 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, OC_DISCOVERABLE);
129 OCRepPayloadSetPropObjectAsOwner(link, OC_RSRVD_POLICY, policy);
132 OCRepPayload *link1 = OCRepPayloadCreate();
133 OCRepPayloadSetPropString(link1, OC_RSRVD_HREF, resourceURI_light.c_str());
134 char **rt1 = (char **)OICMalloc(sizeof(char *) * 1);
135 rt1[0] = OICStrdup(resourceTypeName_light.c_str());
136 OCRepPayloadSetStringArray(link1, OC_RSRVD_RESOURCE_TYPE, (const char **)rt1,
138 OCRepPayloadSetStringArray(link1, OC_RSRVD_INTERFACE, (const char **)itf,
140 OCRepPayloadSetPropInt(link1, OC_RSRVD_INS, 1);
141 OCRepPayloadSetStringArray(link1, OC_RSRVD_MEDIA_TYPE, (const char **)mt,
143 OCRepPayloadSetPropObjectAsOwner(link1, OC_RSRVD_POLICY, policy);
145 OCRepPayloadSetPropObjectArray(repPayload, OC_RSRVD_LINKS, linkArr, dimensions);
147 OIC_LOG_PAYLOAD(DEBUG, (OCPayload *)repPayload);
151 TEST_F(RDDatabaseTests, Create)
153 itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
156 TEST_F(RDDatabaseTests, StoreResources)
158 itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
159 const char *deviceId = "7a960f46-a52e-4837-bd83-460b1a6dd56b";
160 OCRepPayload *repPayload = CreateResources(deviceId);
162 address.port = 54321;
163 OICStrcpy(address.addr,MAX_ADDR_STR_SIZE, "192.168.1.1");
165 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseStoreResources(repPayload, &address));
167 OCDiscoveryPayload *discPayload = NULL;
168 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseDiscoveryPayloadCreate(NULL, "core.light", &discPayload));
169 OCDiscoveryPayloadDestroy(discPayload);
171 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseDiscoveryPayloadCreate(OC_RSRVD_INTERFACE_DEFAULT, NULL, &discPayload));
172 OCDiscoveryPayloadDestroy(discPayload);
174 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseDiscoveryPayloadCreate(NULL, "core.light", &discPayload));
175 OCDiscoveryPayloadDestroy(discPayload);
177 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseDiscoveryPayloadCreate(OC_RSRVD_INTERFACE_DEFAULT, "core.light", &discPayload));
178 OCDiscoveryPayloadDestroy(discPayload);
181 OCPayloadDestroy((OCPayload *)repPayload);
184 TEST_F(RDDatabaseTests, DeleteResourcesDevice)
186 itst::DeadmanTimer killSwitch(SHORT_TEST_TIMEOUT);
187 const char *deviceIds[2] =
189 "7a960f46-a52e-4837-bd83-460b1a6dd56b",
190 "983656a7-c7e5-49c2-a201-edbeb7606fb5",
192 OCRepPayload *payloads[2];
193 payloads[0] = CreateResources(deviceIds[0]);
194 payloads[1] = CreateResources(deviceIds[1]);
196 address.port = 54321;
197 OICStrcpy(address.addr,MAX_ADDR_STR_SIZE, "192.168.1.1");
198 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseStoreResources(payloads[0], &address));
199 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseStoreResources(payloads[1], &address));
201 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseDeleteResources(deviceIds[0], NULL, 0));
203 OCDiscoveryPayload *discPayload = NULL;
204 EXPECT_EQ(OC_STACK_OK, OCRDDatabaseDiscoveryPayloadCreate(OC_RSRVD_INTERFACE_LL, NULL, &discPayload));
207 for (OCDiscoveryPayload *payload = discPayload; payload; payload = payload->next)
209 if (!strcmp((const char *) deviceIds[0], payload->sid))
213 if (!strcmp((const char *) deviceIds[1], payload->sid))
218 EXPECT_FALSE(found0);
220 OCDiscoveryPayloadDestroy(discPayload);
223 OCPayloadDestroy((OCPayload *)payloads[0]);
224 OCPayloadDestroy((OCPayload *)payloads[1]);