clienttest.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "testserviceclient.h"
  26. #include "http2channel.h"
  27. #include "insecurecredentials.h"
  28. #include "blobmessage.h"
  29. #include <sslcredentials.h>
  30. #include <QTimer>
  31. #include <QFile>
  32. #include <QCryptographicHash>
  33. #include <QSslConfiguration>
  34. #include <QCoreApplication>
  35. #include <gtest/gtest.h>
  36. using namespace qtprotobufnamespace::tests;
  37. using namespace qtprotobuf;
  38. class ClientTest : public ::testing::Test
  39. {
  40. protected:
  41. static void SetUpTestCase() {
  42. }
  43. static QCoreApplication m_app;
  44. static int m_argc;
  45. static QUrl m_echoServerAddress;
  46. };
  47. int ClientTest::m_argc(0);
  48. QCoreApplication ClientTest::m_app(m_argc, nullptr);
  49. QUrl ClientTest::m_echoServerAddress("http://localhost:50051", QUrl::StrictMode);
  50. TEST_F(ClientTest, CheckMethodsGeneration)
  51. {
  52. //Dummy compile time check of functions generation and interface compatibility
  53. TestServiceClient testClient;
  54. SimpleStringMessage request;
  55. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  56. testClient.testMethod(request, result);
  57. testClient.testMethod(request);
  58. testClient.testMethod(request, &testClient, [](AsyncReply *){});
  59. delete result;
  60. }
  61. TEST_F(ClientTest, StringEchoTest)
  62. {
  63. TestServiceClient testClient;
  64. testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
  65. SimpleStringMessage request;
  66. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  67. request.setTestFieldString("Hello beach!");
  68. ASSERT_TRUE(testClient.testMethod(request, result));
  69. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Hello beach!");
  70. delete result;
  71. }
  72. TEST_F(ClientTest, StringEchoAsyncTest)
  73. {
  74. TestServiceClient testClient;
  75. testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
  76. SimpleStringMessage request;
  77. SimpleStringMessage result;
  78. request.setTestFieldString("Hello beach!");
  79. QEventLoop waiter;
  80. AsyncReply *reply = testClient.testMethod(request);
  81. QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter, &testClient]() {
  82. if (testClient.lastError() == AbstractChannel::StatusCode::Ok) {
  83. result = reply->read<SimpleStringMessage>();
  84. }
  85. waiter.quit();
  86. });
  87. waiter.exec();
  88. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  89. }
  90. TEST_F(ClientTest, StringEchoAsync2Test)
  91. {
  92. TestServiceClient testClient;
  93. testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
  94. SimpleStringMessage result;
  95. SimpleStringMessage request;
  96. request.setTestFieldString("Hello beach!");
  97. QEventLoop waiter;
  98. testClient.testMethod(request, &m_app, [&result, &waiter, &testClient](AsyncReply *reply) {
  99. if (testClient.lastError() == AbstractChannel::StatusCode::Ok) {
  100. result = reply->read<SimpleStringMessage>();
  101. }
  102. waiter.quit();
  103. });
  104. waiter.exec();
  105. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Hello beach!");
  106. }
  107. TEST_F(ClientTest, StringEchoImmediateAsyncAbortTest)
  108. {
  109. TestServiceClient testClient;
  110. testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
  111. SimpleStringMessage result;
  112. SimpleStringMessage request;
  113. request.setTestFieldString("sleep");
  114. QEventLoop waiter;
  115. AsyncReply *reply = testClient.testMethod(request);
  116. result.setTestFieldString("Result not changed by echo");
  117. QObject::connect(reply, &AsyncReply::finished, &m_app, [&waiter, &result, reply]() {
  118. result = reply->read<SimpleStringMessage>();
  119. waiter.quit();
  120. });
  121. AbstractChannel::StatusCode asyncStatus = AbstractChannel::StatusCode::Ok;
  122. QObject::connect(reply, &AsyncReply::error, reply, [&asyncStatus](AbstractChannel::StatusCode code){
  123. asyncStatus = code;
  124. });
  125. AbstractChannel::StatusCode clientStatus = AbstractChannel::StatusCode::Ok;
  126. QObject::connect(&testClient, &TestServiceClient::error, reply, [&clientStatus](AbstractChannel::StatusCode code, QString errMsg){
  127. clientStatus = code;
  128. std::cerr << code << ":" << errMsg.toStdString();
  129. });
  130. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  131. reply->abort();
  132. waiter.exec();
  133. ASSERT_EQ(clientStatus, AbstractChannel::StatusCode::Aborted);
  134. ASSERT_EQ(asyncStatus, AbstractChannel::StatusCode::Aborted);
  135. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  136. }
  137. TEST_F(ClientTest, StringEchoDeferredAsyncAbortTest)
  138. {
  139. TestServiceClient testClient;
  140. testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
  141. SimpleStringMessage result;
  142. SimpleStringMessage request;
  143. request.setTestFieldString("sleep");
  144. QEventLoop waiter;
  145. AsyncReply *reply = testClient.testMethod(request);
  146. result.setTestFieldString("Result not changed by echo");
  147. bool errorCalled = false;
  148. reply = testClient.testMethod(request);
  149. QObject::connect(reply, &AsyncReply::finished, &m_app, [reply, &result, &waiter, &testClient]() {
  150. result = reply->read<SimpleStringMessage>();
  151. waiter.quit();
  152. });
  153. QObject::connect(reply, &AsyncReply::error, reply, [&errorCalled](AbstractChannel::StatusCode){
  154. errorCalled = true;
  155. });
  156. QTimer::singleShot(2000, reply, &AsyncReply::abort);
  157. QTimer::singleShot(5000, &waiter, &QEventLoop::quit);
  158. waiter.exec();
  159. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Result not changed by echo");
  160. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCode::Aborted);
  161. ASSERT_TRUE(errorCalled);
  162. }
  163. TEST_F(ClientTest, StringEchoStreamTest)
  164. {
  165. TestServiceClient testClient;
  166. testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
  167. SimpleStringMessage result;
  168. SimpleStringMessage request;
  169. request.setTestFieldString("Stream");
  170. QEventLoop waiter;
  171. int i = 0;
  172. QObject::connect(&testClient, &TestServiceClient::testMethodServerStreamUpdated, &m_app, [&result, &i, &waiter](const SimpleStringMessage &ret) {
  173. ++i;
  174. result.setTestFieldString(result.testFieldString() + ret.testFieldString());
  175. if (i == 4) {
  176. waiter.quit();
  177. }
  178. });
  179. testClient.subscribeTestMethodServerStreamUpdates(request);
  180. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  181. waiter.exec();
  182. ASSERT_EQ(i, 4);
  183. ASSERT_STREQ(result.testFieldString().toStdString().c_str(), "Stream1Stream2Stream3Stream4");
  184. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCode::Ok);
  185. }
  186. TEST_F(ClientTest, StringEchoStreamTestRetUpdates)
  187. {
  188. TestServiceClient testClient;
  189. testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
  190. SimpleStringMessage request;
  191. QPointer<SimpleStringMessage> result(new SimpleStringMessage);
  192. request.setTestFieldString("Stream");
  193. QEventLoop waiter;
  194. testClient.subscribeTestMethodServerStreamUpdates(request, result);
  195. int i = 0;
  196. QObject::connect(result.data(), &SimpleStringMessage::testFieldStringChanged, &m_app, [&i]() {
  197. i++;
  198. });
  199. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  200. waiter.exec();
  201. ASSERT_EQ(i, 4);
  202. ASSERT_STREQ(result->testFieldString().toStdString().c_str(), "Stream4");
  203. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCode::Ok);
  204. }
  205. TEST_F(ClientTest, HugeBlobEchoStreamTest)
  206. {
  207. TestServiceClient testClient;
  208. testClient.attachChannel(std::make_shared<Http2Channel>(m_echoServerAddress, InsecureCredentials()));
  209. BlobMessage result;
  210. BlobMessage request;
  211. QFile testFile("testfile");
  212. ASSERT_TRUE(testFile.open(QFile::ReadOnly));
  213. request.setTestBytes(testFile.readAll());
  214. QByteArray dataHash = QCryptographicHash::hash(request.testBytes(), QCryptographicHash::Sha256);
  215. QEventLoop waiter;
  216. QObject::connect(&testClient, &TestServiceClient::testMethodBlobServerStreamUpdated, &m_app, [&result, &waiter](const BlobMessage &ret) {
  217. result.setTestBytes(ret.testBytes());
  218. waiter.quit();
  219. });
  220. testClient.subscribeTestMethodBlobServerStreamUpdates(request);
  221. QTimer::singleShot(20000, &waiter, &QEventLoop::quit);
  222. waiter.exec();
  223. QByteArray returnDataHash = QCryptographicHash::hash(result.testBytes(), QCryptographicHash::Sha256);
  224. ASSERT_TRUE(returnDataHash == dataHash);
  225. ASSERT_EQ(testClient.lastError(), AbstractChannel::StatusCode::Ok);
  226. }