ca ssl unittests 15/29615/10
authorOleksandr Dmytrenko <o.dmytrenko@samsung.com>
Wed, 17 Jul 2019 14:11:08 +0000 (17:11 +0300)
committerAleksey Volkov <a.volkov@samsung.com>
Thu, 25 Jul 2019 07:42:17 +0000 (07:42 +0000)
ca ssl unittests and fix in ca adapter

Change-Id: I76208f7f55d8ef06f66ede67a521c5317657182a
Signed-off-by: Oleksandr Dmytrenko <o.dmytrenko@samsung.com>
resource/csdk/connectivity/src/adapter_util/ca_adapter_net_ssl.c
resource/csdk/connectivity/test/SConscript
resource/csdk/connectivity/test/ca_adapter_net_ssl.cpp [new file with mode: 0644]
resource/csdk/connectivity/test/cert/ca.der [new file with mode: 0644]
resource/csdk/connectivity/test/cert/crl.der [new file with mode: 0644]
resource/csdk/connectivity/test/cert/crt.der [new file with mode: 0644]
resource/csdk/connectivity/test/cert/key.der [new file with mode: 0644]
resource/csdk/connectivity/test/ssladapter_test.cpp
resource/csdk/resource-directory/unittests/rdtests.cpp
resource/csdk/stack/src/ocpayload.c

index 252220f..80f76a9 100644 (file)
  * @def UUID_LENGTHPSK_LENGTH
  * @brief Identity max length
  */
+#if !defined(UUID_LENGTH)
 #define UUID_LENGTH (128/8)
