Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: test/CommunicationTest.cpp

Issue 4882650246414336: Issue 2005 - Refactor working with strings in InputBuffer and OutputBuffer
Patch Set: rebase Created April 2, 2015, 11:09 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/shared/Communication.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/CommunicationTest.cpp
diff --git a/test/CommunicationTest.cpp b/test/CommunicationTest.cpp
index 048cbd7b89d018cadf418a911f01001415600eb9..b8d1a4fd505a3718df61a5ad6c3760810c5d7c34 100644
--- a/test/CommunicationTest.cpp
+++ b/test/CommunicationTest.cpp
@@ -17,134 +17,283 @@
#include <gtest/gtest.h>
+#include <thread>
+#include <chrono>
+#include <memory>
#include "../src/shared/AutoHandle.h"
#include "../src/shared/Communication.h"
const std::wstring pipeName(L"\\\\.\\pipe\\adblockplustests");
-namespace
+TEST(CommunicationTest, PipeConnect)
{
- DWORD WINAPI CreatePipe(LPVOID param)
+ auto serverThread = std::thread([]
{
Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE);
- return 0;
+ });
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ ASSERT_NO_THROW(Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CONNECT));
+ serverThread.join();
+}
+
+TEST(CommunicationTest, PipeSendReceive)
+{
+ std::unique_ptr<Communication::Pipe> client;
+ auto clientThread = std::thread([&client]
+ {
+ for (int i = 0; !client && i < 100; ++i)
Eric 2015/05/14 13:53:38 There's no need to guard the loop with the term "!
sergei 2015/05/15 09:45:55 Before declaration of `clientThread` `client` is a
+ {
+ try
+ {
+ client.reset(new Communication::Pipe(pipeName, Communication::Pipe::MODE_CONNECT));
+ }
+ catch(...)
+ {
+ std::this_thread::sleep_for(i * std::chrono::milliseconds(20));
+ }
+ }
+ });
+ Communication::Pipe server(pipeName, Communication::Pipe::MODE_CREATE);
+ clientThread.join();
+ ASSERT_TRUE(client);
Eric 2015/05/14 13:53:38 This assertion should appear before the declaratio
+ // send to the server
+ {
+ Communication::OutputBuffer message;
+ message << Communication::ProcType::PROC_GET_WHITELISTING_FITER
+ << std::string("This is a test message") << 64ll << 32 << true << false;
+ client->WriteMessage(message);
+ }
+ // receive on the server and send back
+ {
+ Communication::OutputBuffer response;
+ Communication::InputBuffer message = server.ReadMessage();
+ Communication::ProcType proc;
+ message >> proc;
+ EXPECT_EQ(Communication::ProcType::PROC_GET_WHITELISTING_FITER, proc);
+ response << Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED;
+
+ std::string text;
+ message >> text;
+ EXPECT_EQ("This is a test message", text);
+ response << std::string("Response");
+
+ int64_t integer64;
+ message >> integer64;
+ EXPECT_EQ(64ll, integer64);
+ response << 46ll;
+
+ int32_t integer32;
+ message >> integer32;
+ EXPECT_EQ(32, integer32);
+ response << 23;
+
+ bool boolean;
+ message >> boolean;
+ EXPECT_TRUE(boolean);
+ response << false;
+
+ message >> boolean;
+ EXPECT_FALSE(boolean);
+ response << true;
+
+ server.WriteMessage(response);
}
- DWORD WINAPI ReceiveSend(LPVOID param)
+ // receive on the client
{
- Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CREATE);
+ Communication::InputBuffer message = client->ReadMessage();
+ Communication::ProcType proc;
+ message >> proc;
+ EXPECT_EQ(Communication::ProcType::PROC_IS_FIRST_RUN_ACTION_NEEDED, proc);
- Communication::InputBuffer message = pipe.ReadMessage();
+ std::string text;
+ message >> text;
+ EXPECT_EQ("Response", text);
- std::string stringValue;
- std::wstring wstringValue;
- int64_t int64Value;
- int32_t int32Value;
- bool boolValue;
- message >> stringValue >> wstringValue >> int64Value >> int32Value >> boolValue;
+ int64_t integer64;
+ message >> integer64;
+ EXPECT_EQ(46ll, integer64);
- stringValue += " Received";
- wstringValue += L" \u043f\u0440\u0438\u043d\u044f\u0442\u043e";
- int64Value += 1;
- int32Value += 2;
- boolValue = !boolValue;
+ int32_t integer32;
+ message >> integer32;
+ EXPECT_EQ(23, integer32);
- Communication::OutputBuffer response;
- response << stringValue << wstringValue << int64Value << int32Value << boolValue;
- pipe.WriteMessage(response);
+ bool boolean;
+ message >> boolean;
+ EXPECT_FALSE(boolean);
- return 0;
+ message >> boolean;
+ EXPECT_TRUE(boolean);
}
}
-TEST(CommunicationTest, ConnectPipe)
+namespace
{
- AutoHandle thread(CreateThread(0, 0, CreatePipe, 0, 0, 0));
+ template<typename TestedType, typename AnotherType>
+ void SendReceiveTypes(TestedType value, AnotherType anotherTypeValue)
+ {
+ Communication::OutputBuffer message;
+ message << anotherTypeValue << value;
+ Communication::InputBuffer response(message.Get());
+ TestedType received;
+ ASSERT_ANY_THROW(response >> received);
+ AnotherType anotherTypeValueReceived;
+ response >> anotherTypeValueReceived;
+ response >> received;
+ EXPECT_EQ(value, received);
+ }
+}
- Sleep(100);
+TEST(CommunicationTest, TypeProc)
+{
+ SendReceiveTypes(Communication::ProcType::PROC_GET_ELEMHIDE_SELECTORS, std::string("non-ProcType"));
+}
- ASSERT_NO_THROW(Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CONNECT));
+TEST(CommunicationTest, TypeString)
+{
+ int32_t nonStringTypeValue = 10;
+ SendReceiveTypes(std::string("stringValue"), nonStringTypeValue);
}
-TEST(CommunicationTest, SendReceive)
+TEST(CommunicationTest, SendReceiveEmptyString)
{
- AutoHandle thread(CreateThread(0, 0, ReceiveSend, 0, 0, 0));
+ Communication::OutputBuffer message;
+ message << std::string();
+ Communication::InputBuffer response(message.Get());
+ std::string received = "clean me";
+ response >> received;
+ EXPECT_EQ("", received);
+}
- Sleep(100);
+TEST(CommunicationTest, TypeStringSendWideReceiveWide)
+{
+ int32_t nonStringTypeValue = 10;
+ SendReceiveTypes(std::wstring(L"stringValue"), nonStringTypeValue);
+}
- Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CONNECT);
+TEST(CommunicationTest, SendString_ReceiveWString)
+{
+ Communication::OutputBuffer message;
+ message << std::string("test");
+ Communication::InputBuffer response(message.Get());
+ std::wstring received;
+ response >> received;
+ EXPECT_EQ(L"test", received);
+}
+TEST(CommunicationTest, SendWString_ReceiveString)
+{
Communication::OutputBuffer message;
- message << std::string("Foo") << std::wstring(L"Bar") << int64_t(9876543210L) << int32_t(5) << true;
- pipe.WriteMessage(message);
-
- Communication::InputBuffer response = pipe.ReadMessage();
-
- std::string stringValue;
- std::wstring wstringValue;
- int64_t int64Value;
- int32_t int32Value;
- bool boolValue;
-
- ASSERT_ANY_THROW(response >> wstringValue);
- ASSERT_ANY_THROW(response >> int64Value);
- ASSERT_ANY_THROW(response >> int32Value);
- ASSERT_ANY_THROW(response >> boolValue);
-
- response >> stringValue >> wstringValue;
-
- ASSERT_ANY_THROW(response >> stringValue);
- ASSERT_ANY_THROW(response >> wstringValue);
- ASSERT_ANY_THROW(response >> int32Value);
- ASSERT_ANY_THROW(response >> boolValue);
-
- response >> int64Value >> int32Value >> boolValue;
-
- ASSERT_ANY_THROW(response >> stringValue);
- ASSERT_ANY_THROW(response >> wstringValue);
- ASSERT_ANY_THROW(response >> int64Value);
- ASSERT_ANY_THROW(response >> int32Value);
- ASSERT_ANY_THROW(response >> boolValue);
-
- ASSERT_EQ("Foo Received", stringValue);
- ASSERT_EQ(L"Bar \u043f\u0440\u0438\u043d\u044f\u0442\u043e", wstringValue);
- ASSERT_EQ(9876543211L, int64Value);
- ASSERT_EQ(7, int32Value);
- ASSERT_FALSE(boolValue);
-}
-
-void SendReceiveStrings(const std::vector<std::string>& src)
-{
- Communication::OutputBuffer outputBuffer;
- outputBuffer << src;
- Communication::InputBuffer inputBuffer(outputBuffer.Get());
- std::vector<std::string> dst;
- inputBuffer >> dst;
- auto dstSize = dst.size();
- ASSERT_EQ(dstSize, src.size());
- for (auto i = 0; i < dstSize; ++i)
+ message << std::wstring(L"test");
+ Communication::InputBuffer response(message.Get());
+ std::string received;
+ response >> received;
+ EXPECT_EQ("test", received);
+}
+
+TEST(CommunicationTest, SendStrings_ReceiveWStrings)
+{
+ Communication::OutputBuffer message;
+ std::vector<std::string> strings;
+ strings.emplace_back("test0");
+ strings.emplace_back("test1");
+ message << strings;
+ Communication::InputBuffer response(message.Get());
+ std::vector<std::wstring> received;
+ response >> received;
+ ASSERT_EQ(2u, received.size());
+ EXPECT_EQ(L"test0", received[0]);
+ EXPECT_EQ(L"test1", received[1]);
+}
+
+TEST(CommunicationTest, TypeInt64)
+{
+ SendReceiveTypes<int64_t>(10, std::string("non-ProcType"));
+}
+
+TEST(CommunicationTest, TypeInt32)
+{
+ SendReceiveTypes<int32_t>(10, std::string("non-ProcType"));
+}
+
+TEST(CommunicationTest, TypeBool)
+{
+ SendReceiveTypes<bool>(true, std::string("non-ProcType"));
+ SendReceiveTypes<bool>(false, std::string("non-ProcType"));
+}
+
+namespace
+{
+ void SendReceiveStrings(const std::vector<std::string>& src)
+ {
+ Communication::OutputBuffer outputBuffer;
+ outputBuffer << src;
+ Communication::InputBuffer inputBuffer(outputBuffer.Get());
+ std::vector<std::string> dst;
+ inputBuffer >> dst;
+ auto dstSize = dst.size();
+ ASSERT_EQ(src.size(), dstSize);
+ for (auto i = 0u; i < dstSize; ++i)
+ {
+ EXPECT_EQ(src[i], dst[i]);
+ }
+ }
+
+ std::vector<std::wstring> SendReceiveStringsToWStrings(const std::vector<std::string>& src)
{
- EXPECT_EQ(dst[i], src[i]);
+ Communication::OutputBuffer outputBuffer;
+ outputBuffer << src;
+ Communication::InputBuffer inputBuffer(outputBuffer.Get());
+ std::vector<std::wstring> dst;
+ inputBuffer >> dst;
+ return dst;
}
}
-TEST(InputOutputBuffersTests, EmptyStrings)
+TEST(InputOutputBuffersTest, EmptyStrings)
{
SendReceiveStrings(std::vector<std::string>());
}
-TEST(InputOutputBuffersTests, StringsWithOneValue)
+TEST(InputOutputBuffersTest, EmptyStringsToWideStrings)
+{
+ EXPECT_EQ(0u, SendReceiveStringsToWStrings(std::vector<std::string>()).size());
+}
+
+TEST(InputOutputBuffersTest, StringsWithOneValue)
{
std::vector<std::string> src;
src.emplace_back("string1");
SendReceiveStrings(src);
}
-TEST(InputOutputBuffersTests, MultivalueStrings)
+TEST(InputOutputBuffersTest, StringsWithOneValueToWStrings)
+{
+ std::vector<std::string> src;
+ src.emplace_back("string1");
+ auto dst = SendReceiveStringsToWStrings(src);
+ ASSERT_EQ(1u, dst.size());
+ EXPECT_EQ(L"string1", dst[0]);
+}
+
+TEST(InputOutputBuffersTest, WithMultivalueStrings)
{
std::vector<std::string> src;
src.emplace_back("string1");
src.emplace_back("str2");
src.emplace_back("value");
SendReceiveStrings(src);
+}
+
+TEST(InputOutputBuffersTest, WithMultivalueStringsToWStrings)
+{
+ std::vector<std::string> src;
+ src.emplace_back("string1");
+ src.emplace_back("str2");
+ src.emplace_back("value");
+ auto dst = SendReceiveStringsToWStrings(src);
+ ASSERT_EQ(3u, dst.size());
+ EXPECT_EQ(L"string1", dst[0]);
+ EXPECT_EQ(L"str2", dst[1]);
+ EXPECT_EQ(L"value", dst[2]);
}
« no previous file with comments | « src/shared/Communication.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld