Skip to content

Commit

Permalink
API unit tests, move acquire to before null check
Browse files Browse the repository at this point in the history
  • Loading branch information
disa6302 committed Oct 10, 2023
1 parent edcd6e3 commit a803fcc
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
24 changes: 12 additions & 12 deletions src/source/Crypto/Dtls_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ STATUS dtlsTransmissionTimerCallback(UINT32 timerID, UINT64 currentTime, UINT64
UINT64 timeoutValDefaultTimeUnit = 0;
LONG dtlsTimeoutRet = 0;

CHK(pDtlsSession != NULL, STATUS_NULL_ARG);
acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL, STATUS_NULL_ARG);

MEMSET(&timeout, 0x00, SIZEOF(struct timeval));

Expand Down Expand Up @@ -386,6 +386,7 @@ STATUS beginHandshakeProcess(PDtlsSession pDtlsSession, BOOL isServer, PINT32 ss
ENTERS();
STATUS retStatus = STATUS_SUCCESS;

acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL, STATUS_NULL_ARG);

CHK(!ATOMIC_LOAD_BOOL(&pDtlsSession->isStarted), retStatus);
Expand All @@ -408,6 +409,7 @@ STATUS beginHandshakeProcess(PDtlsSession pDtlsSession, BOOL isServer, PINT32 ss
*sslRet = SSL_do_handshake(pDtlsSession->pSsl);
CleanUp:
CHK_LOG_ERR(retStatus);
releaseDtlsSession(pDtlsSession);
LEAVES();
return retStatus;
}
Expand All @@ -419,9 +421,8 @@ STATUS dtlsSessionStart(PDtlsSession pDtlsSession, BOOL isServer)
BOOL locked = FALSE;
INT32 sslRet;

CHK(pDtlsSession != NULL && pDtlsSession != NULL, STATUS_NULL_ARG);

acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL, STATUS_NULL_ARG);

MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;
Expand Down Expand Up @@ -453,9 +454,9 @@ STATUS dtlsSessionHandshakeInThread(PDtlsSession pDtlsSession, BOOL isServer)
BOOL dtlsHandshakeErrored = FALSE;
BOOL timedOut = FALSE;
MEMSET(&timeout, 0x00, SIZEOF(struct timeval));
CHK(pDtlsSession != NULL && pDtlsSession != NULL, STATUS_NULL_ARG);

acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL, STATUS_NULL_ARG);

MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;
Expand Down Expand Up @@ -594,8 +595,8 @@ STATUS dtlsSessionProcessPacket(PDtlsSession pDtlsSession, PBYTE pData, PINT32 p
INT32 sslRet = 0, sslErr;
INT32 dataLen = 0;

CHK(pDtlsSession != NULL && pDtlsSession != NULL && pDataLen != NULL, STATUS_NULL_ARG);
acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL && pDataLen != NULL, STATUS_NULL_ARG);
CHK(ATOMIC_LOAD_BOOL(&pDtlsSession->isStarted), STATUS_SSL_PACKET_BEFORE_DTLS_READY);

MUTEX_LOCK(pDtlsSession->sslLock);
Expand Down Expand Up @@ -662,9 +663,8 @@ STATUS dtlsSessionPutApplicationData(PDtlsSession pDtlsSession, PBYTE pData, INT
SIZE_T pending;
BOOL locked = FALSE;

CHK(pDtlsSession != NULL && pData != NULL, STATUS_NULL_ARG);

acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL && pData != NULL, STATUS_NULL_ARG);

MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;
Expand Down Expand Up @@ -698,9 +698,9 @@ STATUS dtlsSessionShutdown(PDtlsSession pDtlsSession)
STATUS retStatus = STATUS_SUCCESS;
BOOL locked = FALSE;

acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL, STATUS_NULL_ARG);

acquireDtlsSession(pDtlsSession);
MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;

Expand Down Expand Up @@ -755,9 +755,9 @@ STATUS dtlsSessionIsInitFinished(PDtlsSession pDtlsSession, PBOOL pIsConnected)
STATUS retStatus = STATUS_SUCCESS;
BOOL locked = FALSE;

acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL && pIsConnected != NULL, STATUS_NULL_ARG);

acquireDtlsSession(pDtlsSession);
MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;
*pIsConnected = SSL_is_init_finished(pDtlsSession->pSsl);
Expand Down Expand Up @@ -786,9 +786,9 @@ STATUS dtlsSessionPopulateKeyingMaterial(PDtlsSession pDtlsSession, PDtlsKeyingM
BYTE keyingMaterialBuffer[MAX_SRTP_MASTER_KEY_LEN * 2 + MAX_SRTP_SALT_KEY_LEN * 2];
BOOL locked = FALSE;

acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL && pDtlsKeyingMaterial != NULL, STATUS_NULL_ARG);

acquireDtlsSession(pDtlsSession);
MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;

Expand Down Expand Up @@ -834,9 +834,9 @@ STATUS dtlsSessionGetLocalCertificateFingerprint(PDtlsSession pDtlsSession, PCHA
STATUS retStatus = STATUS_SUCCESS;
BOOL locked = FALSE;

acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL && pBuff != NULL, STATUS_NULL_ARG);

