diff --git a/src/IpcMessage.cpp b/src/IpcMessage.cpp index 1ce6388..01325e4 100644 --- a/src/IpcMessage.cpp +++ b/src/IpcMessage.cpp @@ -38,7 +38,6 @@ class MessageImpl public: MessageImpl( unsigned char* message, size_t length ) : size( length ) - , ownRaw( true ) , asRaw( message ) { } @@ -64,14 +63,6 @@ class MessageImpl } } - ~MessageImpl() - { - if ( ownRaw ) - { - free( asRaw ); - } - } - const std::string& AsString() const { if ( size > 0 && asString.empty() ) @@ -95,7 +86,6 @@ class MessageImpl bool isError = false; size_t size = 0; - bool ownRaw = false; unsigned char* asRaw = nullptr; std::vector asByteVect; diff --git a/src/IpcMessage.h b/src/IpcMessage.h index 266519e..c802d7b 100644 --- a/src/IpcMessage.h +++ b/src/IpcMessage.h @@ -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 diff --git a/tests/main.cpp b/tests/main.cpp index 3abf6a1..9f96faf 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -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{ 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{ 0 } ); + return std::vector{ 1 }; } @@ -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( &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 );