src/test.c
changeset 73 2780a73c71f3
parent 72 43084f103c2a
child 74 11ec458d1cbf
equal deleted inserted replaced
72:43084f103c2a 73:2780a73c71f3
     8 #include "log.h"
     8 #include "log.h"
     9 #include "error.h"
     9 #include "error.h"
    10 
    10 
    11 #include <stdlib.h>
    11 #include <stdlib.h>
    12 #include <string.h>
    12 #include <string.h>
       
    13 #include <getopt.h>
    13 #include <assert.h>
    14 #include <assert.h>
    14 #include <ctype.h>
    15 #include <ctype.h>
    15 
    16 
    16 #define DUMP_STR_BUF 1024
    17 #define DUMP_STR_BUF 1024
    17 #define DUMP_STR_COUNT 8
    18 #define DUMP_STR_COUNT 8
   512 
   513 
   513 struct test_chan_ctx {
   514 struct test_chan_ctx {
   514     /** The channel we're supposed to be testing */
   515     /** The channel we're supposed to be testing */
   515     struct irc_chan *chan;
   516     struct irc_chan *chan;
   516     
   517     
   517     /** on_self_join callback called */
   518     /** Flags for callbacks called*/
   518     bool on_chan_self_join;
   519     bool on_chan_self_join, on_chan_join;
       
   520 
   519 };
   521 };
   520 
   522 
   521 void _on_chan_self_join (struct irc_chan *chan, void *arg)
   523 void _on_chan_self_join (struct irc_chan *chan, void *arg)
   522 {
   524 {
   523     struct test_chan_ctx *ctx = arg;
   525     struct test_chan_ctx *ctx = arg;
   525     assert(chan == ctx->chan);
   527     assert(chan == ctx->chan);
   526     
   528     
   527     ctx->on_chan_self_join = true;
   529     ctx->on_chan_self_join = true;
   528 
   530 
   529     log_debug("on_self_join");
   531     log_debug("on_self_join");
       
   532 }
       
   533 
       
   534 void _on_chan_join (struct irc_chan *chan, const struct irc_nm *source, void *arg)
       
   535 {
       
   536     struct test_chan_ctx *ctx = arg;
       
   537 
       
   538     assert(chan == ctx->chan);
       
   539 
       
   540     // XXX: verify source
       
   541 
       
   542     ctx->on_chan_join = true;
       
   543 
       
   544     log_debug("on_join");
   530 }
   545 }
   531 
   546 
   532 struct irc_chan_callbacks _chan_callbacks = {
   547 struct irc_chan_callbacks _chan_callbacks = {
   533     .on_self_join       = &_on_chan_self_join,
   548     .on_self_join       = &_on_chan_self_join,
       
   549     .on_join            = &_on_chan_join,
   534 };
   550 };
   535 
   551 
   536 /**
   552 /**
   537  * Setup an irc_net using the given socket, and consume the register request output, but do not push the RPL_WELCOME
   553  * Setup an irc_net using the given socket, and consume the register request output, but do not push the RPL_WELCOME
   538  */
   554  */
   720 
   736 
   721     check_chan_user(chan, "mynick");
   737     check_chan_user(chan, "mynick");
   722     check_chan_user(chan, "userA");
   738     check_chan_user(chan, "userA");
   723     check_chan_user(chan, "userB");
   739     check_chan_user(chan, "userB");
   724     check_chan_user(chan, "userC");
   740     check_chan_user(chan, "userC");
       
   741 
       
   742     // cleanup
       
   743     irc_net_destroy(net);
       
   744 }
       
   745 
       
   746 void test_irc_chan_user_join (void)
       
   747 {
       
   748     struct test_chan_ctx ctx;
       
   749     struct sock_test *sock = setup_sock_test();
       
   750     struct irc_net *net = setup_irc_net(sock);
       
   751     struct irc_chan *chan = setup_irc_chan(sock, net, &ctx);
       
   752 
       
   753     // RPL_NAMREPLY
       
   754     test_sock_push(sock, "353 mynick = #test :mynick userA +userB @userC\r\n");
       
   755 
       
   756     check_chan_user(chan, "mynick");
       
   757     check_chan_user(chan, "userA");
       
   758     check_chan_user(chan, "userB");
       
   759     check_chan_user(chan, "userC");
       
   760 
       
   761     // have a user join
       
   762     log_info("test irc_chan_on_JOIN");
       
   763     test_sock_push(sock, ":newuser!someone@somewhere JOIN #test\r\n");
       
   764     assert(ctx.on_chan_join);
       
   765     check_chan_user(chan, "newuser");
   725 
   766 
   726     // cleanup
   767     // cleanup
   727     irc_net_destroy(net);
   768     irc_net_destroy(net);
   728 }
   769 }
   729 
   770 
   743     {   "line_proto",           &test_line_proto            },
   784     {   "line_proto",           &test_line_proto            },
   744     {   "irc_conn",             &test_irc_conn              },
   785     {   "irc_conn",             &test_irc_conn              },
   745     {   "irc_net",              &test_irc_net               },
   786     {   "irc_net",              &test_irc_net               },
   746     {   "irc_chan_add_offline", &test_irc_chan_add_offline  },
   787     {   "irc_chan_add_offline", &test_irc_chan_add_offline  },
   747     {   "irc_chan_namreply",    &test_irc_chan_namreply     },
   788     {   "irc_chan_namreply",    &test_irc_chan_namreply     },
       
   789     {   "irc_chan_user_join",   &test_irc_chan_user_join    },
   748     {   NULL,                   NULL                        }
   790     {   NULL,                   NULL                        }
   749 };
   791 };
   750 
   792 
       
   793 /**
       
   794  * Command-line option codes
       
   795  */
       
   796 enum option_code {
       
   797     OPT_HELP            = 'h',
       
   798     OPT_DEBUG           = 'd',
       
   799     OPT_QUIET           = 'q',
       
   800     
       
   801     /** Options without short names */
       
   802     _OPT_EXT_BEGIN      = 0x00ff,
       
   803 
       
   804 };
       
   805 
       
   806 /**
       
   807  * Command-line option definitions
       
   808  */
       
   809 static struct option options[] = {
       
   810     {"help",            0,  NULL,   OPT_HELP        },
       
   811     {"debug",           0,  NULL,   OPT_DEBUG       },
       
   812     {"quiet",           0,  NULL,   OPT_QUIET       },
       
   813     {0,                 0,  0,      0               },
       
   814 };
       
   815 
       
   816 /**
       
   817  * Display --help output on stdout
       
   818  */
       
   819 static void usage (const char *exe) 
       
   820 {
       
   821     printf("Usage: %s [OPTIONS]\n", exe);
       
   822     printf("\n");
       
   823     printf(" --help / -h            display this message\n");
       
   824     printf(" --debug / -d           display DEBUG log messages\n");
       
   825     printf(" --quiet / -q           supress INFO log messages\n");
       
   826 }
       
   827 
   751 int main (int argc, char **argv)
   828 int main (int argc, char **argv)
   752 {
   829 {
   753     const char *filter;
       
   754     struct test *test;
   830     struct test *test;
   755     
   831     size_t test_count = 0;
   756     if (argc == 1) {
   832     
   757         // no arguments
   833     int opt, option_index;
   758         filter = NULL;
   834     const char *filter = NULL;
   759 
   835 
   760     } else if (argc == 2) {
   836     // parse options
   761         // filter
   837     while ((opt = getopt_long(argc, argv, "hdq", options, &option_index)) != -1) {
   762         filter = argv[1];
   838         switch (opt) {
   763         
   839             case OPT_HELP:
   764         log_info("only running tests: %s", filter);
   840                 usage(argv[0]);
   765 
   841                 exit(EXIT_SUCCESS);
   766     } else {
   842             
   767         FATAL("too many arguments");
   843             case OPT_DEBUG:
       
   844                 set_log_level(LOG_DEBUG);
       
   845                 break;
       
   846 
       
   847             case OPT_QUIET:
       
   848                 set_log_level(LOG_WARN);
       
   849                 break;
       
   850             
       
   851             case '?':
       
   852                 usage(argv[0]);
       
   853                 exit(EXIT_FAILURE);
       
   854         }
       
   855     }
       
   856 
       
   857     if (optind < argc) {
       
   858         if (optind == argc - 1) {
       
   859             // filter
       
   860             filter = argv[optind];
       
   861             
       
   862             log_info("only running tests: %s", filter);
       
   863         } else {
       
   864             FATAL("too many arguments");
       
   865         }
   768     }
   866     }
   769 
   867 
   770     // run tests
   868     // run tests
   771     for (test = _tests; test->name; test++) {
   869     for (test = _tests; test->name; test++) {
   772         if (filter && strcmp(test->name, filter))
   870         if (filter && strcmp(test->name, filter))
   773             continue;
   871             continue;
   774 
   872 
   775         log_info("Running test: %s", test->name);
   873         log_info("Running test: %s", test->name);
   776 
   874         
       
   875         test_count++;
   777         test->func();
   876         test->func();
   778     }
   877     }
   779 
   878 
   780     log_info("done");
   879     // no tests run?
   781 }
   880     if (test_count == 0)
       
   881         FATAL("no tests run");
       
   882 
       
   883     log_info("done, ran %zu tests", test_count);
       
   884 }