src/test.c
author Tero Marttila <terom@fixme.fi>
Sun, 03 May 2009 17:19:36 +0300
branchnew-transport
changeset 167 0d2d8ca879d8
parent 129 361740b82fe5
permissions -rw-r--r--
update test.c to reflect new transport stuff
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
/**
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
 * The main test code entry point
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
     4
#include "transport_test.h"
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
     5
#include "line_proto.h"
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
     6
#include "irc_queue.h"
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include "irc_conn.h"
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
     8
#include "irc_net.h"
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
     9
#include "fifo.h"
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
#include "log.h"
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
    11
#include "str.h"
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    12
#include "error.h"
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    14
#include <stdlib.h>
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
#include <string.h>
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
    16
#include <getopt.h>
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
#include <assert.h>
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    18
#include <ctype.h>
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    19
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    20
#define DUMP_STR_BUF 1024
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    21
#define DUMP_STR_COUNT 8
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    22
#define DUMP_STR_TAIL 10
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    23
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
    24
/**
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
    25
 * Global test-running state
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
    26
 */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
    27
struct test_ctx {
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
    28
    /** The event_base that we have setup */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
    29
    struct event_base *ev_base;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
    30
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
    31
} _test_ctx;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
    32
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    33
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    34
/**
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    35
 * This re-formats the given string to escape values, and returns a pointer to an internal static buffer.
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    36
 *
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    37
 * If len is given as >= 0, only the given number of chars will be dumped from str.
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    38
 *
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    39
 * The buffer cycles a bit, so the returned pointers remain valid across DUMP_STR_COUNT calls.
51
cc61eaa841ef add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
    40
 *
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    41
 * The resulting string is truncated to (DUMP_STR_BUF - DUMP_STR_TAIL) bytes, not including the ending "...'\0".
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    42
 *
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    43
 * @param str the string to dump, should be NUL-terminated unless len is given
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    44
 * @param len if negative, ignored, otherwise, only this many bytes are dumped from str
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    45
 * @param return a pointer to a static buffer that remains valid across DUMP_STR_COUNT calls to this function
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    46
 */
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    47
const char *dump_strn (const char *str, ssize_t len)
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    48
{
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    49
    static char dump_buf[DUMP_STR_COUNT][DUMP_STR_BUF];
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    50
    static size_t dump_idx = 0;
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    51
    
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    52
    // pick a buffer to use
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
    53
    char *buf = dump_buf[dump_idx++];
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    54
    
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    55
    // cycle
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    56
    if (dump_idx >= DUMP_STR_COUNT)
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    57
        dump_idx = 0;
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    58
    
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
    59
    str_quote(buf, DUMP_STR_BUF, str, len);
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    60
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    61
    // ok
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
    62
    return buf;
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    63
}
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    64
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    65
const char *dump_str (const char *str) 
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    66
{
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    67
    return dump_strn(str, -1);
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    68
}
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    70
void assert_null (const void *ptr)
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    71
{
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    72
    if (ptr)
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    73
        FATAL("%p != NULL", ptr);
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    74
}
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
    75
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    76
void assert_strcmp (const char *is, const char *should_be)
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    77
{
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    78
    if (!is || strcmp(is, should_be))
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    79
        FATAL("%s != %s", dump_str(is), dump_str(should_be));
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    80
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    81
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    82
void assert_strncmp (const char *is, const char *should_be, size_t n)
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    83
{   
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    84
    if (!is || strncmp(is, should_be, n))
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    85
        FATAL("%s:%u != %s", dump_strn(is, n), (unsigned) n, dump_strn(should_be, n));
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    86
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    87
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    88
void assert_strlen (const char *str, size_t n)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    89
{
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
    90
    if (!str || strlen(str) != n)
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    91
        FATAL("strlen(%s) != %u", dump_str(str), (unsigned) n);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    92
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    93
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
    94
void assert_strnull (const char *str)
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    95
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    96
    if (str != NULL)
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
    97
        FATAL("%s != NULL", dump_str(str));
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    98
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
    99
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   100
void assert_success (err_t err)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   101
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   102
    if (err != SUCCESS)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   103
        FATAL("error: %s", error_name(err));
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   104
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   105
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   106
void assert_err (err_t err, err_t target)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   107
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   108
    if (err != target)
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   109
        FATAL("error: <%s> != <%s>", error_name(err), error_name(target));
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   110
}
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   111
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   112
void assert_error_info (struct error_info *is, struct error_info *should_be)
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   113
{
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   114
    if (ERROR_CODE(is) != ERROR_CODE(should_be) || ERROR_EXTRA(is) != ERROR_EXTRA(should_be))
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   115
        FATAL("error: <%s> != <%s>", error_msg(is), error_msg(should_be));
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   116
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   117
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   118
void assert_transport_read (transport_t *transport, const char *str)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
{
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   120
    size_t len = strlen(str);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   121
    char buf[len];
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   122
    error_t err;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   123
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   124
    log_debug("read: %p: %s", transport, dump_str(str));
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
    // read it
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   127
    assert(transport_read(transport, buf, len, &err) == (int) len);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
    // cmp
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   130
    assert_strncmp(buf, str, len);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   133
void assert_transport_write (transport_t *transport, const char *str)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
{
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   135
    size_t len = strlen(str);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   136
    error_t err;
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   137
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   138
    log_debug("write: %p: %s", transport, dump_str(str));
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
    // write it
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   141
    assert(transport_write(transport, str, len, &err) == (int) len);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   144
void assert_transport_eof (transport_t *transport)
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   145
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   146
    char buf;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   147
    error_t err;
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   148
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   149
    log_debug("eof: %p", transport);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   150
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   151
    assert_err(-transport_read(transport, &buf, 1, &err), ERR_EOF);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   152
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   153
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   154
void assert_transport_data (struct transport_test *tp, const char *fmt, ...)
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   155
{
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   156
    char buf[TRANSPORT_TEST_FMT_MAX];
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   157
    va_list vargs;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   158
    size_t len;
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   159
    
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   160
    va_start(vargs, fmt);
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   161
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   162
    if ((len = vsnprintf(buf, TRANSPORT_TEST_FMT_MAX, fmt, vargs)) >= TRANSPORT_TEST_FMT_MAX)
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   163
        FATAL("input too long: %zu bytes", len);
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   164
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   165
    va_end(vargs);
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   166
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   167
    // get the data out
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   168
    char *out;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   169
    
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   170
    transport_test_pull_buf(tp, &out, &len);
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   171
    
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   172
    log_debug("pull_buf: %s", dump_strn(out, len));
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   173
    
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   174
    // should be the same
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   175
    assert_strncmp(out, buf, len);
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   176
    assert_strlen(buf, len);
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   177
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   178
    // cleanup
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   179
    free(out);
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   180
}
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   181
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
   182
/**
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   183
 * Setup the global sock_stream state
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   184
 */
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   185
struct event_base* setup_sock (void)
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   186
{
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   187
    struct event_base *ev_base;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   188
    struct error_info err;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   189
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   190
    assert((ev_base = event_base_new()));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   191
    assert_success(sock_init(ev_base, &err));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   192
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   193
    return ev_base;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   194
}
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   195
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   196
/**
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   197
 * Create an empty transport_test
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   198
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   199
struct transport_test* setup_transport_test ()
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   200
{
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   201
    struct transport_test *tp;
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   202
   
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   203
    assert ((tp = transport_test_create(NULL)) != NULL);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   204
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   205
    return tp;
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   206
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   207
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   208
void assert_str_quote (size_t buf_size, const char *data, ssize_t len, const char *target, size_t out)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   209
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   210
    char buf[buf_size];
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   211
    
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   212
    size_t ret = str_quote(buf, buf_size, data, len);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   213
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   214
    log_debug("str_quote(%zu, %zd) -> %s:%zu / %s:%zu", buf_size, len, buf, ret, target, out);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   215
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   216
    assert_strcmp(buf, target);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   217
    assert(ret == out);
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   218
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   219
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   220
void test_str_quote (void)
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   221
{
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   222
    log_info("testing str_quote()");
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   223
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   224
    assert_str_quote(5,     NULL,           -1,     "NULL",         4   );
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   225
    assert_str_quote(16,    "foo",          -1,     "'foo'",        5   );
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   226
    assert_str_quote(16,    "foobar",       3,      "'foo'",        5   );
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   227
    assert_str_quote(16,    "\r\n",         -1,     "'\\r\\n'",     6   );
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   228
    assert_str_quote(16,    "\x13",         -1,     "'\\x13'",      6   );
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   229
    assert_str_quote(16,    "x'y",          -1,     "'x\\'y'",      6   );
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   230
    assert_str_quote(7,     "1234567890",   -1,     "'1'...",       12  );
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   231
    assert_str_quote(9,     "1234567890",   -1,     "'123'...",     12  );
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   232
}
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
   233
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   234
struct str_format_ctx {
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   235
    const char *name;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   236
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   237
    const char *value;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   238
};
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   239
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   240
err_t test_str_format_cb (const char *name, const char **value, ssize_t *value_len, void *arg)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   241
{
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   242
    struct str_format_ctx *ctx = arg;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   243
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   244
    assert_strcmp(name, ctx->name);
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   245
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   246
    *value = ctx->value;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   247
    *value_len = -1;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   248
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   249
    return SUCCESS;
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   250
}
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   251
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   252
void assert_str_format (const char *format, const char *name, const char *value, const char *out)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   253
{
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   254
    struct str_format_ctx ctx = { name, value };
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   255
    char buf[512];
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   256
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   257
    assert_success(str_format(buf, sizeof(buf), format, test_str_format_cb, &ctx));
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   258
    
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   259
    log_debug("str_format(%s), { %s:%s } -> %s / %s", format, name, value, buf, out);
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   260
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   261
    assert_strcmp(buf, out);
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   262
}
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   263
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   264
void test_str_format (void)
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   265
{
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   266
    log_info("test str_format()");
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   267
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   268
    assert_str_format("foo", NULL, NULL, "foo");
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   269
    assert_str_format("foo {bar} quux", "bar", "XXX", "foo XXX quux");
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   270
}
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
   271
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   272
void test_dump_str (void)
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   273
{
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   274
    log_info("dumping example strings on stdout:");
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   275
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   276
    log_debug("normal: %s", dump_str("Hello World"));
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   277
    log_debug("escapes: %s", dump_str("foo\r\nbar\a\001"));
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   278
    log_debug("length: %s", dump_strn("<-->**", 4));
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   279
    log_debug("overflow: %s", dump_str( "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   280
    log_debug("null: %s", dump_str(NULL));
51
cc61eaa841ef add some comments and quote (' + \) to dump_str
Tero Marttila <terom@fixme.fi>
parents: 50
diff changeset
   281
    log_debug("quote: %s", dump_str("foo\\bar'quux"));
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   282
}
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   283
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   284
void test_transport_test (void)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   285
{
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   286
    struct transport_info info = { NULL, NULL, 0 };
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   287
    struct transport_test *tp = transport_test_create(&info);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   288
    transport_t *transport = transport_test_cast(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   289
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   290
    // put the read data
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   291
    log_info("test transport_test_push_*");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   292
    transport_test_push_buf(tp, "foo", 3);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   293
    transport_test_push_str(tp, "barx");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   294
    transport_test_push_fmt(tp, "xx %s xx", "quux");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   295
    transport_test_push_eof(tp);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   296
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   297
    // read it out
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   298
    log_info("test transport_test_read");
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   299
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   300
    assert_transport_read(transport, "foo");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   301
    assert_transport_read(transport, "ba");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   302
    assert_transport_read(transport, "rx");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   303
    assert_transport_read(transport, "xx quux xx");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   304
    assert_transport_eof(transport);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   305
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   306
    // write some data in
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   307
    log_info("test transport_test_write");
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   308
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   309
    assert_transport_write(transport, "test ");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   310
    assert_transport_write(transport, "data");
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   311
    
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   312
    // check output
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   313
    log_info("test transport_test_pull_*");
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   314
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   315
    assert_transport_data(tp, "test data");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   316
    assert_transport_data(tp, "");
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   317
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   318
    // cleanup
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   319
    transport_test_destroy(tp);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   320
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   321
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   322
void assert_read_line (struct line_proto *lp, const char *line_str)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   323
{
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   324
    char *line_buf;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   325
    
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   326
    log_debug("expect: %s", dump_str(line_str));
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   327
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   328
    assert_success(line_proto_recv(lp, &line_buf));
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   329
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   330
    if (line_str) {
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   331
        assert_strcmp(line_buf, line_str);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   332
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   333
    } else {
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   334
        assert_strnull(line_buf);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   335
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   336
    }
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   337
}
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   338
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   339
/**
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   340
 * Context info for test_line_proto callbacks
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   341
 */
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   342
struct _lp_test_ctx {
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   343
    /** Expected line */
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   344
    const char *line;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   345
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   346
    /** Expected error */
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   347
    struct error_info err;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   348
};
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   349
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   350
static void _lp_on_line (char *line, void *arg)
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   351
{
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   352
    struct _lp_test_ctx *ctx = arg;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   353
50
46c3983638d3 add dump_str function to test
Tero Marttila <terom@fixme.fi>
parents: 49
diff changeset
   354
    log_debug("%s", dump_str(line));
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   355
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   356
    assert_strcmp(line, ctx->line);
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   357
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   358
    ctx->line = NULL;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   359
}
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   360
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   361
static void _lp_on_error (struct error_info *err, void *arg)
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   362
{
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   363
    struct _lp_test_ctx *ctx = arg;
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   364
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   365
    assert_error_info(err, &ctx->err);
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   366
}
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   367
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   368
static struct line_proto_callbacks _lp_callbacks = {
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   369
    .on_line        = &_lp_on_line,
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   370
    .on_error       = &_lp_on_error,
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   371
};
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   372
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   373
void test_line_proto (void)
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   374
{
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   375
    struct transport_test *tp = transport_test_create(NULL);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   376
    transport_t *transport = transport_test_cast(tp);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   377
    struct line_proto *lp;
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   378
    struct _lp_test_ctx ctx;
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   379
    struct error_info err;
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   380
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   381
    // put the read data
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   382
    log_debug("transport_test_push_*");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   383
    transport_test_push_str(tp, "hello\r\n");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   384
    transport_test_push_str(tp, "world\n");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   385
    transport_test_push_str(tp, "this ");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   386
    transport_test_push_str(tp, "is a line\r");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   387
    transport_test_push_str(tp, "\nfragment");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   388
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   389
    // create the lp
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   390
    assert_success(line_proto_create(&lp, transport, 128, &_lp_callbacks, &ctx, &err));
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   391
    
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   392
    log_info("test line_proto_recv");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   393
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   394
    // then read some lines from it
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   395
    assert_read_line(lp, "hello");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   396
    assert_read_line(lp, "world");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   397
    assert_read_line(lp, "this is a line");
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   398
    assert_read_line(lp, NULL);
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   399
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   400
    // then add a final bit to trigger on_line
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   401
    log_info("test on_line");
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   402
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   403
    ctx.line = "fragment";
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   404
    transport_test_push_str(tp, "\r\n");
42
13cfc41f76a7 test async operation
Tero Marttila <terom@fixme.fi>
parents: 41
diff changeset
   405
    assert_strnull(ctx.line);
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   406
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   407
    // test writing
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   408
    log_info("test line_proto_send");
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   409
    assert_success(-line_proto_send(lp, "foobar\r\n"));
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   410
    assert_success(-line_proto_send(lp, "quux\r\n"));
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   411
    assert_transport_data(tp, "foobar\r\nquux\r\n");
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   412
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   413
    // XXX: test partial writes
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   414
41
40f7aa051acb add line_proto test, enhance others
Tero Marttila <terom@fixme.fi>
parents: 40
diff changeset
   415
    // cleanup
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   416
    line_proto_destroy(lp);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   417
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   418
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   419
void test_irc_queue (void)
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   420
{
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   421
    struct transport_test *tp = transport_test_create(NULL);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   422
    transport_t *transport = transport_test_cast(tp);
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   423
    struct line_proto *lp;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   424
    struct irc_queue *queue;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   425
    struct irc_queue_entry *queue_entry;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   426
    struct error_info err;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   427
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   428
    // create the lp
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   429
    assert_success(line_proto_create(&lp, transport, 128, &_lp_callbacks, NULL, &err));
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   430
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   431
    // create the queue
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   432
    assert_success(irc_queue_create(&queue, _test_ctx.ev_base, lp, &err));
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   433
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   434
    struct irc_line line = {
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   435
        NULL, "TEST", { "fooX" }
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   436
    };
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   437
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   438
    // then test simple writes, we should be able to push five lines directly
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   439
    log_info("test irc_queue_process (irc_queue_send_direct)");
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   440
    line.args[0] = "foo0"; assert_success(irc_queue_process(queue, &line));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   441
    line.args[0] = "foo1"; assert_success(irc_queue_process(queue, &line));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   442
    line.args[0] = "foo2"; assert_success(irc_queue_process(queue, &line));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   443
    line.args[0] = "foo3"; assert_success(irc_queue_process(queue, &line));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   444
    line.args[0] = "foo4"; assert_success(irc_queue_process(queue, &line));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   445
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   446
    // they should all be output
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   447
    assert_transport_data(tp,
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   448
            "TEST foo0\r\n"
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   449
            "TEST foo1\r\n"
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   450
            "TEST foo2\r\n"
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   451
            "TEST foo3\r\n"
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   452
            "TEST foo4\r\n"
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   453
    );
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   454
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   455
    // then enqueue
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   456
    log_info("test irc_queue_process (irc_queue_put)");
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   457
    line.args[0] = "foo5"; assert_success(irc_queue_process(queue, &line));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   458
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   459
    // ensure it was enqueued
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   460
    assert((queue_entry = TAILQ_FIRST(&queue->list)) != NULL);
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   461
    assert_strcmp(queue_entry->line_buf, "TEST foo5\r\n");
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   462
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   463
    // ensure timer is set
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   464
    assert(event_pending(queue->ev, EV_TIMEOUT, NULL));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   465
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   466
    // run the event loop to let the timer run
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   467
    log_info("running the event loop once...");
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   468
    assert(event_base_loop(_test_ctx.ev_base, EVLOOP_ONCE) == 0);
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   469
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   470
    // test to check that the line was now sent
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   471
    log_info("checking that the delayed line was sent...");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   472
    assert_transport_data(tp, "TEST foo5\r\n");
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   473
    assert(TAILQ_EMPTY(&queue->list));
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   474
    assert(!event_pending(queue->ev, EV_TIMEOUT, NULL));
91
bca23cbe1dce implement irc_queue for irc_conn, and add missing irc_queue_destroy, fix irc_conn_destroy
Tero Marttila <terom@fixme.fi>
parents: 90
diff changeset
   475
bca23cbe1dce implement irc_queue for irc_conn, and add missing irc_queue_destroy, fix irc_conn_destroy
Tero Marttila <terom@fixme.fi>
parents: 90
diff changeset
   476
    // cleanup
bca23cbe1dce implement irc_queue for irc_conn, and add missing irc_queue_destroy, fix irc_conn_destroy
Tero Marttila <terom@fixme.fi>
parents: 90
diff changeset
   477
    irc_queue_destroy(queue);
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   478
}
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
   479
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   480
struct test_conn_ctx {
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   481
    /** Callback flags */
49
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   482
    bool on_registered, on_TEST, on_error, on_quit;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   483
};
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   484
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   485
static void _conn_on_registered (struct irc_conn *conn, void *arg)
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   486
{
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   487
    struct test_conn_ctx *ctx = arg;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   488
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   489
    (void) conn;
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   490
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   491
    if (ctx) ctx->on_registered = true;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   492
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   493
    log_debug("registered");
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   494
}
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   495
49
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   496
static void _conn_on_error (struct irc_conn *conn, struct error_info *err, void *arg)
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   497
{
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   498
    struct test_conn_ctx *ctx = arg;
49
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   499
    
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   500
    (void) conn;
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   501
    (void) err;
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   502
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   503
    if (ctx) ctx->on_error = true;
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   504
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   505
    log_debug("on_error");
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   506
}
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   507
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   508
static void _conn_on_quit (struct irc_conn *conn, void *arg)
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   509
{
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   510
    struct test_conn_ctx *ctx = arg;
49
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   511
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   512
    (void) conn;
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   513
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   514
    if (ctx) ctx->on_quit = true;
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   515
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   516
    log_debug("on_quit");
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   517
}
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   518
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   519
static void _conn_on_TEST (const struct irc_line *line, void *arg)
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   520
{
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   521
    struct test_conn_ctx *ctx = arg;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   522
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   523
    assert(line->source);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   524
    assert(!line->source->nickname && !line->source->username && line->source->hostname);
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   525
    assert_strcmp(line->command, "TEST");
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   526
    assert_strcmp(line->args[0], "arg0");
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   527
    assert_strnull(line->args[1]);
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   528
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   529
    if (ctx) ctx->on_TEST = true;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   530
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   531
    log_debug("on_TEST");
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   532
}
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   533
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   534
static struct irc_conn_callbacks _conn_callbacks = {
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   535
    .on_registered      = &_conn_on_registered,
49
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   536
    .on_error           = &_conn_on_error,
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   537
    .on_quit            = &_conn_on_quit,
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   538
};
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   539
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   540
static struct irc_cmd_handler _conn_handlers[] = {
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   541
    {   "TEST",         &_conn_on_TEST  },
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   542
    {   NULL,           NULL            }
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   543
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   544
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   545
/**
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   546
 * Create and return a new irc_conn with the given ctx (will be initialized to zero).
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   547
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   548
struct irc_conn* setup_irc_conn (struct transport_test *tp, bool noisy, struct test_conn_ctx *ctx)
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   549
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   550
    struct irc_conn *conn;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   551
    struct error_info err;
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   552
    struct irc_conn_register_info register_info = {
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   553
        "nick", "user", "realname"
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   554
    };
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   555
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   556
    // init the ctx
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   557
    memset(ctx, 0, sizeof(*ctx));
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   558
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   559
    // create the irc_conn
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   560
    assert_success(irc_conn_create(&conn, transport_test_cast(tp), &_conn_callbacks, ctx, &err));
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   561
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   562
    // test register
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   563
    if (noisy) log_info("test irc_conn_register");
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   564
    assert_success(irc_conn_register(conn, &register_info));
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   565
    assert_transport_data(tp, "NICK nick\r\nUSER user 0 * realname\r\n");
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   566
 
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   567
    // test on_register callback    
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   568
    if (noisy) log_info("test irc_conn_callbacks.on_register");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   569
    transport_test_push_str(tp, "001 mynick :Blaa blaa blaa\r\n");
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   570
    if (ctx) assert(ctx->on_registered);
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   571
    assert_strcmp(conn->nickname, "mynick");
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   572
   
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   573
    // ok
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   574
    return conn;
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   575
}
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   576
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   577
void test_irc_conn (void)
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   578
{
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   579
    struct test_conn_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   580
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   581
    struct irc_conn *conn = setup_irc_conn(tp, true, &ctx);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   582
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   583
    // add our test handlers
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   584
    assert_success(irc_conn_add_cmd_handlers(conn, _conn_handlers, &ctx));
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   585
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   586
    // test on_TEST handler
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
   587
    // XXX: come up with a better prefix
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   588
    log_info("test irc_conn.handlers");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   589
    transport_test_push_str(tp, ":foobar-prefix TEST arg0\r\n");
43
42f5dc680930 improve irc_conn test
Tero Marttila <terom@fixme.fi>
parents: 42
diff changeset
   590
    assert(ctx.on_TEST);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   591
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   592
    // test PING/PONG
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   593
    log_info("test PING/PONG");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   594
    transport_test_push_str(tp, "PING foo\r\n");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   595
    assert_transport_data(tp, "PONG foo\r\n");
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   596
49
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   597
    // quit nicely
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   598
    log_info("test QUIT");
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   599
    assert_success(irc_conn_QUIT(conn, "bye now"));
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   600
    assert_transport_data(tp, "QUIT :bye now\r\n");
49
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   601
    assert(conn->quitting);
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   602
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   603
    transport_test_push_str(tp, "ERROR :Closing Link: Quit\r\n");
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   604
    transport_test_push_eof(tp);
49
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   605
    assert(conn->quit && !conn->quitting && !conn->registered);
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   606
    assert(ctx.on_quit);
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   607
    assert(!ctx.on_error);
96e0f703a58c update irc_conn status, and add tests for irc_conn_QUIT
Tero Marttila <terom@fixme.fi>
parents: 47
diff changeset
   608
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   609
    // destroy it
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   610
    irc_conn_destroy(conn);
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   611
}
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   612
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   613
void test_irc_conn_self_nick (void)
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   614
{
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   615
    struct test_conn_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   616
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   617
    struct irc_conn *conn = setup_irc_conn(tp, false, &ctx);
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   618
    
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   619
    log_info("test irc_conn_on_NICK");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   620
    transport_test_push_fmt(tp, ":mynick!user@somehost NICK mynick2\r\n");
76
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   621
    assert_strcmp(conn->nickname, "mynick2");
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   622
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   623
    // cleanup
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   624
    irc_conn_destroy(conn);
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   625
}
b3672e3d9665 add irc_conn_self_nick test
Tero Marttila <terom@fixme.fi>
parents: 75
diff changeset
   626
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   627
struct test_chan_ctx {
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   628
    /** The channel name */
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   629
    const char *channel;
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   630
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   631
    /** The channel we're supposed to be testing */
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   632
    struct irc_chan *chan;
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   633
    
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   634
    /** Flags for callbacks called*/
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   635
    bool on_chan_self_join, on_chan_self_part, on_chan_join, on_chan_part;
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   636
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   637
};
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   638
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   639
void _on_chan_self_join (struct irc_chan *chan, void *arg)
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   640
{
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   641
    struct test_chan_ctx *ctx = arg;
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   642
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   643
    assert(chan == ctx->chan);
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   644
    
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   645
    ctx->on_chan_self_join = true;
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   646
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   647
    log_debug("on_self_join");
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   648
}
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   649
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   650
void _on_chan_join (struct irc_chan *chan, const struct irc_nm *source, void *arg)
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   651
{
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   652
    struct test_chan_ctx *ctx = arg;
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   653
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   654
    assert(chan == ctx->chan);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   655
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   656
    // XXX: verify source
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   657
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   658
    ctx->on_chan_join = true;
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   659
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   660
    log_debug("on_join");
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   661
}
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   662
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   663
void _on_chan_part (struct irc_chan *chan, const struct irc_nm *source, const char *msg, void *arg)
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   664
{
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   665
    struct test_chan_ctx *ctx = arg;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   666
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   667
    assert(chan == ctx->chan);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   668
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   669
    // XXX: verify source
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   670
    // XXX: verify msg
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   671
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   672
    ctx->on_chan_part = true;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   673
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   674
    log_debug("on_part");
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   675
}
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   676
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   677
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   678
struct irc_chan_callbacks _chan_callbacks = {
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   679
    .on_self_join       = &_on_chan_self_join,
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   680
    .on_join            = &_on_chan_join,
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   681
    .on_part            = &_on_chan_part,
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   682
};
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   683
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   684
/**
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   685
 * Setup an irc_net using the given socket, and consume the register request output, but do not push the RPL_WELCOME
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   686
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   687
struct irc_net* setup_irc_net_unregistered (struct transport_test *tp)
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   688
{
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   689
    struct irc_net *net;
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   690
    struct irc_net_info net_info = {
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   691
        .register_info = {
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   692
            "nick", "user", "realname"
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   693
        },
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   694
    };
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   695
    struct error_info err;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   696
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   697
    // create the irc_net
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   698
    net_info.transport = transport_test_cast(tp);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   699
    assert_success(irc_net_create(&net, &net_info, &err));
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   700
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   701
    // test register output
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   702
    assert_transport_data(tp, "NICK nick\r\nUSER user 0 * realname\r\n");
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   703
    
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   704
    // ok
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   705
    return net; 
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   706
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   707
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   708
/**
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   709
 * Push to RPL_WELCOME reply, and test state
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   710
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   711
void do_irc_net_welcome (struct transport_test *tp, struct irc_net *net)
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   712
{
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   713
    // registration reply
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   714
    transport_test_push_fmt(tp, "001 mynick :Blaa blaa blaa\r\n");
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   715
    assert(net->conn->registered);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   716
    assert_strcmp(net->conn->nickname, "mynick");
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   717
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   718
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   719
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   720
/**
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   721
 * Creates an irc_net and puts it into the registered state
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   722
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   723
struct irc_net* setup_irc_net (struct transport_test *tp)
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   724
{
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   725
    struct irc_net *net;   
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   726
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   727
    net = setup_irc_net_unregistered(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   728
    do_irc_net_welcome(tp, net);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   729
    
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   730
    // ok
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   731
    return net;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   732
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   733
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   734
/**
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   735
 * General test for irc_net to handle startup
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   736
 */
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   737
void test_irc_net (void)
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   738
{
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   739
    struct transport_test *tp = setup_transport_test();
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   740
    
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   741
    // create the network
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   742
    log_info("test irc_net_create");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   743
    struct irc_net *net = setup_irc_net_unregistered(tp);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   744
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   745
    // send the registration reply
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   746
    log_info("test irc_conn_on_RPL_WELCOME");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   747
    do_irc_net_welcome(tp, net);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   748
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   749
    // test errors by setting EOF
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   750
    log_info("test irc_net_error");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   751
    transport_test_push_eof(tp);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   752
    assert(net->conn == NULL);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   753
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   754
    // cleanup
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   755
    irc_net_destroy(net);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   756
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   757
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   758
/**
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   759
 * Ensure that an irc_chan_user exists/doesn't exist for the given channel/nickname, and return it
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   760
 */
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   761
struct irc_chan_user* check_chan_user (struct irc_chan *chan, const char *nickname, bool exists)
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   762
{
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   763
    struct irc_chan_user *chan_user = irc_chan_get_user(chan, nickname);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   764
    
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   765
    if (exists && chan_user == NULL)
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   766
        FATAL("user %s not found in channel %s", dump_str(nickname), dump_str(irc_chan_name(chan)));
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   767
    
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   768
    if (!exists && chan_user)
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   769
        FATAL("user %s should not be on channel %s anymore", dump_str(nickname), dump_str(irc_chan_name(chan)));
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   770
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   771
    log_debug("%s, exists=%d -> %p: user=%p, nickname=%s", 
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   772
            nickname, exists, chan_user, chan_user ? chan_user->user : NULL, chan_user ? chan_user->user->nickname : NULL);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   773
    
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   774
    if (chan_user)
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   775
        assert_strcmp(chan_user->user->nickname, nickname);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   776
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   777
    return chan_user;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   778
}
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   779
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   780
/**
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   781
 * Creates an irc_chan on the given irc_net, but does not check any output (useful for testing offline add).
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   782
 *
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   783
 * You must pass a test_chan_ctx for use with later operations, this will be initialized for you.
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   784
 */
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   785
struct irc_chan* setup_irc_chan_raw (struct irc_net *net, const char *channel, struct test_chan_ctx *ctx)
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   786
{
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   787
    struct irc_chan *chan;
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   788
    struct irc_chan_info chan_info = {
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   789
        .channel        = channel,
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   790
    };
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   791
    struct error_info err;
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   792
    
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   793
    // initialize the given ctx
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   794
    memset(ctx, 0, sizeof(*ctx));
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   795
    ctx->channel = channel;
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   796
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   797
    // add a channel
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   798
    assert_success(irc_net_add_chan(net, &chan, &chan_info, &err));
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   799
    assert(!chan->joined);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   800
    assert_success(irc_chan_add_callbacks(chan, &_chan_callbacks, ctx));
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   801
    ctx->chan = chan;
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   802
    
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   803
    // ok
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   804
    return chan;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   805
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   806
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   807
/**
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   808
 * Checks that the JOIN request for a channel was sent, and sends the basic JOIN reply
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   809
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   810
void do_irc_chan_join (struct transport_test *tp, struct test_chan_ctx *ctx)
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   811
{
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   812
    // JOIN request
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   813
    assert(ctx->chan->joining);
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   814
    assert_transport_data(tp, "JOIN %s\r\n", ctx->channel);
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   815
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   816
    // JOIN reply
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   817
    transport_test_push_fmt(tp, ":mynick!user@host JOIN %s\r\n", ctx->channel);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   818
    assert(!ctx->chan->joining && ctx->chan->joined);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   819
    assert(ctx->on_chan_self_join);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   820
}
46
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
   821
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   822
/**
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   823
 * Sends a short RPL_NAMREPLY/RPL_ENDOFNAMES reply and checks that the users list matches
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   824
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   825
void do_irc_chan_namreply (struct transport_test *tp, struct test_chan_ctx *ctx)
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   826
{
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   827
    // RPL_NAMREPLY
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   828
    transport_test_push_fmt(tp, "353 mynick = %s :mynick userA +userB @userC\r\n", ctx->channel);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   829
    transport_test_push_fmt(tp, "353 mynick = %s :trailingspace \r\n", ctx->channel);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   830
    transport_test_push_fmt(tp, "366 mynick %s :End of NAMES\r\n", ctx->channel);
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   831
    
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   832
    // XXX: this should be an exclusive test, i.e. these should be the only ones...
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   833
    check_chan_user(ctx->chan, "mynick", true);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   834
    check_chan_user(ctx->chan, "userA", true);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   835
    check_chan_user(ctx->chan, "userB", true);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   836
    check_chan_user(ctx->chan, "userC", true);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   837
}
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   838
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   839
/**
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   840
 * Creates an irc_chan on the given irc_net, and checks up to the JOIN reply
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   841
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   842
struct irc_chan* setup_irc_chan_join (struct transport_test *tp, struct irc_net *net, const char *channel, struct test_chan_ctx *ctx)
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   843
{
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   844
    setup_irc_chan_raw(net, channel, ctx);
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   845
    do_irc_chan_join(tp, ctx);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   846
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   847
    // ok
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   848
    return ctx->chan;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   849
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   850
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   851
/**
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   852
 * Creates an irc_chan on the given irc_net, sends the JOIN stuff plus RPL_NAMREPLY
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   853
 */
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   854
struct irc_chan* setup_irc_chan (struct transport_test *tp, struct irc_net *net, const char *channel, struct test_chan_ctx *ctx)
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   855
{
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   856
    setup_irc_chan_raw(net, channel, ctx);
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   857
    do_irc_chan_join(tp, ctx);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   858
    do_irc_chan_namreply(tp, ctx);
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   859
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   860
    // ok
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   861
    return ctx->chan;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   862
}
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   863
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   864
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   865
/**
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   866
 * Call irc_net_add_chan while offline, and ensure that we send the JOIN request after RPL_WELCOME, and handle the join
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   867
 * reply OK.
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   868
 */
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   869
void test_irc_chan_add_offline (void)
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   870
{
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   871
    struct test_chan_ctx ctx;
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   872
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   873
    struct transport_test *tp = setup_transport_test();
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   874
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   875
    log_info("test irc_net_create");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   876
    struct irc_net *net = setup_irc_net_unregistered(tp);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   877
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   878
    // add an offline channel
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   879
    log_info("test offline irc_net_add_chan");
77
5478ade62546 use vsnprintf in assert_sock_data/test_sock_push
Tero Marttila <terom@fixme.fi>
parents: 76
diff changeset
   880
    struct irc_chan *chan = setup_irc_chan_raw(net, "#test", &ctx);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   881
    assert(!chan->joining && !chan->joined);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   882
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   883
    // send the registration reply
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   884
    log_info("test irc_conn_on_RPL_WELCOME");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   885
    do_irc_net_welcome(tp, net);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   886
    
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   887
    // test the join sequence
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   888
    log_info("test irc_chan_join/irc_chan_on_JOIN");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   889
    do_irc_chan_join(tp, &ctx);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   890
    
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   891
    // cleanup
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   892
    irc_net_destroy(net);
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   893
}
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   894
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   895
void test_irc_chan_namreply (void)
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   896
{
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   897
    struct test_chan_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   898
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   899
    struct irc_net *net = setup_irc_net(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   900
    setup_irc_chan_join(tp, net, "#test", &ctx);
72
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   901
43084f103c2a add irc_user module for irc_chan to track users on a channel
Tero Marttila <terom@fixme.fi>
parents: 52
diff changeset
   902
    log_info("test irc_chan_on_RPL_NAMREPLY");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   903
    do_irc_chan_namreply(tp, &ctx);
47
7d4094eb3117 add irc_net_destroy and fix code to be valgrind-clean on bin/test
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   904
7d4094eb3117 add irc_net_destroy and fix code to be valgrind-clean on bin/test
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   905
    // cleanup
7d4094eb3117 add irc_net_destroy and fix code to be valgrind-clean on bin/test
Tero Marttila <terom@fixme.fi>
parents: 46
diff changeset
   906
    irc_net_destroy(net);
44
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   907
}
6bd70113e1ed add irc_net test
Tero Marttila <terom@fixme.fi>
parents: 43
diff changeset
   908
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   909
void test_irc_chan_user_join (void)
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   910
{
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   911
    struct test_chan_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   912
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   913
    struct irc_net *net = setup_irc_net(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   914
    struct irc_chan *chan = setup_irc_chan(tp, net, "#test", &ctx);
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   915
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   916
    // have a user join
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   917
    log_info("test irc_chan_on_JOIN");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   918
    transport_test_push_fmt(tp, ":newuser!someone@somewhere JOIN %s\r\n", "#test");
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   919
    assert(ctx.on_chan_join);
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   920
    check_chan_user(chan, "newuser", true);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   921
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   922
    // cleanup
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   923
    irc_net_destroy(net);
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   924
}
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   925
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   926
void test_irc_chan_user_part (void)
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   927
{
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   928
    struct test_chan_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   929
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   930
    struct irc_net *net = setup_irc_net(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   931
    struct irc_chan *chan = setup_irc_chan(tp, net, "#test", &ctx);
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   932
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   933
    // have a user join
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   934
    log_info("test irc_chan_on_PART");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   935
    transport_test_push_fmt(tp, ":userA!someone@somewhere PART %s\r\n", "#test");
74
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   936
    assert(ctx.on_chan_part); ctx.on_chan_part = NULL;
11ec458d1cbf add irc_chan_on_PART, irc_net_put_user and test_irc_chan_user_part
Tero Marttila <terom@fixme.fi>
parents: 73
diff changeset
   937
    check_chan_user(chan, "userA", false);
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   938
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   939
    // cleanup
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   940
    irc_net_destroy(net);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   941
}
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
   942
89
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   943
void test_irc_chan_user_kick (void)
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   944
{
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   945
    struct test_chan_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   946
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   947
    struct irc_net *net = setup_irc_net(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   948
    struct irc_chan *chan = setup_irc_chan(tp, net, "#test", &ctx);
89
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   949
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   950
    // kick a user
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   951
    log_info("test irc_chan_on_KICK (other)");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   952
    transport_test_push_fmt(tp, ":userA!someone@somewhere KICK %s userB\r\n", "#test");
89
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   953
    check_chan_user(chan, "userA", true);
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   954
    check_chan_user(chan, "userB", false);
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   955
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   956
    // cleanup
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   957
    irc_net_destroy(net);
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   958
}
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   959
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   960
void test_irc_chan_self_kick (void)
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   961
{
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   962
    struct test_chan_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   963
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   964
    struct irc_net *net = setup_irc_net(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   965
    struct irc_chan *chan = setup_irc_chan(tp, net, "#test", &ctx);
89
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   966
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   967
    // kick a user
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   968
    log_info("test irc_chan_on_KICK (self)");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   969
    transport_test_push_fmt(tp, ":userA!someone@somewhere KICK %s mynick foobar\r\n", "#test");
89
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   970
    assert(!chan->joined);
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   971
    assert(chan->kicked);
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   972
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   973
    // cleanup
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   974
    irc_net_destroy(net);
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   975
}
68345a9b99a3 irc_chan_on_KICK
Tero Marttila <terom@fixme.fi>
parents: 88
diff changeset
   976
78
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   977
void test_irc_chan_user_nick (void)
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   978
{
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   979
    struct test_chan_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   980
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   981
    struct irc_net *net = setup_irc_net(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   982
    struct irc_chan *chan = setup_irc_chan(tp, net, "#test", &ctx);
78
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   983
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   984
    // rename one of the users
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   985
    log_info("test irc_net_on_chanuser");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   986
    transport_test_push_fmt(tp, ":userA!someone@somewhere NICK userA2\r\n");
78
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   987
    check_chan_user(chan, "userA", false);
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   988
    check_chan_user(chan, "userB", true);
81
d90edc052352 update TODO and fix some valgrind errors in test
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   989
d90edc052352 update TODO and fix some valgrind errors in test
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   990
    // cleanup
d90edc052352 update TODO and fix some valgrind errors in test
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
   991
    irc_net_destroy(net);
78
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   992
}
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   993
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   994
void test_irc_chan_user_quit (void)
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   995
{
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   996
    struct test_chan_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   997
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   998
    struct irc_net *net = setup_irc_net(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
   999
    struct irc_chan *chan = setup_irc_chan(tp, net, "#test", &ctx);
78
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
  1000
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
  1001
    // rename one of the users
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
  1002
    log_info("test irc_net_on_chanuser");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1003
    transport_test_push_fmt(tp, ":userA!someone@somewhere QUIT foo\r\n");
78
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
  1004
    check_chan_user(chan, "userA", false);
81
d90edc052352 update TODO and fix some valgrind errors in test
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
  1005
d90edc052352 update TODO and fix some valgrind errors in test
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
  1006
    // cleanup
d90edc052352 update TODO and fix some valgrind errors in test
Tero Marttila <terom@fixme.fi>
parents: 78
diff changeset
  1007
    irc_net_destroy(net);
78
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
  1008
}
941bb8379d3d implement NICK/QUIT handling for irc_net/irc_chan
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
  1009
88
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1010
void _test_irc_chan_on_CTCP_ACTION (const struct irc_line *line, void *arg)
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1011
{
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1012
    bool *flag = arg;
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1013
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1014
    log_debug("CTCP ACTION");
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1015
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1016
    *flag = true;
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1017
}
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1018
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1019
static struct irc_cmd_handler _test_irc_chan_handlers[] = {
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1020
    {   "CTCP ACTION",      &_test_irc_chan_on_CTCP_ACTION  },
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1021
    {   NULL,               NULL                            }
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1022
};
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1023
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1024
void test_irc_chan_CTCP_ACTION (void)
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1025
{
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1026
    struct test_chan_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1027
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1028
    struct irc_net *net = setup_irc_net(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1029
    struct irc_chan *chan = setup_irc_chan(tp, net, "#test", &ctx);
88
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1030
    bool cb_ok = false;
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1031
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1032
    // add our handler
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1033
    assert_success(irc_cmd_add(&chan->handlers, _test_irc_chan_handlers, &cb_ok));
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1034
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1035
    // rename one of the users
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1036
    log_info("test irc_conn_on_CTCP_ACTION");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1037
    transport_test_push_fmt(tp, ":userA!someone@somewhere PRIVMSG #test \001ACTION hello  world\001\r\n");
88
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1038
    assert(cb_ok);
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1039
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1040
    // cleanup
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1041
    irc_net_destroy(net);
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1042
}
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1043
97
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1044
void test_irc_chan_privmsg (void)
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1045
{
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1046
    struct test_chan_ctx ctx;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1047
    struct transport_test *tp = setup_transport_test();
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1048
    struct irc_net *net = setup_irc_net(tp);
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1049
    struct irc_chan *chan = setup_irc_chan(tp, net, "#test", &ctx);
97
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1050
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1051
    // rename one of the users
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1052
    log_info("test irc_chan_PRIVMSG");
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1053
    assert_success(irc_chan_PRIVMSG(chan, "foobar quux"));
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1054
    assert_transport_data(tp, "PRIVMSG #test :foobar quux\r\n");
97
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1055
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1056
    // cleanup
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1057
    irc_net_destroy(net);
d3bc82ee76cb add irc_conn_PRIVMSG/irc_chan_PRIVMSG and lua bindings
Tero Marttila <terom@fixme.fi>
parents: 91
diff changeset
  1058
}
88
233916a00429 implement CTCP-ACTION for irc_log and test
Tero Marttila <terom@fixme.fi>
parents: 82
diff changeset
  1059
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1060
// XXX: needs to be split off into its own test_fifo.c
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1061
#include <sys/types.h>
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1062
#include <sys/stat.h>
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1063
#include <fcntl.h>
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1064
#include <unistd.h>
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1065
struct test_fifo_ctx {
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1066
    /** Path to the fifo */
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1067
    const char *path;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1068
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1069
    /** The write end */
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1070
    int fd;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1071
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1072
    /** callback invoked? */
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1073
    bool on_read;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1074
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1075
    /** Still running? */
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1076
    bool run;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1077
};
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1078
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1079
/**
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1080
 * Open the FIFO and write the test string to it
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1081
 */
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1082
static void test_fifo_open_write (struct test_fifo_ctx *ctx)
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1083
{
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1084
    // ...raw, for writing
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1085
    if ((ctx->fd = open(ctx->path, O_WRONLY)) < 0)
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1086
        FATAL_PERROR("open");
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1087
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1088
    // write something into it
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1089
    if (write(ctx->fd, "test", 4) != 4)
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1090
        FATAL_PERROR("write");
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1091
 
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1092
}
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1093
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1094
static void test_fifo_close (struct test_fifo_ctx *ctx)
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1095
{
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1096
    close(ctx->fd);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1097
    ctx->fd = -1;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1098
}
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1099
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1100
static void test_fifo_on_read (transport_t *fifo, void *arg)
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1101
{
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1102
    int ret;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1103
    char buf[16];
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1104
    struct test_fifo_ctx *ctx = arg;
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1105
    error_t err;
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1106
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1107
    // read it back out
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1108
    log_info("test fifo_read");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1109
    if ((ret = transport_read(fifo, buf, 16, &err)) < 0)
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1110
        assert_success(-ret);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1111
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1112
    assert(ret == 4);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1113
    assert_strncmp(buf, "test", 4);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1114
 
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1115
    if (ctx->on_read) {
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1116
        test_fifo_close(ctx);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1117
        ctx->run = false;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1118
        return;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1119
    }
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1120
   
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1121
    // re-open the fifo
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1122
    log_info("test fifo-re-open");
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1123
    test_fifo_close(ctx);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1124
    test_fifo_open_write(ctx);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1125
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1126
    ctx->on_read = true;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1127
}
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1128
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1129
static struct transport_callbacks test_fifo_callbacks = {
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1130
    .on_read = test_fifo_on_read,
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1131
};
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1132
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1133
void test_fifo (void)
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1134
{
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1135
    transport_t *fifo;
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1136
    struct error_info err;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1137
    struct test_fifo_ctx _ctx, *ctx = &_ctx; memset(ctx, 0, sizeof(*ctx));
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1138
    struct transport_info info = { &test_fifo_callbacks, ctx, TRANSPORT_READ };
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1139
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1140
    // XXX: requires that this be run in a suitable CWD
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1141
    ctx->path = "test.fifo";
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1142
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1143
    // create the fifo
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1144
    if ((mkfifo(ctx->path, 0600) < 0) && (errno != EEXIST))
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1145
        FATAL_PERROR("mkfifo");
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1146
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1147
    // open it
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1148
    log_info("test fifo_open_read");
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1149
    assert_success(fifo_open_read(&info, &fifo, _test_ctx.ev_base, ctx->path, &err));
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1150
    
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1151
    // put some data into it
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1152
    test_fifo_open_write(ctx);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1153
   
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1154
    // run the event loop
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1155
    log_debug("running the event loop...");
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1156
    ctx->run = true;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1157
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1158
    while (ctx->run)
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1159
        assert(event_base_loop(_test_ctx.ev_base, EVLOOP_ONCE) == 0);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1160
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1161
    // check
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1162
    assert(ctx->fd < 0);
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1163
    
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1164
    // cleanup
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1165
    transport_destroy(fifo);
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1166
}
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1167
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1168
/**
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1169
 * Test definition
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1170
 */
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1171
struct test {
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1172
    /** Test name */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1173
    const char *name;
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1174
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1175
    /** Test func */
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1176
    void (*func) (void);
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1177
    
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1178
    bool optional;
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1179
};
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1180
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1181
#define DEF_TEST(name) { #name, &test_ ## name, false }
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1182
#define DEF_TEST_OPTIONAL(name) { #name, &test_ ## name, true }
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1183
#define DEF_TEST_END { NULL, NULL, false }
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1184
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1185
static struct test _tests[] = {
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1186
    DEF_TEST(           str_quote                   ),
129
361740b82fe5 implement str_format with tests
Tero Marttila <terom@fixme.fi>
parents: 125
diff changeset
  1187
    DEF_TEST(           str_format                  ),
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1188
    DEF_TEST(           dump_str                    ),
167
0d2d8ca879d8 update test.c to reflect new transport stuff
Tero Marttila <terom@fixme.fi>
parents: 129
diff changeset
  1189
    DEF_TEST(           transport_test              ),
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1190
    DEF_TEST(           line_proto                  ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1191
    DEF_TEST(           irc_queue                   ),
75
ff6272398d2e change irc_line.prefix into a
Tero Marttila <terom@fixme.fi>
parents: 74
diff changeset
  1192
    // XXX: irc_line_parse_invalid_prefix
125
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1193
    DEF_TEST(           irc_conn                    ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1194
    DEF_TEST(           irc_conn_self_nick          ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1195
    DEF_TEST(           irc_net                     ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1196
    DEF_TEST(           irc_chan_add_offline        ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1197
    DEF_TEST(           irc_chan_namreply           ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1198
    DEF_TEST(           irc_chan_user_join          ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1199
    DEF_TEST(           irc_chan_user_part          ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1200
    DEF_TEST(           irc_chan_user_kick          ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1201
    DEF_TEST(           irc_chan_self_kick          ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1202
    DEF_TEST(           irc_chan_user_nick          ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1203
    DEF_TEST(           irc_chan_user_quit          ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1204
    DEF_TEST(           irc_chan_CTCP_ACTION        ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1205
    DEF_TEST(           irc_chan_privmsg            ),
5c70fb2d6793 move test.c dump_str implemention into str.h str_quote, and improve it
Tero Marttila <terom@fixme.fi>
parents: 118
diff changeset
  1206
    DEF_TEST_OPTIONAL(  fifo                        ),
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1207
    DEF_TEST_END
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1208
};
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1209
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1210
/**
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1211
 * Command-line option codes
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1212
 */
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1213
enum option_code {
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1214
    OPT_HELP            = 'h',
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1215
    OPT_DEBUG           = 'd',
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1216
    OPT_QUIET           = 'q',
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1217
    OPT_LIST            = 'l',
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1218
    
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1219
    /** Options without short names */
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1220
    _OPT_EXT_BEGIN      = 0x00ff,
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1221
};
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1222
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1223
/**
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1224
 * Command-line option definitions
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1225
 */
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1226
static struct option options[] = {
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1227
    {"help",            0,  NULL,   OPT_HELP        },
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1228
    {"debug",           0,  NULL,   OPT_DEBUG       },
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1229
    {"quiet",           0,  NULL,   OPT_QUIET       },
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1230
    {"list",            0,  NULL,   OPT_LIST        },
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1231
    {0,                 0,  0,      0               },
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1232
};
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1233
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1234
/**
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1235
 * Display --help output on stdout
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1236
 */
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1237
static void usage (const char *exe) 
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1238
{
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1239
    printf("Usage: %s [OPTIONS]\n", exe);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1240
    printf("\n");
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1241
    printf(" --help / -h            display this message\n");
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1242
    printf(" --debug / -d           display DEBUG log messages\n");
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1243
    printf(" --quiet / -q           supress INFO log messages\n");
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1244
    printf(" --list / -l            list all tests\n");
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1245
}
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1246
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1247
static void list_tests (struct test *tests)
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1248
{
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1249
    struct test *test;
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1250
    
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1251
    printf("Available tests:\n");
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1252
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1253
    for (test = tests; test->name; test++) {
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1254
        printf("\t%s\n", test->name);
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1255
    }
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1256
}
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1257
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1258
int main (int argc, char **argv)
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1259
{
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1260
    struct test *test;
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1261
    size_t test_count = 0;
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
  1262
    
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1263
    int opt, option_index;
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1264
    const char *filter = NULL;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1265
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1266
    // parse options
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1267
    while ((opt = getopt_long(argc, argv, "hdql", options, &option_index)) != -1) {
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1268
        switch (opt) {
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1269
            case OPT_HELP:
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1270
                usage(argv[0]);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1271
                exit(EXIT_SUCCESS);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1272
            
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1273
            case OPT_DEBUG:
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1274
                set_log_level(LOG_DEBUG);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1275
                break;
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
  1276
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1277
            case OPT_QUIET:
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1278
                set_log_level(LOG_WARN);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1279
                break;
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1280
           
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1281
            case OPT_LIST:
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1282
                list_tests(_tests);
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1283
                exit(EXIT_SUCCESS);
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1284
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1285
            case '?':
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1286
                usage(argv[0]);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1287
                exit(EXIT_FAILURE);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1288
        }
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1289
    }
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1290
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1291
    if (optind < argc) {
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1292
        if (optind == argc - 1) {
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1293
            // filter
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1294
            filter = argv[optind];
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1295
            
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1296
            log_info("only running tests: %s", filter);
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1297
        } else {
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1298
            FATAL("too many arguments");
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1299
        }
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
  1300
    }
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1301
90
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1302
    // setup the sockets stuff
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1303
    _test_ctx.ev_base = setup_sock();
9d489b1039b2 implement irc_queue, with some basic functionality tests
Tero Marttila <terom@fixme.fi>
parents: 89
diff changeset
  1304
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1305
    // run tests
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1306
    for (test = _tests; test->name; test++) {
118
05b8d5150313 implement fifo (sock_fifo.c) and test_fifo
Tero Marttila <terom@fixme.fi>
parents: 97
diff changeset
  1307
        if ((filter && strcmp(test->name, filter)) || (!filter && test->optional))
52
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
  1308
            continue;
97604efda1ce add test_sock_push, and filter argument to test main()
Tero Marttila <terom@fixme.fi>
parents: 51
diff changeset
  1309
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1310
        log_info("Running test: %s", test->name);
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1311
        
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1312
        test_count++;
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1313
        test->func();
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1314
    }
46
0c13bca53ae1 add irc_net_error, and improve test_irc_net to cover it as well
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
  1315
73
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1316
    // no tests run?
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1317
    if (test_count == 0)
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1318
        FATAL("no tests run");
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1319
2780a73c71f3 add set_log_level function, and add --debug/--quiet options to test
Tero Marttila <terom@fixme.fi>
parents: 72
diff changeset
  1320
    log_info("done, ran %zu tests", test_count);
40
51678c7eae03 add sock_test module and some basic initial tests
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
  1321
}