summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2015-08-13 09:13:37 +0300
committerimarom <imarom@cisco.com>2015-08-13 09:13:37 +0300
commit583ef32a82dcd52cce9c1320f5141f91e40a0056 (patch)
tree4521469aadf46f46136ccf75bd3c63a019851dc2
parent877eba6c21febeafd1122b60768c5ca5f4a69945 (diff)
just a few enums
-rw-r--r--src/gtest/rpc_test.cpp78
-rw-r--r--src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp20
-rw-r--r--src/rpc-server/src/trex_rpc_req_resp.cpp5
3 files changed, 79 insertions, 24 deletions
diff --git a/src/gtest/rpc_test.cpp b/src/gtest/rpc_test.cpp
index 59b4b189..f1510ef2 100644
--- a/src/gtest/rpc_test.cpp
+++ b/src/gtest/rpc_test.cpp
@@ -26,29 +26,80 @@ limitations under the License.
#include <json/json.h>
#include <sstream>
+using namespace std;
+
class RpcTest : public testing::Test {
virtual void SetUp() {
+ m_rpc = new TrexRpcServerArray(TrexRpcServerArray::RPC_PROT_TCP, 5050);
+ 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);
+ }
+
+ TrexRpcServerArray *m_rpc;
+ void *m_context;
+ void *m_socket;
};
TEST_F(RpcTest, basic_rpc_test) {
- TrexRpcServerArray rpc(TrexRpcServerArray::RPC_PROT_TCP, 5050);
- rpc.start();
+ Json::Value request;
+ Json::Value response;
+ Json::Reader reader;
- sleep(1);
+ 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_TRUE(response["jsonrpc"] == "2.0");
+ EXPECT_TRUE(response["id"] == Json::Value::null);
+ EXPECT_TRUE(response["error"]["code"] == -32700);
+
+ // check bad version
+ req_str = "{\"jsonrpc\": \"1.5\", \"method\": \"foobar\", \"id\": \"1\"}";
+ resp_str = send_msg(req_str);
- printf ("Connecting to hello world server…\n");
- void *context = zmq_ctx_new ();
- void *requester = zmq_socket (context, ZMQ_REQ);
- zmq_connect (requester, "tcp://localhost:5050");
+ EXPECT_TRUE(reader.parse(resp_str, response, false));
+ EXPECT_TRUE(response["jsonrpc"] == "2.0");
+ EXPECT_TRUE(response["id"] == "1");
+ EXPECT_TRUE(response["error"]["code"] == -32600);
+ // no method name present
+ req_str = "{\"jsonrpc\": \"1.5\", \"id\": 482}";
+ resp_str = send_msg(req_str);
+
+ EXPECT_TRUE(reader.parse(resp_str, response, false));
+ EXPECT_TRUE(response["jsonrpc"] == "2.0");
+ EXPECT_TRUE(response["id"] == 482);
+ EXPECT_TRUE(response["error"]["code"] == -32600);
+
+ #if 0
+
- char buffer[250];
- Json::Value request;
int id = 1;
request["jsonrpc"] = "2.0";
@@ -61,16 +112,16 @@ TEST_F(RpcTest, basic_rpc_test) {
for (int request_nbr = 0; request_nbr != 1; request_nbr++) {
//request["id"] = "itay_id";
- std::stringstream ss;
+ stringstream ss;
ss << request;
- std::cout << "Sending : '" << ss.str() << "'\n";
+ cout << "Sending : '" << ss.str() << "'\n";
zmq_send (requester, ss.str().c_str(), ss.str().size(), 0);
int len = zmq_recv (requester, buffer, 250, 0);
- std::string resp(buffer, buffer + len);
- std::cout << "Got: " << resp << "\n";
+ string resp(buffer, buffer + len);
+ cout << "Got: " << resp << "\n";
}
zmq_close (requester);
zmq_ctx_destroy (context);
@@ -78,5 +129,6 @@ TEST_F(RpcTest, basic_rpc_test) {
sleep(1);
rpc.stop();
+ #endif
}
diff --git a/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp b/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp
index d4895557..2083ab2a 100644
--- a/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp
+++ b/src/rpc-server/src/trex_rpc_jsonrpc_v2.cpp
@@ -25,6 +25,17 @@ limitations under the License.
#include <iostream>
+/**
+ * error as described in the RFC
+ */
+enum {
+ JSONRPC_V2_ERR_PARSE = -32700,
+ JSONRPC_V2_ERR_INVALID_REQ = -32600,
+ JSONRPC_V2_ERR_METHOD_NOT_FOUND = -32601,
+ JSONRPC_V2_ERR_INVALID_PARAMS = -32602,
+ JSONRPC_V2_ERR_INTERNAL_ERROR = -32603
+};
+
/* dummy command */
class DumymCommand : public TrexJsonRpcV2Command {
public:
@@ -56,7 +67,6 @@ public:
/* encode to string */
response = writer.write(response_json);
-
}
private:
@@ -77,24 +87,22 @@ TrexJsonRpcV2Command * TrexJsonRpcV2Parser::parse() {
/* basic JSON parsing */
bool rc = reader.parse(m_msg, request, false);
if (!rc) {
- return new JsonRpcError(Json::Value::null, -32700, "Bad JSON Format");
+ return new JsonRpcError(Json::Value::null, JSONRPC_V2_ERR_PARSE, "Bad JSON Format");
}
Json::Value msg_id = request["id"];
/* check version */
if (request["jsonrpc"] != "2.0") {
- return new JsonRpcError(msg_id, -32600, "Invalid JSONRPC Version");
+ return new JsonRpcError(msg_id, JSONRPC_V2_ERR_INVALID_REQ, "Invalid JSONRPC Version");
}
/* check method name */
std::string method_name = request["method"].asString();
if (method_name == "") {
- return new JsonRpcError(msg_id, -32600, "Missing Method Name");
+ return new JsonRpcError(msg_id, JSONRPC_V2_ERR_INVALID_REQ, "Missing Method Name");
}
- std::cout << "method name: " << method_name << "\n";
-
TrexJsonRpcV2Command *command = new DumymCommand();
return (command);
diff --git a/src/rpc-server/src/trex_rpc_req_resp.cpp b/src/rpc-server/src/trex_rpc_req_resp.cpp
index 5c28d90d..b423c8e7 100644
--- a/src/rpc-server/src/trex_rpc_req_resp.cpp
+++ b/src/rpc-server/src/trex_rpc_req_resp.cpp
@@ -57,8 +57,6 @@ void TrexRpcServerReqRes::_rpc_thread_cb() {
throw TrexRpcException("Unable to start ZMQ server at: " + ss.str());
}
- printf("listening on %s\n", ss.str().c_str());
-
/* server main loop */
while (m_is_running) {
int msg_size = zmq_recv (m_socket, m_msg_buffer, sizeof(m_msg_buffer), 0);
@@ -89,9 +87,6 @@ void TrexRpcServerReqRes::_stop_rpc_thread() {
void TrexRpcServerReqRes::handle_request(const std::string &request) {
std::string response;
- /* debug */
- std::cout << request << std::endl;
-
TrexJsonRpcV2Parser rpc_request(request);
TrexJsonRpcV2Command *rpc_command = rpc_request.parse();