terom@40: /** terom@40: * The main test code entry point terom@40: */ terom@40: #include "sock_test.h" terom@41: #include "line_proto.h" terom@40: #include "irc_conn.h" terom@44: #include "irc_net.h" terom@40: #include "log.h" terom@41: #include "error.h" terom@40: terom@41: #include terom@40: #include terom@73: #include terom@40: #include terom@50: #include terom@50: terom@50: #define DUMP_STR_BUF 1024 terom@50: #define DUMP_STR_COUNT 8 terom@50: #define DUMP_STR_TAIL 10 terom@50: terom@50: char *dump_str_append (char *buf, const char *str) terom@50: { terom@50: while (*str) terom@50: *buf++ = *str++; terom@50: terom@50: return buf; terom@50: } terom@50: terom@50: /** terom@50: * This re-formats the given string to escape values, and returns a pointer to an internal static buffer. terom@50: * terom@52: * If len is given as >= 0, only the given number of chars will be dumped from str. terom@50: * terom@52: * The buffer cycles a bit, so the returned pointers remain valid across DUMP_STR_COUNT calls. terom@51: * terom@52: * The resulting string is truncated to (DUMP_STR_BUF - DUMP_STR_TAIL) bytes, not including the ending "...'\0". terom@52: * terom@52: * @param str the string to dump, should be NUL-terminated unless len is given terom@52: * @param len if negative, ignored, otherwise, only this many bytes are dumped from str terom@52: * @param return a pointer to a static buffer that remains valid across DUMP_STR_COUNT calls to this function terom@50: */ terom@50: const char *dump_strn (const char *str, ssize_t len) terom@50: { terom@50: static char dump_buf[DUMP_STR_COUNT][DUMP_STR_BUF]; terom@50: static size_t dump_idx = 0; terom@50: terom@50: // pick a buffer to use terom@50: const char *str_ptr = str; terom@50: char *buf_ptr = dump_buf[dump_idx++], *buf = buf_ptr; terom@50: terom@50: // cycle terom@50: if (dump_idx >= DUMP_STR_COUNT) terom@50: dump_idx = 0; terom@50: terom@50: // NULL? terom@50: if (str == NULL) { terom@50: buf = dump_str_append(buf, "NULL"); terom@50: *buf = '\0'; terom@50: terom@50: return buf_ptr; terom@50: } terom@50: terom@50: // quote terom@50: *buf++ = '\''; terom@50: terom@50: // dump each char terom@51: for (; ( terom@51: // ...stop on NUL terom@51: *str terom@51: terom@51: // ...leave DUMP_STR_TAIL bytes room at the end of buf terom@51: && (size_t) (buf - buf_ptr) < DUMP_STR_BUF - DUMP_STR_TAIL terom@51: terom@51: // ...don't consume more than len bytes of str, unless len < 0 terom@51: && (len < 0 || (size_t) (str - str_ptr) < (size_t) len) terom@51: ); str++ terom@51: ) { terom@50: if (*str == '\'') { terom@51: // escape quotes terom@51: buf = dump_str_append(buf, "\\'"); terom@51: terom@51: } else if (*str == '\\') { terom@51: // escape escapes terom@51: buf = dump_str_append(buf, "\\\\"); terom@50: terom@50: } else if (isprint(*str)) { terom@51: // normal char terom@50: *buf++ = *str; terom@52: terom@50: } else { terom@51: // something more special terom@50: switch (*str) { terom@50: case '\r': terom@50: buf = dump_str_append(buf, "\\r"); break; terom@50: terom@50: case '\n': terom@50: buf = dump_str_append(buf, "\\n"); break; terom@50: terom@50: default: terom@51: // format as "\xFF" terom@50: buf += snprintf(buf, (DUMP_STR_BUF - (buf - buf_ptr)), "\\x%02x", *str); terom@50: break; terom@50: } terom@50: } terom@50: } terom@50: terom@51: // end quote terom@50: *buf++ = '\''; terom@50: terom@50: // overflow? terom@50: if ((size_t)(buf - buf_ptr) == DUMP_STR_BUF - DUMP_STR_TAIL) terom@50: buf = dump_str_append(buf, "..."); terom@50: terom@50: // terminate terom@50: *buf = '\0'; terom@50: terom@50: // ok terom@50: return buf_ptr; terom@50: } terom@50: terom@50: const char *dump_str (const char *str) terom@50: { terom@50: return dump_strn(str, -1); terom@50: } terom@40: terom@42: void assert_strcmp (const char *is, const char *should_be) terom@41: { terom@52: if (!is || strcmp(is, should_be)) terom@50: FATAL("%s != %s", dump_str(is), dump_str(should_be)); terom@41: } terom@41: terom@42: void assert_strncmp (const char *is, const char *should_be, size_t n) terom@50: { terom@52: if (!is || strncmp(is, should_be, n)) terom@50: FATAL("%s:%u != %s", dump_strn(is, n), (unsigned) n, dump_strn(should_be, n)); terom@41: } terom@41: terom@41: void assert_strlen (const char *str, size_t n) terom@41: { terom@52: if (!str || strlen(str) != n) terom@50: FATAL("strlen(%s) != %u", dump_str(str), (unsigned) n); terom@41: } terom@41: terom@42: void assert_strnull (const char *str) terom@41: { terom@41: if (str != NULL) terom@50: FATAL("%s != NULL", dump_str(str)); terom@41: } terom@41: terom@41: void assert_success (err_t err) terom@41: { terom@41: if (err != SUCCESS) terom@41: FATAL("error: %s", error_name(err)); terom@41: } terom@41: terom@41: void assert_err (err_t err, err_t target) terom@41: { terom@41: if (err != target) terom@42: FATAL("error: <%s> != <%s>", error_name(err), error_name(target)); terom@42: } terom@42: terom@42: void assert_error_info (struct error_info *is, struct error_info *should_be) terom@42: { terom@42: if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be)) terom@42: FATAL("error: <%s> != <%s>", error_msg(is), error_msg(should_be)); terom@41: } terom@41: terom@40: void assert_sock_read (struct sock_stream *sock, const char *str) terom@40: { terom@40: char buf[strlen(str)]; terom@40: terom@50: log_debug("read: %p: %s", sock, dump_str(str)); terom@40: terom@40: // read it terom@40: assert(sock_stream_read(sock, buf, strlen(str)) == (int) strlen(str)); terom@40: terom@40: // cmp terom@41: assert_strncmp(buf, str, strlen(str)); terom@40: } terom@40: terom@40: void assert_sock_write (struct sock_stream *sock, const char *str) terom@40: { terom@50: log_debug("write: %p: %s", sock, dump_str(str)); terom@40: terom@40: // write it terom@40: assert(sock_stream_write(sock, str, strlen(str)) == (int) strlen(str)); terom@40: } terom@40: terom@41: void assert_sock_eof (struct sock_stream *sock) terom@41: { terom@41: char buf; terom@41: terom@41: log_debug("eof: %p", sock); terom@41: terom@41: assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF); terom@41: } terom@41: terom@44: void assert_sock_data (struct sock_test *sock, const char *data) terom@43: { terom@43: // get the data out terom@43: char *buf; terom@43: size_t len; terom@43: terom@43: sock_test_get_send_data(sock, &buf, &len); terom@43: terom@50: log_debug("get_send_data: %s", dump_strn(buf, len)); terom@43: terom@43: // should be the same terom@43: assert_strncmp(buf, data, len); terom@43: assert_strlen(data, len); terom@43: terom@43: // cleanup terom@43: free(buf); terom@43: } terom@43: terom@52: /** terom@52: * Nicer name for test_sock_add_recv_str terom@52: */ terom@52: void test_sock_push (struct sock_test *sock, const char *str) terom@52: { terom@52: return sock_test_add_recv_str(sock, str); terom@52: } terom@52: terom@72: /** terom@72: * Create an empty sock_test terom@72: */ terom@72: struct sock_test* setup_sock_test (void) terom@72: { terom@72: struct sock_test *sock; terom@72: terom@72: assert ((sock = sock_test_create()) != NULL); terom@72: terom@72: return sock; terom@72: } terom@72: terom@50: void test_dump_str (void) terom@50: { terom@50: log_info("dumping example strings on stdout:"); terom@50: terom@50: log_debug("normal: %s", dump_str("Hello World")); terom@50: log_debug("escapes: %s", dump_str("foo\r\nbar\a\001")); terom@50: log_debug("length: %s", dump_strn("<-->**", 4)); terom@50: log_debug("overflow: %s", dump_str( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")); terom@50: log_debug("null: %s", dump_str(NULL)); terom@51: log_debug("quote: %s", dump_str("foo\\bar'quux")); terom@50: } terom@50: terom@40: void test_sock_test (void) terom@40: { terom@40: struct sock_test *sock = sock_test_create(); terom@40: struct io_vec _read_data[] = { terom@40: { "foo", 3 }, terom@40: { "barx", 4 } terom@40: }; terom@40: const char *_write_data = "test data"; terom@40: terom@40: // put the read data terom@40: log_debug("set_recv_buffer: %p, %d", _read_data, 2); terom@41: sock_test_set_recv_buffer(sock, _read_data, 2, true); terom@41: terom@41: // read it out terom@41: log_info("test sock_test_read"); terom@40: terom@40: assert_sock_read(SOCK_TEST_BASE(sock), "foo"); terom@40: assert_sock_read(SOCK_TEST_BASE(sock), "ba"); terom@40: assert_sock_read(SOCK_TEST_BASE(sock), "rx"); terom@41: assert_sock_eof(SOCK_TEST_BASE(sock)); terom@40: terom@40: // write the data in terom@41: log_info("test sock_test_write"); terom@41: terom@40: assert_sock_write(SOCK_TEST_BASE(sock), "test "); terom@40: assert_sock_write(SOCK_TEST_BASE(sock), "data"); terom@43: terom@43: // check output terom@44: assert_sock_data(sock, _write_data); terom@40: terom@43: // check output terom@44: assert_sock_data(sock, ""); terom@40: terom@41: // cleanup terom@41: sock_test_destroy(sock); terom@41: } terom@41: terom@41: void assert_read_line (struct line_proto *lp, const char *line_str) terom@41: { terom@41: char *line_buf; terom@41: terom@50: log_debug("expect: %s", dump_str(line_str)); terom@41: terom@41: assert_success(line_proto_recv(lp, &line_buf)); terom@41: terom@41: if (line_str) { terom@41: assert_strcmp(line_buf, line_str); terom@41: terom@41: } else { terom@42: assert_strnull(line_buf); terom@41: terom@41: } terom@41: } terom@41: terom@42: /** terom@42: * Context info for test_line_proto callbacks terom@42: */ terom@42: struct _lp_test_ctx { terom@42: /** Expected line */ terom@42: const char *line; terom@42: terom@42: /** Expected error */ terom@42: struct error_info err; terom@42: }; terom@42: terom@42: static void _lp_on_line (char *line, void *arg) terom@42: { terom@42: struct _lp_test_ctx *ctx = arg; terom@42: terom@50: log_debug("%s", dump_str(line)); terom@42: terom@42: assert_strcmp(line, ctx->line); terom@42: terom@42: ctx->line = NULL; terom@42: } terom@42: terom@42: static void _lp_on_error (struct error_info *err, void *arg) terom@42: { terom@42: struct _lp_test_ctx *ctx = arg; terom@42: terom@42: assert_error_info(err, &ctx->err); terom@42: } terom@42: terom@41: static struct line_proto_callbacks _lp_callbacks = { terom@42: .on_line = &_lp_on_line, terom@42: .on_error = &_lp_on_error, terom@41: }; terom@41: terom@41: void test_line_proto (void) terom@41: { terom@41: struct sock_test *sock = sock_test_create(); terom@41: struct io_vec _read_data[] = { terom@41: { "hello\r\n", 7 }, terom@41: { "world\n", 6 }, terom@41: { "this ", 5 }, terom@41: { "is a line\r", 10 }, terom@41: { "\nfragment", 9 }, terom@41: }, _trailing_data = { "\r\n", 2 }; terom@41: struct line_proto *lp; terom@42: struct _lp_test_ctx ctx; terom@41: struct error_info err; terom@41: terom@41: // put the read data terom@41: log_debug("set_recv_buffer: %p, %d", _read_data, 5); terom@41: sock_test_set_recv_buffer(sock, _read_data, 5, false); terom@41: terom@41: // create the lp terom@42: assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, &ctx, &err)); terom@41: terom@41: log_info("test line_proto_recv"); terom@41: terom@41: // then read some lines from it terom@41: assert_read_line(lp, "hello"); terom@41: assert_read_line(lp, "world"); terom@41: assert_read_line(lp, "this is a line"); terom@41: assert_read_line(lp, NULL); terom@41: terom@42: // then add a final bit to trigger on_line terom@42: log_info("test on_line"); terom@42: terom@42: ctx.line = "fragment"; terom@41: sock_test_add_recv_vec(sock, _trailing_data); terom@42: assert_strnull(ctx.line); terom@41: terom@43: // test writing terom@43: log_info("test line_proto_send"); terom@43: assert_success(-line_proto_send(lp, "foobar\r\n")); terom@43: assert_success(-line_proto_send(lp, "quux\r\n")); terom@44: assert_sock_data(sock, "foobar\r\nquux\r\n"); terom@43: terom@43: // XXX: test partial writes terom@43: terom@41: // cleanup terom@41: line_proto_release(lp); terom@40: } terom@40: terom@43: struct _test_irc_conn_ctx { terom@49: bool on_registered, on_TEST, on_error, on_quit; terom@43: }; terom@43: terom@43: static void _conn_on_registered (struct irc_conn *conn, void *arg) terom@43: { terom@43: struct _test_irc_conn_ctx *ctx = arg; terom@43: terom@43: (void) conn; terom@43: terom@44: if (ctx) ctx->on_registered = true; terom@43: terom@43: log_debug("registered"); terom@43: } terom@43: terom@49: static void _conn_on_error (struct irc_conn *conn, struct error_info *err, void *arg) terom@49: { terom@49: struct _test_irc_conn_ctx *ctx = arg; terom@49: terom@49: (void) conn; terom@49: (void) err; terom@49: terom@49: if (ctx) ctx->on_error = true; terom@49: terom@49: log_debug("on_error"); terom@49: } terom@49: terom@49: static void _conn_on_quit (struct irc_conn *conn, void *arg) terom@49: { terom@49: struct _test_irc_conn_ctx *ctx = arg; terom@49: terom@49: (void) conn; terom@49: terom@49: if (ctx) ctx->on_quit = true; terom@49: terom@49: log_debug("on_quit"); terom@49: } terom@49: terom@43: static void _conn_on_TEST (const struct irc_line *line, void *arg) terom@43: { terom@43: struct _test_irc_conn_ctx *ctx = arg; terom@43: terom@43: assert_strcmp(line->prefix, "foobar-prefix"); terom@43: assert_strcmp(line->command, "TEST"); terom@43: assert_strcmp(line->args[0], "arg0"); terom@43: assert_strnull(line->args[1]); terom@43: terom@44: if (ctx) ctx->on_TEST = true; terom@43: terom@43: log_debug("on_TEST"); terom@43: } terom@43: terom@40: static struct irc_conn_callbacks _conn_callbacks = { terom@43: .on_registered = &_conn_on_registered, terom@49: .on_error = &_conn_on_error, terom@49: .on_quit = &_conn_on_quit, terom@43: }; terom@43: terom@43: static struct irc_cmd_handler _conn_handlers[] = { terom@43: { "TEST", &_conn_on_TEST }, terom@43: { NULL, NULL } terom@40: }; terom@40: terom@44: struct irc_conn* setup_irc_conn (struct sock_test *sock, bool noisy, struct _test_irc_conn_ctx *ctx) terom@40: { terom@40: struct irc_conn *conn; terom@40: struct error_info err; terom@43: struct irc_conn_register_info register_info = { terom@43: "nick", "user", "realname" terom@43: }; terom@44: terom@44: // create the irc_conn terom@44: assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, ctx, &err)); terom@44: terom@44: // test register terom@44: if (noisy) log_info("test irc_conn_register"); terom@44: assert_success(irc_conn_register(conn, ®ister_info)); terom@44: assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n"); terom@44: terom@44: // test on_register callback terom@44: if (noisy) log_info("test irc_conn_callbacks.on_register"); terom@52: test_sock_push(sock, "001 mynick :Blaa blaa blaa\r\n"); terom@44: if (ctx) assert(ctx->on_registered); terom@44: assert_strcmp(conn->nickname, "mynick"); terom@44: terom@44: // ok terom@44: return conn; terom@44: } terom@44: terom@44: void test_irc_conn (void) terom@44: { terom@44: struct sock_test *sock; terom@44: struct irc_conn *conn; terom@49: struct _test_irc_conn_ctx ctx = { false, false, false, false }; terom@40: terom@40: // create the test socket terom@40: assert((sock = sock_test_create())); terom@43: terom@44: // setup the basic irc_conn terom@44: conn = setup_irc_conn(sock, true, &ctx); terom@43: terom@44: // add our test handlers terom@44: assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx)); terom@43: terom@43: // test on_TEST handler terom@43: log_info("test irc_conn.handlers"); terom@52: test_sock_push(sock, ":foobar-prefix TEST arg0\r\n"); terom@43: assert(ctx.on_TEST); terom@40: terom@44: // test PING/PONG terom@44: log_info("test PING/PONG"); terom@52: test_sock_push(sock, "PING foo\r\n"); terom@44: assert_sock_data(sock, "PONG foo\r\n"); terom@44: terom@49: // quit nicely terom@49: log_info("test QUIT"); terom@49: assert_success(irc_conn_QUIT(conn, "bye now")); terom@49: assert_sock_data(sock, "QUIT :bye now\r\n"); terom@49: assert(conn->quitting); terom@49: terom@52: test_sock_push(sock, "ERROR :Closing Link: Quit\r\n"); terom@49: sock_test_set_recv_eof(sock); terom@49: assert(conn->quit && !conn->quitting && !conn->registered); terom@49: assert(ctx.on_quit); terom@49: assert(!ctx.on_error); terom@49: terom@40: // destroy it terom@40: irc_conn_destroy(conn); terom@40: } terom@40: terom@72: struct test_chan_ctx { terom@72: /** The channel we're supposed to be testing */ terom@44: struct irc_chan *chan; terom@72: terom@73: /** Flags for callbacks called*/ terom@73: bool on_chan_self_join, on_chan_join; terom@73: terom@44: }; terom@44: terom@44: void _on_chan_self_join (struct irc_chan *chan, void *arg) terom@44: { terom@72: struct test_chan_ctx *ctx = arg; terom@44: terom@44: assert(chan == ctx->chan); terom@44: terom@44: ctx->on_chan_self_join = true; terom@44: terom@44: log_debug("on_self_join"); terom@44: } terom@44: terom@73: void _on_chan_join (struct irc_chan *chan, const struct irc_nm *source, void *arg) terom@73: { terom@73: struct test_chan_ctx *ctx = arg; terom@73: terom@73: assert(chan == ctx->chan); terom@73: terom@73: // XXX: verify source terom@73: terom@73: ctx->on_chan_join = true; terom@73: terom@73: log_debug("on_join"); terom@73: } terom@73: terom@44: struct irc_chan_callbacks _chan_callbacks = { terom@44: .on_self_join = &_on_chan_self_join, terom@73: .on_join = &_on_chan_join, terom@44: }; terom@44: terom@72: /** terom@72: * Setup an irc_net using the given socket, and consume the register request output, but do not push the RPL_WELCOME terom@72: */ terom@72: struct irc_net* setup_irc_net_unregistered (struct sock_test *sock) terom@44: { terom@44: struct irc_net *net; terom@44: struct irc_net_info net_info = { terom@44: .register_info = { terom@44: "nick", "user", "realname" terom@44: }, terom@44: }; terom@72: struct error_info err; terom@72: terom@72: // create the irc_net terom@72: net_info.raw_sock = SOCK_TEST_BASE(sock); terom@72: assert_success(irc_net_create(&net, &net_info, &err)); terom@72: terom@72: // test register output terom@72: assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n"); terom@72: terom@72: // ok terom@72: return net; terom@72: } terom@72: terom@72: /** terom@72: * Push to RPL_WELCOME reply, and test state terom@72: */ terom@72: void do_irc_net_welcome (struct sock_test *sock, struct irc_net *net) terom@72: { terom@72: // registration reply terom@72: test_sock_push(sock, "001 mynick :Blaa blaa blaa\r\n"); terom@72: assert(net->conn->registered); terom@72: assert_strcmp(net->conn->nickname, "mynick"); terom@72: terom@72: } terom@72: terom@72: /** terom@72: * Creates an irc_net and puts it into the registered state terom@72: */ terom@72: struct irc_net* setup_irc_net (struct sock_test *sock) terom@72: { terom@72: struct irc_net *net; terom@72: terom@72: net = setup_irc_net_unregistered(sock); terom@72: do_irc_net_welcome(sock, net); terom@72: terom@72: // ok terom@72: return net; terom@72: } terom@72: terom@72: /** terom@72: * General test for irc_net to handle startup terom@72: */ terom@72: void test_irc_net (void) terom@72: { terom@72: struct sock_test *sock = setup_sock_test(); terom@72: terom@72: // create the network terom@72: log_info("test irc_net_create"); terom@72: struct irc_net *net = setup_irc_net_unregistered(sock); terom@72: terom@72: // send the registration reply terom@72: log_info("test irc_conn_on_RPL_WELCOME"); terom@72: do_irc_net_welcome(sock, net); terom@72: terom@72: // test errors by setting EOF terom@72: log_info("test irc_net_error"); terom@72: sock_test_set_recv_eof(sock); terom@72: assert(net->conn == NULL); terom@72: terom@72: // cleanup terom@72: irc_net_destroy(net); terom@72: } terom@72: terom@72: /** terom@72: * Creates an irc_chan on the given irc_net, but does not check any output (useful for testing offline add). terom@72: * terom@72: * You must pass a test_chan_ctx for use with later operations, this will be initialized for you. terom@72: */ terom@72: struct irc_chan* setup_irc_chan_raw (struct irc_net *net, struct test_chan_ctx *ctx) terom@72: { terom@44: struct irc_chan *chan; terom@44: struct irc_chan_info chan_info = { terom@44: .channel = "#test", terom@44: }; terom@44: struct error_info err; terom@44: terom@72: // initialize the given ctx terom@72: memset(ctx, 0, sizeof(*ctx)); terom@44: terom@44: // add a channel terom@72: assert_success(irc_net_add_chan(net, &chan, &chan_info, &err)); terom@72: assert(!chan->joined); terom@72: assert_success(irc_chan_add_callbacks(chan, &_chan_callbacks, ctx)); terom@72: ctx->chan = chan; terom@44: terom@72: // ok terom@72: return chan; terom@72: } terom@72: terom@72: /** terom@72: * Checks that the JOIN request for a channel was sent, and sends the basic JOIN reply terom@72: */ terom@72: void do_irc_chan_join (struct sock_test *sock, struct test_chan_ctx *ctx) terom@72: { terom@44: // JOIN request terom@72: assert(ctx->chan->joining); terom@44: assert_sock_data(sock, "JOIN #test\r\n"); terom@44: terom@44: // JOIN reply terom@52: test_sock_push(sock, ":mynick!user@host JOIN #test\r\n"); terom@72: assert(!ctx->chan->joining && ctx->chan->joined); terom@72: assert(ctx->on_chan_self_join); terom@72: } terom@46: terom@72: /** terom@72: * Creates an irc_chan on the given irc_net, and checks up to the JOIN reply terom@72: */ terom@72: struct irc_chan* setup_irc_chan (struct sock_test *sock, struct irc_net *net, struct test_chan_ctx *ctx) terom@72: { terom@72: setup_irc_chan_raw(net, ctx); terom@72: do_irc_chan_join(sock, ctx); terom@72: terom@72: // ok terom@72: return ctx->chan; terom@72: } terom@72: terom@72: /** terom@72: * Call irc_net_add_chan while offline, and ensure that we send the JOIN request after RPL_WELCOME, and handle the join terom@72: * reply OK. terom@72: */ terom@72: void test_irc_chan_add_offline (void) terom@72: { terom@72: struct test_chan_ctx ctx; terom@72: terom@72: struct sock_test *sock = setup_sock_test(); terom@72: terom@72: log_info("test irc_net_create"); terom@72: struct irc_net *net = setup_irc_net_unregistered(sock); terom@72: terom@72: // add an offline channel terom@72: log_info("test offline irc_net_add_chan"); terom@72: struct irc_chan *chan = setup_irc_chan_raw(net, &ctx); terom@72: assert(!chan->joining && !chan->joined); terom@72: terom@72: // send the registration reply terom@72: log_info("test irc_conn_on_RPL_WELCOME"); terom@72: do_irc_net_welcome(sock, net); terom@72: terom@72: // test the join sequence terom@72: log_info("test irc_chan_join/irc_chan_on_JOIN"); terom@72: do_irc_chan_join(sock, &ctx); terom@72: terom@72: // cleanup terom@72: irc_net_destroy(net); terom@72: } terom@72: terom@72: /** terom@72: * Ensure that an irc_chan_user exists for the given channel/nickname, and return it terom@72: */ terom@72: struct irc_chan_user* check_chan_user (struct irc_chan *chan, const char *nickname) terom@72: { terom@72: struct irc_chan_user *chan_user; terom@72: terom@72: if ((chan_user = irc_chan_get_user(chan, nickname)) == NULL) terom@72: FATAL("user %s not found in channel %s", dump_str(nickname), dump_str(irc_chan_name(chan))); terom@72: terom@72: log_debug("%s -> %p: user=%p, nickname=%s", nickname, chan_user, chan_user->user, chan_user->user->nickname); terom@72: terom@72: assert_strcmp(chan_user->user->nickname, nickname); terom@72: terom@72: return chan_user; terom@72: } terom@72: terom@72: void test_irc_chan_namreply (void) terom@72: { terom@72: struct test_chan_ctx ctx; terom@72: struct sock_test *sock = setup_sock_test(); terom@72: struct irc_net *net = setup_irc_net(sock); terom@72: struct irc_chan *chan = setup_irc_chan(sock, net, &ctx); terom@72: terom@72: // RPL_NAMREPLY terom@72: log_info("test irc_chan_on_RPL_NAMREPLY"); terom@72: test_sock_push(sock, "353 mynick = #test :mynick userA +userB @userC\r\n"); terom@72: terom@72: check_chan_user(chan, "mynick"); terom@72: check_chan_user(chan, "userA"); terom@72: check_chan_user(chan, "userB"); terom@72: check_chan_user(chan, "userC"); terom@47: terom@47: // cleanup terom@47: irc_net_destroy(net); terom@44: } terom@44: terom@73: void test_irc_chan_user_join (void) terom@73: { terom@73: struct test_chan_ctx ctx; terom@73: struct sock_test *sock = setup_sock_test(); terom@73: struct irc_net *net = setup_irc_net(sock); terom@73: struct irc_chan *chan = setup_irc_chan(sock, net, &ctx); terom@73: terom@73: // RPL_NAMREPLY terom@73: test_sock_push(sock, "353 mynick = #test :mynick userA +userB @userC\r\n"); terom@73: terom@73: check_chan_user(chan, "mynick"); terom@73: check_chan_user(chan, "userA"); terom@73: check_chan_user(chan, "userB"); terom@73: check_chan_user(chan, "userC"); terom@73: terom@73: // have a user join terom@73: log_info("test irc_chan_on_JOIN"); terom@73: test_sock_push(sock, ":newuser!someone@somewhere JOIN #test\r\n"); terom@73: assert(ctx.on_chan_join); terom@73: check_chan_user(chan, "newuser"); terom@73: terom@73: // cleanup terom@73: irc_net_destroy(net); terom@73: } terom@73: terom@40: /** terom@40: * Test definition terom@40: */ terom@40: static struct test { terom@40: /** Test name */ terom@40: const char *name; terom@40: terom@40: /** Test func */ terom@40: void (*func) (void); terom@40: terom@40: } _tests[] = { terom@72: { "dump_str", &test_dump_str }, terom@72: { "sock_test", &test_sock_test }, terom@72: { "line_proto", &test_line_proto }, terom@72: { "irc_conn", &test_irc_conn }, terom@72: { "irc_net", &test_irc_net }, terom@72: { "irc_chan_add_offline", &test_irc_chan_add_offline }, terom@72: { "irc_chan_namreply", &test_irc_chan_namreply }, terom@73: { "irc_chan_user_join", &test_irc_chan_user_join }, terom@72: { NULL, NULL } terom@40: }; terom@40: terom@73: /** terom@73: * Command-line option codes terom@73: */ terom@73: enum option_code { terom@73: OPT_HELP = 'h', terom@73: OPT_DEBUG = 'd', terom@73: OPT_QUIET = 'q', terom@73: terom@73: /** Options without short names */ terom@73: _OPT_EXT_BEGIN = 0x00ff, terom@73: terom@73: }; terom@73: terom@73: /** terom@73: * Command-line option definitions terom@73: */ terom@73: static struct option options[] = { terom@73: {"help", 0, NULL, OPT_HELP }, terom@73: {"debug", 0, NULL, OPT_DEBUG }, terom@73: {"quiet", 0, NULL, OPT_QUIET }, terom@73: {0, 0, 0, 0 }, terom@73: }; terom@73: terom@73: /** terom@73: * Display --help output on stdout terom@73: */ terom@73: static void usage (const char *exe) terom@73: { terom@73: printf("Usage: %s [OPTIONS]\n", exe); terom@73: printf("\n"); terom@73: printf(" --help / -h display this message\n"); terom@73: printf(" --debug / -d display DEBUG log messages\n"); terom@73: printf(" --quiet / -q supress INFO log messages\n"); terom@73: } terom@73: terom@40: int main (int argc, char **argv) terom@40: { terom@40: struct test *test; terom@73: size_t test_count = 0; terom@52: terom@73: int opt, option_index; terom@73: const char *filter = NULL; terom@40: terom@73: // parse options terom@73: while ((opt = getopt_long(argc, argv, "hdq", options, &option_index)) != -1) { terom@73: switch (opt) { terom@73: case OPT_HELP: terom@73: usage(argv[0]); terom@73: exit(EXIT_SUCCESS); terom@73: terom@73: case OPT_DEBUG: terom@73: set_log_level(LOG_DEBUG); terom@73: break; terom@52: terom@73: case OPT_QUIET: terom@73: set_log_level(LOG_WARN); terom@73: break; terom@73: terom@73: case '?': terom@73: usage(argv[0]); terom@73: exit(EXIT_FAILURE); terom@73: } terom@73: } terom@73: terom@73: if (optind < argc) { terom@73: if (optind == argc - 1) { terom@73: // filter terom@73: filter = argv[optind]; terom@73: terom@73: log_info("only running tests: %s", filter); terom@73: } else { terom@73: FATAL("too many arguments"); terom@73: } terom@52: } terom@40: terom@40: // run tests terom@40: for (test = _tests; test->name; test++) { terom@52: if (filter && strcmp(test->name, filter)) terom@52: continue; terom@52: terom@40: log_info("Running test: %s", test->name); terom@73: terom@73: test_count++; terom@40: test->func(); terom@40: } terom@46: terom@73: // no tests run? terom@73: if (test_count == 0) terom@73: FATAL("no tests run"); terom@73: terom@73: log_info("done, ran %zu tests", test_count); terom@40: }