src/test.c
changeset 40 51678c7eae03
child 41 40f7aa051acb
equal deleted inserted replaced
39:a4891d71aca9 40:51678c7eae03
       
     1 /**
       
     2  * The main test code entry point
       
     3  */
       
     4 #include "sock_test.h"
       
     5 #include "irc_conn.h"
       
     6 #include "log.h"
       
     7 
       
     8 #include <string.h>
       
     9 #include <assert.h>
       
    10 
       
    11 void assert_sock_read (struct sock_stream *sock, const char *str)
       
    12 {
       
    13     char buf[strlen(str)];
       
    14 
       
    15     log_debug("read: %p: '%s'", sock, str);
       
    16     
       
    17     // read it
       
    18     assert(sock_stream_read(sock, buf, strlen(str)) == (int) strlen(str));
       
    19 
       
    20     // cmp
       
    21     assert(strncmp(buf, str, strlen(str)) == 0);
       
    22 }
       
    23 
       
    24 void assert_sock_write (struct sock_stream *sock, const char *str)
       
    25 {
       
    26     log_debug("write: %p: '%s'", sock, str);
       
    27 
       
    28     // write it
       
    29     assert(sock_stream_write(sock, str, strlen(str)) == (int) strlen(str));
       
    30 }
       
    31 
       
    32 void test_sock_test (void)
       
    33 {
       
    34     struct sock_test *sock = sock_test_create();
       
    35     struct io_vec _read_data[] = {
       
    36         {   "foo",      3   },
       
    37         {   "barx",     4   }
       
    38     };
       
    39     const char *_write_data = "test data";
       
    40     
       
    41     // put the read data
       
    42     log_debug("set_recv_buffer: %p, %d", _read_data, 2);
       
    43     sock_test_set_recv_buffer(sock, _read_data, 2);
       
    44 
       
    45     // read it out
       
    46     assert_sock_read(SOCK_TEST_BASE(sock), "foo");
       
    47     assert_sock_read(SOCK_TEST_BASE(sock), "ba");
       
    48     assert_sock_read(SOCK_TEST_BASE(sock), "rx");
       
    49 
       
    50     // write the data in
       
    51     assert_sock_write(SOCK_TEST_BASE(sock), "test ");
       
    52     assert_sock_write(SOCK_TEST_BASE(sock), "data");
       
    53 
       
    54     // get the data out
       
    55     char *data;
       
    56     size_t len;
       
    57 
       
    58     sock_test_get_send_data(sock, &data, &len);
       
    59     
       
    60     log_debug("get_send_data: %u: '%s'", len, data);
       
    61 
       
    62     // should be the same
       
    63     assert(len == strlen(_write_data));
       
    64     assert(strncmp(data, _write_data, len) == 0);
       
    65 }
       
    66 
       
    67 static struct irc_conn_callbacks _conn_callbacks = {
       
    68     .on_registered      = NULL,
       
    69 };
       
    70 
       
    71 void test_irc_conn (void)
       
    72 {
       
    73     struct sock_test *sock;
       
    74     struct irc_conn *conn;
       
    75     struct error_info err;
       
    76 
       
    77     // create the test socket
       
    78     assert((sock = sock_test_create()));
       
    79     
       
    80     // create the irc_conn
       
    81     assert(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, NULL, &err) == SUCCESS);
       
    82 
       
    83     // destroy it
       
    84     irc_conn_destroy(conn);
       
    85 }
       
    86 
       
    87 /**
       
    88  * Test definition
       
    89  */
       
    90 static struct test {
       
    91     /** Test name */
       
    92     const char *name;
       
    93 
       
    94     /** Test func */
       
    95     void (*func) (void);
       
    96 
       
    97 } _tests[] = {
       
    98     {   "sock_test",    &test_sock_test     },
       
    99     {   "irc_conn",     &test_irc_conn      },
       
   100     {   NULL,           NULL                }
       
   101 };
       
   102 
       
   103 int main (int argc, char **argv)
       
   104 {
       
   105     struct test *test;
       
   106 
       
   107     (void) argv;
       
   108     
       
   109     // no arguments
       
   110     assert(argc == 1);
       
   111 
       
   112     // run tests
       
   113     for (test = _tests; test->name; test++) {
       
   114         log_info("Running test: %s", test->name);
       
   115 
       
   116         test->func();
       
   117     }
       
   118 }