xref: /spdk/test/rpc_client/rpc_client_test.c (revision 8fb123553a690dca615baec02990aa0dc2c655d0)
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 	spdk_jsonrpc_client_free_request(request);
76 
77 	rc = spdk_jsonrpc_client_recv_response(client);
78 	if (rc) {
79 		goto out;
80 	}
81 
82 	json_resp = spdk_jsonrpc_client_get_response(client);
83 	if (json_resp == NULL) {
84 		rc = -errno;
85 		goto out;
86 
87 	}
88 
89 	/* Check for error response */
90 	if (json_resp->error != NULL) {
91 		rc = -1;
92 		goto out;
93 	}
94 
95 	assert(json_resp->result);
96 
97 	rc = get_jsonrpc_method_json_parser(&resp, json_resp->result);
98 	if (rc) {
99 		goto out;
100 	}
101 
102 	for (i = 0; i < (int)resp.method_num; i++) {
103 		if (strcmp(method_name, resp.method_names[i]) == 0) {
104 			rc = 0;
105 			goto out;
106 		}
107 	}
108 
109 	rc = -1;
110 
111 out:
112 	for (i = 0; i < (int)resp.method_num; i++) {
113 		SPDK_NOTICELOG("%s\n", resp.method_names[i]);
114 		free(resp.method_names[i]);
115 	}
116 
117 	spdk_jsonrpc_client_free_response(json_resp);
118 	return rc;
119 }
120 
121 int main(int argc, char **argv)
122 {
123 	struct spdk_jsonrpc_client *client;
124 	int rc;
125 	char *method_name = "get_rpc_methods";
126 
127 	client = spdk_jsonrpc_client_connect(g_rpcsock_addr, g_addr_family);
128 	if (!client) {
129 		return EXIT_FAILURE;
130 	}
131 
132 	rc = spdk_jsonrpc_client_check_rpc_method(client, method_name);
133 
134 	spdk_jsonrpc_client_close(client);
135 
136 	return rc ? EXIT_FAILURE : 0;
137 }
138