Hi Martin,
following there are a test code
I compiled it using:
gcc testMock.cpp -lgmock -lgtest -lpthread -o testMock -g3
and check using valgrind:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using ::testing::_;
using ::testing::Return;
using ::testing::InSequence;
using ::testing::Mock;
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
class UDPSocket {
public:
UDPSocket() {}
virtual ~UDPSocket() {}
virtual bool open(int port){return true;}
virtual bool close(){return true;}
virtual int recv(unsigned char * buffer, unsigned int maxSize){return
0;}
};
class stb_time {
public:
virtual long long stb_timeMsecLongLong(){return 0;};
};
class DetectUDPStream {
public:
DetectUDPStream(int port);
virtual ~DetectUDPStream();
bool streamPresent();
void setUDPSocket(UDPSocket *);
void setStbTime(stb_time *);
public:
static const int C_Timeout = 100;
static const int C_MaxDataSocket = 32768;
private:
UDPSocket * udpSocket;
stb_time * stbTime;
long long lastTime;
int udpPort;
};
DetectUDPStream::DetectUDPStream(int port) {
udpSocket = new UDPSocket;
stbTime = new stb_time;
lastTime = -1;
udpSocket->open(port);
udpPort = port;
}
DetectUDPStream::~DetectUDPStream() {
delete udpSocket;
delete stbTime;
}
inline void DetectUDPStream::setUDPSocket(UDPSocket * newSocket){
delete udpSocket;
udpSocket = newSocket;
udpSocket->open(udpPort);
}
inline void DetectUDPStream::setStbTime(stb_time * newTime){
delete stbTime;
stbTime = newTime;
}
bool DetectUDPStream::streamPresent(){
bool isStreamPresent = false;
long long time;
unsigned char data[C_MaxDataSocket];
time = stbTime->stb_timeMsecLongLong();
if (lastTime == -1) {
lastTime = time;
}
if (udpSocket->recv(data,C_MaxDataSocket) > 0) {
isStreamPresent = true;
lastTime = time;
} else {
if ( (time - lastTime) < C_Timeout){
isStreamPresent = true;
}
}
return isStreamPresent;
}
class TestDetectUDPStream : public ::testing::Test {
public:
class MockUDPSocket : public UDPSocket{
public:
MOCK_METHOD1(open, bool(int port));
MOCK_METHOD0(close, bool());
MOCK_METHOD2(recv, int(unsigned char * buffer, unsigned int
maxSize));
};
class Mockstb_time : public stb_time {
public:
MOCK_METHOD0(stb_timeMsecLongLong, long long());
};
public:
void SetUp();
void TearDown();
protected:
DetectUDPStream * toTest;
Mockstb_time * mockstb_time;
MockUDPSocket * mockUDPSocket;
static const int portToTest = 1234;
};
void TestDetectUDPStream::SetUp() {
mockUDPSocket = new MockUDPSocket;
EXPECT_CALL(*mockUDPSocket,
open(portToTest)).WillOnce(Return(true));
mockstb_time = new Mockstb_time;
toTest = new DetectUDPStream(portToTest);
toTest->setUDPSocket(mockUDPSocket);
toTest->setStbTime(mockstb_time);
Mock::AllowLeak(mockstb_time);
}
void TestDetectUDPStream::TearDown() {
delete toTest;
}
TEST_F(TestDetectUDPStream,stream_is_not_present){
const int UDPPacketSize=1368;
const long long startTime = 12543535;
{
InSequence dummy;
EXPECT_CALL(*mockstb_time,
stb_timeMsecLongLong()).WillOnce(Return(startTime));
EXPECT_CALL(*mockUDPSocket,
recv(_,_)).WillOnce(Return(UDPPacketSize));
EXPECT_CALL(*mockstb_time,
stb_timeMsecLongLong()).WillOnce(Return(startTime +
DetectUDPStream::C_Timeout-1 ));
EXPECT_CALL(*mockUDPSocket, recv(_,_)).WillOnce(Return(0));
EXPECT_CALL(*mockstb_time,
stb_timeMsecLongLong()).WillOnce(Return(startTime +
DetectUDPStream::C_Timeout ));
EXPECT_CALL(*mockUDPSocket, recv(_,_)).WillOnce(Return(0));
}
ASSERT_THAT(toTest->streamPresent(),true);
ASSERT_THAT(toTest->streamPresent(),true);
ASSERT_THAT(toTest->streamPresent(),false);
}
TEST_F(TestDetectUDPStream,stream_is_present){
const int UDPPacketSize=1368;
const long long startTime = 12543535;
const long long time1 = startTime+DetectUDPStream::C_Timeout/3;
const long long time2 = time1+DetectUDPStream::C_Timeout/3;
const long long time3 = startTime+DetectUDPStream::C_Timeout;
const long long time4 = time2+DetectUDPStream::C_Timeout;
{
InSequence dummy;
EXPECT_CALL(*mockstb_time,
stb_timeMsecLongLong()).WillOnce(Return(startTime));
EXPECT_CALL(*mockUDPSocket,
recv(_,_)).WillOnce(Return(UDPPacketSize));
EXPECT_CALL(*mockstb_time,
stb_timeMsecLongLong()).WillOnce(Return(time1 ));
EXPECT_CALL(*mockUDPSocket, recv(_,_)).WillOnce(Return(0));
EXPECT_CALL(*mockstb_time,
stb_timeMsecLongLong()).WillOnce(Return(time2));
EXPECT_CALL(*mockUDPSocket,
recv(_,_)).WillOnce(Return(UDPPacketSize));
EXPECT_CALL(*mockstb_time,
stb_timeMsecLongLong()).WillOnce(Return(time3));
EXPECT_CALL(*mockUDPSocket, recv(_,_)).WillOnce(Return(0));
EXPECT_CALL(*mockstb_time,
stb_timeMsecLongLong()).WillOnce(Return(time4 ));