Skip to content

Commit

Permalink
Add more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusTomlinson committed Jan 9, 2024
1 parent db5a3f5 commit 34b8fdc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
10 changes: 0 additions & 10 deletions src/IpcMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class MessageImpl
public:
MessageImpl( unsigned char* message, size_t length )
: size( length )
, ownRaw( true )
, asRaw( message )
{
}
Expand All @@ -64,14 +63,6 @@ class MessageImpl
}
}

~MessageImpl()
{
if ( ownRaw )
{
free( asRaw );
}
}

const std::string& AsString() const
{
if ( size > 0 && asString.empty() )
Expand All @@ -95,7 +86,6 @@ class MessageImpl
bool isError = false;

size_t size = 0;
bool ownRaw = false;
unsigned char* asRaw = nullptr;

std::vector<unsigned char> asByteVect;
Expand Down
1 change: 0 additions & 1 deletion src/IpcMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class MessageImpl;
class Message final
{
public:
// Ownership of message is transferred to Message and will be freed on destruction
Message( unsigned char* message, size_t length );

// cppcheck-suppress noExplicitConstructor
Expand Down
48 changes: 46 additions & 2 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,19 @@ Ipc::Message RecvCallback( const Ipc::Message& recvHeader, const Ipc::Message& r
{
if ( recvMessage.AsString() != "Hello?" )
{
EXPECT_FALSE( recvHeader.IsError() );
EXPECT_EQ( recvHeader.AsString(), "bin" );
EXPECT_EQ( recvMessage.AsByteVect(), std::vector<unsigned char>{ 0 } );
EXPECT_EQ( recvHeader.Size(), 3 );
for ( size_t i = 0; i < recvHeader.Size(); ++i )
{
EXPECT_EQ( recvHeader.AsByteVect()[i], recvHeader.AsRaw()[i] );
}

EXPECT_FALSE( recvMessage.IsError() );
EXPECT_EQ( recvMessage.AsRaw()[0], 0 );
EXPECT_EQ( recvMessage.AsString()[0], '\0' );
EXPECT_EQ( recvMessage.AsByteVect(), std::vector<unsigned char>{ 0 } );

return std::vector<unsigned char>{ 1 };
}

Expand Down Expand Up @@ -148,10 +158,44 @@ TEST( Ipc, PathTooLong )

// Client
Ipc::Client client( longPath );
auto response = client.Send( Ipc::Message( "" ), Ipc::Message( "" ) );
auto response = client.Send( Ipc::Message( "header" ), Ipc::Message( "message" ) );
ASSERT_TRUE( response.IsError() );
ASSERT_EQ( response.AsString(), std::string( "socket path too long: " ) + longPath );
}

TEST( Ipc, EmptyMessage )
{
Ipc::Client client( c_serverSocket );

auto response1 = client.Send( Ipc::Message( "" ), Ipc::Message( "message" ) );
ASSERT_TRUE( response1.IsError() );
ASSERT_EQ( response1.AsString(), "header can not be empty" );

auto response2 = client.Send( Ipc::Message( "header" ), Ipc::Message( "" ) );
ASSERT_TRUE( response2.IsError() );
ASSERT_EQ( response2.AsString(), "message can not be empty" );
}

TEST( Ipc, MessageConversion )
{
std::string messageStr = "test message 1 2 3";
Ipc::Message message( reinterpret_cast<unsigned char*>( &messageStr[0] ), messageStr.size() );

ASSERT_FALSE( message.IsError() );

ASSERT_EQ( message.Size(), messageStr.size() );
ASSERT_EQ( message.AsString().size(), messageStr.size() );
ASSERT_EQ( message.AsByteVect().size(), messageStr.size() );

ASSERT_EQ( message.AsString(), messageStr );

for ( size_t i = 0; i < messageStr.size(); ++i )
{
ASSERT_EQ( message.AsByteVect()[i], (unsigned char)messageStr[i] );
ASSERT_EQ( message.AsRaw()[i], (unsigned char)messageStr[i] );
}
}

int main( int argc, char** argv )
{
::testing::InitGoogleTest( &argc, argv );
Expand Down

0 comments on commit 34b8fdc

Please sign in to comment.