+#endif
 /**
  * @def MASTER_SECRET_LEN
  * @brief TLS master secret length
@@ -267,7 +269,7 @@ static const int tlsCipher[SSL_CIPHER_MAX][2] =
 
 static int g_cipherSuitesList[SSL_CIPHER_MAX];
 
-mbedtls_ecp_group_id curve[ADAPTER_CURVE_MAX][2] =
+static mbedtls_ecp_group_id curve[ADAPTER_CURVE_MAX][2] =
 {
     {MBEDTLS_ECP_DP_SECP256R1, MBEDTLS_ECP_DP_NONE}
 };
@@ -982,7 +984,7 @@ bool SetCASecureEndpointAttribute(const CAEndpoint_t* peer, uint32_t newAttribut
     bool result = false;
 
     OIC_LOG_V(DEBUG, NET_SSL_TAG, "In %s(peer = %s:%u, attribute = %#x)", __func__,
-        peer->addr, (uint32_t)peer->port, newAttribute);
+        peer? peer->addr : "null", peer ? (uint32_t)peer->port : 0, newAttribute);
 
     // In the current implementation, the caller already owns g_sslContextMutex.
     oc_mutex_assert_owner(g_sslContextMutex, true);
index 6bea1c7..cd13596 100644 (file)
@@ -109,11 +109,12 @@ tests_src = [
 if 'IP' in target_transport or 'ALL' in target_transport:
     tests_src.append('cablocktransfertest.cpp')
 
-if catest_env.get('SECURED') == '1' and catest_env.get('WITH_TCP') == True:
-    tests_src.append('ssladapter_test.cpp')
-
 if catest_env.get('SECURED') == '1':
-    tests_src.append('cacertprofiletest.cpp')
+    tests_src += ['cacertprofiletest.cpp']
+    if target_os in ('linux'):
+        tests_src += ['ca_adapter_net_ssl.cpp']
+    if catest_env.get('WITH_TCP') == True:
+        tests_src += ['ssladapter_test.cpp']
 
 catests = [catest_env.Program('catests', tests_src)]
 
diff --git a/resource/csdk/connectivity/test/ca_adapter_net_ssl.cpp b/resource/csdk/connectivity/test/ca_adapter_net_ssl.cpp
new file mode 100644 (file)
index 0000000..b847f55
--- /dev/null
@@ -0,0 +1,528 @@
+/* *****************************************************************
+ *
+ * Copyright 2019 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * *****************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <gtest/gtest.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "srmutility.h"
+#include "../src/adapter_util/ca_adapter_net_ssl.c"
+#include "mbedtls/pem.h"
+#include "mbedtls_messages.h"
+
+#undef TAG
+#ifdef __cplusplus
+}
+#endif
+
+#undef TAG
+#define TAG "OCAA_UNITTEST"
+
+using namespace std;
+
+#define TESTDIR "resource/csdk/connectivity/test"
+
+static ByteArray_t *ca  = NULL;
+static ByteArray_t *crl = NULL;
+static ByteArray_t *key = NULL;
+static ByteArray_t *cert= NULL;
+
+#if defined(__PEM2DER__)
+#define PEM_BEGIN_CRT           "-----BEGIN CERTIFICATE-----"
+#define PEM_END_CRT             "-----END CERTIFICATE-----"
+static void pem2der(const unsigned char *crt, const char *begin, const char *end,
+                    unsigned char **buf, size_t *len)
+{
+    size_t use_len;
+    mbedtls_pem_context pem;
+    mbedtls_pem_init(&pem);
+    LOG_MBEDTLS_ERROR(mbedtls_pem_read_buffer(&pem, begin, end, crt, NULL, 0, &use_len));
+
+    *buf = (unsigned char *)OICCalloc(1, pem.buflen);
+    *len = pem.buflen;
+    memcpy(*buf, pem.buf, pem.buflen);
+
+    mbedtls_pem_free( &pem );
+}
+
+static void getCertDer(const unsigned char *crt, unsigned char **buf, size_t *len)
+{
+    pem2der(crt, PEM_BEGIN_CRT, PEM_END_CRT, buf, len);
+}
+
+static void getKeyDer(const unsigned char *crt, unsigned char **buf, size_t *len)
+{
+    pem2der(crt, "-----BEGIN RSA PRIVATE KEY-----", "-----END RSA PRIVATE KEY-----", buf, len);
+}
+#endif
+
+static ByteArray_t *readFile(const char *fname)
+{
+    ByteArray_t *ret = NULL;
+    size_t rb = 0;
+    int fd = open(fname, O_RDONLY);
+    if (0 > fd)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: open file %s", __func__, fname);
+        return NULL;
+    }
+
+    struct stat st;
+    memset(&st, 0, sizeof(struct stat));
+    if (0 != fstat(fd, &st))
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: fstat: (%d) %s", __func__, errno, strerror(errno));
+        goto exit;
+    }
+
+    ret = (ByteArray_t *)OICCalloc(1, sizeof(ByteArray_t));
+    if (!ret)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: alloc", __func__);
+        goto exit;
+    }
+    ret->data = (unsigned char *)OICCalloc(st.st_size, sizeof(unsigned char));
+    if (!ret->data)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: alloc", __func__);
+        goto exit;
+    }
+    ret->len = st.st_size;
+    rb = read(fd, ret->data,
+#if !defined(__unix__)
+              (unsigned)
+#endif
+              ret->len);
+    if ((size_t)ret->len != rb)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: read %s (%lu)", __func__, fname, rb);
+    }
+exit:
+    close(fd);
+    return ret;
+}
+
+static oc_thread pInternalThread;
+static bool pInternalWorking;
+
+static void *__process__(void *p)
+{
+    OIC_LOG_V(DEBUG, TAG, "%s: IN", __func__);
+    OC_UNUSED(p);
+    struct timespec timeout = {0, 100000000L};
+    pInternalWorking = true;
+    while (pInternalWorking)
+    {
+        if (OCProcess() != OC_STACK_OK)
+        {
+            OIC_LOG(FATAL, TAG, "OCStack process error");
+            return NULL;
+        }
+        nanosleep(&timeout, NULL);
+    }
+    OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+    return NULL;
+}
+
+static void PkixInfoHandler(PkiInfo_t *inf)
+{
+    OIC_LOG_V(DEBUG, TAG, "%s: IN", __func__);
+
+    inf->crt.next = NULL;
+    inf->crt.cert = (ByteArray_t *)OICCalloc(1, sizeof(ByteArray_t));
+    inf->crt.cert->data = (unsigned char *)OICCalloc(cert->len, sizeof(unsigned char));
+    inf->crt.cert->len = cert->len;
+    memcpy(inf->crt.cert->data, cert->data, cert->len);
+
+    inf->ca.next = NULL;
+    inf->ca.cert = (ByteArray_t *)OICCalloc(1, sizeof(ByteArray_t));
+    inf->ca.cert->data = (unsigned char *)OICCalloc(ca->len, sizeof(unsigned char));
+    inf->ca.cert->len = ca->len;
+    memcpy(inf->ca.cert->data, ca->data, ca->len);
+
+    inf->crl.len = crl->len;
+    inf->crl.data = (unsigned char *)OICCalloc(crl->len, sizeof(unsigned char));
+    memcpy(inf->crl.data, crl->data, crl->len);
+
+    inf->key.len = key->len;
+    inf->key.data = (unsigned char *)OICCalloc(key->len, sizeof(unsigned char));
+    memcpy(inf->key.data, key->data, key->len);
+
+    OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+}
+
+class OCAA : public ::testing::Test
+{
+    public:
+        static void SetUpTestCase()
+        {
+            OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+            ca = readFile(TESTDIR"/cert/ca.der");
+            crl = readFile(TESTDIR"/cert/crl.der");
+            key = readFile(TESTDIR"/cert/key.der");
+            cert = readFile(TESTDIR"/cert/crt.der");
+
+            CAsetPkixInfoCallback(PkixInfoHandler);
+
+            oc_thread_new(&pInternalThread, __process__, NULL);
+
+            EXPECT_EQ(CA_STATUS_OK, CAinitSslAdapter());
+            oc_mutex_lock(g_sslContextMutex);
+
+            OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+        }
+
+        static void TearDownTestCase()
+        {
+            OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+            pInternalWorking = false;
+            oc_mutex_unlock(g_sslContextMutex);
+            CAdeinitSslAdapter();
+            OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+        }
+};
+
+TEST_F(OCAA, CAcloseSslConnection)
+{
+    CAEndpoint_t *ep = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
+    sprintf(ep->addr, "127.0.0.1");
+    ep->port = 3335;
+    ep->adapter = CA_ADAPTER_IP;
+
+    SslContext_t *backup = g_caSslContext;
+    g_caSslContext = NULL;
+    CAcloseSslConnection(ep);
+    g_caSslContext = backup;
+    CAcloseSslConnection(ep);
+}
+
+TEST_F(OCAA, CAcloseSslConnectionAll)
+{
+    CAEndpoint_t *ep = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
+    sprintf(ep->addr, "127.0.0.1");
+    ep->port = 3335;
+    ep->adapter = CA_ADAPTER_IP;
+    SslEndPoint_t *sep = NULL;
+    sep = NewSslEndPoint(ep, &(ep->adapter == CA_ADAPTER_IP ?
+                               g_caSslContext->clientDtlsConf : g_caSslContext->clientTlsConf));
+    if (sep)
+    {
+        oc_mutex_lock(g_sslContextMutex);
+        u_arraylist_add(g_caSslContext->peerList, (void *) sep);
+        oc_mutex_unlock(g_sslContextMutex);
+    }
+
+    SslContext_t *backup = g_caSslContext;
+    g_caSslContext = NULL;
+    CAcloseSslConnectionAll(CA_ADAPTER_IP);
+    g_caSslContext = backup;
+    CAcloseSslConnectionAll(CA_ADAPTER_IP);
+}
+
+TEST_F(OCAA, GetAlertCode)
+{
+    EXPECT_EQ(0x2D, GetAlertCode(MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED));
+    EXPECT_EQ(0x2D, GetAlertCode(MBEDTLS_X509_BADCERT_EXPIRED));
+}
+
+static CAResult_t peerCNVerifyCallbackOk(const unsigned char *cn, size_t cnLen)
+{
+    OC_UNUSED(cn);
+    OC_UNUSED(cnLen);
+    return CA_STATUS_OK;
+}
+
+static CAResult_t peerCNVerifyCallbackFailed(const unsigned char *cn, size_t cnLen)
+{
+    OC_UNUSED(cn);
+    OC_UNUSED(cnLen);
+    return CA_STATUS_FAILED;
+}
+
+TEST_F(OCAA, PeerCertExtractCN)
+{
+    mbedtls_x509_crt *crt = (mbedtls_x509_crt *)OICCalloc(1, sizeof(mbedtls_x509_crt));
+    mbedtls_x509_crt_init(crt);
+
+    ASSERT_TRUE(NULL != cert);
+
+    unsigned ret = mbedtls_x509_crt_parse(crt, cert->data, cert->len);
+    LOG_MBEDTLS_ERROR(ret);
+
+    CAsetPeerCNVerifyCallback(peerCNVerifyCallbackOk);
+    EXPECT_EQ(CA_STATUS_OK, PeerCertExtractCN(crt));
+
+    CAsetPeerCNVerifyCallback(peerCNVerifyCallbackFailed);
+    EXPECT_EQ(CA_STATUS_FAILED, PeerCertExtractCN(crt));
+
+    CAsetPeerCNVerifyCallback(NULL);
+    EXPECT_EQ(CA_STATUS_OK, PeerCertExtractCN(crt));
+
+    mbedtls_x509_crt_free(crt);
+
+    OICFree(crt);
+}
+
+TEST_F(OCAA, GetAdapterIndex)
+{
+    EXPECT_EQ(0, GetAdapterIndex(CA_ADAPTER_IP));
+    EXPECT_EQ(1, GetAdapterIndex(CA_ADAPTER_TCP));
+    EXPECT_EQ(2, GetAdapterIndex(CA_ADAPTER_GATT_BTLE));
+    EXPECT_EQ(-1, GetAdapterIndex(CA_ALL_ADAPTERS));
+}
+
+TEST_F(OCAA, DeInitPkixInfo)
+{
+    PkiInfo_t *inf = NULL;
+    DeInitPkixInfo(inf);
+}
+
+TEST_F(OCAA, SetCASecureEndpointAttribute)
+{
+    CAEndpoint_t *peer = NULL;
+    SslContext_t *backup = g_caSslContext;
+    g_caSslContext = NULL;
+    EXPECT_FALSE(SetCASecureEndpointAttribute(peer, 0));
+    g_caSslContext = backup;
+    EXPECT_FALSE(SetCASecureEndpointAttribute(peer, 0));
+}
+
+void _CAgetIdentityHandler(UuidContext_t *list, unsigned char *p, size_t len)
+{
+    OC_UNUSED(list);
+    OC_UNUSED(p);
+    OC_UNUSED(len);
+}
+
+TEST_F(OCAA, verifyIdentity)
+{
+    EXPECT_EQ(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE, verifyIdentity(NULL, NULL, 1, NULL));
+    CAsetIdentityCallback(_CAgetIdentityHandler);
+
+    mbedtls_x509_crt *crt = (mbedtls_x509_crt *)OICCalloc(1, sizeof(mbedtls_x509_crt));
+    mbedtls_x509_crt_init(crt);
+
+    EXPECT_EQ(MBEDTLS_ERR_X509_BAD_INPUT_DATA, verifyIdentity(NULL, crt, 1, NULL));
+
+    ASSERT_TRUE(NULL != cert);
+    LOG_MBEDTLS_ERROR(mbedtls_x509_crt_parse(crt, cert->data, cert->len));
+
+    EXPECT_EQ(0, verifyIdentity(NULL, crt, 1, NULL));
+    EXPECT_EQ(-1, verifyIdentity(NULL, crt, 0, NULL));
+
+    mbedtls_x509_crt_free(crt);
+    OICFree(crt);
+}
+
+/*
+TEST_F(OCAA, PKIX)
+{
+    InitPKIX(CA_ADAPTER_IP);
+}
+*/
+TEST_F(OCAA, ConvertMbedtlsCodesToCAResult)
+{
+    EXPECT_EQ(CA_STATUS_OK, ConvertMbedtlsCodesToCAResult(0));
+    EXPECT_EQ(CA_DTLS_AUTHENTICATION_FAILURE, ConvertMbedtlsCodesToCAResult(1));
+}
+
+TEST_F(OCAA, checkSslOperation)
+{
+    SslEndPoint_t *peer = (SslEndPoint_t *)OICCalloc(1, sizeof(SslEndPoint_t));
+    mbedtls_ssl_init( &peer->ssl );
+    peer->ssl.conf = (mbedtls_ssl_config *)OICCalloc(1, sizeof(mbedtls_ssl_config));
+    mbedtls_ssl_config_defaults((mbedtls_ssl_config *)peer->ssl.conf,
+                                MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
+
+    EXPECT_TRUE(checkSslOperation(peer, MBEDTLS_ERR_SSL_WANT_READ, NULL, 0));
+    EXPECT_TRUE(checkSslOperation(peer, MBEDTLS_ERR_SSL_NON_FATAL, NULL, 0));
+    EXPECT_FALSE(checkSslOperation(peer, MBEDTLS_ERR_SSL_CONN_EOF, NULL, 0));
+
+    OICFree((void *)peer->ssl.conf);
+    OICFree(peer);
+}
+
+TEST_F(OCAA, CA_EncDeCryptSsl)
+{
+    CASecureEndpoint_t *sep = NULL;
+    uint8_t *data = NULL;
+    size_t dataLen = 0;
+    CAEndpoint_t *ep = NULL;
+
+    EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAdecryptSsl(sep, data, dataLen));
+    EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAencryptSsl(ep, data, dataLen));
+
+    ep = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
+    sprintf(ep->addr, "127.0.0.1");
+    ep->port = 3333;
+    ep->adapter = CA_ADAPTER_IP;
+
+    data = (uint8_t *)OICCalloc(512, 1);
+
+    EXPECT_EQ(CA_STATUS_FAILED, CAencryptSsl(ep, data, dataLen));
+    dataLen = 512;
+
+    SslContext_t *backup = g_caSslContext;
+    g_caSslContext = NULL;
+    EXPECT_EQ(CA_STATUS_FAILED, CAencryptSsl(ep, data, dataLen));
+    EXPECT_EQ(CA_STATUS_FAILED, CAdecryptSsl(sep, data, dataLen));
+    g_caSslContext = backup;
+
+    EXPECT_EQ(CA_STATUS_INVALID_PARAM, CAdecryptSsl(sep, data, dataLen));
+}
+
+TEST_F(OCAA, GetCASecureEndpointData)
+{
+    CASecureEndpoint_t *sep = NULL;
+    CAEndpoint_t *ep = NULL;
+    SslContext_t *backup = g_caSslContext;
+    g_caSslContext = NULL;
+    EXPECT_EQ(CA_STATUS_NOT_INITIALIZED, GetCASecureEndpointData(ep, sep));
+    g_caSslContext = backup;
+    EXPECT_EQ(CA_STATUS_INVALID_PARAM, GetCASecureEndpointData(ep, sep));
+
+}
+
+static void *process(void *p)
+{
+    OIC_LOG_V(DEBUG, TAG, "%s: IN", __func__);
+    OC_UNUSED(p);
+
+    uint32_t attr;
+    CAEndpoint_t *ep = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
+    sprintf(ep->addr, "127.0.0.1");
+    ep->port = 3333;
+
+    SslContext_t *backup = g_caSslContext;
+    g_caSslContext = NULL;
+    EXPECT_FALSE(GetCASecureEndpointAttributes(ep, &attr));
+    g_caSslContext = backup;
+
+    EXPECT_FALSE(GetCASecureEndpointAttributes(ep, &attr));
+
+    OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+    return NULL;
+}
+
+TEST_F(OCAA, GetCASecureEndpointAttributes)
+{
+    oc_thread p1;
+    oc_mutex_unlock(g_sslContextMutex);
+    oc_thread_new(&p1, process, NULL);
+    oc_thread_wait(p1);
+    oc_mutex_lock(g_sslContextMutex);
+}
+
+TEST_F(OCAA, CAsslGenerateOwnerPsk)
+{
+    CAEndpoint_t *ep = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
+    sprintf(ep->addr, "127.0.0.1");
+    ep->port = 3333;
+    ep->adapter = CA_ADAPTER_IP;
+    SslEndPoint_t *sep = NULL;
+    sep = NewSslEndPoint(ep, &(ep->adapter == CA_ADAPTER_IP ?
+                               g_caSslContext->clientDtlsConf : g_caSslContext->clientTlsConf));
+    if (sep)
+    {
+        oc_mutex_lock(g_sslContextMutex);
+        u_arraylist_add(g_caSslContext->peerList, (void *) sep);
+        oc_mutex_unlock(g_sslContextMutex);
+    }
+
+    size_t labelLen = 32;
+    unsigned char *label = (unsigned char *)OICCalloc(labelLen, 1);
+    size_t random1Len = 64;
+    unsigned char *random1 = (unsigned char *)OICCalloc(random1Len, 1);
+    size_t random2Len = 64;
+    unsigned char *random2 = (unsigned char *)OICCalloc(random2Len, 1);
+    size_t ownerPskSize = 512;
+    uint8_t *ownerPsk = (unsigned char *)OICCalloc(ownerPskSize, 1);
+    SslContext_t *backup = g_caSslContext;
+    g_caSslContext = NULL;
+    EXPECT_EQ(CA_STATUS_FAILED, CAsslGenerateOwnerPsk(ep, label, labelLen, random1, random1Len, random2,
+              random2Len, ownerPsk, ownerPskSize));
+    g_caSslContext = backup;
+    g_caSslContext->selectedCipher = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM;
+    EXPECT_EQ(CA_STATUS_OK, CAsslGenerateOwnerPsk(ep, label, labelLen, random1, random1Len, random2,
+              random2Len, ownerPsk, ownerPskSize));
+    g_caSslContext->selectedCipher = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256;
+    EXPECT_EQ(CA_STATUS_OK, CAsslGenerateOwnerPsk(ep, label, labelLen, random1, random1Len, random2,
+              random2Len, ownerPsk, ownerPskSize));
+
+    g_caSslContext->selectedCipher = MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA256;
+    EXPECT_EQ(CA_STATUS_OK, CAsslGenerateOwnerPsk(ep, label, labelLen, random1, random1Len, random2,
+              random2Len, ownerPsk, ownerPskSize));
+
+    g_caSslContext->selectedCipher = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384;
+    EXPECT_EQ(CA_STATUS_OK, CAsslGenerateOwnerPsk(ep, label, labelLen, random1, random1Len, random2,
+              random2Len, ownerPsk, ownerPskSize));
+    g_caSslContext->selectedCipher = MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384;
+    EXPECT_EQ(CA_STATUS_OK, CAsslGenerateOwnerPsk(ep, label, labelLen, random1, random1Len, random2,
+              random2Len, ownerPsk, ownerPskSize));
+}
+
+TEST_F(OCAA, CAinitiateSslHandshake)
+{
+    CAEndpoint_t *ep = (CAEndpoint_t *)OICCalloc(1, sizeof(CAEndpoint_t));
+    sprintf(ep->addr, "127.0.0.7");
+    ep->port = 8833;
+    ep->adapter = CA_ADAPTER_IP;
+    SslEndPoint_t *sep = NULL;
+    sep = NewSslEndPoint(ep, &(ep->adapter == CA_ADAPTER_IP ?
+                               g_caSslContext->clientDtlsConf : g_caSslContext->clientTlsConf));
+    if (sep)
+    {
+        oc_mutex_lock(g_sslContextMutex);
+        u_arraylist_add(g_caSslContext->peerList, (void *) sep);
+        oc_mutex_unlock(g_sslContextMutex);
+    }
+
+    g_caSslContext->cipherFlag[1] = false;
+    EXPECT_EQ(CA_STATUS_OK, CAinitiateSslHandshake(ep));
+}
+
+TEST_F(OCAA, CAsetTlsCipherSuite)
+{
+    SslContext_t *backup = g_caSslContext;
+    g_caSslContext = NULL;
+    EXPECT_EQ(CA_STATUS_NOT_INITIALIZED, CAsetTlsCipherSuite(SSL_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256));
+    g_caSslContext = backup;
+}
+
+TEST_F(OCAA, GetCipherIndex)
+{
+    for (int i = 0; i < 0xffff; i++)
+    {
+        GetCipherIndex(i);
+    }
+}
+
+TEST_F(OCAA, StartRetransmit)
+{
+    SslContext_t *backup = g_caSslContext;
+    g_caSslContext = NULL;
+    StartRetransmit(NULL);
+    g_caSslContext = backup;
+}
+
+
diff --git a/resource/csdk/connectivity/test/cert/ca.der b/resource/csdk/connectivity/test/cert/ca.der
new file mode 100644 (file)
index 0000000..3cf072f
Binary files /dev/null and b/resource/csdk/connectivity/test/cert/ca.der differ
diff --git a/resource/csdk/connectivity/test/cert/crl.der b/resource/csdk/connectivity/test/cert/crl.der
new file mode 100644 (file)
index 0000000..ed05cee
Binary files /dev/null and b/resource/csdk/connectivity/test/cert/crl.der differ
diff --git a/resource/csdk/connectivity/test/cert/crt.der b/resource/csdk/connectivity/test/cert/crt.der
new file mode 100644 (file)
index 0000000..ed05cee
Binary files /dev/null and b/resource/csdk/connectivity/test/cert/crt.der differ
diff --git a/resource/csdk/connectivity/test/cert/key.der b/resource/csdk/connectivity/test/cert/key.der
new file mode 100644 (file)
index 0000000..20f62ef
Binary files /dev/null and b/resource/csdk/connectivity/test/cert/key.der differ
index f7d0729..3376bf6 100644 (file)
@@ -31,7 +31,7 @@
 #ifdef HAVE_WINSOCK2_H
 #include <winsock2.h>
 #endif
