src/test.c
changeset 44 6bd70113e1ed
parent 43 42f5dc680930
child 45 71e65564afd2
equal deleted inserted replaced
43:42f5dc680930 44:6bd70113e1ed
     2  * The main test code entry point
     2  * The main test code entry point
     3  */
     3  */
     4 #include "sock_test.h"
     4 #include "sock_test.h"
     5 #include "line_proto.h"
     5 #include "line_proto.h"
     6 #include "irc_conn.h"
     6 #include "irc_conn.h"
       
     7 #include "irc_net.h"
     7 #include "log.h"
     8 #include "log.h"
     8 #include "error.h"
     9 #include "error.h"
     9 
    10 
    10 #include <stdlib.h>
    11 #include <stdlib.h>
    11 #include <string.h>
    12 #include <string.h>
    18 }
    19 }
    19 
    20 
    20 void assert_strncmp (const char *is, const char *should_be, size_t n)
    21 void assert_strncmp (const char *is, const char *should_be, size_t n)
    21 {
    22 {
    22     if (strncmp(is, should_be, n))
    23     if (strncmp(is, should_be, n))
    23         FATAL("'%s':%d != '%s'", is, n, should_be);
    24         FATAL("'%s':%u != '%s'", is, (unsigned) n, should_be);
    24 }
    25 }
    25 
    26 
    26 void assert_strlen (const char *str, size_t n)
    27 void assert_strlen (const char *str, size_t n)
    27 {
    28 {
    28     if (strlen(str) != n)
    29     if (strlen(str) != n)
    29         FATAL("strlen('%s') != %u", str, n);
    30         FATAL("strlen('%s') != %u", str, (unsigned) n);
    30 }
    31 }
    31 
    32 
    32 void assert_strnull (const char *str)
    33 void assert_strnull (const char *str)
    33 {
    34 {
    34     if (str != NULL)
    35     if (str != NULL)
    82     log_debug("eof: %p", sock);
    83     log_debug("eof: %p", sock);
    83 
    84 
    84     assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF);
    85     assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF);
    85 }
    86 }
    86 
    87 
    87 void assert_sock_send_data (struct sock_test *sock, const char *data)
    88 void assert_sock_data (struct sock_test *sock, const char *data)
    88 {
    89 {
    89     // get the data out
    90     // get the data out
    90     char *buf;
    91     char *buf;
    91     size_t len;
    92     size_t len;
    92     
    93     
   128 
   129 
   129     assert_sock_write(SOCK_TEST_BASE(sock), "test ");
   130     assert_sock_write(SOCK_TEST_BASE(sock), "test ");
   130     assert_sock_write(SOCK_TEST_BASE(sock), "data");
   131     assert_sock_write(SOCK_TEST_BASE(sock), "data");
   131     
   132     
   132     // check output
   133     // check output
   133     assert_sock_send_data(sock, _write_data);
   134     assert_sock_data(sock, _write_data);
   134 
   135 
   135     // check output
   136     // check output
   136     assert_sock_send_data(sock, "");
   137     assert_sock_data(sock, "");
   137 
   138 
   138     // cleanup
   139     // cleanup
   139     sock_test_destroy(sock);
   140     sock_test_destroy(sock);
   140 }
   141 }
   141 
   142 
   229 
   230 
   230     // test writing
   231     // test writing
   231     log_info("test line_proto_send");
   232     log_info("test line_proto_send");
   232     assert_success(-line_proto_send(lp, "foobar\r\n"));
   233     assert_success(-line_proto_send(lp, "foobar\r\n"));
   233     assert_success(-line_proto_send(lp, "quux\r\n"));
   234     assert_success(-line_proto_send(lp, "quux\r\n"));
   234     assert_sock_send_data(sock, "foobar\r\nquux\r\n");
   235     assert_sock_data(sock, "foobar\r\nquux\r\n");
   235 
   236 
   236     // XXX: test partial writes
   237     // XXX: test partial writes
   237 
   238 
   238     // cleanup
   239     // cleanup
   239     line_proto_release(lp);
   240     line_proto_release(lp);
   247 {
   248 {
   248     struct _test_irc_conn_ctx *ctx = arg;
   249     struct _test_irc_conn_ctx *ctx = arg;
   249 
   250 
   250     (void) conn;
   251     (void) conn;
   251 
   252 
   252     ctx->on_registered = true;
   253     if (ctx) ctx->on_registered = true;
   253 
   254 
   254     log_debug("registered");
   255     log_debug("registered");
   255 }
   256 }
   256 
   257 
   257 static void _conn_on_TEST (const struct irc_line *line, void *arg)
   258 static void _conn_on_TEST (const struct irc_line *line, void *arg)
   261     assert_strcmp(line->prefix, "foobar-prefix");
   262     assert_strcmp(line->prefix, "foobar-prefix");
   262     assert_strcmp(line->command, "TEST");
   263     assert_strcmp(line->command, "TEST");
   263     assert_strcmp(line->args[0], "arg0");
   264     assert_strcmp(line->args[0], "arg0");
   264     assert_strnull(line->args[1]);
   265     assert_strnull(line->args[1]);
   265 
   266 
   266     ctx->on_TEST = true;
   267     if (ctx) ctx->on_TEST = true;
   267 
   268 
   268     log_debug("on_TEST");
   269     log_debug("on_TEST");
   269 }
   270 }
   270 
   271 
   271 static struct irc_conn_callbacks _conn_callbacks = {
   272 static struct irc_conn_callbacks _conn_callbacks = {
   275 static struct irc_cmd_handler _conn_handlers[] = {
   276 static struct irc_cmd_handler _conn_handlers[] = {
   276     {   "TEST",         &_conn_on_TEST  },
   277     {   "TEST",         &_conn_on_TEST  },
   277     {   NULL,           NULL            }
   278     {   NULL,           NULL            }
   278 };
   279 };
   279 
   280 
   280 void test_irc_conn (void)
   281 struct irc_conn* setup_irc_conn (struct sock_test *sock, bool noisy, struct _test_irc_conn_ctx *ctx)
   281 {
   282 {
   282     struct sock_test *sock;
       
   283     struct irc_conn *conn;
   283     struct irc_conn *conn;
   284     struct error_info err;
   284     struct error_info err;
   285     struct irc_conn_register_info register_info = {
   285     struct irc_conn_register_info register_info = {
   286         "nick", "user", "realname"
   286         "nick", "user", "realname"
   287     };
   287     };
       
   288 
       
   289     // create the irc_conn
       
   290     assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, ctx, &err));
       
   291 
       
   292     // test register
       
   293     if (noisy) log_info("test irc_conn_register");
       
   294     assert_success(irc_conn_register(conn, &register_info));
       
   295     assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n");
       
   296  
       
   297     // test on_register callback    
       
   298     if (noisy) log_info("test irc_conn_callbacks.on_register");
       
   299     sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n");
       
   300     if (ctx) assert(ctx->on_registered);
       
   301     assert_strcmp(conn->nickname, "mynick");
       
   302    
       
   303     // ok
       
   304     return conn;
       
   305 }
       
   306 
       
   307 void test_irc_conn (void)
       
   308 {
       
   309     struct sock_test *sock;
       
   310     struct irc_conn *conn;
   288     struct _test_irc_conn_ctx ctx = { false, false };
   311     struct _test_irc_conn_ctx ctx = { false, false };
   289 
   312 
   290     // create the test socket
   313     // create the test socket
   291     assert((sock = sock_test_create()));
   314     assert((sock = sock_test_create()));
   292     
   315 
   293     // create the irc_conn
   316     // setup the basic irc_conn
   294     assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, &ctx, &err));
   317     conn = setup_irc_conn(sock, true, &ctx);
       
   318     
       
   319     // add our test handlers
   295     assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx));
   320     assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx));
   296 
       
   297     // test register
       
   298     log_info("test irc_conn_register");
       
   299     assert_success(irc_conn_register(conn, &register_info));
       
   300     assert_sock_send_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n");
       
   301     
       
   302     // test on_register callback    
       
   303     log_info("test irc_conn_callbacks.on_register");
       
   304     sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n");
       
   305     assert(ctx.on_registered);
       
   306     assert_strcmp(conn->nickname, "mynick");
       
   307 
   321 
   308     // test on_TEST handler
   322     // test on_TEST handler
   309     log_info("test irc_conn.handlers");
   323     log_info("test irc_conn.handlers");
   310     sock_test_add_recv_str(sock, ":foobar-prefix TEST arg0\r\n");
   324     sock_test_add_recv_str(sock, ":foobar-prefix TEST arg0\r\n");
   311     assert(ctx.on_TEST);
   325     assert(ctx.on_TEST);
   312 
   326 
       
   327     // test PING/PONG
       
   328     log_info("test PING/PONG");
       
   329     sock_test_add_recv_str(sock, "PING foo\r\n");
       
   330     assert_sock_data(sock, "PONG foo\r\n");
       
   331 
   313     // destroy it
   332     // destroy it
   314     irc_conn_destroy(conn);
   333     irc_conn_destroy(conn);
       
   334 }
       
   335 
       
   336 struct _test_net_ctx {
       
   337     struct irc_chan *chan;
       
   338 
       
   339     bool on_chan_self_join;
       
   340 };
       
   341 
       
   342 void _on_chan_self_join (struct irc_chan *chan, void *arg)
       
   343 {
       
   344     struct _test_net_ctx *ctx = arg;
       
   345 
       
   346     assert(chan == ctx->chan);
       
   347     
       
   348     ctx->on_chan_self_join = true;
       
   349 
       
   350     log_debug("on_self_join");
       
   351 }
       
   352 
       
   353 struct irc_chan_callbacks _chan_callbacks = {
       
   354     .on_self_join       = &_on_chan_self_join,
       
   355 };
       
   356 
       
   357 void test_irc_net (void)
       
   358 {
       
   359     struct sock_test *sock;
       
   360     struct irc_net *net;
       
   361     struct irc_net_info net_info = {
       
   362         .register_info = {
       
   363             "nick", "user", "realname"
       
   364         },
       
   365     };
       
   366     struct irc_chan *chan;
       
   367     struct irc_chan_info chan_info = {
       
   368         .channel        = "#test",
       
   369     };
       
   370     struct error_info err;
       
   371     struct _test_net_ctx ctx = { NULL, false };
       
   372 
       
   373     // create the test socket
       
   374     assert((sock = sock_test_create()));
       
   375     
       
   376     // create the irc_net
       
   377     net_info.raw_sock = SOCK_TEST_BASE(sock);
       
   378     assert_success(irc_net_create(&net, &net_info, &err));
       
   379 
       
   380     // add a channel
       
   381     assert((chan = irc_net_add_chan(net, &chan_info)));
       
   382     assert(!chan->state.joining && !chan->state.joined);
       
   383     assert_success(irc_chan_add_callbacks(chan, &_chan_callbacks, &ctx));
       
   384     ctx.chan = chan;
       
   385     
       
   386     // test register output
       
   387     assert_sock_data(sock, "NICK nick\r\nUSER user 0 * realname\r\n");
       
   388     
       
   389     // registration reply
       
   390     sock_test_add_recv_str(sock, "001 mynick :Blaa blaa blaa\r\n");
       
   391     assert(net->conn->registered);
       
   392     assert_strcmp(net->conn->nickname, "mynick");
       
   393     
       
   394     // JOIN request
       
   395     assert(chan->state.joining);
       
   396     assert_sock_data(sock, "JOIN #test\r\n");
       
   397 
       
   398     // JOIN reply
       
   399     sock_test_add_recv_str(sock, ":mynick!user@host JOIN #test\r\n");
       
   400     assert(!chan->state.joining && chan->state.joined);
       
   401     assert(ctx.on_chan_self_join);
   315 }
   402 }
   316 
   403 
   317 /**
   404 /**
   318  * Test definition
   405  * Test definition
   319  */
   406  */
   326 
   413 
   327 } _tests[] = {
   414 } _tests[] = {
   328     {   "sock_test",    &test_sock_test     },
   415     {   "sock_test",    &test_sock_test     },
   329     {   "line_proto",   &test_line_proto    },
   416     {   "line_proto",   &test_line_proto    },
   330     {   "irc_conn",     &test_irc_conn      },
   417     {   "irc_conn",     &test_irc_conn      },
       
   418     {   "irc_net",      &test_irc_net       },
   331     {   NULL,           NULL                }
   419     {   NULL,           NULL                }
   332 };
   420 };
   333 
   421 
   334 int main (int argc, char **argv)
   422 int main (int argc, char **argv)
   335 {
   423 {