From e7119da5071b10d04c0ab2f78f9a56cf4846a033 Mon Sep 17 00:00:00 2001 From: frekky Date: Fri, 28 Aug 2015 14:59:34 +0800 Subject: [PATCH] Added sliding window test --- tests/Makefile | 4 +-- tests/test.c | 5 +++ tests/test.h | 1 + tests/window.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 tests/window.c diff --git a/tests/Makefile b/tests/Makefile index 03eed98..6d35a7a 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,6 @@ TEST = test -OBJS = test.o base32.o base64.o common.o read.o dns.o encoding.o login.o user.o fw_query.o -SRCOBJS = ../src/base32.o ../src/base64.o ../src/common.o ../src/read.o ../src/dns.o ../src/encoding.o ../src/login.o ../src/md5.o ../src/user.o ../src/fw_query.o +OBJS = test.o base32.o base64.o common.o read.o dns.o encoding.o login.o user.o fw_query.o window.o +SRCOBJS = ../src/base32.o ../src/base64.o ../src/window.o ../src/common.o ../src/read.o ../src/dns.o ../src/encoding.o ../src/login.o ../src/md5.o ../src/user.o ../src/fw_query.o OS = `uname | tr "a-z" "A-Z"` diff --git a/tests/test.c b/tests/test.c index eda792b..73c04b5 100644 --- a/tests/test.c +++ b/tests/test.c @@ -23,6 +23,8 @@ #include "test.h" +int check_ip = 0; + int main() { @@ -60,6 +62,9 @@ main() test = test_fw_query_create_tests(); suite_add_tcase(iodine, test); + test = test_window_create_tests(); + suite_add_tcase(iodine, test); + runner = srunner_create(iodine); srunner_run_all(runner, CK_NORMAL); failed = srunner_ntests_failed(runner); diff --git a/tests/test.h b/tests/test.h index d3f7985..f1cc5fd 100644 --- a/tests/test.h +++ b/tests/test.h @@ -27,6 +27,7 @@ TCase *test_read_create_tests(); TCase *test_login_create_tests(); TCase *test_user_create_tests(); TCase *test_fw_query_create_tests(); +TCase *test_window_create_tests(); char *va_str(const char *, ...); diff --git a/tests/window.c b/tests/window.c new file mode 100644 index 0000000..6b3975e --- /dev/null +++ b/tests/window.c @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2015 Frekk van Blagh + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common.h" +#include "window.h" +#include "test.h" + +struct frag_buffer *in, *out; +char origdata[1000] = ""; + +START_TEST(test_window_everything) +{ + in = window_buffer_init(1000, 10, 5, WINDOW_RECVING); + out = window_buffer_init(1000, 10, 5, WINDOW_SENDING); + for (unsigned i = 0; i < 20; i++) { + char c[100] = "0ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()-=`';\\|][{}/?~"; + c[0] += i; + window_add_outgoing_data(out, (uint8_t *)c, i + 1, 0); + strncat(origdata, c, i + 1); + //warnx(" OUT: %u available, current seq %u, new seq %u\n", window_buffer_available(out), i, out->cur_seq_id); + } +// printf("Original data: '%s' (%lu)\n", origdata, strlen(origdata)); +// warnx("Added data, fragmented into %lu frags, next seq %u.", out->numitems, out->cur_seq_id); + // "send" data + int a = -1; + for (; out->numitems > 0;) { + fragment *f = window_get_next_sending_fragment(out, &a); + if (!f) { +// warnx("Nothing to send."); + continue; + } + fail_if(!window_process_incoming_fragment(in, f), "Incoming fragment failed!"); +// warnx("Received fragment with seqid %u, remaining space %lu.", f->seqID, window_buffer_available(in)); + int a = window_get_next_ack(in); + window_tick(in); + window_ack(out, a); + window_tick(out); + } +// warnx("Added %lu fragments, reassembling into data.", in->numitems); + uint8_t data[100]; + uint8_t newdata[1000]; + memset(newdata, 0, 1000); + unsigned i; + int c; + for (i = 0; i < 50; i++) { + memset(data, 0, 100); + size_t len = window_reassemble_data(in, data, 100, &c); + fail_if(c != 0, "Compression flag weird"); +// printf("Reassembled %lu bytes, num frags %lu: '", len, in->numitems); +// for (unsigned i = 0; i < len; i++) { +// printf("%c", data[i]); +// } +// printf("'\n"); + strncat((char *)newdata, data, len); + if (in->numitems <= 0) break; + } +// printf("New data: '%s' (%lu)\n", newdata, strlen((char *)newdata)); + fail_if(strcmp((char *)newdata, origdata), "Reassembled data didn't match original data."); +} +END_TEST + + +TCase * +test_window_create_tests() +{ + TCase *tc; + + tc = tcase_create("Windowing"); + tcase_add_test(tc, test_window_everything); + + return tc; +}