From 3fe8623358a77c7ac0444660359a9a94168a8d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l-Erik=20Martinsen?= Date: Fri, 15 Jan 2016 14:53:53 +0100 Subject: [PATCH] Adding tests and fixing possible chrash. Fixed return value. --- include/stunclient.h | 57 ++++++++++++++++++-------------- src/stunclient.c | 10 ++---- test/stunclient_test.c | 75 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 106 insertions(+), 36 deletions(-) diff --git a/include/stunclient.h b/include/stunclient.h index 96dd8a8..696e3fe 100644 --- a/include/stunclient.h +++ b/include/stunclient.h @@ -40,10 +40,10 @@ typedef enum StunResult_BindOk, /* successful */ StunResult_BindFail, /* Received BindErrorResp */ StunResult_BindFailNoAnswer, /* Bind Req failed - no contact with - *stun server */ + * stun server */ StunResult_BindFailUnauthorised, /* passwd/username is incorrect */ StunResult_CancelComplete, /* Request is cancelled and timed out - **/ + **/ StunResult_ICMPResp, /* Received ICMP */ StunResult_InternalError, StunResult_MalformedResp @@ -51,7 +51,7 @@ typedef enum /* Signalled back to the caller as a paramter in the TURN callback (see TURNCB) - **/ +**/ typedef struct @@ -61,11 +61,11 @@ typedef struct struct sockaddr_storage rflxAddr; struct sockaddr_storage srcAddr; struct sockaddr_storage dstBaseAddr; /* The destination seen from the - *sender of the response */ - uint32_t rtt; /* Rtt in microseconds */ - uint32_t retransmits; - uint32_t ICMPtype; - uint32_t ttl; + * sender of the response */ + uint32_t rtt; /* Rtt in microseconds */ + uint32_t retransmits; + uint32_t ICMPtype; + uint32_t ttl; } StunCallBackData_T; /* for output of managment info (optional) */ @@ -75,9 +75,9 @@ typedef void (* STUN_INFO_FUNC_PTR)(void* userData, /* Signalling back to user e.g. result of BindResp. * userCtx - User provided context, as provided in - *StunClient_startxxxx(userCtx,...) + * StunClient_startxxxx(userCtx,...) * stunCbData - User provided stun callback data. stunbind writes status - *here. e.g. Alloc ok + reflexive + relay address + * here. e.g. Alloc ok + reflexive + relay address */ typedef void (* STUNCB)(void* userCtx, StunCallBackData_T* stunCbData); @@ -119,9 +119,9 @@ StunClient_free(STUN_CLIENT_DATA* clientData); * Should be called (once only) before StunClient_startxxxxxx(). * InstanceData - memory allocated by user, to be used by the StunClient. * funcPtr - Will be called by Stun when it outputs management info - *and trace. + * and trace. * If this is NULL, then there is no output. You can - *provide a function such as below:: + * provide a function such as below:: * userData - void pointer to be returned with logger callback */ void @@ -137,31 +137,32 @@ StunClient_RegisterLogger(STUN_CLIENT_DATA* clientData, * Optional, can be NULL. STUN does not write to this data. * serverAddr - Address of TURN server in format "a.b.c.d:port" * baseAddr - Address of BASE in format "a.b.c.d:port" - * proto - Optional context passed to sendFunc. eg. IPPROTO_UDP/TCP. + * proto - Optional context passed to sendFunc. eg. + *IPPROTO_UDP/TCP. * useRelay - True to send via TURN server * uFrag - Combination of local and remote ufrag exchanged in - *INVITE(LFRAG) / OK(RFRAG) in format : + * INVITE(LFRAG) / OK(RFRAG) in format : * password - Remote password, exchanged in invite/ok. \0 - *terminated string. Max 512 chars. + * terminated string. Max 512 chars. * peerPriority - Candidate Priority. See ICE-19 spec. * useCandidate - * iceControlling - * tieBreaker - * transactionId - * sockhandle - used as 1st parameter in STUN_SENDFUNC(), typically a - *socket. + * socket. * sendFunc - function used to send STUN packet. - *send(sockhandle,buff, len, turnServerAddr, userCtx) + * send(sockhandle,buff, len, turnServerAddr, userCtx) * stunCbFunc - user provided callback function used by turn to - *signal the result of an allocation or channel bind etc... + * signal the result of an allocation or channel bind etc... * stunCbData - user provided callback turn data. turn writes to this - *data area. + * data area. * * returns - Turn instance/context. Application should store this - *in further calls to TurnClient_StartChannelBindReq(), - *TurnClient_HandleIncResp(). + * in further calls to TurnClient_StartChannelBindReq(), + * TurnClient_HandleIncResp(). */ -uint32_t +int32_t StunClient_startBindTransaction(STUN_CLIENT_DATA* clientData, void* userCtx, const struct sockaddr* serverAddr, @@ -179,12 +180,19 @@ StunClient_startBindTransaction(STUN_CLIENT_DATA* clientData, STUN_SENDFUNC sendFunc, STUNCB stunCbFunc, DiscussData* discussData); /* + * *nullptr + * *if + * *no + * *malicedata + * *should + * *be + * *sent. **/ @@ -202,12 +210,13 @@ StunClient_startSTUNTrace(STUN_CLIENT_DATA* clientData, STUN_SENDFUNC sendFunc, STUNCB stunCbFunc, DiscussData* discussData); /*NULL - *if + * if + * *none*/ /* * This function must be called by the application every N msec. N must be same - *as in StunClientBind_Init(instances, N) + * as in StunClientBind_Init(instances, N) */ void StunClient_HandleTick(STUN_CLIENT_DATA* clientData, diff --git a/src/stunclient.c b/src/stunclient.c index 39242b1..cd52682 100644 --- a/src/stunclient.c +++ b/src/stunclient.c @@ -255,7 +255,7 @@ StunClient_Alloc(STUN_CLIENT_DATA** clientDataPtr) if (!clientDataPtr) { - return false; + return false; } clientData = malloc(sizeof *clientData); @@ -328,7 +328,7 @@ StunClient_HandleTick(STUN_CLIENT_DATA* clientData, } -uint32_t +int32_t StunClient_startBindTransaction(STUN_CLIENT_DATA* clientData, void* userCtx, const struct sockaddr* serverAddr, @@ -351,11 +351,7 @@ StunClient_startBindTransaction(STUN_CLIENT_DATA* clientData, if (clientData == NULL) { - StunPrint(clientData->logUserData, - clientData->Log_cb, - StunInfoCategory_Error, - " startBindTransaction() failed, Not initialised or no memory"); - return STUNCLIENT_CTX_UNKNOWN; + return STUNCLIENT_CTX_UNKNOWN; } memset( &m, 0, sizeof(m) ); diff --git a/test/stunclient_test.c b/test/stunclient_test.c index f22ae53..1751eff 100644 --- a/test/stunclient_test.c +++ b/test/stunclient_test.c @@ -42,6 +42,9 @@ STUN_CLIENT_DATA* stunInstance; struct sockaddr_storage stunServerAddr; DiscussData discussData; + +char logStr[200]; + CTEST_DATA(data) { int a; @@ -103,6 +106,16 @@ SendRawStun(void* ctx, } +void +stundbg(void* ctx, + StunInfoCategory_T category, + char* errStr) +{ + (void) category; + (void) ctx; + strncpy(logStr, errStr, sizeof logStr); +} + static int StartBindTransaction(int n) { @@ -121,13 +134,16 @@ StartBindTransaction(int n) false, "pem", "pem", - 0, /* uint32_t 1845494271 - * (priority) */ + 34567, /* uint32_t + * 1845494271 + * (priority) */ false, false, - 0, /* uint64_t - * 0x932FF9B151263B36LL - * (tieBreaker) */ + 4567, /* uint64_t + * + * + *0x932FF9B151263B36LL + * (tieBreaker) */ LastTransId, 0, /* socket */ SendRawStun, /* send func */ @@ -250,11 +266,60 @@ CTEST_TEARDOWN(data) /* } */ + CTEST(stunclient, empty) { ASSERT_TRUE(true); } +CTEST(stunclient, alloc) +{ + ASSERT_FALSE( StunClient_Alloc(NULL) ); +} + +CTEST(stunclient, logger) +{ + StunClient_Alloc(&stunInstance); + StunClient_RegisterLogger(stunInstance, + stundbg, + NULL); + StartBindTransaction(0); + ASSERT_TRUE( 0 == strncmp("", logStr, 15) ); + +} + +CTEST(stunclient, bindtrans) +{ + int32_t ret = StunClient_startBindTransaction(NULL, + NULL, + (struct sockaddr*)&stunServerAddr, + NULL, + 0, + false, + "pem", + "pem", + 34567, + /* uint32_t + * 1845494271 + * (priority) */ + false, + false, + 4567, + /* uint64_t + * + * 0x932FF9B151263B36LL + * (tieBreaker) */ + LastTransId, + 0, + /* socket */ + SendRawStun, + /* send func */ + StunStatusCallBack, + NULL); + ASSERT_TRUE(ret == -1); + +} + CTEST(stunclient, WaitBindRespNotAut_Timeout) { ASSERT_TRUE(stunResult == StunResult_Empty);