-
+#include <ocstack.h>
 // Test function hooks
 #define CAcloseSslConnection CAcloseSslConnectionTest
 #define CAdecryptSsl CAdecryptSslTest
@@ -90,6 +90,9 @@
 #include "experimental/logger.h"
 
 
+#undef TAG
+#define TAG "SSLADAPTER_UNITTEST"
+
 #define SEED "PREDICTED_SEED"
 #define dummyHandler 0xF123
 
@@ -1034,7 +1037,7 @@ static void PacketReceive(unsigned char *data, int * datalen)
     *datalen = n + 5;
     memcpy(data, buffer, *datalen);
 }
-
+/*
 static void infoCallback_that_loads_x509(PkiInfo_t * inf)
 {
     ByteArray_t * ca = (ByteArray_t *)OICMalloc(sizeof(ByteArray_t));
@@ -1061,7 +1064,7 @@ static void infoCallback_that_loads_x509(PkiInfo_t * inf)
     inf->crl.data = NULL;
     inf->crl.len = 0;
 }
-
+*/
 static bool socketOpen_server()
 {
     unsigned short portno;
@@ -1883,6 +1886,139 @@ exit:
  *
  *
  * *************************/
+static oc_thread pInternalThread;
+static bool pInternalWorking;
+
+static ByteArray_t *ca;
+static ByteArray_t *crl;
+static ByteArray_t *key;
+static ByteArray_t *cert;
+
+#define TESTDIR "resource/csdk/connectivity/test"
+
+static ByteArray_t *readFile(const char *fname)
+{
+    ByteArray_t *ret = NULL;
+    size_t rb = 0;
+    int fd = open(fname, O_RDONLY);
+    if (0 > fd)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: open file %s", __func__, fname);
+        return NULL;
+    }
+
+    struct stat st;
+    memset(&st, 0, sizeof(struct stat));
+    if (0 != fstat(fd, &st))
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: fstat: (%d) %s", __func__, errno, strerror(errno));
+        goto exit;
+    }
+
+    ret = (ByteArray_t *)OICCalloc(1, sizeof(ByteArray_t));
+    if (!ret)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: alloc", __func__);
+        goto exit;
+    }
+    ret->data = (unsigned char *)OICCalloc(st.st_size, sizeof(unsigned char));
+    if (!ret->data)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: alloc", __func__);
+        goto exit;
+    }
+    ret->len = st.st_size;
+    rb = read(fd, ret->data,
+#if !defined(__unix__)
+              (unsigned)
+#endif
+              ret->len);
+    if ((size_t)ret->len != rb)
+    {
+        OIC_LOG_V(ERROR, TAG, "%s: read %s (%lu)", __func__, fname, rb);
+    }
+exit:
+    close(fd);
+    return ret;
+}
+
+
+static void *__process__(void *p)
+{
+    OIC_LOG_V(DEBUG, TAG, "%s: IN", __func__);
+    OC_UNUSED(p);
+    struct timespec timeout = {0, 100000000L};
+    pInternalWorking = true;
+    while (pInternalWorking)
+    {
+        if (OCProcess() != OC_STACK_OK)
+        {
+            OIC_LOG(FATAL, TAG, "OCStack process error");
+            return NULL;
+        }
+        nanosleep(&timeout, NULL);
+    }
+    OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+    return NULL;
+}
+
+static void PkixInfoHandler(PkiInfo_t *inf)
+{
+    OIC_LOG_V(DEBUG, TAG, "%s: IN", __func__);
+
+    inf->crt.next = NULL;
+    inf->crt.cert = (ByteArray_t *)OICCalloc(1, sizeof(ByteArray_t));
+    inf->crt.cert->data = (unsigned char *)OICCalloc(cert->len, sizeof(unsigned char));
+    inf->crt.cert->len = cert->len;
+    memcpy(inf->crt.cert->data, cert->data, cert->len);
+
+    inf->ca.next = NULL;
+    inf->ca.cert = (ByteArray_t *)OICCalloc(1, sizeof(ByteArray_t));
+    inf->ca.cert->data = (unsigned char *)OICCalloc(ca->len, sizeof(unsigned char));
+    inf->ca.cert->len = ca->len;
+    memcpy(inf->ca.cert->data, ca->data, ca->len);
+
+    inf->crl.len = crl->len;
+    inf->crl.data = (unsigned char *)OICCalloc(crl->len, sizeof(unsigned char));
+    memcpy(inf->crl.data, crl->data, crl->len);
+
+    inf->key.len = key->len;
+    inf->key.data = (unsigned char *)OICCalloc(key->len, sizeof(unsigned char));
+    memcpy(inf->key.data, key->data, key->len);
+
+    OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+}
+
+class TLSAdapter: public ::testing::Test
+{
+    public:
+        static void SetUpTestCase()
+        {
+            OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+            ca = readFile(TESTDIR"/cert/ca.der");
+            crl = readFile(TESTDIR"/cert/crl.der");
+            key = readFile(TESTDIR"/cert/key.der");
+            cert = readFile(TESTDIR"/cert/crt.der");
+
+            CAsetPkixInfoCallback(PkixInfoHandler);
+
+            oc_thread_new(&pInternalThread, __process__, NULL);
+
+            EXPECT_EQ(CA_STATUS_OK, CAinitSslAdapter());
+            oc_mutex_lock(g_sslContextMutex);
+
+            OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+        }
+
+        static void TearDownTestCase()
+        {
+            OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+            pInternalWorking = false;
+            oc_mutex_unlock(g_sslContextMutex);
+            CAdeinitSslAdapter();
+            OIC_LOG_V(DEBUG, TAG, "%s: OUT", __func__);
+        }
+};
 
 static int testCAinitSslAdapter()
 {
@@ -1903,7 +2039,7 @@ static int testCAinitSslAdapter()
 }
 
 // CAinitTlsAdapter()
