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