brpc_esp_protocol_unittest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // brpc - A framework to host and access services throughout Baidu.
  18. // Date: Sun Jul 13 15:04:18 CST 2014
  19. #include <sys/ioctl.h>
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <gtest/gtest.h>
  23. #include <gflags/gflags.h>
  24. #include <google/protobuf/descriptor.h>
  25. #include "butil/time.h"
  26. #include "butil/macros.h"
  27. #include "brpc/socket.h"
  28. #include "brpc/policy/most_common_message.h"
  29. #include "brpc/controller.h"
  30. #include "brpc/esp_message.h"
  31. #include "brpc/policy/esp_protocol.h"
  32. #include "brpc/policy/esp_authenticator.h"
  33. int main(int argc, char* argv[]) {
  34. testing::InitGoogleTest(&argc, argv);
  35. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  36. return RUN_ALL_TESTS();
  37. }
  38. namespace {
  39. static const std::string EXP_REQUEST = "hello";
  40. static const std::string EXP_RESPONSE = "world";
  41. static const int STUB = 2;
  42. static const int MSG_ID = 123456;
  43. static const int MSG = 0;
  44. static const int WRONG_MSG = 1;
  45. class EspTest : public ::testing::Test{
  46. protected:
  47. EspTest() {
  48. EXPECT_EQ(0, pipe(_pipe_fds));
  49. brpc::SocketId id;
  50. brpc::SocketOptions options;
  51. options.fd = _pipe_fds[1];
  52. EXPECT_EQ(0, brpc::Socket::Create(options, &id));
  53. EXPECT_EQ(0, brpc::Socket::Address(id, &_socket));
  54. };
  55. virtual ~EspTest() {};
  56. virtual void SetUp() {};
  57. virtual void TearDown() {};
  58. void WriteResponse(brpc::Controller& cntl, int msg) {
  59. brpc::EspMessage req;
  60. req.head.to.stub = STUB;
  61. req.head.msg = msg;
  62. req.head.msg_id = MSG_ID;
  63. req.body.append(EXP_RESPONSE);
  64. butil::IOBuf req_buf;
  65. brpc::policy::SerializeEspRequest(&req_buf, &cntl, &req);
  66. butil::IOBuf packet_buf;
  67. brpc::policy::PackEspRequest(&packet_buf, NULL, cntl.call_id().value, NULL, &cntl, req_buf, NULL);
  68. packet_buf.cut_into_file_descriptor(_pipe_fds[1], packet_buf.size());
  69. }
  70. int _pipe_fds[2];
  71. brpc::SocketUniquePtr _socket;
  72. };
  73. TEST_F(EspTest, complete_flow) {
  74. brpc::EspMessage req;
  75. brpc::EspMessage res;
  76. req.head.to.stub = STUB;
  77. req.head.msg = MSG;
  78. req.head.msg_id = MSG_ID;
  79. req.body.append(EXP_REQUEST);
  80. butil::IOBuf req_buf;
  81. brpc::Controller cntl;
  82. cntl._response = &res;
  83. ASSERT_EQ(0, brpc::Socket::Address(_socket->id(), &cntl._current_call.sending_sock));
  84. brpc::policy::SerializeEspRequest(&req_buf, &cntl, &req);
  85. ASSERT_FALSE(cntl.Failed());
  86. ASSERT_EQ(sizeof(req.head) + req.body.size(), req_buf.size());
  87. const brpc::Authenticator* auth = brpc::policy::global_esp_authenticator();
  88. butil::IOBuf packet_buf;
  89. brpc::policy::PackEspRequest(&packet_buf, NULL, cntl.call_id().value, NULL, &cntl, req_buf, auth);
  90. std::string auth_str;
  91. auth->GenerateCredential(&auth_str);
  92. ASSERT_FALSE(cntl.Failed());
  93. ASSERT_EQ(req_buf.size() + auth_str.size(), packet_buf.size());
  94. WriteResponse(cntl, MSG);
  95. butil::IOPortal response_buf;
  96. response_buf.append_from_file_descriptor(_pipe_fds[0], 1024);
  97. brpc::ParseResult res_pr =
  98. brpc::policy::ParseEspMessage(&response_buf, NULL, false, NULL);
  99. ASSERT_EQ(brpc::PARSE_OK, res_pr.error());
  100. brpc::InputMessageBase* res_msg = res_pr.message();
  101. _socket->ReAddress(&res_msg->_socket);
  102. brpc::policy::ProcessEspResponse(res_msg);
  103. ASSERT_FALSE(cntl.Failed());
  104. ASSERT_EQ(EXP_RESPONSE, res.body.to_string());
  105. }
  106. TEST_F(EspTest, wrong_response_head) {
  107. brpc::EspMessage res;
  108. brpc::Controller cntl;
  109. cntl._response = &res;
  110. ASSERT_EQ(0, brpc::Socket::Address(_socket->id(), &cntl._current_call.sending_sock));
  111. WriteResponse(cntl, WRONG_MSG);
  112. butil::IOPortal response_buf;
  113. response_buf.append_from_file_descriptor(_pipe_fds[0], 1024);
  114. brpc::ParseResult res_pr =
  115. brpc::policy::ParseEspMessage(&response_buf, NULL, false, NULL);
  116. ASSERT_EQ(brpc::PARSE_OK, res_pr.error());
  117. brpc::InputMessageBase* res_msg = res_pr.message();
  118. _socket->ReAddress(&res_msg->_socket);
  119. brpc::policy::ProcessEspResponse(res_msg);
  120. ASSERT_TRUE(cntl.Failed());
  121. }
  122. } //namespace