-TEST(TLSAdapter, Test_1)
+TEST_F(TLSAdapter, Test_1)
 {
     int ret = 0xFF;
     ret = testCAinitSslAdapter();
@@ -1965,7 +2101,7 @@ static int testCAsetSslAdapterCallbacks()
 }
 
 // CAsetSslAdapterCallbacks()
-TEST(TLSAdapter, Test_2)
+TEST_F(TLSAdapter, Test_2)
 {
     int ret = 0xFF;
     ret = testCAsetSslAdapterCallbacks();
@@ -2020,7 +2156,12 @@ static void * test0CAinitiateSslHandshake(void * arg)
     serverAddr.ifindex = 0;
 
     g_sslContextMutex = oc_mutex_new_recursive();
+    if (NULL == g_sslContextMutex)
+    {
+        g_sslContextMutex = oc_mutex_new_recursive();
+    }
     oc_mutex_lock(g_sslContextMutex);
+
     g_caSslContext = (SslContext_t *)OICCalloc(1, sizeof(SslContext_t));
     g_caSslContext->peerList = u_arraylist_create();
     mbedtls_entropy_init(&g_caSslContext->entropy);
@@ -2052,7 +2193,7 @@ static void * test0CAinitiateSslHandshake(void * arg)
     g_caSslContext->adapterCallbacks[1].sendCallback = CATCPPacketSendCB_forInitHsTest;
 
     // CAsetPkixInfoCallback
-    g_getPkixInfoCallback = infoCallback_that_loads_x509;
+    g_getPkixInfoCallback = PkixInfoHandler;//rinfoCallback_that_loads_x509;
 
     // CAsetCredentialTypesCallback
     g_getCredentialTypesCallback = clutch;
@@ -2136,9 +2277,9 @@ static int test1CAinitiateSslHandshake()
 //  server() listens only on IPv6 on Windows (because IPV6_V6ONLY defaults
 //  to true) and socketConnect() is hard coded to try only IPv4.
 #ifdef HAVE_WINSOCK2_H
-TEST(TLSAdapter, DISABLED_Test_3_0)
+TEST_F(TLSAdapter, DISABLED_Test_3_0)
 #else
-TEST(TLSAdapter, Test_3_0)
+TEST_F(TLSAdapter, Test_3_0)
 #endif
 {
     socket_error = false;
@@ -2168,7 +2309,7 @@ TEST(TLSAdapter, Test_3_0)
     EXPECT_EQ(0, arg);
 }
 
-TEST(TLSAdapter, Test_3_1)
+TEST_F(TLSAdapter, Test_3_1)
 {
     int ret = 0xFF;
     ret = test1CAinitiateSslHandshake();
@@ -2190,7 +2331,7 @@ static void * testCAencryptSsl(void * arg)
     memset(&serverAddr, 0, sizeof(serverAddr));
     serverAddr.adapter = CA_ADAPTER_TCP;
     serverAddr.flags = CA_SECURE;
-    serverAddr.port = 4433;
+    serverAddr.port = 4438;
     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
     memcpy(serverAddr.addr, addr, sizeof(addr));
     serverAddr.ifindex = 0;
@@ -2199,7 +2340,7 @@ static void * testCAencryptSsl(void * arg)
 
     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CATCPPacketErrorCB, CA_ADAPTER_TCP);
 
-    CAsetPkixInfoCallback(infoCallback_that_loads_x509);
+    CAsetPkixInfoCallback(PkixInfoHandler);
 
     // CAsetCredentialTypesCallback
     g_getCredentialTypesCallback = clutch;
@@ -2370,9 +2511,9 @@ static void * testCAencryptSsl(void * arg)
 //  server() listens only on IPv6 on Windows (because IPV6_V6ONLY defaults
 //  to true) and socketConnect() is hard coded to try only IPv4.
 #ifdef HAVE_WINSOCK2_H
-TEST(TLSAdapter, DISABLED_Test_4_0)
+TEST_F(TLSAdapter, DISABLED_Test_4_0)
 #else
-TEST(TLSAdapter, Test_4_0)
+TEST_F(TLSAdapter, Test_4_0)
 #endif
 {
     socket_error = false;
@@ -2407,9 +2548,9 @@ TEST(TLSAdapter, Test_4_0)
 //  server() listens only on IPv6 on Windows (because IPV6_V6ONLY defaults
 //  to true) and socketConnect() is hard coded to try only IPv4.
 #ifdef HAVE_WINSOCK2_H
-TEST(TLSAdapter, DISABLED_Test_4_1)
+TEST_F(TLSAdapter, DISABLED_Test_4_1)
 #else
-TEST(TLSAdapter, Test_4_1)
+TEST_F(TLSAdapter, Test_4_1)
 #endif
 {
     socket_error = false;
@@ -2444,9 +2585,9 @@ TEST(TLSAdapter, Test_4_1)
 //  server() listens only on IPv6 on Windows (because IPV6_V6ONLY defaults
 //  to true) and socketConnect() is hard coded to try only IPv4.
 #ifdef HAVE_WINSOCK2_H
-TEST(TLSAdapter, DISABLED_Test_4_2)
+TEST_F(TLSAdapter, DISABLED_Test_4_2)
 #else
-TEST(TLSAdapter, Test_4_2)
+TEST_F(TLSAdapter, Test_4_2)
 #endif
 {
     socket_error = false;
@@ -2481,9 +2622,9 @@ TEST(TLSAdapter, Test_4_2)
 //  server() listens only on IPv6 on Windows (because IPV6_V6ONLY defaults
 //  to true) and socketConnect() is hard coded to try only IPv4.
 #ifdef HAVE_WINSOCK2_H
-TEST(TLSAdapter, DISABLED_Test_4_3)
+TEST_F(TLSAdapter, DISABLED_Test_4_3)
 #else
-TEST(TLSAdapter, Test_4_3)
+TEST_F(TLSAdapter, Test_4_3)
 #endif
 {
     socket_error = false;
@@ -2518,9 +2659,9 @@ TEST(TLSAdapter, Test_4_3)
 //  server() listens only on IPv6 on Windows (because IPV6_V6ONLY defaults
 //  to true) and socketConnect() is hard coded to try only IPv4.
 #ifdef HAVE_WINSOCK2_H
-TEST(TLSAdapter, DISABLED_Test_4_4)
+TEST_F(TLSAdapter, DISABLED_Test_4_4)
 #else
-TEST(TLSAdapter, Test_4_4)
+TEST_F(TLSAdapter, Test_4_4)
 #endif
 {
     socket_error = false;
@@ -2554,9 +2695,9 @@ TEST(TLSAdapter, Test_4_4)
 //  server() listens only on IPv6 on Windows (because IPV6_V6ONLY defaults
 //  to true) and socketConnect() is hard coded to try only IPv4.
 #ifdef HAVE_WINSOCK2_H
-TEST(TLSAdapter, DISABLED_Test_4_5)
+TEST_F(TLSAdapter, DISABLED_Test_4_5)
 #else
-TEST(TLSAdapter, Test_4_5)
+TEST_F(TLSAdapter, Test_4_5)
 #endif
 {
     socket_error = false;
@@ -2613,7 +2754,7 @@ static void * testCAdecryptSsl(void * arg)
 
     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CATCPPacketErrorCB, CA_ADAPTER_TCP);
 
-    CAsetPkixInfoCallback(infoCallback_that_loads_x509);
+    CAsetPkixInfoCallback(PkixInfoHandler);
 
     CAsetCredentialTypesCallback(clutch);
 
@@ -2681,9 +2822,9 @@ static void * testCAdecryptSsl(void * arg)
 //  server() listens only on IPv6 on Windows (because IPV6_V6ONLY defaults
 //  to true) and socketConnect() is hard coded to try only IPv4.
 #ifdef HAVE_WINSOCK2_H
-TEST(TLSAdapter, DISABLED_Test_5)
+TEST_F(TLSAdapter, DISABLED_Test_5)
 #else
-TEST(TLSAdapter, Test_5)
+TEST_F(TLSAdapter, Test_5)
 #endif
 {
     socket_error = false;
@@ -2736,7 +2877,7 @@ static int testCAdeinitSslAdapter()
 
     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CATCPPacketErrorCB, CA_ADAPTER_TCP);
 
-    CAsetPkixInfoCallback(infoCallback_that_loads_x509);
+    CAsetPkixInfoCallback(PkixInfoHandler);
 
     CAsetTlsCipherSuite(SSL_ECDHE_ECDSA_WITH_AES_128_CCM);
 
@@ -2755,7 +2896,7 @@ static int testCAdeinitSslAdapter()
 }
 
 // CAdeinitSslAdapter()
-TEST(TLSAdapter, Test_6)
+TEST_F(TLSAdapter, Test_6)
 {
     int ret = 0xFF;
     ret = testCAdeinitSslAdapter();
@@ -2785,7 +2926,7 @@ static void * testServer(void * arg)
     CAinitSslAdapter();
 
     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB_server, CATCPPacketSendCB_server, CATCPPacketErrorCB_server, CA_ADAPTER_TCP);
-    CAsetPkixInfoCallback(infoCallback_that_loads_x509);
+    CAsetPkixInfoCallback(PkixInfoHandler);
 
     CAsetCredentialTypesCallback(clutch);
 
@@ -2836,9 +2977,9 @@ static void * testServer(void * arg)
 // This test doesn't work on Windows:
 // testServer opens a socket without first calling WSAStartup
 // PacketReceive_server calls read() rather than recv() on a socket handle
-TEST(TLSAdapter, DISABLED_Test_7)
+TEST_F(TLSAdapter, DISABLED_Test_7)
 #else
-TEST(TLSAdapter, Test_7)
+TEST_F(TLSAdapter, Test_7)
 #endif
 {
     socket_error = false;
@@ -2919,21 +3060,21 @@ static int testCAsetCredentialTypesCallback()
 }
 
 // CAsetPskCredentialsCallback()
-TEST(TLSAdapter, Test_9_0)
+TEST_F(TLSAdapter, Test_9_0)
 {
     int ret = 0xFF;
     ret = testCAsetPskCredentialsCallback();
     EXPECT_EQ(0, ret);
 }
 // CAsetPkixInfoCallback()
-TEST(TLSAdapter, Test_9_1)
+TEST_F(TLSAdapter, Test_9_1)
 {
     int ret = 0xFF;
     ret = testCAsetPkixInfoCallback();
     EXPECT_EQ(0, ret);
 }
 // CAsetCredentialTypesCallback()
-TEST(TLSAdapter, Test_9_2)
+TEST_F(TLSAdapter, Test_9_2)
 {
     int ret = 0xFF;
     ret = testCAsetCredentialTypesCallback();
@@ -3034,7 +3175,7 @@ static int testCAsetTlsCipherSuite()
 }
 
 // CAinitTlsAdapter()
-TEST(TLSAdapter, Test_10)
+TEST_F(TLSAdapter, Test_10)
 {
     int ret = 0xff;
     ret = testCAsetTlsCipherSuite();
@@ -3045,18 +3186,18 @@ TEST(TLSAdapter, Test_10)
 //  server() listens only on IPv6 on Windows (because IPV6_V6ONLY defaults
 //  to true) and socketConnect() is hard coded to try only IPv4.
 #ifdef HAVE_WINSOCK2_H
-TEST(TLSAdapter, DISABLED_Test_11)
+TEST_F(TLSAdapter, DISABLED_Test_11)
 #else
-TEST(TLSAdapter, Test_11)
+TEST_F(TLSAdapter, Test_11)
 #endif
 {
     uint8_t predictedPSK[] = {
-        0xba, 0x72, 0x16, 0xbc, 0x7f, 0x8c, 0xfe, 0xfc, 0xd0, 0xac, 0x1a, 0x37, 0xad, 0x60, 0xe8, 0x9e, 
-        0xb3, 0x31, 0xa2, 0x30, 0xaf, 0x68, 0xc9, 0xa6, 0x89, 0x8a, 0x04, 0x21, 0x6c, 0xbd, 0x04, 0x08, 
-        0x68, 0x11, 0x54, 0x9e, 0x2a, 0x10, 0x91, 0x94, 0x3c, 0x44, 0x52, 0xc7, 0xfa, 0x78, 0x44, 0x87, 
-        0xea, 0x30, 0x08, 0x5f, 0xc1, 0x64, 0xaa, 0x0d, 0xfd, 0x84, 0x16, 0x83, 0x20, 0xc9, 0x08, 0x65, 
-        0xd2, 0x4a, 0x55, 0x9e, 0x8f, 0x88, 0x3c, 0x57, 0x10, 0xbd, 0x5a, 0x30, 0x01, 0xb4, 0x59, 0x63, 
-        0x64, 0x19, 0x8d, 0xfa, 0x5c, 0x86, 0x92, 0xf7, 0x60, 0x99, 0xdb, 0xae, 0x0e, 0xad, 0x80, 0xf1, 
+        0xba, 0x72, 0x16, 0xbc, 0x7f, 0x8c, 0xfe, 0xfc, 0xd0, 0xac, 0x1a, 0x37, 0xad, 0x60, 0xe8, 0x9e,
+        0xb3, 0x31, 0xa2, 0x30, 0xaf, 0x68, 0xc9, 0xa6, 0x89, 0x8a, 0x04, 0x21, 0x6c, 0xbd, 0x04, 0x08,
+        0x68, 0x11, 0x54, 0x9e, 0x2a, 0x10, 0x91, 0x94, 0x3c, 0x44, 0x52, 0xc7, 0xfa, 0x78, 0x44, 0x87,
+        0xea, 0x30, 0x08, 0x5f, 0xc1, 0x64, 0xaa, 0x0d, 0xfd, 0x84, 0x16, 0x83, 0x20, 0xc9, 0x08, 0x65,
+        0xd2, 0x4a, 0x55, 0x9e, 0x8f, 0x88, 0x3c, 0x57, 0x10, 0xbd, 0x5a, 0x30, 0x01, 0xb4, 0x59, 0x63,
+        0x64, 0x19, 0x8d, 0xfa, 0x5c, 0x86, 0x92, 0xf7, 0x60, 0x99, 0xdb, 0xae, 0x0e, 0xad, 0x80, 0xf1,
         0x82, 0xaf, 0x1b, 0x14
     };
     size_t predictedPSK_len = sizeof(predictedPSK);
@@ -3115,7 +3256,7 @@ TEST(TLSAdapter, Test_11)
 
     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CATCPPacketErrorCB, CA_ADAPTER_TCP);
 
-    CAsetPkixInfoCallback(infoCallback_that_loads_x509);
+    CAsetPkixInfoCallback(PkixInfoHandler);
 
     CAsetCredentialTypesCallback(clutch);
 
@@ -3152,7 +3293,7 @@ TEST(TLSAdapter, Test_11)
     OICFree(ownerPsk);
 }
 
-TEST(TLSAdapter, Test_ParseChain)
+TEST_F(TLSAdapter, Test_ParseChain)
 {
     mbedtls_x509_crt crt;
     mbedtls_x509_crt_init(&crt);
@@ -3206,17 +3347,17 @@ TEST(TLSAdapter, Test_ParseChain)
     EXPECT_EQ(10, ret + errNum);
 }
 
-TEST(TLSAdapter, TestCertsValid)
+TEST_F(TLSAdapter, TestCertsValid)
 {
-    mbedtls_x509_crt cert;
+    mbedtls_x509_crt crt;
 
-    mbedtls_x509_crt_init(&cert);
-    int ret = mbedtls_x509_crt_parse(&cert, (const unsigned char *)serverCert, serverCertLen);
+    mbedtls_x509_crt_init(&crt);
+    int ret = mbedtls_x509_crt_parse(&crt, (const unsigned char *)serverCert, serverCertLen);
     EXPECT_EQ(0, ret) << "Failed to parse server cert";
-    mbedtls_x509_crt_free(&cert);
+    mbedtls_x509_crt_free(&crt);
 
-    mbedtls_x509_crt_init(&cert);
-    ret = mbedtls_x509_crt_parse(&cert, (const unsigned char *)caCert, caCertLen);
+    mbedtls_x509_crt_init(&crt);
+    ret = mbedtls_x509_crt_parse(&crt, (const unsigned char *)caCert, caCertLen);
     EXPECT_EQ(0, ret) << "Failed to parse CA cert";
-    mbedtls_x509_crt_free(&cert);
+    mbedtls_x509_crt_free(&crt);
 }
index 62d5089..8c255ee 100644 (file)
@@ -63,7 +63,8 @@ namespace itst = iotivity::test;
 //-----------------------------------------------------------------------------
 #define TAG "RDTests"
 
-std::chrono::seconds const SHORT_TEST_TIMEOUT = std::chrono::seconds(5);
+int const _TIMEOUT_ = 7;
+std::chrono::seconds const SHORT_TEST_TIMEOUT = std::chrono::seconds(_TIMEOUT_ + 5);
 
 //-----------------------------------------------------------------------------
 // Callback functions
@@ -322,7 +323,7 @@ TEST_F(RDTests, UpdateSelValue)
     EXPECT_TRUE(OCRepPayloadSetPropInt(payload, "sel", 90));
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_POST, "127.0.0.1/oic/rd?if=oic.if.baseline", NULL,
             (OCPayload*) payload, CT_DEFAULT, OC_HIGH_QOS, postCB, NULL, 0));
-    EXPECT_EQ(OC_STACK_OK, postCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, postCB.Wait(_TIMEOUT_));
 
     EXPECT_EQ(OC_STACK_OK, OCRDStop());
 }
@@ -341,6 +342,7 @@ class RDDiscoverTests : public ::testing::TestWithParam<uint16_t>
             OCSetHeaderOption(options, &numOptions, CA_OPTION_ACCEPT, &format, sizeof(format));
             EXPECT_EQ(OC_STACK_OK, OCInit("127.0.0.1", 5683, OC_CLIENT_SERVER));
             EXPECT_EQ(OC_STACK_OK, OCRDStart());
+            std::set_terminate([](){ OIC_LOG_V(DEBUG, TAG, "%s",__func__); });
         }
 
         virtual void TearDown()
@@ -425,12 +427,12 @@ TEST_P(RDDiscoverTests, DiscoverAllResources)
     itst::Callback publishCB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, handles,
               2, OIC_RD_PUBLISH_TTL, publishCB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&DiscoverAllResourcesVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res", NULL, 0, CT_DEFAULT,
                     OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult ResourceQueryMatchesLocalAndRemoteVerify(void *ctx,
@@ -471,12 +473,12 @@ TEST_P(RDDiscoverTests, ResourceQueryMatchesLocalAndRemote)
     itst::Callback publishCB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, handles,
               1, OIC_RD_PUBLISH_TTL, publishCB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&ResourceQueryMatchesLocalAndRemoteVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=core.light", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult ResourceQueryMatchesLocalOnlyVerify(void *ctx,
@@ -511,14 +513,14 @@ TEST_P(RDDiscoverTests, ResourceQueryMatchesLocalOnly)
     itst::Callback publishCB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, &handles[1],
               1, OIC_RD_PUBLISH_TTL, publishCB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(_TIMEOUT_));
 
     OIC_LOG(INFO, TAG, "Published");
 
     itst::Callback discoverCB(&ResourceQueryMatchesLocalOnlyVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=core.light", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult ResourceQueryMatchesRemoteOnlyVerify(void *ctx,
@@ -587,12 +589,12 @@ TEST_P(RDDiscoverTests, ResourceQueryMatchesRemoteOnly)
     itst::Callback publishCB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_POST, "127.0.0.1/oic/rd", NULL,
                     (OCPayload *)repPayload, CT_DEFAULT, OC_HIGH_QOS, publishCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&ResourceQueryMatchesRemoteOnlyVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=core.light2", NULL, 0,
                     CT_DEFAULT,OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult DatabaseHas0ResourceQueryMatchesVerify(void *ctx,
@@ -632,20 +634,20 @@ TEST_P(RDDiscoverTests, DatabaseHas0ResourceQueryMatches)
     itst::Callback publish0CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, &handles[1],
               1, OIC_RD_PUBLISH_TTL, publish0CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(_TIMEOUT_));
     itst::Callback publish1CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[1], CT_ADAPTER_IP, &handles[2],
               1, OIC_RD_PUBLISH_TTL, publish1CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(_TIMEOUT_));
     itst::Callback publish2CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[2], CT_ADAPTER_IP, &handles[3],
               1, OIC_RD_PUBLISH_TTL, publish2CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&DatabaseHas0ResourceQueryMatchesVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=core.light", NULL, 0,
                     CT_DEFAULT,OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult DatabaseHas1ResourceQueryMatchVerify(void *ctx,
@@ -698,20 +700,20 @@ TEST_P(RDDiscoverTests, DatabaseHas1ResourceQueryMatch)
     itst::Callback publish0CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, &handles[1],
               1, OIC_RD_PUBLISH_TTL, publish0CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(_TIMEOUT_));
     itst::Callback publish1CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[1], CT_ADAPTER_IP, &handles[2],
               1, OIC_RD_PUBLISH_TTL, publish1CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(_TIMEOUT_));
     itst::Callback publish2CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[2], CT_ADAPTER_IP, &handles[3],
               1, OIC_RD_PUBLISH_TTL, publish2CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&DatabaseHas1ResourceQueryMatchVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=core.light2", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult DatabaseHasNResourceQueryMatchesVerify(void *ctx,
