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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
/*
Itay Marom
Cisco Systems, Inc.
*/
/*
Copyright (c) 2015-2015 Cisco Systems, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <common/gtest.h>
#include <trex_rpc_server_api.h>
#include <zmq.h>
#include <json/json.h>
#include <sstream>
using namespace std;
class RpcTest : public testing::Test {
virtual void SetUp() {
TrexRpcServerConfig cfg = TrexRpcServerConfig(TrexRpcServerConfig::RPC_PROT_TCP, 5050);
m_rpc = new TrexRpcServer(cfg);
m_rpc->start();
m_context = zmq_ctx_new ();
m_socket = zmq_socket (m_context, ZMQ_REQ);
zmq_connect (m_socket, "tcp://localhost:5050");
}
virtual void TearDown() {
m_rpc->stop();
delete m_rpc;
zmq_close(m_socket);
zmq_term(m_context);
}
public:
string send_msg(const string &msg) {
char buffer[512];
zmq_send (m_socket, msg.c_str(), msg.size(), 0);
int len = zmq_recv(m_socket, buffer, sizeof(buffer), 0);
return string(buffer, len);
}
TrexRpcServer *m_rpc;
void *m_context;
void *m_socket;
};
TEST_F(RpcTest, basic_rpc_test) {
Json::Value request;
Json::Value response;
Json::Reader reader;
string req_str;
string resp_str;
// check bad JSON format
req_str = "bad format message";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_EQ(response["jsonrpc"], "2.0");
EXPECT_EQ(response["id"], Json::Value::null);
EXPECT_EQ(response["error"]["code"], -32700);
// check bad version
req_str = "{\"jsonrpc\": \"1.5\", \"method\": \"foobar\", \"id\": \"1\"}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_EQ(response["jsonrpc"], "2.0");
EXPECT_EQ(response["id"], "1");
EXPECT_EQ(response["error"]["code"], -32600);
// no method name present
req_str = "{\"jsonrpc\": \"2.0\", \"id\": 482}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_EQ(response["jsonrpc"], "2.0");
EXPECT_EQ(response["id"], 482);
EXPECT_EQ(response["error"]["code"], -32600);
/* method does not exist */
req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"jfgldjlfds\", \"id\": 482}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_EQ(response["jsonrpc"], "2.0");
EXPECT_EQ(response["id"], 482);
EXPECT_EQ(response["error"]["code"], -32601);
/* error but as notification */
req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"jfgldjlfds\"}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_TRUE(response == Json::Value::null);
}
TEST_F(RpcTest, test_add_command) {
Json::Value request;
Json::Value response;
Json::Reader reader;
string req_str;
string resp_str;
/* simple add - missing paramters */
req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"id\": 488}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_EQ(response["jsonrpc"], "2.0");
EXPECT_EQ(response["id"], 488);
EXPECT_EQ(response["error"]["code"], -32602);
/* simple add that works */
req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"x\": 17, \"y\": -13} , \"id\": \"itay\"}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_EQ(response["jsonrpc"], "2.0");
EXPECT_EQ(response["id"], "itay");
EXPECT_EQ(response["result"], 4);
/* add with bad paratemers types */
req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"x\": \"blah\", \"y\": -13} , \"id\": 17}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_EQ(response["jsonrpc"], "2.0");
EXPECT_EQ(response["id"], 17);
EXPECT_EQ(response["error"]["code"], -32602);
/* add with invalid count of parameters */
req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"y\": -13} , \"id\": 17}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_EQ(response["jsonrpc"], "2.0");
EXPECT_EQ(response["id"], 17);
EXPECT_EQ(response["error"]["code"], -32602);
/* big numbers */
req_str = "{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"x\": 4827371, \"y\": -39181273} , \"id\": \"itay\"}";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_EQ(response["jsonrpc"], "2.0");
EXPECT_EQ(response["id"], "itay");
EXPECT_EQ(response["result"], -34353902);
}
TEST_F(RpcTest, batch_rpc_test) {
Json::Value request;
Json::Value response;
Json::Reader reader;
string req_str;
string resp_str;
req_str = "[ \
{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"x\": 22, \"y\": 17}, \"id\": \"1\"}, \
{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_sub\", \"params\": {\"x\": 22, \"y\": 17}, \"id\": \"2\"}, \
{\"jsonrpc\": \"2.0\", \"method\": \"test_rpc_add\", \"params\": {\"x\": 22, \"y\": \"itay\"}, \"id\": \"2\"}, \
{\"foo\": \"boo\"}, \
{\"jsonrpc\": \"2.0\", \"method\": \"foo.get\", \"params\": {\"name\": \"myself\"}, \"id\": \"5\"}, \
{\"jsonrpc\": \"2.0\", \"method\": \"get_data\", \"id\": \"9\"} \
]";
resp_str = send_msg(req_str);
EXPECT_TRUE(reader.parse(resp_str, response, false));
EXPECT_TRUE(response.isArray());
// message 1
EXPECT_TRUE(response[0]["jsonrpc"] == "2.0");
EXPECT_TRUE(response[0]["id"] == "1");
EXPECT_TRUE(response[0]["result"] == 39);
// message 2
EXPECT_TRUE(response[1]["jsonrpc"] == "2.0");
EXPECT_TRUE(response[1]["id"] == "2");
EXPECT_TRUE(response[1]["result"] == 5);
// message 3
EXPECT_TRUE(response[2]["jsonrpc"] == "2.0");
EXPECT_TRUE(response[2]["id"] == "2");
EXPECT_TRUE(response[2]["error"]["code"] == -32602);
// message 4
EXPECT_TRUE(response[3] == Json::Value::null);
return;
}
|