~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to lib/erl_interface/test/ei_connect_SUITE_data/ei_connect_test.c

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2010-03-09 17:34:57 UTC
  • mfrom: (10.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100309173457-4yd6hlcb2osfhx31
Tags: 1:13.b.4-dfsg-3
Manpages in section 1 are needed even if only arch-dependent packages are
built. So, re-enabled them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * %CopyrightBegin%
 
3
 * 
 
4
 * Copyright Ericsson AB 2001-2009. All Rights Reserved.
 
5
 * 
 
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/.
 
11
 * 
 
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
 
15
 * under the License.
 
16
 * 
 
17
 * %CopyrightEnd%
 
18
 */
 
19
 
 
20
/*
 
21
 * Purpose: Tests the functions in ei_connect.c.
 
22
 * Author: Bjorn Gustavsson (rewritten somewhat by Jakob Cederlund)
 
23
 *
 
24
 * See the ei_connect_SUITE.erl file for a "table of contents".
 
25
 */
 
26
 
 
27
#include <stdio.h>
 
28
#include <string.h>
 
29
#ifdef VXWORKS
 
30
#include "reclaim.h"
 
31
#endif
 
32
 
 
33
#include "ei_runner.h"
 
34
 
 
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_send_funs(char* buf, int len);
 
39
static void cmd_ei_reg_send(char* buf, int len);
 
40
static void cmd_ei_rpc(char* buf, int len);
 
41
static void cmd_ei_set_get_tracelevel(char* buf, int len);
 
42
 
 
43
static void send_errno_result(int value);
 
44
 
 
45
ei_cnode ec;
 
46
 
 
47
 
 
48
static struct {
 
49
    char* name;
 
50
    int num_args;               /* Number of arguments. */
 
51
    void (*func)(char* buf, int len);
 
52
} commands[] = {
 
53
    "ei_connect_init",       3, cmd_ei_connect_init,
 
54
    "ei_connect",            1, cmd_ei_connect,
 
55
    "ei_send",               3, cmd_ei_send,
 
56
    "ei_send_funs",          3, cmd_ei_send_funs,
 
57
    "ei_reg_send",           3, cmd_ei_reg_send,
 
58
    "ei_rpc",                4, cmd_ei_rpc,
 
59
    "ei_set_get_tracelevel", 1, cmd_ei_set_get_tracelevel,
 
60
};
 
61
 
 
62
 
 
63
/*
 
64
 * Sends a list contaning all data types to the Erlang side.
 
65
 */
 
66
 
 
67
TESTCASE(interpret)
 
68
{
 
69
    ei_x_buff x;
 
70
    int i;
 
71
    ei_term term;
 
72
 
 
73
    ei_x_new(&x);
 
74
    for (;;) {
 
75
        if (get_bin_term(&x, &term)) {
 
76
            report(1);
 
77
            return;
 
78
        } else {
 
79
            char* buf = x.buff, func[MAXATOMLEN];
 
80
            int index = x.index, arity;
 
81
            if (term.ei_type != ERL_SMALL_TUPLE_EXT || term.arity != 2)
 
82
                fail("term should be a tuple of size 2");
 
83
            if (ei_decode_atom(buf, &index, func) < 0)
 
84
                fail("function name should be an atom");
 
85
            if (ei_decode_tuple_header(buf, &index, &arity) != 0)
 
86
                fail("function arguments should be a tuple");
 
87
            for (i = 0; i < sizeof(commands)/sizeof(commands[0]); i++) {
 
88
                if (strcmp(func, commands[i].name) == 0) {
 
89
                    if (arity != commands[i].num_args)
 
90
                        fail("wrong number of arguments");
 
91
                    commands[i].func(buf + index, x.buffsz - index);
 
92
                    break;
 
93
                }
 
94
            }
 
95
            if (i >= sizeof(commands)/sizeof(commands[0])) {
 
96
                message("\"%d\" \n", func);
 
97
                fail("bad command");
 
98
            }
 
99
        }
 
100
    }   
 
101
}
 
102
 
 
103
 
 
104
static void cmd_ei_connect_init(char* buf, int len)
 
105
{
 
106
    int index = 0, r = 0;
 
107
    int type, size;
 
108
    long l;
 
109
    char b[100];
 
110
    char cookie[MAXATOMLEN], * cp = cookie;
 
111
    ei_x_buff res;
 
112
    if (ei_decode_long(buf, &index, &l) < 0)
 
113
        fail("expected int");
 
114
    sprintf(b, "c%d", l);
 
115
    /* FIXME don't use internal and maybe use skip?! */
 
116
    ei_get_type_internal(buf, &index, &type, &size);
 
117
    if (ei_decode_atom(buf, &index, cookie) < 0)
 
118
        fail("expected atom (cookie)");
 
119
    if (cookie[0] == '\0')
 
120
        cp = NULL;
 
121
    r = ei_connect_init(&ec, b, cp, 0);
 
122
    ei_x_new_with_version(&res);
 
123
    ei_x_encode_long(&res, r);
 
124
    send_bin_term(&res);
 
125
    ei_x_free(&res);
 
126
}
 
127
 
 
128
static void cmd_ei_connect(char* buf, int len)
 
129
{
 
130
    int index = 0;
 
131
    char node[256];
 
132
    int i;
 
133
    if (ei_decode_atom(buf, &index, node) < 0)
 
134
        fail("expected atom");
 
135
    i=ei_connect(&ec, node);
 
136
#ifdef VXWORKS
 
137
    if(i >= 0) {
 
138
        save_fd(i);
 
139
    }
 
140
#endif
 
141
    send_errno_result(i);
 
142
}
 
143
 
 
144
static void cmd_ei_set_get_tracelevel(char* buf, int len)
 
145
{
 
146
    int  index = 0;
 
147
    long level = 0;
 
148
    long ret   = 0;
 
149
    ei_x_buff x;
 
150
 
 
151
    if (ei_decode_long(buf, &index, &level) < 0) {
 
152
        fail("expected long");
 
153
    }
 
154
    
 
155
    ei_set_tracelevel((int)level);
 
156
 
 
157
    ret = (long) ei_get_tracelevel();
 
158
 
 
159
    ei_x_new_with_version(&x);
 
160
    ei_x_encode_tuple_header(&x, 2);
 
161
    ei_x_encode_atom(&x, "tracelevel");
 
162
    ei_x_encode_long(&x, ret);
 
163
    send_bin_term(&x);
 
164
    ei_x_free(&x);
 
165
}
 
166
 
 
167
static void cmd_ei_send(char* buf, int len)
 
168
{
 
169
    int index = 0;
 
170
    long fd;
 
171
    erlang_pid pid;
 
172
    ei_x_buff x;
 
173
 
 
174
    if (ei_decode_long(buf, &index, &fd) < 0)
 
175
        fail("expected long");
 
176
    if (ei_decode_pid(buf, &index, &pid) < 0)
 
177
        fail("expected pid (node)");
 
178
    if (ei_x_new_with_version(&x) < 0)
 
179
        fail("ei_x_new_with_version");
 
180
    if (ei_x_append_buf(&x, &buf[index], len - index) < 0)
 
181
        fail("append");
 
182
    send_errno_result(ei_send(fd, &pid, x.buff, x.index));
 
183
    ei_x_free(&x);
 
184
}
 
185
 
 
186
static void cmd_ei_send_funs(char* buf, int len)
 
187
{
 
188
    int index = 0, n;
 
189
    long fd;
 
190
    erlang_pid pid;
 
191
    ei_x_buff x;
 
192
    erlang_fun fun1, fun2;
 
193
 
 
194
    if (ei_decode_long(buf, &index, &fd) < 0)
 
195
        fail("expected long");
 
196
    if (ei_decode_pid(buf, &index, &pid) < 0)
 
197
        fail("expected pid (node)");
 
198
    if (ei_decode_tuple_header(buf, &index, &n) < 0)
 
199
        fail("expected tuple");
 
200
    if (n != 2)
 
201
        fail("expected tuple");
 
202
    if (ei_decode_fun(buf, &index, &fun1) < 0)
 
203
        fail("expected Fun1");
 
204
    if (ei_decode_fun(buf, &index, &fun2) < 0)
 
205
        fail("expected Fun2");
 
206
    if (ei_x_new_with_version(&x) < 0)
 
207
        fail("ei_x_new_with_version");
 
208
    if (ei_x_encode_tuple_header(&x, 2) < 0)
 
209
        fail("encode tuple header");
 
210
    if (ei_x_encode_fun(&x, &fun1) < 0)
 
211
        fail("encode fun1");
 
212
    if (ei_x_encode_fun(&x, &fun2) < 0)
 
213
        fail("encode fun2");
 
214
    free_fun(&fun1);
 
215
    free_fun(&fun2);
 
216
    send_errno_result(ei_send(fd, &pid, x.buff, x.index));
 
217
    ei_x_free(&x);
 
218
}
 
219
 
 
220
static void cmd_ei_reg_send(char* buf, int len)
 
221
{
 
222
    int index = 0;
 
223
    long fd;
 
224
    char reg_name[MAXATOMLEN];
 
225
    erlang_pid pid;
 
226
    ei_x_buff x;
 
227
    
 
228
    if (ei_decode_long(buf, &index, &fd) < 0)
 
229
        fail("expected long (fd)");
 
230
    if (ei_decode_atom(buf, &index, reg_name) < 0)
 
231
        fail("expected atom (reg name)");
 
232
    if (ei_x_new_with_version(&x) < 0)
 
233
        fail("ei_x_new_with_version");
 
234
    if (ei_x_append_buf(&x, &buf[index], len - index) < 0)
 
235
        fail("append");
 
236
    send_errno_result(ei_reg_send(&ec, fd,
 
237
                                  reg_name, x.buff, x.index));
 
238
    ei_x_free(&x);
 
239
}
 
240
 
 
241
static void cmd_ei_rpc(char* buf, int len)
 
242
{
 
243
    int index = 0, n;
 
244
    long fd;
 
245
    erlang_pid pid;
 
246
    ei_x_buff x, rpc_x;
 
247
    int r;
 
248
    char mod[MAXATOMLEN], func[MAXATOMLEN];
 
249
 
 
250
#if 0 && defined(__WIN32__) 
 
251
    DebugBreak();
 
252
#endif
 
253
 
 
254
    if (ei_decode_long(buf, &index, &fd) < 0)
 
255
        fail("expected long");
 
256
    if (ei_decode_pid(buf, &index, &pid) < 0)
 
257
        fail("expected pid (node)");
 
258
    if (ei_decode_tuple_header(buf, &index, &n) < 0 && n < 2)
 
259
        fail("expected tuple {module, function}");
 
260
    if (ei_decode_atom(buf, &index, mod) < 0)
 
261
        fail("expected atom (module)");
 
262
    if (ei_decode_atom(buf, &index, func) < 0)
 
263
        fail("expected atom (function)");
 
264
    message("pid %s %d %d %d\n", pid.node, pid.num, pid.serial, pid.creation);
 
265
    message("{%s, %s}\n", mod, func);
 
266
    if (ei_x_new(&rpc_x) < 0)
 
267
        fail("ei_x_new");
 
268
    if (ei_rpc(&ec, fd, mod, func, &buf[index], len - index, &rpc_x) < 0)
 
269
        fail("ei_rpc");
 
270
    if (ei_x_new_with_version(&x) < 0)
 
271
        fail("ei_x_new_with_version");
 
272
    if (ei_x_append(&x, &rpc_x) < 0)
 
273
        fail("append");
 
274
    send_bin_term(&x);
 
275
    /*send_errno_result(ei_send(&ec, fd, &pid, x.buff, x.index));*/
 
276
    ei_x_free(&x);
 
277
    ei_x_free(&rpc_x);
 
278
}
 
279
 
 
280
static void send_errno_result(int value)
 
281
{
 
282
    ei_x_buff x;
 
283
    ei_x_new_with_version(&x);
 
284
    ei_x_encode_tuple_header(&x, 2);
 
285
    ei_x_encode_long(&x, value);
 
286
    ei_x_encode_long(&x, erl_errno);
 
287
    send_bin_term(&x);
 
288
    ei_x_free(&x);
 
289
}