xref: /spdk/test/rpc_client/rpc_client_test.c (revision 2f557958d0762ad00e068f997e2d25a205ded4b7)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 #include "spdk/event.h"
36 #include "spdk/jsonrpc.h"
37 
38 #define RPC_MAX_METHODS 200
39 
40 static const char *g_rpcsock_addr = SPDK_DEFAULT_RPC_ADDR;
41 static int g_addr_family = AF_UNIX;
42 
43 #define RPC_MAX_METHODS 200
44 
45 struct get_jsonrpc_methods_resp {
46 	char *method_names[RPC_MAX_METHODS];
47 	size_t method_num;
48 };
49 
50 static int
51 get_jsonrpc_method_json_parser(struct get_jsonrpc_methods_resp *resp,
52 			       const struct spdk_json_val *result)
53 {
54 	return spdk_json_decode_array(result, spdk_json_decode_string, resp->method_names,
55 				      RPC_MAX_METHODS, &resp->method_num, sizeof(char *));
56 }
57 
58 static int
59 spdk_jsonrpc_client_check_rpc_method(struct spdk_jsonrpc_client *client, char *method_name)
60 {
61 	int rc, i;
62 	struct spdk_jsonrpc_client_response *json_resp = NULL;
63 	struct get_jsonrpc_methods_resp resp = {};
64 	struct spdk_json_write_ctx *w;
65 	struct spdk_jsonrpc_client_request *request;
66 
67 	request = spdk_jsonrpc_client_create_request();
68 	if (request == NULL) {
69 		return -ENOMEM;
70 	}
71 
72 	w = spdk_jsonrpc_begin_request(request, 1, "get_rpc_methods");
73 	spdk_jsonrpc_end_request(request, w);
74 	spdk_jsonrpc_client_send_request(client, request);
75 
76 	rc = spdk_jsonrpc_client_recv_response(client);
77 	if (rc) {
78 		goto out;
79 	}
80 
81 	json_resp = spdk_jsonrpc_client_get_response(client);
82 	if (json_resp == NULL) {
83 		rc = -errno;
84 		goto out;
85 
86 	}
87 
88 	/* Check for error response */
89 	if (json_resp->error != NULL) {
90 		rc = -1;
91 		goto out;
92 	}
93 
94 	assert(json_resp->result);
95 
96 	rc = get_jsonrpc_method_json_parser(&resp, json_resp->result);
97 	if (rc) {
98 		goto out;
99 	}
100 
101 	for (i = 0; i < (int)resp.method_num; i++) {
102 		if (strcmp(method_name, resp.method_names[i]) == 0) {
103 			rc = 0;
104 			goto out;
105 		}
106 	}
107 
108 	rc = -1;
109 
110 out:
111 	for (i = 0; i < (int)resp.method_num; i++) {
112 		SPDK_NOTICELOG("%s\n", resp.method_names[i]);
113 		free(resp.method_names[i]);
114 	}
115 
116 	spdk_jsonrpc_client_free_response(json_resp);
117 	return rc;
118 }
119 
120 int main(int argc, char **argv)
121 {
122 	struct spdk_jsonrpc_client *client;
123 	int rc;
124 	char *method_name = "get_rpc_methods";
125 
126 	client = spdk_jsonrpc_client_connect(g_rpcsock_addr, g_addr_family);
127 	if (!client) {
128 		return EXIT_FAILURE;
129 	}
130 
131 	rc = spdk_jsonrpc_client_check_rpc_method(client, method_name);
132 
133 	spdk_jsonrpc_client_close(client);
134 
135 	return rc ? EXIT_FAILURE : 0;
136 }
137