2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
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.
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.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
28 #include "testutils.h"
29 #include "glib-util.h"
30 #include "ipc-binary.h"
38 MY_PROTO_HOSTNAME = 1,
44 ipc_binary_proto_t *proto;
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.
54 test_sync_child(gpointer d)
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;
62 g_usleep(G_USEC_PER_SEC / 8);
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;
69 g_usleep(G_USEC_PER_SEC / 8);
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;
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;
80 return GINT_TO_POINTER(1);
84 test_sync_parent(ipc_binary_proto_t *proto, int fd)
86 ipc_binary_channel_t *chan;
87 ipc_binary_message_t *msg;
89 chan = ipc_binary_new_channel(proto);
90 tu_dbg("parent: created channel\n");
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);
98 if (msg->args[MY_PROTO_HOSTNAME].data == NULL) {
99 tu_dbg("got NULL hostname\n");
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);
106 if (msg->args[MY_PROTO_DISK].data != NULL) {
107 tu_dbg("got non-NULL disk\n");
110 ipc_binary_free_message(msg);
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);
118 if (msg->args[MY_PROTO_HOSTNAME].data == NULL) {
119 tu_dbg("got NULL hostname\n");
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);
126 if (msg->args[MY_PROTO_DISK].data == NULL) {
127 tu_dbg("got NULL disk\n");
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);
134 ipc_binary_free_message(msg);
136 g_usleep(G_USEC_PER_SEC / 8);
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);
144 if (msg->args[MY_PROTO_DATA].data == NULL) {
145 tu_dbg("got NULL data\n");
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);
152 if (0 != strncmp((gchar *)msg->args[MY_PROTO_DATA].data, "some-data", 9)) {
153 tu_dbg("got bad data\n");
156 ipc_binary_free_message(msg);
166 ipc_binary_proto_t *proto;
167 ipc_binary_cmd_t *cmd;
168 struct proto_and_fd data;
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);
183 /* start the child thread */
186 child = g_thread_create(test_sync_child, &data, TRUE, NULL);
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) ? TRUE : FALSE;
194 main(int argc, char **argv)
196 static TestUtilsTest tests[] = {
197 TU_TEST(test_sync, 60),
202 return testutils_run_tests(argc, argv, tests);