src/test/fifo.c
author Tero Marttila <terom@fixme.fi>
Mon, 04 May 2009 20:55:04 +0300
branchnew-transport
changeset 168 a58ad50911fc
permissions -rw-r--r--
refactor test.c into tests/*
168
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "test.h"
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include "../fifo.h"
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include <sys/types.h>
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include <sys/stat.h>
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
#include <fcntl.h>
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
#include <unistd.h>
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
struct test_fifo_ctx {
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
    /** Path to the fifo */
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
    const char *path;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    /** The write end */
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    int fd;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    /** callback invoked? */
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    bool on_read;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
    /** Still running? */
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
    bool run;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
};
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
/**
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
 * Open the FIFO and write the test string to it
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
 */
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
static void test_fifo_open_write (struct test_fifo_ctx *ctx)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    // ...raw, for writing
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
    if ((ctx->fd = open(ctx->path, O_WRONLY)) < 0)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
        FATAL_PERROR("open");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    // write something into it
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    if (write(ctx->fd, "test", 4) != 4)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
        FATAL_PERROR("write");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
 
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
static void test_fifo_close (struct test_fifo_ctx *ctx)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
    close(ctx->fd);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    ctx->fd = -1;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
static void test_fifo_on_read (transport_t *fifo, void *arg)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    int ret;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
    char buf[16];
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    struct test_fifo_ctx *ctx = arg;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    error_t err;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
    // read it back out
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
    log_info("test fifo_read");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    if ((ret = transport_read(fifo, buf, 16, &err)) < 0)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        assert_success(-ret);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    assert(ret == 4);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
    assert_strncmp(buf, "test", 4);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
 
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
    if (ctx->on_read) {
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        test_fifo_close(ctx);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        ctx->run = false;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
        return;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
    }
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
   
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    // re-open the fifo
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    log_info("test fifo-re-open");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
    test_fifo_close(ctx);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
    test_fifo_open_write(ctx);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
    ctx->on_read = true;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
static struct transport_callbacks test_fifo_callbacks = {
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    .on_read = test_fifo_on_read,
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
};
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
void test_fifo (void)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
{
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
    transport_t *fifo;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
    struct error_info err;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
    struct test_fifo_ctx _ctx, *ctx = &_ctx; memset(ctx, 0, sizeof(*ctx));
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
    struct transport_info info = { &test_fifo_callbacks, ctx, TRANSPORT_READ };
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
    // XXX: requires that this be run in a suitable CWD
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
    ctx->path = "test.fifo";
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
    // create the fifo
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
    if ((mkfifo(ctx->path, 0600) < 0) && (errno != EEXIST))
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
        FATAL_PERROR("mkfifo");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    // open it
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    log_info("test fifo_open_read");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    assert_success(fifo_open_read(&info, &fifo, _test_ctx.ev_base, ctx->path, &err));
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
    // put some data into it
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
    test_fifo_open_write(ctx);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
   
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    // run the event loop
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    99
    log_debug("running the event loop...");
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
    ctx->run = true;
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
    while (ctx->run)
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
        assert(event_base_loop(_test_ctx.ev_base, EVLOOP_ONCE) == 0);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
    // check
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
    assert(ctx->fd < 0);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
    
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    // cleanup
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
    transport_destroy(fifo);
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
}
a58ad50911fc refactor test.c into tests/*
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111