4
* Copyright Ericsson AB 2001-2011. All Rights Reserved.
6
* The contents of this file are subject to the Erlang Public License,
7
* Version 1.1, (the "License"); you may not use this file except in
8
* compliance with the License. You should have received a copy of the
9
* Erlang Public License along with this software. If not, it can be
10
* retrieved online at http://www.erlang.org/.
12
* Software distributed under the License is distributed on an "AS IS"
13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
* the License for the specific language governing rights and limitations
21
* Purpose: Tests the functions in ei_connect.c.
22
* Author: Bjorn Gustavsson (rewritten somewhat by Jakob Cederlund)
24
* See the ei_connect_SUITE.erl file for a "table of contents".
33
#include "ei_runner.h"
35
static void cmd_ei_connect_init(char* buf, int len);
36
static void cmd_ei_connect(char* buf, int len);
37
static void cmd_ei_send(char* buf, int len);
38
static void cmd_ei_format_pid(char* buf, int len);
39
static void cmd_ei_send_funs(char* buf, int len);
40
static void cmd_ei_reg_send(char* buf, int len);
41
static void cmd_ei_rpc(char* buf, int len);
42
static void cmd_ei_set_get_tracelevel(char* buf, int len);
44
static void send_errno_result(int value);
51
int num_args; /* Number of arguments. */
52
void (*func)(char* buf, int len);
54
"ei_connect_init", 3, cmd_ei_connect_init,
55
"ei_connect", 1, cmd_ei_connect,
56
"ei_send", 3, cmd_ei_send,
57
"ei_send_funs", 3, cmd_ei_send_funs,
58
"ei_reg_send", 3, cmd_ei_reg_send,
59
"ei_rpc", 4, cmd_ei_rpc,
60
"ei_set_get_tracelevel", 1, cmd_ei_set_get_tracelevel,
61
"ei_format_pid", 2, cmd_ei_format_pid,
66
* Sends a list contaning all data types to the Erlang side.
77
if (get_bin_term(&x, &term)) {
81
char* buf = x.buff, func[MAXATOMLEN];
82
int index = x.index, arity;
83
if (term.ei_type != ERL_SMALL_TUPLE_EXT || term.arity != 2)
84
fail("term should be a tuple of size 2");
85
if (ei_decode_atom(buf, &index, func) < 0)
86
fail("function name should be an atom");
87
if (ei_decode_tuple_header(buf, &index, &arity) != 0)
88
fail("function arguments should be a tuple");
89
for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++) {
90
if (strcmp(func, commands[i].name) == 0) {
91
if (arity != commands[i].num_args)
92
fail("wrong number of arguments");
93
commands[i].func(buf + index, x.buffsz - index);
97
if (i >= sizeof(commands)/sizeof(commands[0])) {
98
message("\"%d\" \n", func);
106
static void cmd_ei_connect_init(char* buf, int len)
108
int index = 0, r = 0;
112
char cookie[MAXATOMLEN], * cp = cookie;
114
if (ei_decode_long(buf, &index, &l) < 0)
115
fail("expected int");
116
sprintf(b, "c%ld", l);
117
/* FIXME don't use internal and maybe use skip?! */
118
ei_get_type_internal(buf, &index, &type, &size);
119
if (ei_decode_atom(buf, &index, cookie) < 0)
120
fail("expected atom (cookie)");
121
if (cookie[0] == '\0')
123
r = ei_connect_init(&ec, b, cp, 0);
124
ei_x_new_with_version(&res);
125
ei_x_encode_long(&res, r);
130
static void cmd_ei_connect(char* buf, int len)
135
if (ei_decode_atom(buf, &index, node) < 0)
136
fail("expected atom");
137
i=ei_connect(&ec, node);
143
send_errno_result(i);
146
static void cmd_ei_set_get_tracelevel(char* buf, int len)
153
if (ei_decode_long(buf, &index, &level) < 0) {
154
fail("expected long");
157
ei_set_tracelevel((int)level);
159
ret = (long) ei_get_tracelevel();
161
ei_x_new_with_version(&x);
162
ei_x_encode_tuple_header(&x, 2);
163
ei_x_encode_atom(&x, "tracelevel");
164
ei_x_encode_long(&x, ret);
169
static void cmd_ei_send(char* buf, int len)
176
if (ei_decode_long(buf, &index, &fd) < 0)
177
fail("expected long");
178
if (ei_decode_pid(buf, &index, &pid) < 0)
179
fail("expected pid (node)");
180
if (ei_x_new_with_version(&x) < 0)
181
fail("ei_x_new_with_version");
182
if (ei_x_append_buf(&x, &buf[index], len - index) < 0)
184
send_errno_result(ei_send(fd, &pid, x.buff, x.index));
188
static void cmd_ei_format_pid(char* buf, int len)
195
if (ei_decode_long(buf, &index, &fd) < 0)
196
fail("expected long");
197
if (ei_decode_pid(buf, &index, &pid) < 0)
198
fail("expected pid (node)");
199
if (ei_x_new_with_version(&x) < 0)
200
fail("ei_x_new_with_version");
201
if (ei_x_format_wo_ver(&x, "~p", &pid) < 0)
202
fail("ei_x_format_wo_ver");
203
send_errno_result(ei_send(fd, &pid, x.buff, x.index));
207
static void cmd_ei_send_funs(char* buf, int len)
213
erlang_fun fun1, fun2;
215
if (ei_decode_long(buf, &index, &fd) < 0)
216
fail("expected long");
217
if (ei_decode_pid(buf, &index, &pid) < 0)
218
fail("expected pid (node)");
219
if (ei_decode_tuple_header(buf, &index, &n) < 0)
220
fail("expected tuple");
222
fail("expected tuple");
223
if (ei_decode_fun(buf, &index, &fun1) < 0)
224
fail("expected Fun1");
225
if (ei_decode_fun(buf, &index, &fun2) < 0)
226
fail("expected Fun2");
227
if (ei_x_new_with_version(&x) < 0)
228
fail("ei_x_new_with_version");
229
if (ei_x_encode_tuple_header(&x, 2) < 0)
230
fail("encode tuple header");
231
if (ei_x_encode_fun(&x, &fun1) < 0)
233
if (ei_x_encode_fun(&x, &fun2) < 0)
237
send_errno_result(ei_send(fd, &pid, x.buff, x.index));
241
static void cmd_ei_reg_send(char* buf, int len)
245
char reg_name[MAXATOMLEN];
249
if (ei_decode_long(buf, &index, &fd) < 0)
250
fail("expected long (fd)");
251
if (ei_decode_atom(buf, &index, reg_name) < 0)
252
fail("expected atom (reg name)");
253
if (ei_x_new_with_version(&x) < 0)
254
fail("ei_x_new_with_version");
255
if (ei_x_append_buf(&x, &buf[index], len - index) < 0)
257
send_errno_result(ei_reg_send(&ec, fd,
258
reg_name, x.buff, x.index));
262
static void cmd_ei_rpc(char* buf, int len)
269
char mod[MAXATOMLEN], func[MAXATOMLEN];
271
#if 0 && defined(__WIN32__)
275
if (ei_decode_long(buf, &index, &fd) < 0)
276
fail("expected long");
277
if (ei_decode_pid(buf, &index, &pid) < 0)
278
fail("expected pid (node)");
279
if (ei_decode_tuple_header(buf, &index, &n) < 0 && n < 2)
280
fail("expected tuple {module, function}");
281
if (ei_decode_atom(buf, &index, mod) < 0)
282
fail("expected atom (module)");
283
if (ei_decode_atom(buf, &index, func) < 0)
284
fail("expected atom (function)");
285
message("pid %s %d %d %d\n", pid.node, pid.num, pid.serial, pid.creation);
286
message("{%s, %s}\n", mod, func);
287
if (ei_x_new(&rpc_x) < 0)
289
if (ei_rpc(&ec, fd, mod, func, &buf[index], len - index, &rpc_x) < 0)
291
if (ei_x_new_with_version(&x) < 0)
292
fail("ei_x_new_with_version");
293
if (ei_x_append(&x, &rpc_x) < 0)
296
/*send_errno_result(ei_send(&ec, fd, &pid, x.buff, x.index));*/
301
static void send_errno_result(int value)
304
ei_x_new_with_version(&x);
305
ei_x_encode_tuple_header(&x, 2);
306
ei_x_encode_long(&x, value);
307
ei_x_encode_long(&x, erl_errno);