1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Baidu RPC - A framework to host and access services throughout Baidu.
// Copyright (c) 2014 Baidu, Inc.
// Date: Sun Jul 13 15:04:18 CST 2014
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <gtest/gtest.h>
#include <gflags/gflags.h>
#include <gperftools/profiler.h>
#include <google/protobuf/descriptor.h>
#include "base/time.h"
#include "base/macros.h"
#include "brpc/socket.h"
#include "brpc/policy/most_common_message.h"
#include "brpc/controller.h"
#include "brpc/esp_message.h"
#include "brpc/policy/esp_protocol.h"
#include "brpc/policy/esp_authenticator.h"
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true);
return RUN_ALL_TESTS();
}
namespace {
void* RunClosure(void* arg) {
google::protobuf::Closure* done = (google::protobuf::Closure*)arg;
done->Run();
return NULL;
}
static const std::string EXP_REQUEST = "hello";
static const std::string EXP_RESPONSE = "world";
static const int STUB = 2;
static const int MSG_ID = 123456;
static const int MSG = 0;
static const int WRONG_MSG = 1;
class EspTest : public ::testing::Test{
protected:
EspTest() {
EXPECT_EQ(0, pipe(_pipe_fds));
brpc::SocketId id;
brpc::SocketOptions options;
options.fd = _pipe_fds[1];
EXPECT_EQ(0, brpc::Socket::Create(options, &id));
EXPECT_EQ(0, brpc::Socket::Address(id, &_socket));
};
virtual ~EspTest() {};
virtual void SetUp() {};
virtual void TearDown() {};
void WriteResponse(brpc::Controller& cntl, int msg) {
brpc::EspMessage req;
req.head.to.stub = STUB;
req.head.msg = msg;
req.head.msg_id = MSG_ID;
req.body.append(EXP_RESPONSE);
base::IOBuf req_buf;
brpc::policy::SerializeEspRequest(&req_buf, &cntl, &req);
base::IOBuf packet_buf;
brpc::policy::PackEspRequest(&packet_buf, NULL, cntl.call_id().value, NULL, &cntl, req_buf, NULL);
packet_buf.cut_into_file_descriptor(_pipe_fds[1], packet_buf.size());
}
int _pipe_fds[2];
brpc::SocketUniquePtr _socket;
};
TEST_F(EspTest, complete_flow) {
brpc::EspMessage req;
brpc::EspMessage res;
req.head.to.stub = STUB;
req.head.msg = MSG;
req.head.msg_id = MSG_ID;
req.body.append(EXP_REQUEST);
base::IOBuf req_buf;
brpc::Controller cntl;
cntl._response = &res;
ASSERT_EQ(0, brpc::Socket::Address(_socket->id(), &cntl._current_call.sending_sock));
brpc::policy::SerializeEspRequest(&req_buf, &cntl, &req);
ASSERT_FALSE(cntl.Failed());
ASSERT_EQ(sizeof(req.head) + req.body.size(), req_buf.size());
const brpc::Authenticator* auth = brpc::policy::global_esp_authenticator();
base::IOBuf packet_buf;
brpc::policy::PackEspRequest(&packet_buf, NULL, cntl.call_id().value, NULL, &cntl, req_buf, auth);
std::string auth_str;
auth->GenerateCredential(&auth_str);
ASSERT_FALSE(cntl.Failed());
ASSERT_EQ(req_buf.size() + auth_str.size(), packet_buf.size());
WriteResponse(cntl, MSG);
base::IOPortal response_buf;
response_buf.append_from_file_descriptor(_pipe_fds[0], 1024);
brpc::ParseResult res_pr =
brpc::policy::ParseEspMessage(&response_buf, NULL, false, NULL);
ASSERT_EQ(brpc::PARSE_OK, res_pr.error());
brpc::InputMessageBase* res_msg = res_pr.message();
_socket->ReAddress(&res_msg->_socket);
brpc::policy::ProcessEspResponse(res_msg);
ASSERT_FALSE(cntl.Failed());
ASSERT_EQ(EXP_RESPONSE, res.body.to_string());
}
TEST_F(EspTest, wrong_response_head) {
brpc::EspMessage res;
brpc::Controller cntl;
cntl._response = &res;
ASSERT_EQ(0, brpc::Socket::Address(_socket->id(), &cntl._current_call.sending_sock));
WriteResponse(cntl, WRONG_MSG);
base::IOPortal response_buf;
response_buf.append_from_file_descriptor(_pipe_fds[0], 1024);
brpc::ParseResult res_pr =
brpc::policy::ParseEspMessage(&response_buf, NULL, false, NULL);
ASSERT_EQ(brpc::PARSE_OK, res_pr.error());
brpc::InputMessageBase* res_msg = res_pr.message();
_socket->ReAddress(&res_msg->_socket);
brpc::policy::ProcessEspResponse(res_msg);
ASSERT_TRUE(cntl.Failed());
}
} //namespace