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: address some comments Created Feb. 18, 2015, 2:05 p.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 fb9d4c34f35220fdaab146f480a15be2e9fff40d..c3ee57e89e989790e3f486ade913e60a5910bfeb 100644
--- a/test/CommunicationTest.cpp
+++ b/test/CommunicationTest.cpp
@@ -17,134 +17,273 @@
#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]
+ {
+ while(!client)
+ {
+ try
+ {
+ client.reset(new Communication::Pipe(pipeName, Communication::Pipe::MODE_CONNECT));
+ }
+ catch(...)
+ {
+ std::this_thread::yield();
+ }
+ }
+ });
+ Communication::Pipe server(pipeName, Communication::Pipe::MODE_CREATE);
+ clientThread.join();
+
+ // send to the server
+ {
+ Communication::OutputBuffer message;
+ message << Communication::ProcType::PROC_IS_WHITELISTED_URL
+ << 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_IS_WHITELISTED_URL, 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, TypeStringSendWideReceiveWide)
{
- AutoHandle thread(CreateThread(0, 0, ReceiveSend, 0, 0, 0));
+ int32_t nonStringTypeValue = 10;
+ SendReceiveTypes(std::wstring(L"stringValue"), nonStringTypeValue);
+}
- Sleep(100);
+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);
+}
- Communication::Pipe pipe(pipeName, Communication::Pipe::MODE_CONNECT);
+TEST(CommunicationTest, SendWString_ReceiveString)
+{
+ Communication::OutputBuffer message;
+ 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;
- 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)
+ 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)
{
- EXPECT_EQ(dst[i], src[i]);
+ 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)
+ {
+ Communication::OutputBuffer outputBuffer;
+ outputBuffer << src;
+ Communication::InputBuffer inputBuffer(outputBuffer.Get());
+ std::vector<std::wstring> dst;
+ inputBuffer >> dst;
+ return dst;
}
}
-TEST(InputOutputBuffersTests, EmptyStrings)
+TEST(InputOutputBuffersTests, InputOutputBufferEmptyStrings)
{
SendReceiveStrings(std::vector<std::string>());
}
-TEST(InputOutputBuffersTests, StringsWithOneValue)
+TEST(InputOutputBuffersTests, InputOutputBufferEmptyStringsToWideStrings)
+{
+ EXPECT_EQ(0u, SendReceiveStringsToWStrings(std::vector<std::string>()).size());
+}
+
+TEST(InputOutputBuffersTests, InputOutputBufferStringsWithOneValue)
{
std::vector<std::string> src;
src.emplace_back("string1");
SendReceiveStrings(src);
}
-TEST(InputOutputBuffersTests, MultivalueStrings)
+TEST(InputOutputBuffersTests, InputOutputBufferStringsWithOneValueToWStrings)
+{
+ 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(InputOutputBuffersTests, InputOutputBufferWithMultivalueStrings)
{
std::vector<std::string> src;
src.emplace_back("string1");
src.emplace_back("str2");
src.emplace_back("value");
SendReceiveStrings(src);
+}
+
+TEST(InputOutputBuffersTests, InputOutputBufferWithMultivalueStringsToWStrings)
+{
+ 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