--- a/src/test.c Thu Mar 12 20:00:48 2009 +0200
+++ b/src/test.c Thu Mar 12 21:12:48 2009 +0200
@@ -2,12 +2,52 @@
* The main test code entry point
*/
#include "sock_test.h"
+#include "line_proto.h"
#include "irc_conn.h"
#include "log.h"
+#include "error.h"
+#include <stdlib.h>
#include <string.h>
#include <assert.h>
+void assert_strcmp (const char *a, const char *b)
+{
+ if (strcmp(a, b))
+ FATAL("'%s' != '%s'", a, b);
+}
+
+void assert_strncmp (const char *a, const char *b, size_t n)
+{
+ if (strncmp(a, b, n))
+ FATAL("'%s':%d != '%s'", a, n, b);
+}
+
+void assert_strlen (const char *str, size_t n)
+{
+ if (strlen(str) != n)
+ FATAL("strlen('%s') != %u", str, n);
+}
+
+void assert_strnul (const char *str)
+{
+ if (str != NULL)
+ FATAL("'%s' != NULL", str);
+}
+
+void assert_success (err_t err)
+{
+ if (err != SUCCESS)
+ FATAL("error: %s", error_name(err));
+
+}
+
+void assert_err (err_t err, err_t target)
+{
+ if (err != target)
+ FATAL("error: <%s> != target <%s>", error_name(err), error_name(target));
+}
+
void assert_sock_read (struct sock_stream *sock, const char *str)
{
char buf[strlen(str)];
@@ -18,7 +58,7 @@
assert(sock_stream_read(sock, buf, strlen(str)) == (int) strlen(str));
// cmp
- assert(strncmp(buf, str, strlen(str)) == 0);
+ assert_strncmp(buf, str, strlen(str));
}
void assert_sock_write (struct sock_stream *sock, const char *str)
@@ -29,6 +69,15 @@
assert(sock_stream_write(sock, str, strlen(str)) == (int) strlen(str));
}
+void assert_sock_eof (struct sock_stream *sock)
+{
+ char buf;
+
+ log_debug("eof: %p", sock);
+
+ assert_err(-sock_stream_read(sock, &buf, 1), ERR_READ_EOF);
+}
+
void test_sock_test (void)
{
struct sock_test *sock = sock_test_create();
@@ -40,14 +89,19 @@
// put the read data
log_debug("set_recv_buffer: %p, %d", _read_data, 2);
- sock_test_set_recv_buffer(sock, _read_data, 2);
+ sock_test_set_recv_buffer(sock, _read_data, 2, true);
+
+ // read it out
+ log_info("test sock_test_read");
- // read it out
assert_sock_read(SOCK_TEST_BASE(sock), "foo");
assert_sock_read(SOCK_TEST_BASE(sock), "ba");
assert_sock_read(SOCK_TEST_BASE(sock), "rx");
+ assert_sock_eof(SOCK_TEST_BASE(sock));
// write the data in
+ log_info("test sock_test_write");
+
assert_sock_write(SOCK_TEST_BASE(sock), "test ");
assert_sock_write(SOCK_TEST_BASE(sock), "data");
@@ -55,13 +109,78 @@
char *data;
size_t len;
+ log_info("test get_send_data");
+
sock_test_get_send_data(sock, &data, &len);
- log_debug("get_send_data: %u: '%s'", len, data);
+ // should be the same
+ assert_strlen(_write_data, len);
+ assert_strncmp(data, _write_data, len);
- // should be the same
- assert(len == strlen(_write_data));
- assert(strncmp(data, _write_data, len) == 0);
+ // cleanup
+ free(data);
+ sock_test_destroy(sock);
+}
+
+void assert_read_line (struct line_proto *lp, const char *line_str)
+{
+ char *line_buf;
+
+ log_debug("expect: '%s'", line_str);
+
+ assert_success(line_proto_recv(lp, &line_buf));
+
+ if (line_str) {
+ assert(line_buf != NULL);
+ assert_strcmp(line_buf, line_str);
+
+ } else {
+ assert_strnul(line_buf);
+
+ }
+}
+
+static struct line_proto_callbacks _lp_callbacks = {
+ .on_line = NULL,
+ .on_error = NULL,
+};
+
+void test_line_proto (void)
+{
+ struct sock_test *sock = sock_test_create();
+ struct io_vec _read_data[] = {
+ { "hello\r\n", 7 },
+ { "world\n", 6 },
+ { "this ", 5 },
+ { "is a line\r", 10 },
+ { "\nfragment", 9 },
+ }, _trailing_data = { "\r\n", 2 };
+ struct line_proto *lp;
+ struct error_info err;
+
+ // put the read data
+ log_debug("set_recv_buffer: %p, %d", _read_data, 5);
+ sock_test_set_recv_buffer(sock, _read_data, 5, false);
+
+ // create the lp
+ assert_success(line_proto_create(&lp, SOCK_TEST_BASE(sock), 128, &_lp_callbacks, NULL, &err));
+
+ log_info("test line_proto_recv");
+
+ // then read some lines from it
+ assert_read_line(lp, "hello");
+ assert_read_line(lp, "world");
+ assert_read_line(lp, "this is a line");
+ assert_read_line(lp, NULL);
+
+ // then add a final bit
+ sock_test_add_recv_vec(sock, _trailing_data);
+
+ // read the final bit
+ assert_read_line(lp, "fragment");
+
+ // cleanup
+ line_proto_release(lp);
}
static struct irc_conn_callbacks _conn_callbacks = {
@@ -78,7 +197,7 @@
assert((sock = sock_test_create()));
// create the irc_conn
- assert(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, NULL, &err) == SUCCESS);
+ assert_success(irc_conn_create(&conn, SOCK_TEST_BASE(sock), &_conn_callbacks, NULL, &err));
// destroy it
irc_conn_destroy(conn);
@@ -96,6 +215,7 @@
} _tests[] = {
{ "sock_test", &test_sock_test },
+ { "line_proto", &test_line_proto },
{ "irc_conn", &test_irc_conn },
{ NULL, NULL }
};