src/sock_test.h
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 21:44:34 +0200
changeset 43 42f5dc680930
parent 42 13cfc41f76a7
child 46 0c13bca53ae1
permissions -rw-r--r--
improve irc_conn test
#ifndef SOCK_TEST_H
#define SOCK_TEST_H

/**
 * @file
 *
 * Dummy sock_stream implemention for local testing.
 */
#include "sock_internal.h"
#include <stdbool.h>

/**
 * IO vector
 */
struct io_vec {
    /** The buffer */
    char *buf;

    /** Buffer size */
    size_t len;
};

/**
 * Vectored IO-buffer
 */
struct io_buf {
    /** The array of buffer-vectors, {NULL}-terminated */
    struct io_vec *vecs;

    /** The number of io_vecs */
    size_t count;

    /** Current read/write vector */
    struct io_vec *read_vec, *write_vec;

    /** Offset into current vector */
    size_t off;
};

/**
 * The per-socket state
 */
struct sock_test {
    /** The base struct for sock_stream_* functions */
    struct sock_stream base;

    /** The send/recieve buffers */
    struct io_buf send_buf, recv_buf;

    /** non-blocking mode? */
    bool nonblocking;

    /** No more data is going to be added, return EOF once all the rest is consumed */
    bool eof;

    /** event flags */
    int ev_mask;
};

/**
 * Get a sock_stream pointer from a sock_tcp pointer
 */
#define SOCK_TEST_BASE(sock_ptr) (&(sock_ptr)->base)

/**
 * Get the sock_stream.err pointer from a sock_tcp pointer
 */
#define SOCK_TEST_ERR(sock_ptr) SOCK_ERR(SOCK_TEST_BASE(sock_ptr))

/**
 * A dummy stream socket intended for testing purposes.
 */
struct sock_test* sock_test_create (void);

/**
 * Destroy the sock buffer, releasing any resource we allocated ourself
 */
void sock_test_destroy (struct sock_test *sock);

/**
 * Set the recieve buffer contents.
 *
 * The vectors themselves are copied, but the data they contain is not.
 *
 * If the EOF flag is given, it indicates that no more data will be added, otherwise the eof status is unchanged.
 */
void sock_test_set_recv_buffer (struct sock_test *sock, struct io_vec *vecs, size_t count, bool eof);

/**
 * Add some data to the recieve buffer.
 *
 * If events are enabled, they are triggered.
 */
void sock_test_add_recv_vec (struct sock_test *sock, struct io_vec vec);

/**
 * Add a string to the recieve buffer using sock_test_add_recv_vec()
 */
void sock_test_add_recv_str (struct sock_test *sock, const char *str);

/**
 * Get the send buffer contents as a single string, free() after use if you care about that.
 *
 * Clears the send buffer, so this doesn't return the same data twice.
 */
void sock_test_get_send_data (struct sock_test *sock, char **buf, size_t *len);

#endif