awtk/tests/iostream_shdlc_test.cc

148 lines
4.8 KiB
C++
Raw Normal View History

2019-10-11 08:21:14 +08:00
#include "gtest/gtest.h"
#include "tkc/thread.h"
#include "tkc/socket_pair.h"
2019-10-11 21:45:36 +08:00
#include "streams/shdlc/iostream_shdlc.h"
#include "streams/inet/iostream_tcp.h"
2019-10-16 15:03:41 +08:00
#include "streams/noisy/iostream_noisy.h"
2019-10-11 08:21:14 +08:00
2019-10-13 07:03:46 +08:00
uint8_t rbuff[2048];
uint8_t sbuff[2048];
uint32_t data_size;
uint32_t compress_threshold = 1024;
2019-10-11 08:21:14 +08:00
static void* server_thread_entry1(void* args) {
tk_iostream_t* b_io = TK_IOSTREAM(args);
tk_ostream_t* os = tk_iostream_get_ostream(b_io);
tk_object_set_prop_int(TK_OBJECT(os), TK_STREAM_PROP_COMPRESS_THRESHOLD, compress_threshold);
2021-09-16 16:06:55 +08:00
assert(tk_ostream_write(os, sbuff, data_size) == (int32_t)data_size);
2019-10-11 08:21:14 +08:00
return NULL;
}
TEST(IOStreamSHDLC, basic) {
int socks[2];
tk_socketpair(socks);
tk_iostream_t* a_tcp = tk_iostream_tcp_create(socks[0]);
tk_iostream_t* a_io = tk_iostream_shdlc_create(a_tcp);
tk_iostream_t* b_tcp = tk_iostream_tcp_create(socks[1]);
tk_iostream_t* b_io = tk_iostream_shdlc_create(b_tcp);
tk_istream_t* is = tk_iostream_get_istream(a_io);
tk_ostream_t* os = tk_iostream_get_ostream(a_io);
ASSERT_EQ(tk_object_get_prop_int(TK_OBJECT(is), TK_STREAM_PROP_FD, 0), socks[0]);
ASSERT_EQ(tk_object_get_prop_int(TK_OBJECT(os), TK_STREAM_PROP_FD, 0), socks[0]);
2019-10-12 11:33:57 +08:00
ASSERT_EQ(tk_object_set_prop_int(TK_OBJECT(is), TK_STREAM_PROP_TIMEOUT, 1234), RET_OK);
ASSERT_EQ(tk_object_set_prop_int(TK_OBJECT(os), TK_STREAM_PROP_TIMEOUT, 1235), RET_OK);
ASSERT_EQ(tk_object_set_prop_int(TK_OBJECT(os), TK_STREAM_PROP_COMPRESS_THRESHOLD, 2048), RET_OK);
ASSERT_EQ(tk_object_set_prop_int(TK_OBJECT(os), TK_STREAM_PROP_RETRY_TIMES, 12), RET_OK);
2019-10-12 11:33:57 +08:00
ASSERT_EQ(tk_object_get_prop_int(TK_OBJECT(is), TK_STREAM_PROP_TIMEOUT, 0), 1234);
ASSERT_EQ(tk_object_get_prop_int(TK_OBJECT(os), TK_STREAM_PROP_TIMEOUT, 0), 1235);
ASSERT_EQ(tk_object_get_prop_int(TK_OBJECT(os), TK_STREAM_PROP_COMPRESS_THRESHOLD, 0), 2048);
ASSERT_EQ(tk_object_get_prop_int(TK_OBJECT(os), TK_STREAM_PROP_RETRY_TIMES, 0), 12);
2019-10-12 11:33:57 +08:00
TK_OBJECT_UNREF(a_tcp);
TK_OBJECT_UNREF(a_io);
TK_OBJECT_UNREF(b_tcp);
TK_OBJECT_UNREF(b_io);
2019-10-13 07:03:46 +08:00
}
static void gen_data() {
uint32_t n = sizeof(sbuff);
for (uint32_t i = 0; i < n; i++) {
sbuff[i] = random() / 256;
}
}
TEST(IOStreamSHDLC, small) {
int socks[2];
tk_socketpair(socks);
tk_iostream_t* a_tcp = tk_iostream_tcp_create(socks[0]);
tk_iostream_t* a_io = tk_iostream_shdlc_create(a_tcp);
tk_iostream_t* b_tcp = tk_iostream_tcp_create(socks[1]);
tk_iostream_t* b_io = tk_iostream_shdlc_create(b_tcp);
tk_istream_t* is = tk_iostream_get_istream(a_io);
tk_thread_t* t = tk_thread_create(server_thread_entry1, b_io);
data_size = 64;
gen_data();
tk_thread_start(t);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(tk_istream_read_len(is, rbuff, data_size, 30000), (int32_t)data_size);
2019-10-13 07:03:46 +08:00
ASSERT_EQ(memcmp(rbuff, sbuff, data_size), 0);
tk_thread_join(t);
2020-08-24 21:26:57 +08:00
tk_thread_destroy(t);
2019-10-13 07:03:46 +08:00
TK_OBJECT_UNREF(a_tcp);
TK_OBJECT_UNREF(a_io);
TK_OBJECT_UNREF(b_tcp);
TK_OBJECT_UNREF(b_io);
2019-10-13 07:03:46 +08:00
}
TEST(IOStreamSHDLC, large) {
int socks[2];
tk_socketpair(socks);
tk_iostream_t* a_tcp = tk_iostream_tcp_create(socks[0]);
tk_iostream_t* a_io = tk_iostream_shdlc_create(a_tcp);
tk_iostream_t* b_tcp = tk_iostream_tcp_create(socks[1]);
tk_iostream_t* b_io = tk_iostream_shdlc_create(b_tcp);
tk_istream_t* is = tk_iostream_get_istream(a_io);
tk_thread_t* t = tk_thread_create(server_thread_entry1, b_io);
data_size = sizeof(rbuff);
gen_data();
2019-10-11 08:21:14 +08:00
tk_thread_start(t);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(tk_istream_read_len(is, rbuff, data_size, 30000), (int32_t)data_size);
2019-10-13 07:03:46 +08:00
ASSERT_EQ(memcmp(rbuff, sbuff, data_size), 0);
2019-10-11 08:21:14 +08:00
tk_thread_join(t);
2020-08-24 21:26:57 +08:00
tk_thread_destroy(t);
2019-10-11 08:21:14 +08:00
TK_OBJECT_UNREF(a_tcp);
TK_OBJECT_UNREF(a_io);
TK_OBJECT_UNREF(b_tcp);
TK_OBJECT_UNREF(b_io);
2019-10-11 08:21:14 +08:00
}
2019-10-16 15:03:41 +08:00
static void* server_thread_entry_noisy(void* args) {
tk_iostream_t* b_io = TK_IOSTREAM(args);
tk_ostream_t* os = tk_iostream_get_ostream(b_io);
tk_object_set_prop_int(TK_OBJECT(os), TK_STREAM_PROP_COMPRESS_THRESHOLD, compress_threshold);
2021-09-16 16:06:55 +08:00
assert(tk_ostream_write(os, sbuff, data_size) == (int32_t)data_size);
2019-10-16 15:03:41 +08:00
return NULL;
}
TEST(IOStreamSHDLC, noisy) {
int socks[2];
tk_socketpair(socks);
tk_iostream_t* a_tcp = tk_iostream_tcp_create(socks[0]);
tk_iostream_t* a_io = tk_iostream_shdlc_create(a_tcp);
tk_iostream_t* b_tcp = tk_iostream_tcp_create(socks[1]);
tk_iostream_t* b_noisy = tk_iostream_noisy_create(b_tcp);
tk_iostream_t* b_io = tk_iostream_shdlc_create(b_noisy);
tk_istream_t* is = tk_iostream_get_istream(a_io);
tk_thread_t* t = tk_thread_create(server_thread_entry_noisy, b_io);
data_size = 4;
gen_data();
tk_thread_start(t);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(tk_istream_read_len(is, rbuff, data_size, 30000u), (int32_t)data_size);
2019-10-16 15:03:41 +08:00
ASSERT_EQ(memcmp(rbuff, sbuff, data_size), 0);
tk_thread_join(t);
2020-08-24 21:26:57 +08:00
tk_thread_destroy(t);
2019-10-16 15:03:41 +08:00
TK_OBJECT_UNREF(a_tcp);
TK_OBJECT_UNREF(a_io);
TK_OBJECT_UNREF(b_tcp);
TK_OBJECT_UNREF(b_io);
TK_OBJECT_UNREF(b_noisy);
2019-10-16 15:03:41 +08:00
}