@@ -766,20 +768,20 @@ TEST_P(RDDiscoverTests, DatabaseHasNResourceQueryMatches)
     itst::Callback publish0CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, &handles[1],
               1, OIC_RD_PUBLISH_TTL, publish0CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(_TIMEOUT_));
     itst::Callback publish1CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[1], CT_ADAPTER_IP, &handles[2],
               1, OIC_RD_PUBLISH_TTL, publish1CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(_TIMEOUT_));
     itst::Callback publish2CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[2], CT_ADAPTER_IP, &handles[3],
               1, OIC_RD_PUBLISH_TTL, publish2CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&DatabaseHasNResourceQueryMatchesVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=core.light", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult DatabaseHas0InterfaceQueryMatchesVerify(void *ctx,
@@ -819,20 +821,20 @@ TEST_P(RDDiscoverTests, DatabaseHas0InterfaceQueryMatches)
     itst::Callback publish0CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, &handles[1],
               1, OIC_RD_PUBLISH_TTL, publish0CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(_TIMEOUT_));
     itst::Callback publish1CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[1], CT_ADAPTER_IP, &handles[2],
               1, OIC_RD_PUBLISH_TTL, publish1CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(_TIMEOUT_));
     itst::Callback publish2CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[2], CT_ADAPTER_IP, &handles[3],
               1, OIC_RD_PUBLISH_TTL, publish2CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&DatabaseHas0InterfaceQueryMatchesVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?if=oic.if.one", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult DatabaseHas1InterfaceQueryMatchVerify(void *ctx,
