From 9ad6f67f6c39ec4ff49c382d5beaf9006f664fcb Mon Sep 17 00:00:00 2001 From: Christian Linke Date: Wed, 15 Feb 2017 01:47:24 -0800 Subject: handle overflow of handles correctly Signed-off-by: Christian Linke --- AudioManagerCore/src/CAmRoutingSender.cpp | 33 ++++++-- .../CAmRoutingInterfaceTest.cpp | 97 ++++++++++++++++++++++ 2 files changed, 121 insertions(+), 9 deletions(-) diff --git a/AudioManagerCore/src/CAmRoutingSender.cpp b/AudioManagerCore/src/CAmRoutingSender.cpp index 0e25e68..9cc013d 100644 --- a/AudioManagerCore/src/CAmRoutingSender.cpp +++ b/AudioManagerCore/src/CAmRoutingSender.cpp @@ -813,17 +813,32 @@ am_Error_e CAmRoutingSender::getListHandles(std::vector & listHandl am_Handle_s CAmRoutingSender::createHandle(std::shared_ptr handleData, const am_Handle_e type) { am_Handle_s handle; - if (++mHandleCount>=1024) //defined by 10 bit (out if structure!) - mHandleCount=1; - handle.handle = mHandleCount; handle.handleType = type; - mlistActiveHandles.insert(std::make_pair(handle, handleData)); - if ((mlistActiveHandles.size()%100) == 0) - { - logInfo("CAmRoutingSender::createHandle warning: too many open handles, number of handles: ", mlistActiveHandles.size()); + + for (int checkOverflow=0;checkOverflow<1024;checkOverflow++) + { + if (++mHandleCount>=1024) //defined by 10 bit (out if structure!) + { + mHandleCount=1; + } + handle.handle = mHandleCount; + + if ( mlistActiveHandles.find(handle) == mlistActiveHandles.end() ) + { + mlistActiveHandles.insert(std::make_pair(handle, handleData)); + if (mlistActiveHandles.size()>100) + { + logWarning(__METHOD_NAME__,"too many open handles, number of handles: ", mlistActiveHandles.size()); + } + logInfo(__METHOD_NAME__,handle.handle, handle.handleType); + return (handle); + } } - logInfo(__METHOD_NAME__,handle.handle, handle.handleType); - return (handle); + + logError(__METHOD_NAME__,"could not create new handle, all handles in use!"); + handle.handle=0; + + return(handle); } void CAmRoutingSender::setRoutingReady() diff --git a/AudioManagerCore/test/AmRoutingInterfaceTest/CAmRoutingInterfaceTest.cpp b/AudioManagerCore/test/AmRoutingInterfaceTest/CAmRoutingInterfaceTest.cpp index 2cd2b55..3a576e6 100644 --- a/AudioManagerCore/test/AmRoutingInterfaceTest/CAmRoutingInterfaceTest.cpp +++ b/AudioManagerCore/test/AmRoutingInterfaceTest/CAmRoutingInterfaceTest.cpp @@ -546,6 +546,103 @@ TEST_F(CAmRoutingInterfaceTest,nothingTodisconnect) ASSERT_TRUE(listHandles.empty()); } +TEST_F(CAmRoutingInterfaceTest,handleOverflow) +{ + am_Handle_s handle,handleOverflow1,handleOverflow2,handleOverflowCheck1,handleOverflowCheck2; + am_sinkID_t sinkID; + am_Sink_s sink; + am_Domain_s domain; + am_domainID_t domainID; + + pCF.createSink(sink); + pCF.createDomain(domain); + domain.name = "mock"; + domain.busname = "mock"; + sink.sinkID = 2; + sink.domainID = DYNAMIC_ID_BOUNDARY; + am_SoundProperty_s soundProperty; + soundProperty.type = SP_GENIVI_TREBLE; + soundProperty.value = 23; + + sink.listSoundProperties.push_back(soundProperty); + ASSERT_EQ(E_OK, pDatabaseHandler.enterDomainDB(domain,domainID)); + ASSERT_EQ(E_OK, pDatabaseHandler.enterSinkDB(sink,sinkID)); + + + + EXPECT_CALL(pMockInterface,asyncSetSinkSoundProperty(_,sinkID,_)).WillRepeatedly(Return(E_OK)); + + //open handles till 50 + for(int i=0;i<50;i++) + { + handle.handle=0; + soundProperty.value = i; + ASSERT_EQ(E_OK, pControlReceiver.setSinkSoundProperty(handle,sinkID,soundProperty)); + } + //now we ack 2 handles + EXPECT_CALL(pMockControlInterface,cbAckSetSinkSoundProperty(_,E_OK)); + ASSERT_EQ(E_OK, pControlReceiver.setSinkSoundProperty(handleOverflow1,sinkID,soundProperty)); + pRoutingReceiver.ackSetSinkSoundProperty(handleOverflow1,E_OK); + + EXPECT_CALL(pMockControlInterface,cbAckSetSinkSoundProperty(_,E_OK)); + ASSERT_EQ(E_OK, pControlReceiver.setSinkSoundProperty(handleOverflow2,sinkID,soundProperty)); + pRoutingReceiver.ackSetSinkSoundProperty(handleOverflow2,E_OK); + + for(int i=52;i<1023;i++) //now we get into the overflow areay + { + handle.handle=0; + soundProperty.value = i; + ASSERT_EQ(E_OK, pControlReceiver.setSinkSoundProperty(handle,sinkID,soundProperty)); + } + + //the next two handles must be the one we already acked + ASSERT_EQ(E_OK, pControlReceiver.setSinkSoundProperty(handleOverflowCheck1,sinkID,soundProperty)); + ASSERT_EQ(handleOverflow1.handle,handleOverflowCheck1.handle); + + ASSERT_EQ(E_OK, pControlReceiver.setSinkSoundProperty(handleOverflowCheck2,sinkID,soundProperty)); + ASSERT_EQ(handleOverflow2.handle,handleOverflowCheck2.handle); + +} + +TEST_F(CAmRoutingInterfaceTest,handleOverflowAbsolute) +{ + am_Handle_s handle,handleOverflow1,handleOverflow2,handleOverflowCheck1,handleOverflowCheck2; + am_sinkID_t sinkID; + am_Sink_s sink; + am_Domain_s domain; + am_domainID_t domainID; + + pCF.createSink(sink); + pCF.createDomain(domain); + domain.name = "mock"; + domain.busname = "mock"; + sink.sinkID = 2; + sink.domainID = DYNAMIC_ID_BOUNDARY; + am_SoundProperty_s soundProperty; + soundProperty.type = SP_GENIVI_TREBLE; + soundProperty.value = 23; + + sink.listSoundProperties.push_back(soundProperty); + ASSERT_EQ(E_OK, pDatabaseHandler.enterDomainDB(domain,domainID)); + ASSERT_EQ(E_OK, pDatabaseHandler.enterSinkDB(sink,sinkID)); + + + + EXPECT_CALL(pMockInterface,asyncSetSinkSoundProperty(_,sinkID,_)).WillRepeatedly(Return(E_OK)); + + + for(int i=0;i<1023;i++) //we fill up the handles + { + handle.handle=0; + soundProperty.value = i; + ASSERT_EQ(E_OK, pControlReceiver.setSinkSoundProperty(handle,sinkID,soundProperty)); + } + + //the next handle must return 0! + ASSERT_EQ(E_OK, pControlReceiver.setSinkSoundProperty(handleOverflowCheck1,sinkID,soundProperty)); + ASSERT_EQ(handleOverflowCheck1.handle,0); +} + int main(int argc, char **argv) -- cgit v1.2.1