sslclienttest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "testservice_grpc.qpb.h"
  26. #include <QGrpcHttp2Channel>
  27. #ifdef QT_PROTOBUF_NATIVE_GRPC_CHANNEL
  28. #include <QGrpcChannel>
  29. #endif
  30. #include <QGrpcSslCredentials>
  31. #include <QGrpcInsecureCallCredentials>
  32. #include <QGrpcInsecureChannelCredentials>
  33. #include <QTimer>
  34. #include <QFile>
  35. #include <QCryptographicHash>
  36. #include <QSslConfiguration>
  37. #include <QCoreApplication>
  38. #include <gtest/gtest.h>
  39. using namespace qtprotobufnamespace::tests;
  40. class ClientTest : public ::testing::Test
  41. {
  42. protected:
  43. static void SetUpTestCase() {
  44. qRegisterProtobufTypes();
  45. qRegisterProtobufType<SimpleStringMessage>();
  46. }
  47. static QCoreApplication m_app;
  48. static int m_argc;
  49. static QUrl m_echoServerAddress;
  50. };
  51. int ClientTest::m_argc(0);
  52. QCoreApplication ClientTest::m_app(m_argc, nullptr);
  53. TEST_F(ClientTest, Http2ChannelIncorrectSecureCredentialsTest)
  54. {
  55. //Prepare ssl configuration
  56. QSslConfiguration conf = QSslConfiguration::defaultConfiguration();
  57. conf.setProtocol(QSsl::TlsV1_2);
  58. // NOTE: CA certificate is not setup on purpose to induce the ssl handshake error
  59. // QFile certFile("cert.pem");
  60. // certFile.open(QIODevice::ReadOnly);
  61. // QByteArray cert = certFile.readAll();
  62. // conf.setCaCertificates({QSslCertificate(cert)});
  63. TestServiceClient testClient;
  64. testClient.attachChannel(std::make_shared<QGrpcHttp2Channel>(QUrl("https://localhost:60051", QUrl::StrictMode), QGrpcInsecureCallCredentials()|QGrpcSslCredentials(conf)));
  65. std::unique_ptr<SimpleStringMessage> result = std::make_unique<SimpleStringMessage>();
  66. EXPECT_FALSE(testClient.testMethod(SimpleStringMessage{"Hello Qt!"}, QPointer<SimpleStringMessage>(result.get())) == QGrpcStatus::Ok);
  67. }
  68. #ifdef QT_PROTOBUF_NATIVE_GRPC_CHANNEL
  69. TEST_F(ClientTest, GrpcHttpChannelIncorrectSecureCredentialsTest)
  70. {
  71. TestServiceClient testClient;
  72. testClient.attachChannel(std::make_shared<QGrpcChannel>(QUrl("localhost:60051"), grpc::SslCredentials(grpc::SslCredentialsOptions())));
  73. std::unique_ptr<SimpleStringMessage> result = std::make_unique<SimpleStringMessage>();
  74. EXPECT_FALSE(testClient.testMethod(SimpleStringMessage{"Hello Qt!"}, QPointer<SimpleStringMessage>(result.get())) == QGrpcStatus::Ok);
  75. }
  76. TEST_F(ClientTest, GrpcSocketChannelIncorrectSecureCredentialsTest)
  77. {
  78. TestServiceClient testClient;
  79. testClient.attachChannel(std::make_shared<QGrpcChannel>(QUrl("unix:///tmp/test.sock"), grpc::SslCredentials(grpc::SslCredentialsOptions())));
  80. std::unique_ptr<SimpleStringMessage> result = std::make_unique<SimpleStringMessage>();
  81. EXPECT_FALSE(testClient.testMethod(SimpleStringMessage{"Hello Qt!"}, QPointer<SimpleStringMessage>(result.get())) == QGrpcStatus::Ok);
  82. }
  83. #endif