@@ -895,20 +897,20 @@ TEST_P(RDDiscoverTests, DatabaseHas1InterfaceQueryMatch)
     itst::Callback publish0CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, &handles[1],
               1, OIC_RD_PUBLISH_TTL, publish0CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(_TIMEOUT_));
     itst::Callback publish1CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[1], CT_ADAPTER_IP, &handles[2],
               1, OIC_RD_PUBLISH_TTL, publish1CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(_TIMEOUT_));
     itst::Callback publish2CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[2], CT_ADAPTER_IP, &handles[3],
               1, OIC_RD_PUBLISH_TTL, publish2CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&DatabaseHas1InterfaceQueryMatchVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?if=oic.if.two", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult DatabaseHasNInterfaceQueryMatchesVerify(void *ctx,
@@ -970,20 +972,20 @@ TEST_P(RDDiscoverTests, DatabaseHasNInterfaceQueryMatches)
     itst::Callback publish0CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, &handles[1],
               1, OIC_RD_PUBLISH_TTL, publish0CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(_TIMEOUT_));
     itst::Callback publish1CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[1], CT_ADAPTER_IP, &handles[2],
               1, OIC_RD_PUBLISH_TTL, publish1CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish1CB.Wait(_TIMEOUT_));
     itst::Callback publish2CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[2], CT_ADAPTER_IP, &handles[3],
               1, OIC_RD_PUBLISH_TTL, publish2CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish2CB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&DatabaseHasNInterfaceQueryMatchesVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?if=oic.if.a", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult ResourceAndInterfaceQueryMatchVerify(void *ctx,
