diff options
author | Kush <kush.agrawal@samsung.com> | 2020-05-01 03:20:21 +0530 |
---|---|---|
committer | Sudipto <sudipto.bal@samsung.com> | 2020-06-16 16:14:38 +0530 |
commit | 58601b5f496839508a612132f9dc58d8dc61582c (patch) | |
tree | 7a13bccf3741d5365f85ee71eded75ba448024c9 /resource | |
parent | 63fa4bc91119732c5a44b320adfe6cb2336bcf73 (diff) | |
download | iotivity-58601b5f496839508a612132f9dc58d8dc61582c.tar.gz iotivity-58601b5f496839508a612132f9dc58d8dc61582c.tar.bz2 iotivity-58601b5f496839508a612132f9dc58d8dc61582c.zip |
[Unit Test]Refactor KeepAlive test cases
https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/694
(cherry-picked from 4a805f46d2652d9f9f50610fb4c240576f859947)
Change-Id: I8ac9754ad4033a50a35b44823314087e7fedd790
Signed-off-by: Kush <kush.agrawal@samsung.com>
Signed-off-by: Sudipto <sudipto.bal@samsung.com>
Diffstat (limited to 'resource')
-rw-r--r-- | resource/csdk/stack/test/oickeepalive_test.cpp | 302 |
1 files changed, 95 insertions, 207 deletions
diff --git a/resource/csdk/stack/test/oickeepalive_test.cpp b/resource/csdk/stack/test/oickeepalive_test.cpp index 9b340ae4e..46cf8052f 100644 --- a/resource/csdk/stack/test/oickeepalive_test.cpp +++ b/resource/csdk/stack/test/oickeepalive_test.cpp @@ -31,232 +31,120 @@ #include "ocpayload.h" #include "ocresourcehandler.h" #include "gtest_helper.h" +#include "cautilinterface.h" +#include "cacommon.h" +#include "cainterface.h" +#include "octhread.h" -OCStackApplicationResult callbackFunc(void* ctx, - OCDoHandle handle, OCClientResponse * clientResponse) -{ - -} -TEST(KeepAliveInitialiseTest, OCInitializeKeepAlive) +#define KEEPALIVE_IN "in" + +class Callback { - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); -} -TEST(KeepAliveTerminateTest, OCTerminateKeepAlive) + public: + Callback(OCClientResponseHandler cb) : m_cb(cb), m_called(false) + { + m_cbData.cb = &Callback::handler; + m_cbData.cd = NULL; + m_cbData.context = this; + } + void Wait(long waitTime) + { + uint64_t startTime = OICGetCurrentTime(TIME_IN_MS); + while (!m_called) + { + uint64_t currTime = OICGetCurrentTime(TIME_IN_MS); + long elapsed = (long)((currTime - startTime) / MS_PER_SEC); + if (elapsed > waitTime) + { + m_called = true; + } + OCProcess(); + } + } + operator OCCallbackData *() + { + return &m_cbData; + } + private: + OCCallbackData m_cbData; + OCClientResponseHandler m_cb; + bool m_called; + static OCStackApplicationResult handler(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse) + { + Callback *callback = (Callback *) ctx; + OCStackApplicationResult result = callback->m_cb(NULL, handle, clientResponse); + callback->m_called = true; + return result; + } +}; + +OCStackApplicationResult findKeepAliveResourceCB(void* ctx, + OCDoHandle handle, OCClientResponse * clientResponse) { - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); - EXPECT_EQ(OC_STACK_OK, OCTerminateKeepAlive(OC_SERVER)); + return OC_STACK_DELETE_TRANSACTION; } -TEST(FindKeepAliveResourceTest, OCFindKeepAliveResource) + +TEST(KeepAliveTest, FindKeepAliveResource_RemoteAddrNULL) { - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); OCDoHandle handle; - char *remoteAddr = "coaps+tcp://[fe80::224:54ff:fe98:80d2%25enp6s0]:59384"; - OCCallbackData cbData; - cbData.cb = callbackFunc; - EXPECT_EQ(OC_STACK_OK, OCFindKeepAliveResource(&handle, remoteAddr, &cbData)); -} -TEST(OCHandleKeepAliveResponseTest, OCHandleKeepAliveResponse) -{ - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); - CAEndpoint_t* tempRep = NULL; - CACreateEndpoint(CA_IPV4, CA_ADAPTER_TCP, "127.0.0.1", 5683, &tempRep); - OCPayload payload; - payload.type = PAYLOAD_TYPE_REPRESENTATION; - EXPECT_EQ(OC_STACK_OK, OCHandleKeepAliveResponse(tempRep, &payload)); -} -TEST(OCProcessKeepAliveTest, OCProcessKeepAlive) -{ - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); - char query[] = "?rt=core.light"; - char address[] = "127.0.0.1"; - OCServerRequest *request; - OCResource resource; - const char devicename1[] = "device name"; - OCRepPayload *device = OCRepPayloadCreate(); - OCRepPayloadAddResourceType(device, "oic.d.tv"); - OCRepPayloadSetPropString(device, OC_RSRVD_SPEC_VERSION, OC_SPEC_VERSION); - OCRepPayloadSetPropInt(device, "in", 0); - OCRepPayloadSetPropString(device, "x.org.iotivity.newproperty", "value"); - size_t dim[MAX_REP_ARRAY_DEPTH] = {1, 0, 0}; - char **dt = (char **)OICMalloc(sizeof(char *) * 1); - dt[0] = OICStrdup(OC_DATA_MODEL_VERSION); - OCRepPayloadSetStringArray(device, OC_RSRVD_DATA_MODEL_VERSION, (const char **)dt, dim); - OICFree(dt[0]); - OICFree(dt); - uint8_t* cborData; - size_t cborSize; - OCPayload* parsedDevice; - OCConvertPayload((OCPayload*)device, &cborData, &cborSize); - request = (OCServerRequest *) OICCalloc(1, sizeof(OCServerRequest) + - (cborSize ? cborSize : 1) - 1); - request->method = OC_REST_PUT; - strncpy(request->query, query, sizeof(query)); - request->devAddr.flags = OC_DEFAULT_FLAGS; - request->devAddr.adapter = OC_DEFAULT_ADAPTER; - strncpy(request->devAddr.addr, address, sizeof(query)); - request->devAddr.port = 5364; - request->qos = OC_LOW_QOS; - request->coapID = 0; - request->delayedResNeeded = 0; - request->requestId = 0x1234; - request->payloadSize = cborSize; - memcpy(request->payload, cborData, cborSize); - OCHandleKeepAliveRequest(request, &resource); - EXPECT_EQ(OC_STACK_OK, OCProcess()); -} -TEST(HandleKeepAliveGetRequestErrorWithObservation, OCHandleKeepAliveRequest) -{ - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); - EXPECT_EQ(OC_STACK_OK, OCInitializeKeepAlive(OC_SERVER)); - char query[] = "?rt=core.light"; - char address[] = "127.0.0.1"; - char uri[] = "/oic/ping"; - OCServerRequest request; - request.method = OC_REST_GET; - strncpy(request.query, query, sizeof(query)+1); - strncpy(request.resourceUrl, uri, 63); - request.devAddr.flags = OC_IP_USE_V4; - request.devAddr.adapter = OC_ADAPTER_TCP; - strncpy(request.devAddr.addr, address, sizeof(query)+1); - request.devAddr.port = 5364; - request.qos = OC_LOW_QOS; - request.coapID = 0; - request.delayedResNeeded = 0; - request.requestId = 0x1234; - request.observationOption = 1; - OCResource resource; - OCServerRequest *ActualRequest; - EXPECT_EQ(OC_STACK_ERROR,OCHandleKeepAliveRequest(&request, &resource)); -} -TEST(HandleKeepAliveGetRequestWithoutObservation, OCHandleKeepAliveRequest) -{ - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); - char query[] = "?rt=core.light"; - char address[] = "127.0.0.1"; - OCServerRequest request; - request.method = OC_REST_GET; - strncpy(request.query, query, sizeof(query)); - request.devAddr.flags = OC_DEFAULT_FLAGS; - request.devAddr.adapter = OC_DEFAULT_ADAPTER; - strncpy(request.devAddr.addr, address, sizeof(query)); - request.devAddr.port = 5364; - request.qos = OC_LOW_QOS; - request.coapID = 0; - request.delayedResNeeded = 0; - request.observationOption = 6; - request.requestId = 0x1234; - OCResource resource; - EXPECT_EQ(OC_STACK_ERROR, OCHandleKeepAliveRequest(&request, &resource)); + Callback keepAliveCB(&findKeepAliveResourceCB); + EXPECT_EQ(OC_STACK_INVALID_CALLBACK, OCFindKeepAliveResource(&handle, NULL, keepAliveCB)); } -TEST(HandleKeepAlivePutRequestError, OCHandleKeepAliveRequest) -{ - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); - char query[] = "?rt=core.light"; - char address[] = "127.0.0.1"; - OCServerRequest *request; - OCResource resource; - const char devicename1[] = "device name"; - OCRepPayload *device = OCRepPayloadCreate(); - OCRepPayloadAddResourceType(device, "oic.d.tv"); - OCRepPayloadSetPropString(device, OC_RSRVD_SPEC_VERSION, OC_SPEC_VERSION); - OCRepPayloadSetPropInt(device, "in", 0); - OCRepPayloadSetPropString(device, "x.org.iotivity.newproperty", "value"); - size_t dim[MAX_REP_ARRAY_DEPTH] = {1, 0, 0}; - char **dt = (char **)OICMalloc(sizeof(char *) * 1); - dt[0] = OICStrdup(OC_DATA_MODEL_VERSION); - OCRepPayloadSetStringArray(device, OC_RSRVD_DATA_MODEL_VERSION, (const char **)dt, dim); - OICFree(dt[0]); - OICFree(dt); - uint8_t* cborData; - size_t cborSize; - OCPayload* parsedDevice; - OCConvertPayload((OCPayload*)device, &cborData, &cborSize); - request = (OCServerRequest *) OICCalloc(1, sizeof(OCServerRequest) + - (cborSize ? cborSize : 1) - 1); - request->method = OC_REST_PUT; - strncpy(request->query, query, sizeof(query)); - request->devAddr.flags = OC_DEFAULT_FLAGS; - request->devAddr.adapter = OC_DEFAULT_ADAPTER; - strncpy(request->devAddr.addr, address, sizeof(query)); - request->devAddr.port = 5364; - request->qos = OC_LOW_QOS; - request->coapID = 0; - request->delayedResNeeded = 0; - request->requestId = 0x1234; - request->payloadSize = cborSize; - memcpy(request->payload, cborData, cborSize); - EXPECT_EQ(OC_STACK_ERROR, OCHandleKeepAliveRequest(request, &resource)); -} -TEST(HandleKeepAliveInvalidRequest, OCHandleKeepAliveRequest) +TEST(KeepAliveTest, FindKeepAliveResource_CallbackDataNULL) { - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); - char query[] = "?rt=core.light"; - char address[] = "127.0.0.1"; - OCServerRequest request; - request.method = OC_REST_DELETE; - strncpy(request.query, query, sizeof(query)); - request.devAddr.flags = OC_DEFAULT_FLAGS; - request.devAddr.adapter = OC_DEFAULT_ADAPTER; - strncpy(request.devAddr.addr, address, sizeof(query)); - request.devAddr.port = 5364; - request.qos = OC_LOW_QOS; - request.coapID = 0; - request.delayedResNeeded = 0; - request.requestId = 0x1234; - OCResource resource; - EXPECT_EQ(OC_STACK_ERROR, OCHandleKeepAliveRequest(&request,&resource)); + OCDoHandle handle; + char *remoteAddr = "coaps+tcp://[fe80::224:54ff:fe98:80d2%25enp6s0]:59384"; + EXPECT_EQ(OC_STACK_INVALID_CALLBACK, OCFindKeepAliveResource(&handle, remoteAddr, NULL)); } -TEST(SendKeepAliveRequestTest, OCSendKeepAliveRequest) +TEST(KeepAliveTest, FindKeepAliveResource_CallbackNULL) { - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); OCDoHandle handle; char *remoteAddr = "coaps+tcp://[fe80::224:54ff:fe98:80d2%25enp6s0]:59384"; OCCallbackData cbData; - cbData.cd = NULL; - OCRepPayload p; - p.base.type = PAYLOAD_TYPE_REPRESENTATION; - p.uri = NULL; - p.types = NULL; - p.interfaces = NULL; - p.values = NULL; - p.next = NULL; - OCPayload *payload = (OCPayload*)&p; - EXPECT_EQ(OC_STACK_OK, OCSendKeepAliveRequest(&handle, remoteAddr, payload, &cbData)); + cbData.cb = NULL; + EXPECT_EQ(OC_STACK_INVALID_CALLBACK, OCFindKeepAliveResource(&handle, remoteAddr, &cbData)); } -TEST(SendKeepAliveRequestTestWithInvalidParam, OCSendKeepAliveRequest) + +TEST(KeepAliveTest, FindKeepAliveResource_Success) { - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); + EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_CLIENT_SERVER)); + OCDoHandle handle; - char *remoteAddr = ""; - OCCallbackData cbData; - cbData.cd = NULL; - OCRepPayload p; - p.base.type = PAYLOAD_TYPE_REPRESENTATION; - p.uri = NULL; - p.types = NULL; - p.interfaces = NULL; - p.values = NULL; - p.next = NULL; - OCPayload *payload = (OCPayload*)&p; - EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSendKeepAliveRequest(&handle, remoteAddr, payload, &cbData)); + char remoteAddr[100]; + CAEndpoint_t *Epinfo = NULL; + uint32_t size; + int port = CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV4); + CAGetNetworkInformation(&Epinfo, &size); + + sprintf(remoteAddr, "coap+tcp://%s:%d", Epinfo[0].addr ,port); + + Callback keepAliveCB(&findKeepAliveResourceCB); + EXPECT_EQ(OC_STACK_OK, OCFindKeepAliveResource(&handle, remoteAddr, keepAliveCB)); + + keepAliveCB.Wait(100); + EXPECT_EQ(OC_STACK_OK, OCStop()); } -TEST(SendKeepAliveRequestTestWithoutCallback, OCSendKeepAliveRequest) + +TEST(SendKeepAliveRequestTest, OCSendKeepAliveRequest_Success) { - EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_SERVER)); + EXPECT_EQ(OC_STACK_OK, OCInit(NULL, 0, OC_CLIENT_SERVER)); OCDoHandle handle; - char *remoteAddr = "coaps://[fe80::224:54ff:fe98:80d2%25enp6s0]:59384"; - OCCallbackData cbData; - cbData.cd = NULL; - OCRepPayload p; - p.base.type=PAYLOAD_TYPE_REPRESENTATION; - p.uri = NULL; - p.types = NULL; - p.interfaces = NULL; - p.values = NULL; - p.next = NULL; - OCPayload *payload = (OCPayload*)&p; - EXPECT_EQ(OC_STACK_ERROR, OCSendKeepAliveRequest(&handle, remoteAddr, payload, &cbData)); -} + char remoteAddr[100]; + CAEndpoint_t *Epinfo = NULL; + uint32_t size; + int port = CAGetAssignedPortNumber(CA_ADAPTER_TCP, CA_IPV4); + CAGetNetworkInformation(&Epinfo, &size); + sprintf(remoteAddr, "coap+tcp://%s:%d", Epinfo[0].addr ,port); + + Callback keepAliveCB(&findKeepAliveResourceCB); + + OCRepPayload *repPayload = OCRepPayloadCreate(); + OCRepPayloadSetPropInt(repPayload, KEEPALIVE_IN, 1); + + EXPECT_EQ(OC_STACK_OK, OCSendKeepAliveRequest(&handle, remoteAddr, (OCPayload*)repPayload, keepAliveCB)); + keepAliveCB.Wait(100); + EXPECT_EQ(OC_STACK_OK, OCStop()); +} |