acquireDtlsSession(pDtlsSession);
CHK(buffLen >= CERTIFICATE_FINGERPRINT_LENGTH, STATUS_INVALID_ARG_LEN);

MUTEX_LOCK(pDtlsSession->sslLock);
Expand All @@ -862,9 +862,9 @@ STATUS dtlsSessionVerifyRemoteCertificateFingerprint(PDtlsSession pDtlsSession,
X509* pRemoteCertificate = NULL;
BOOL locked = FALSE;

acquireDtlsSession(pDtlsSession);
CHK(pDtlsSession != NULL && pExpectedFingerprint != NULL, STATUS_NULL_ARG);

acquireDtlsSession(pDtlsSession);
MUTEX_LOCK(pDtlsSession->sslLock);
locked = TRUE;

Expand Down
12 changes: 6 additions & 6 deletions src/source/PeerConnection/PeerConnection.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,11 @@ STATUS onFrameDroppedFunc(UINT64 customData, UINT16 startIndex, UINT16 endIndex,
PVOID dtlsSessionStartThread(PVOID args)
{
PKvsPeerConnection pKvsPeerConnection = (PKvsPeerConnection) args;
dtlsSessionHandshakeInThread(pKvsPeerConnection->pDtlsSession, pKvsPeerConnection->dtlsIsServer);
if (pKvsPeerConnection != NULL) {
dtlsSessionHandshakeInThread(pKvsPeerConnection->pDtlsSession, pKvsPeerConnection->dtlsIsServer);
} else {
DLOGE("Peer connection object NULL, cannot start DTLS handshake");
}
return NULL;
}

Expand Down Expand Up @@ -496,12 +500,8 @@ VOID onIceConnectionStateChange(UINT64 customData, UINT64 connectionState)
// wait until DTLS state changes to CONNECTED.
//
// Reference: https://w3c.github.io/webrtc-pc/#rtcpeerconnectionstate-enum
#ifdef ENABLE_KVS_THREADPOOL
#ifdef KVS_USE_OPENSSL
#if defined(ENABLE_KVS_THREADPOOL) && defined(KVS_USE_OPENSSL)
CHK_STATUS(threadpoolContextPush(dtlsSessionStartThread, (PVOID) pKvsPeerConnection));
#else
CHK_STATUS(dtlsSessionStart(pKvsPeerConnection->pDtlsSession, pKvsPeerConnection->dtlsIsServer));
#endif
#else
CHK_STATUS(dtlsSessionStart(pKvsPeerConnection->pDtlsSession, pKvsPeerConnection->dtlsIsServer));
#endif
Expand Down
37 changes: 37 additions & 0 deletions tst/DtlsApiTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,43 @@ TEST_F(DtlsApiTest, createCertificateAndKey_Returns_Success)
EXPECT_EQ(pKey, nullptr);
}

TEST_F(DtlsApiTest, dtlsSessionIsInitFinished_Null_Check)
{
PDtlsSession pClient = NULL;
BOOL isDtlsConnected = FALSE;
EXPECT_EQ(STATUS_NULL_ARG, dtlsSessionIsInitFinished(pClient, &isDtlsConnected));
EXPECT_EQ(FALSE, isDtlsConnected);
}

TEST_F(DtlsApiTest, dtlsSessionCreated_RefCount)
{
DtlsSessionCallbacks callbacks;
PDtlsSession pClient = NULL;
TIMER_QUEUE_HANDLE timerQueueHandle = INVALID_TIMER_QUEUE_HANDLE_VALUE;
EXPECT_EQ(STATUS_SUCCESS, timerQueueCreate(&timerQueueHandle));
EXPECT_EQ(STATUS_SUCCESS, createDtlsSession(&callbacks, timerQueueHandle, 0, FALSE, NULL, &pClient));
EXPECT_EQ(0, pClient->objRefCount);
freeDtlsSession(&pClient);
EXPECT_EQ(NULL, pClient);
timerQueueFree(&timerQueueHandle);
}

TEST_F(DtlsApiTest, dtlsProcessPacket_Api_Check)
{
DtlsSessionCallbacks callbacks;
PDtlsSession pClient = NULL;
BOOL isDtlsConnected = FALSE;
INT32 length;
TIMER_QUEUE_HANDLE timerQueueHandle = INVALID_TIMER_QUEUE_HANDLE_VALUE;
EXPECT_EQ(STATUS_NULL_ARG, dtlsSessionProcessPacket(pClient, NULL, &length));
EXPECT_EQ(STATUS_SUCCESS, timerQueueCreate(&timerQueueHandle));
EXPECT_EQ(STATUS_SUCCESS, createDtlsSession(&callbacks, timerQueueHandle, 0, FALSE, NULL, &pClient));
EXPECT_EQ(STATUS_NULL_ARG, dtlsSessionProcessPacket(pClient, NULL, NULL));
EXPECT_EQ(STATUS_SSL_PACKET_BEFORE_DTLS_READY, dtlsSessionProcessPacket(pClient, NULL, &length));
freeDtlsSession(&pClient);
timerQueueFree(&timerQueueHandle);
}

#elif KVS_USE_MBEDTLS
TEST_F(DtlsApiTest, createCertificateAndKey_Returns_Success)
{
Expand Down

0 comments on commit a803fcc

Please sign in to comment.