@@ -1048,12 +1050,12 @@ TEST_P(RDDiscoverTests, ResourceAndInterfaceQueryMatch)
     itst::Callback publish0CB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, &handles[1],
               3, OIC_RD_PUBLISH_TTL, publish0CB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publish0CB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&ResourceAndInterfaceQueryMatchVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?rt=core.light2&if=oic.if.two",
                     NULL, 0, CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult BaselineVerify(void *ctx, OCDoHandle handle,
@@ -1080,12 +1082,12 @@ TEST_P(RDDiscoverTests, Baseline)
     itst::Callback publishCB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, handles,
               1, OIC_RD_PUBLISH_TTL, publishCB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&BaselineVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res?if=oic.if.baseline", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult DeleteDeviceVerify(void *ctx, OCDoHandle handle,
@@ -1118,17 +1120,17 @@ TEST_P(RDDiscoverTests, DeleteDevice)
     itst::Callback publishCB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, handles,
               1, OIC_RD_PUBLISH_TTL, publishCB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(_TIMEOUT_));
 
     itst::Callback deleteCB(&handleDeleteCB);
     EXPECT_EQ(OC_STACK_OK, OCRDDeleteWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, nullptr, 0,
                                                   deleteCB, OC_HIGH_QOS));
-    EXPECT_EQ(OC_STACK_OK, deleteCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, deleteCB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&DeleteDeviceVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult Delete1Verify(void *ctx, OCDoHandle handle,
@@ -1184,17 +1186,17 @@ TEST_P(RDDiscoverTests, Delete1)
     itst::Callback publishCB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, handles,
               2, OIC_RD_PUBLISH_TTL, publishCB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(_TIMEOUT_));
 
     itst::Callback deleteCB(&handleDeleteCB);
     EXPECT_EQ(OC_STACK_OK, OCRDDeleteWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, handles, 1,
                                                   deleteCB, OC_HIGH_QOS));
