src/sock_test.h
branchnew-transport
changeset 166 cb8cb023cf06
parent 165 b3e95108c884
child 167 0d2d8ca879d8
equal deleted inserted replaced
165:b3e95108c884 166:cb8cb023cf06
     1 #ifndef SOCK_TEST_H
       
     2 #define SOCK_TEST_H
       
     3 
       
     4 /**
       
     5  * @file
       
     6  *
       
     7  * Dummy sock_stream implemention for local testing.
       
     8  */
       
     9 #include "sock_internal.h"
       
    10 #include <stdbool.h>
       
    11 
       
    12 /**
       
    13  * Simple IO vector
       
    14  */
       
    15 struct io_vec {
       
    16     /** The buffer */
       
    17     char *buf;
       
    18 
       
    19     /** Buffer size */
       
    20     size_t len;
       
    21 };
       
    22 
       
    23 /**
       
    24  * Simple vectored IO-buffer
       
    25  */
       
    26 struct io_buf {
       
    27     /** The array of buffer-vectors, {NULL}-terminated */
       
    28     struct io_vec *vecs;
       
    29 
       
    30     /** The number of io_vecs */
       
    31     size_t count;
       
    32 
       
    33     /** Current read/write vector */
       
    34     struct io_vec *read_vec, *write_vec;
       
    35 
       
    36     /** Offset into current vector */
       
    37     size_t off;
       
    38 };
       
    39 
       
    40 /**
       
    41  * A dummy sock_stream implementation intended for testing purposes.
       
    42  */
       
    43 struct sock_test {
       
    44     /** The base struct for sock_stream_* functions */
       
    45     struct sock_stream base;
       
    46 
       
    47     /** The send/recieve buffers */
       
    48     struct io_buf send_buf, recv_buf;
       
    49 
       
    50     /** non-blocking mode? */
       
    51     bool nonblocking;
       
    52 
       
    53     /** No more data is going to be added, return EOF once all the rest is consumed */
       
    54     bool eof;
       
    55 
       
    56     /** event flags */
       
    57     int ev_mask;
       
    58 };
       
    59 
       
    60 /**
       
    61  * Get a sock_stream pointer from a sock_tcp pointer
       
    62  */
       
    63 #define SOCK_TEST_BASE(sock_ptr) (&(sock_ptr)->base)
       
    64 
       
    65 /**
       
    66  * Get the sock_stream.err pointer from a sock_tcp pointer
       
    67  */
       
    68 #define SOCK_TEST_ERR(sock_ptr) SOCK_ERR(SOCK_TEST_BASE(sock_ptr))
       
    69 
       
    70 /**
       
    71  * A dummy stream socket intended for testing purposes.
       
    72  */
       
    73 struct sock_test* sock_test_create (void);
       
    74 
       
    75 /**
       
    76  * Destroy the sock buffer, releasing any resource we allocated ourself
       
    77  */
       
    78 void sock_test_destroy (struct sock_test *sock);
       
    79 
       
    80 /**
       
    81  * Set the recieve buffer contents.
       
    82  *
       
    83  * The vectors themselves are copied, but the data they contain is not.
       
    84  *
       
    85  * If the EOF flag is given, it indicates that no more data will be added, otherwise the eof status is unchanged.
       
    86  */
       
    87 void sock_test_set_recv_buffer (struct sock_test *sock, struct io_vec *vecs, size_t count, bool eof);
       
    88 
       
    89 /**
       
    90  * Add some data to the recieve buffer.
       
    91  *
       
    92  * If events are enabled, they are triggered.
       
    93  */
       
    94 void sock_test_add_recv_vec (struct sock_test *sock, struct io_vec vec);
       
    95 
       
    96 /**
       
    97  * Add a string to the recieve buffer using sock_test_add_recv_vec()
       
    98  */
       
    99 void sock_test_add_recv_str (struct sock_test *sock, const char *str);
       
   100 
       
   101 /**
       
   102  * Set EOF on recv, and trigger events.
       
   103  */
       
   104 void sock_test_set_recv_eof (struct sock_test *sock);
       
   105 
       
   106 /**
       
   107  * Get the send buffer contents as a single string, free() after use if you care about that.
       
   108  *
       
   109  * Clears the send buffer, so this doesn't return the same data twice.
       
   110  */
       
   111 void sock_test_get_send_data (struct sock_test *sock, char **buf, size_t *len);
       
   112 
       
   113 #endif