src/test.c
changeset 76 b3672e3d9665
parent 75 ff6272398d2e
child 77 5478ade62546
equal deleted inserted replaced
75:ff6272398d2e 76:b3672e3d9665
   385 
   385 
   386     // cleanup
   386     // cleanup
   387     line_proto_release(lp);
   387     line_proto_release(lp);
   388 }
   388 }
   389 
   389 
   390 struct _test_irc_conn_ctx {
   390 struct test_conn_ctx {
       
   391     /** Callback flags */
   391     bool on_registered, on_TEST, on_error, on_quit;
   392     bool on_registered, on_TEST, on_error, on_quit;
   392 };
   393 };
   393 
   394 
   394 static void _conn_on_registered (struct irc_conn *conn, void *arg)
   395 static void _conn_on_registered (struct irc_conn *conn, void *arg)
   395 {
   396 {
   396     struct _test_irc_conn_ctx *ctx = arg;
   397     struct test_conn_ctx *ctx = arg;
   397 
   398 
   398     (void) conn;
   399     (void) conn;
   399 
   400 
   400     if (ctx) ctx->on_registered = true;
   401     if (ctx) ctx->on_registered = true;
   401 
   402 
   402     log_debug("registered");
   403     log_debug("registered");
   403 }
   404 }
   404 
   405 
   405 static void _conn_on_error (struct irc_conn *conn, struct error_info *err, void *arg)
   406 static void _conn_on_error (struct irc_conn *conn, struct error_info *err, void *arg)
   406 {
   407 {
   407     struct _test_irc_conn_ctx *ctx = arg;
   408     struct test_conn_ctx *ctx = arg;
   408     
   409     
   409     (void) conn;
   410     (void) conn;
   410     (void) err;
   411     (void) err;
   411 
   412 
   412     if (ctx) ctx->on_error = true;
   413     if (ctx) ctx->on_error = true;
   414     log_debug("on_error");
   415     log_debug("on_error");
   415 }
   416 }
   416 
   417 
   417 static void _conn_on_quit (struct irc_conn *conn, void *arg)
   418 static void _conn_on_quit (struct irc_conn *conn, void *arg)
   418 {
   419 {
   419     struct _test_irc_conn_ctx *ctx = arg;
   420     struct test_conn_ctx *ctx = arg;
   420 
   421 
   421     (void) conn;
   422     (void) conn;
   422 
   423 
   423     if (ctx) ctx->on_quit = true;
   424     if (ctx) ctx->on_quit = true;
   424 
   425 
   425     log_debug("on_quit");
   426     log_debug("on_quit");
   426 }
   427 }
   427 
   428 
   428 static void _conn_on_TEST (const struct irc_line *line, void *arg)
   429 static void _conn_on_TEST (const struct irc_line *line, void *arg)
   429 {
   430 {
   430     struct _test_irc_conn_ctx *ctx = arg;
   431     struct test_conn_ctx *ctx = arg;
   431 
   432 
   432     assert_null(line->source);
   433     assert_null(line->source);
   433     assert_strcmp(line->command, "TEST");
   434     assert_strcmp(line->command, "TEST");
   434     assert_strcmp(line->args[0], "arg0");
   435     assert_strcmp(line->args[0], "arg0");
   435     assert_strnull(line->args[1]);
   436     assert_strnull(line->args[1]);
   448 static struct irc_cmd_handler _conn_handlers[] = {
   449 static struct irc_cmd_handler _conn_handlers[] = {
   449     {   "TEST",         &_conn_on_TEST  },
   450     {   "TEST",         &_conn_on_TEST  },
   450     {   NULL,           NULL            }
   451     {   NULL,           NULL            }
   451 };
   452 };
   452 
   453 
   453 struct irc_conn* setup_irc_conn (struct sock_test *sock, bool noisy, struct _test_irc_conn_ctx *ctx)
   454 /**
       
   455  * Create and return a new irc_conn with the given ctx (will be initialized to zero).
       
   456  */
       
   457 struct irc_conn* setup_irc_conn (struct sock_test *sock, bool noisy, struct test_conn_ctx *ctx)
   454 {
   458 {
   455     struct irc_conn *conn;
   459     struct irc_conn *conn;
   456     struct error_info err;
   460     struct error_info err;
   457     struct irc_conn_register_info register_info = {
   461     struct irc_conn_register_info register_info = {
   458         "nick", "user", "realname"
   462         "nick", "user", "realname"
   459     };
   463     };
       
   464 
       
   465     // init the ctx
       
   466     memset(ctx, 0, sizeof(*ctx));
   460 
   467 
   461     // create the irc_conn
   468     // create the irc_conn
   462     assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, ctx, &err));
   469     assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, ctx, &err));
   463 
   470 
   464     // test register
   471     // test register
   476     return conn;
   483     return conn;
   477 }
   484 }
   478 
   485 
   479 void test_irc_conn (void)
   486 void test_irc_conn (void)
   480 {
   487 {
   481     struct sock_test *sock;
   488     struct test_conn_ctx ctx;
   482     struct irc_conn *conn;
   489     struct sock_test *sock = setup_sock_test();
   483     struct _test_irc_conn_ctx ctx = { false, false, false, false };
   490     struct irc_conn *conn = setup_irc_conn(sock, true, &ctx);
   484 
   491 
   485     // create the test socket
       
   486     assert((sock = sock_test_create()));
       
   487 
       
   488     // setup the basic irc_conn
       
   489     conn = setup_irc_conn(sock, true, &ctx);
       
   490     
       
   491     // add our test handlers
   492     // add our test handlers
   492     assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx));
   493     assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx));
   493 
   494 
   494     // test on_TEST handler
   495     // test on_TEST handler
   495     // XXX: come up with a better prefix
   496     // XXX: come up with a better prefix
   513     assert(conn->quit && !conn->quitting && !conn->registered);
   514     assert(conn->quit && !conn->quitting && !conn->registered);
   514     assert(ctx.on_quit);
   515     assert(ctx.on_quit);
   515     assert(!ctx.on_error);
   516     assert(!ctx.on_error);
   516 
   517 
   517     // destroy it
   518     // destroy it
       
   519     irc_conn_destroy(conn);
       
   520 }
       
   521 
       
   522 void test_irc_conn_self_nick (void)
       
   523 {
       
   524     struct test_conn_ctx ctx;
       
   525     struct sock_test *sock = setup_sock_test();
       
   526     struct irc_conn *conn = setup_irc_conn(sock, false, &ctx);
       
   527     
       
   528     log_info("test irc_conn_on_NICK");
       
   529     test_sock_push(sock, ":mynick!user@somehost NICK mynick2\r\n");
       
   530     assert_strcmp(conn->nickname, "mynick2");
       
   531 
       
   532     // cleanup
   518     irc_conn_destroy(conn);
   533     irc_conn_destroy(conn);
   519 }
   534 }
   520 
   535 
   521 struct test_chan_ctx {
   536 struct test_chan_ctx {
   522     /** The channel we're supposed to be testing */
   537     /** The channel we're supposed to be testing */
   843     {   "dump_str",             &test_dump_str              },
   858     {   "dump_str",             &test_dump_str              },
   844     {   "sock_test",            &test_sock_test             },
   859     {   "sock_test",            &test_sock_test             },
   845     {   "line_proto",           &test_line_proto            },
   860     {   "line_proto",           &test_line_proto            },
   846     // XXX: irc_line_parse_invalid_prefix
   861     // XXX: irc_line_parse_invalid_prefix
   847     {   "irc_conn",             &test_irc_conn              },
   862     {   "irc_conn",             &test_irc_conn              },
   848     // XXX: irc_conn_self_nick
   863     {   "irc_conn_self_nick",   &test_irc_conn_self_nick    },
   849     {   "irc_net",              &test_irc_net               },
   864     {   "irc_net",              &test_irc_net               },
   850     {   "irc_chan_add_offline", &test_irc_chan_add_offline  },
   865     {   "irc_chan_add_offline", &test_irc_chan_add_offline  },
   851     {   "irc_chan_namreply",    &test_irc_chan_namreply     },
   866     {   "irc_chan_namreply",    &test_irc_chan_namreply     },
   852     {   "irc_chan_user_join",   &test_irc_chan_user_join    },
   867     {   "irc_chan_user_join",   &test_irc_chan_user_join    },
   853     {   "irc_chan_user_part",   &test_irc_chan_user_part    },
   868     {   "irc_chan_user_part",   &test_irc_chan_user_part    },
   862     OPT_DEBUG           = 'd',
   877     OPT_DEBUG           = 'd',
   863     OPT_QUIET           = 'q',
   878     OPT_QUIET           = 'q',
   864     
   879     
   865     /** Options without short names */
   880     /** Options without short names */
   866     _OPT_EXT_BEGIN      = 0x00ff,
   881     _OPT_EXT_BEGIN      = 0x00ff,
   867 
       
   868 };
   882 };
   869 
   883 
   870 /**
   884 /**
   871  * Command-line option definitions
   885  * Command-line option definitions
   872  */
   886  */