-    EXPECT_EQ(OC_STACK_OK, deleteCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, deleteCB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&Delete1Verify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 static OCStackApplicationResult DeleteNVerify(void *ctx, OCDoHandle handle,
@@ -1259,17 +1261,17 @@ TEST_P(RDDiscoverTests, DeleteN)
     itst::Callback publishCB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, handles,
               3, OIC_RD_PUBLISH_TTL, publishCB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(_TIMEOUT_));
 
     itst::Callback deleteCB(&handleDeleteCB);
     EXPECT_EQ(OC_STACK_OK, OCRDDeleteWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, handles, 2,
                                                   deleteCB, OC_HIGH_QOS));
-    EXPECT_EQ(OC_STACK_OK, deleteCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, deleteCB.Wait(_TIMEOUT_));
 
     itst::Callback discoverCB(&DeleteNVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 INSTANTIATE_TEST_CASE_P(ContentFormat, RDDiscoverTests,
@@ -1286,14 +1288,14 @@ TEST_P(RDDiscoverTests, TTLLapsedDeleteDevice)
     itst::Callback publishCB(&handlePublishCB);
     EXPECT_EQ(OC_STACK_OK, OCRDPublishWithDeviceId(NULL, "127.0.0.1", di[0], CT_ADAPTER_IP, handles,
               1, 1, publishCB, OC_LOW_QOS));
-    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, publishCB.Wait(_TIMEOUT_));
 
     std::this_thread::sleep_for(std::chrono::seconds(1));
 
     itst::Callback discoverCB(&DeleteDeviceVerify);
     EXPECT_EQ(OC_STACK_OK, OCDoResource(NULL, OC_REST_DISCOVER, "/oic/res", NULL, 0,
                     CT_DEFAULT, OC_HIGH_QOS, discoverCB, options, numOptions));
-    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(100));
+    EXPECT_EQ(OC_STACK_OK, discoverCB.Wait(_TIMEOUT_));
 }
 
 #endif
index 2d62a7f..4d4fca5 100644 (file)
@@ -902,10 +902,7 @@ size_t OC_CALL calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
     }
 
     size_t total = 1;
-    for(uint8_t i = 0; i < MAX_REP_ARRAY_DEPTH && dimensions[i] != 0; ++i)
-    {
-        total *= dimensions[i];
-    }
+    for(; total < MAX_REP_ARRAY_DEPTH && dimensions[total] != 0; ++total);
     return total;
 }
 
@@ -1211,7 +1208,7 @@ bool OC_CALL OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
         return false;
     }
 
-    char** newArray = (char**)OICMalloc(dimTotal * sizeof(char*));
+    char** newArray = (char**)OICCalloc(dimTotal, sizeof(char*));
 
     if (!newArray)
     {