Imported Upstream version 3.1.0
[debian/amanda] / common-src / ipc-binary-test.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26
27 #include "amanda.h"
28 #include "testutils.h"
29 #include "glib-util.h"
30 #include "ipc-binary.h"
31
32 enum {
33     MY_PROTO_CMD1 = 1,
34     MY_PROTO_CMD2 = 2,
35 };
36
37 enum {
38     MY_PROTO_HOSTNAME = 1,
39     MY_PROTO_DISK = 2,
40     MY_PROTO_DATA = 3,
41 };
42
43 struct proto_and_fd {
44     ipc_binary_proto_t *proto;
45     int fd;
46 };
47
48 /*
49  * Test a basic synchronous conversation between two endpoints.  Because
50  * sync is implemented in terms of the async functions, this tests all of
51  * the module's functionality.
52  */
53 static gpointer
54 test_sync_child(gpointer d)
55 {
56     struct proto_and_fd *data = (struct proto_and_fd *)d;
57     ipc_binary_proto_t *proto = data->proto;
58     ipc_binary_channel_t *chan;
59     ipc_binary_message_t *msg;
60     int fd = data->fd;
61
62     g_usleep(G_USEC_PER_SEC / 8);
63
64     chan = ipc_binary_new_channel(proto);
65     msg = ipc_binary_new_message(chan, MY_PROTO_CMD1);
66     ipc_binary_add_arg(msg, MY_PROTO_HOSTNAME, 0, "localhost", 0);
67     if (ipc_binary_write_message(chan, fd, msg) < 0) return NULL;
68
69     g_usleep(G_USEC_PER_SEC / 8);
70
71     msg = ipc_binary_new_message(chan, MY_PROTO_CMD1);
72     ipc_binary_add_arg(msg, MY_PROTO_HOSTNAME, 0, "otherhost", 0);
73     ipc_binary_add_arg(msg, MY_PROTO_DISK, 0, "/usr", 0);
74     if (ipc_binary_write_message(chan, fd, msg) < 0) return NULL;
75
76     msg = ipc_binary_new_message(chan, MY_PROTO_CMD2);
77     ipc_binary_add_arg(msg, MY_PROTO_DATA, 9, "some-data", 0);
78     if (ipc_binary_write_message(chan, fd, msg) < 0) return NULL;
79
80     return GINT_TO_POINTER(1);
81 }
82
83 static int
84 test_sync_parent(ipc_binary_proto_t *proto, int fd)
85 {
86     ipc_binary_channel_t *chan;
87     ipc_binary_message_t *msg;
88
89     chan = ipc_binary_new_channel(proto);
90     tu_dbg("parent: created channel\n");
91
92     msg = ipc_binary_read_message(chan, fd);
93     tu_dbg("parent: read message 1\n");
94     if (msg->cmd_id != MY_PROTO_CMD1) {
95         tu_dbg("got bad cmd_id %d\n", (int)msg->cmd_id);
96         return 0;
97     }
98     if (msg->args[MY_PROTO_HOSTNAME].data == NULL) {
99         tu_dbg("got NULL hostname\n");
100         return 0;
101     }
102     if (0 != strcmp((gchar *)msg->args[MY_PROTO_HOSTNAME].data, "localhost")) {
103         tu_dbg("got bad hostname %s\n", (gchar *)msg->args[MY_PROTO_HOSTNAME].data);
104         return 0;
105     }
106     if (msg->args[MY_PROTO_DISK].data != NULL) {
107         tu_dbg("got non-NULL disk\n");
108         return 0;
109     }
110     ipc_binary_free_message(msg);
111
112     msg = ipc_binary_read_message(chan, fd);
113     tu_dbg("parent: read message 2\n");
114     if (msg->cmd_id != MY_PROTO_CMD1) {
115         tu_dbg("got bad cmd_id %d\n", (int)msg->cmd_id);
116         return 0;
117     }
118     if (msg->args[MY_PROTO_HOSTNAME].data == NULL) {
119         tu_dbg("got NULL hostname\n");
120         return 0;
121     }
122     if (0 != strcmp((gchar *)msg->args[MY_PROTO_HOSTNAME].data, "otherhost")) {
123         tu_dbg("got bad hostname %s\n", (gchar *)msg->args[MY_PROTO_HOSTNAME].data);
124         return 0;
125     }
126     if (msg->args[MY_PROTO_DISK].data == NULL) {
127         tu_dbg("got NULL disk\n");
128         return 0;
129     }
130     if (0 != strcmp((gchar *)msg->args[MY_PROTO_DISK].data, "/usr")) {
131         tu_dbg("got bad disk %s\n", (gchar *)msg->args[MY_PROTO_DISK].data);
132         return 0;
133     }
134     ipc_binary_free_message(msg);
135
136     g_usleep(G_USEC_PER_SEC / 8);
137
138     msg = ipc_binary_read_message(chan, fd);
139     tu_dbg("parent: read message 3\n");
140     if (msg->cmd_id != MY_PROTO_CMD2) {
141         tu_dbg("got bad cmd_id %d\n", (int)msg->cmd_id);
142         return 0;
143     }
144     if (msg->args[MY_PROTO_DATA].data == NULL) {
145         tu_dbg("got NULL data\n");
146         return 0;
147     }
148     if (msg->args[MY_PROTO_DATA].len != 9) {
149         tu_dbg("got data length %d, expected 9\n", (int)msg->args[MY_PROTO_DATA].len);
150         return 0;
151     }
152     if (0 != strncmp((gchar *)msg->args[MY_PROTO_DATA].data, "some-data", 9)) {
153         tu_dbg("got bad data\n");
154         return 0;
155     }
156     ipc_binary_free_message(msg);
157
158     return 1;
159 }
160
161 static int
162 test_sync(void)
163 {
164     int rv;
165     int p[2];
166     ipc_binary_proto_t *proto;
167     ipc_binary_cmd_t *cmd;
168     struct proto_and_fd data;
169     GThread *child;
170
171     if (pipe(p) == -1) {
172         perror("pipe");
173         return 0;
174     }
175
176     proto = ipc_binary_proto_new(0xE10E);
177     cmd = ipc_binary_proto_add_cmd(proto, MY_PROTO_CMD1);
178     ipc_binary_cmd_add_arg(cmd, MY_PROTO_HOSTNAME, IPC_BINARY_STRING);
179     ipc_binary_cmd_add_arg(cmd, MY_PROTO_DISK, IPC_BINARY_STRING|IPC_BINARY_OPTIONAL);
180     cmd = ipc_binary_proto_add_cmd(proto, MY_PROTO_CMD2);
181     ipc_binary_cmd_add_arg(cmd, MY_PROTO_DATA, 0);
182
183     /* start the child thread */
184     data.proto = proto;
185     data.fd = p[1];
186     child = g_thread_create(test_sync_child, &data, TRUE, NULL);
187
188     /* run the parent and collect the results */
189     rv = test_sync_parent(proto, p[0]) && GPOINTER_TO_INT(g_thread_join(child));
190     return rv;
191 }
192
193 int
194 main(int argc, char **argv)
195 {
196     static TestUtilsTest tests[] = {
197         TU_TEST(test_sync, 60),
198         TU_END()
199     };
200
201     glib_init();
202     return testutils_run_tests(argc, argv, tests);
203 }