From fb94efca1b125c9b5f7753e4e85ec76b5060118e Mon Sep 17 00:00:00 2001 From: Tezc Date: Wed, 17 Feb 2021 20:04:08 +0300 Subject: [PATCH] test (#50) * test --- buffer/buf_test.c | 16 +++++ condition/cond_test.c | 9 +++ condition/sc_cond.c | 6 +- crc32/crc32_test.c | 16 +++++ ini/ini_test.c | 126 ++++++++++++++++++++++++++++++++++++++ ini/sc_ini.c | 6 +- logger/log_test.c | 3 + memory-map/CMakeLists.txt | 4 +- memory-map/mmap_test.c | 77 +++++++++++++++++------ option/option_test.c | 48 ++++++++++++++- socket/sock_test.c | 4 ++ 11 files changed, 289 insertions(+), 26 deletions(-) diff --git a/buffer/buf_test.c b/buffer/buf_test.c index 5906adf..a01700a 100644 --- a/buffer/buf_test.c +++ b/buffer/buf_test.c @@ -225,6 +225,22 @@ void test2() sc_buf_term(&buf); + sc_buf_init(&buf, 1000); + sc_buf_mark_write(&buf, 1000); + sc_buf_mark_read(&buf, 800); + sc_buf_reserve(&buf, 400); + sc_buf_term(&buf); + + sc_buf_init(&buf, 100); + sc_buf_get_str(&buf); + sc_buf_set_8(&buf, 100); + sc_buf_set_16(&buf, 100); + sc_buf_set_32(&buf, 100); + sc_buf_set_64(&buf, 100); + sc_buf_set_data(&buf, 19, "d", 1); + sc_buf_peek_data(&buf, 10, NULL, 0); + assert(!sc_buf_valid(&buf)); + sc_buf_term(&buf); } #ifdef SC_HAVE_WRAP diff --git a/condition/cond_test.c b/condition/cond_test.c index e16c665..17dae27 100644 --- a/condition/cond_test.c +++ b/condition/cond_test.c @@ -265,10 +265,19 @@ void fail_test() assert(sc_cond_init(&cond) == 0); mock_conddestroy = true; assert(sc_cond_term(&cond) != 0); + mock_conddestroy = false; assert(sc_cond_init(&cond) == 0); mock_mutexdestroy = true; assert(sc_cond_term(&cond) != 0); + mock_mutexdestroy = false; + + assert(sc_cond_init(&cond) == 0); + mock_mutexdestroy = true; + mock_conddestroy = true; + assert(sc_cond_term(&cond) != 0); + mock_mutexdestroy = false; + mock_conddestroy = false; } #else diff --git a/condition/sc_cond.c b/condition/sc_cond.c index 9dc65c9..15d726f 100644 --- a/condition/sc_cond.c +++ b/condition/sc_cond.c @@ -134,17 +134,19 @@ int sc_cond_term(struct sc_cond *cond) { int rc; + errno = 0; + rc = pthread_mutex_destroy(&cond->mtx); if (rc != 0) { errno = rc; } rc = pthread_cond_destroy(&cond->cond); - if (rc != 0 && errno != 0) { + if (rc != 0 && errno == 0) { errno = rc; } - return rc; + return errno; } void sc_cond_signal(struct sc_cond *cond, void *data) diff --git a/crc32/crc32_test.c b/crc32/crc32_test.c index 82d7569..32b9c85 100644 --- a/crc32/crc32_test.c +++ b/crc32/crc32_test.c @@ -24,4 +24,20 @@ int main(int argc, char *argv[]) crc3 = sc_crc32(0, buf2, 4096 * 8); assert(crc2 == crc3); + + crc1 = sc_crc32(100, buf, 8); + crc2 = sc_crc32(100, buf, 7); + assert(crc1 != crc2); + + crc2 = sc_crc32(100, buf + 7, 7); + assert(crc1 != crc2); + + crc2 = sc_crc32(100, buf + 8, 7); + assert(crc1 != crc2); + + crc2 = sc_crc32(100, buf + 8, 8); + assert(crc1 != crc2); + + crc2 = sc_crc32(100, buf + 8, 0); + assert(crc1 != crc2); } diff --git a/ini/ini_test.c b/ini/ini_test.c index 70e391b..10f3fd3 100644 --- a/ini/ini_test.c +++ b/ini/ini_test.c @@ -117,6 +117,9 @@ void test4() rc = sc_ini_parse_string(&count, cb4, ini); assert(rc == 0); assert(count == 3); + + rc = sc_ini_parse_string(&count, cb4, NULL); + assert(rc == 0); } int cb5(int line, void *arg, const char *section, const char *key, @@ -305,6 +308,41 @@ void test8() assert(count == 3); remove("config.ini"); + unsigned char boms[2] = {0xE3, 0xBB}; + fp = fopen("config.ini", "w+"); + fwrite(boms, 1, sizeof(boms), fp); + fclose(fp); + rc = sc_ini_parse_file(&count, cb8, "config.ini"); + assert(rc != 0); + remove("config.ini"); + + unsigned char bom0[3] = {0xE3, 0xBB, 0xBF}; + fp = fopen("config.ini", "w+"); + fwrite(bom0, 1, sizeof(bom0), fp); + fwrite(ini3, 1, strlen(ini3), fp); + fclose(fp); + rc = sc_ini_parse_file(&count, cb8, "config.ini"); + assert(rc != 0); + remove("config.ini"); + + unsigned char bom2[3] = {0xEF, 0xB3, 0xBF}; + fp = fopen("config.ini", "w+"); + fwrite(bom2, 1, sizeof(bom2), fp); + fwrite(ini3, 1, strlen(ini3), fp); + fclose(fp); + rc = sc_ini_parse_file(&count, cb8, "config.ini"); + assert(rc != 0); + remove("config.ini"); + + unsigned char bom3[3] = {0xEF, 0xBB, 0xB3}; + fp = fopen("config.ini", "w+"); + fwrite(bom3, 1, sizeof(bom3), fp); + fwrite(ini3, 1, strlen(ini3), fp); + fclose(fp); + rc = sc_ini_parse_file(&count, cb8, "config.ini"); + assert(rc != 0); + remove("config.ini"); + rc = sc_ini_parse_file(&count, cb8, "config.ini"); assert(rc == -1); } @@ -443,6 +481,91 @@ void test13() assert(count == 2); } +int cb14(void *arg, int line, const char *section, const char *key, + const char *value) +{ + (void) arg; + (void) line; + + assert(strcmp(section, "section") == 0); + assert(strcmp(key, "key") == 0); + assert(strcmp(value, "value") == 0); + return -1; +} + +void test14() +{ + int rc; + int count = 0; + + static const char *ini = "#Sample \n" + "[section] \n" + "key = value \n" + "key : value "; + + rc = sc_ini_parse_string(&count, cb14, ini); + assert(rc != 0); +} + +int cb15(void *arg, int line, const char *section, const char *key, + const char *value) +{ + (void) line; + (void) arg; + (void) section; + (void) key; + (void) value; + + return -1; +} + +void test15() +{ + int rc; + int count = 0; + FILE *fp; + + static const char *ini = " ;Sample \n" + " [section] \n" + "key = value0 #;comment\n" + " value1 \n" + " value2 "; + + fp = fopen("config.ini", "w+"); + fwrite(ini, 1, strlen(ini), fp); + fclose(fp); + + rc = sc_ini_parse_file(&count, cb15, "config.ini"); + assert(rc != 0); + remove("config.ini"); +} + +int cb16(void *arg, int line, const char *section, const char *key, + const char *value) +{ + (void) arg; + (void) line; + (void) section; + (void) key; + (void) value; + + return 0; +} + +void test16() +{ + int rc; + + rc = sc_ini_parse_string(NULL, cb16, "key# = ;comment \n"); + assert(rc == 0); + rc = sc_ini_parse_string(NULL, cb16, "key# = ;comment \n\n"); + assert(rc == 0); + rc = sc_ini_parse_string(NULL, cb16, "key# = ;comment \nx=3\n"); + assert(rc == 0); + rc = sc_ini_parse_string(NULL, cb16, "\n\0"); + assert(rc == 0); +} + const char *example_ini = "# My configuration" "[Network] \n" "hostname = github.com \n" @@ -557,6 +680,9 @@ int main() test11(); test12(); test13(); + test14(); + test15(); + test16(); test_fail(); return 0; diff --git a/ini/sc_ini.c b/ini/sc_ini.c index ac54d51..5cbce43 100644 --- a/ini/sc_ini.c +++ b/ini/sc_ini.c @@ -40,12 +40,12 @@ static char *trim_space(char *str) { char *end; - while (isspace(*str)) { + while (isspace((unsigned char) *str)) { str++; } end = str + strlen(str) - 1; - while (end > str && isspace(*end)) { + while (end > str && isspace((unsigned char) *end)) { end--; } @@ -76,7 +76,7 @@ static char *trim_comment(char *str) static char *trim_bom(char *str) { - if (str != NULL && strlen(str) >= 3) { + if (strlen(str) >= 3) { if ((uint8_t) str[0] == 0xEF && (uint8_t) str[1] == 0xBB && (uint8_t) str[2] == 0xBF) { str += 3; diff --git a/logger/log_test.c b/logger/log_test.c index e850888..e2e814e 100644 --- a/logger/log_test.c +++ b/logger/log_test.c @@ -26,6 +26,8 @@ void test1(void) sc_log_init(); sc_log_set_callback(&count, callback); assert(sc_log_set_level("errrorr") == -1); + assert(sc_log_set_level("errox") == -1); + assert(sc_log_set_level("err") == -1); sc_log_debug("test \n"); assert(count == 0); @@ -271,6 +273,7 @@ void fail_test(void) fprintf_ret = 0; assert(sc_log_set_file(NULL, "test.txt") == 0); + assert(sc_log_set_file("test.txt", NULL) == 0); mock_fopen = true; assert(sc_log_set_file("prev.txt", "current.txt") == -1); mock_fopen = false; diff --git a/memory-map/CMakeLists.txt b/memory-map/CMakeLists.txt index fdb9f08..2e71928 100644 --- a/memory-map/CMakeLists.txt +++ b/memory-map/CMakeLists.txt @@ -38,7 +38,9 @@ if(SC_BUILD_TEST) target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_HAVE_WRAP) target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin) target_link_options(${PROJECT_NAME}_test PRIVATE - -Wl,--wrap=open,--wrap=stat,--wrap=mmap,--wrap=mlock,--wrap=msync,--wrap=munlock,--wrap=munmap) + -Wl,--wrap=open,--wrap=stat,--wrap=mmap,--wrap=mlock + -Wl,--wrap=msync,--wrap=munlock,--wrap=munmap + -Wl,--wrap=posix_fallocate) endif () endif () diff --git a/memory-map/mmap_test.c b/memory-map/mmap_test.c index 399e995..d6181cd 100644 --- a/memory-map/mmap_test.c +++ b/memory-map/mmap_test.c @@ -55,10 +55,14 @@ void test1() } #ifdef SC_HAVE_WRAP + #include + #include + bool fail_open; extern int __real_open(const char *pathname, int flags, mode_t mode); -int __wrap_open(const char *pathname, int flags, mode_t mode) { +int __wrap_open(const char *pathname, int flags, mode_t mode) +{ if (fail_open) { return -1; } @@ -68,7 +72,8 @@ int __wrap_open(const char *pathname, int flags, mode_t mode) { bool fail_stat; extern int __real_stat(const char *pathname, struct stat *statbuf); -int __wrap_stat(const char *pathname, struct stat *statbuf) { +int __wrap_stat(const char *pathname, struct stat *statbuf) +{ if (fail_stat) { return -1; } @@ -77,20 +82,22 @@ int __wrap_stat(const char *pathname, struct stat *statbuf) { } bool fail_mmap; -extern void *__real_mmap(void *addr, size_t length, int prot, int flags, - int fd, off_t offset); -void* __wrap_mmap(void *addr, size_t length, int prot, int flags, - int fd, off_t offset) { +extern void *__real_mmap(void *addr, size_t length, int prot, int flags, int fd, + off_t offset); +void *__wrap_mmap(void *addr, size_t length, int prot, int flags, int fd, + off_t offset) +{ if (fail_mmap) { return MAP_FAILED; } - return __real_mmap(addr, length, prot, flags ,fd, offset); + return __real_mmap(addr, length, prot, flags, fd, offset); } bool fail_mlock; -extern int __real_mlock (const void *addr, size_t len); -int __wrap_mlock (const void *addr, size_t len) { +extern int __real_mlock(const void *addr, size_t len); +int __wrap_mlock(const void *addr, size_t len) +{ if (fail_mlock) { return -1; } @@ -99,8 +106,9 @@ int __wrap_mlock (const void *addr, size_t len) { } bool fail_munlock; -extern int __real_munlock (const void *addr, size_t len); -int __wrap_munlock (const void *addr, size_t len) { +extern int __real_munlock(const void *addr, size_t len); +int __wrap_munlock(const void *addr, size_t len) +{ if (fail_munlock) { return -1; } @@ -109,8 +117,9 @@ int __wrap_munlock (const void *addr, size_t len) { } bool fail_msync; -extern int __real_msync (void *addr, size_t len, int flags); -int __wrap_msync (void *addr, size_t len, int flags) { +extern int __real_msync(void *addr, size_t len, int flags); +int __wrap_msync(void *addr, size_t len, int flags) +{ if (fail_msync) { return -1; } @@ -119,8 +128,9 @@ int __wrap_msync (void *addr, size_t len, int flags) { } bool fail_munmap; -extern int __real_munmap (void *addr, size_t len); -int __wrap_munmap(void *addr, size_t len) { +extern int __real_munmap(void *addr, size_t len); +int __wrap_munmap(void *addr, size_t len) +{ if (fail_munmap) { return -1; } @@ -128,6 +138,20 @@ int __wrap_munmap(void *addr, size_t len) { return __real_munmap(addr, len); } +uint32_t fail_posix_fallocate = UINT32_MAX; +int fail_posix_fallocate_errno = 0; +extern int __real_posix_fallocate(int fd, off_t offset, off_t len); +int __wrap_posix_fallocate(int fd, off_t offset, off_t len) +{ + fail_posix_fallocate--; + if (fail_posix_fallocate == 0) { + errno = fail_posix_fallocate_errno; + return fail_posix_fallocate_errno; + } + + return __real_posix_fallocate(fd, offset, len); +} + void fail_test() { int rc; @@ -140,7 +164,7 @@ void fail_test() assert(sc_mmap_err(&mmap) != NULL); fail_open = false; - mmap = (struct sc_mmap) {0}; + mmap = (struct sc_mmap){0}; fail_stat = true; rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC, PROT_READ | PROT_WRITE, MAP_SHARED, 0, 4095); @@ -148,7 +172,7 @@ void fail_test() assert(sc_mmap_err(&mmap) != NULL); fail_stat = false; - mmap = (struct sc_mmap) {0}; + mmap = (struct sc_mmap){0}; fail_mmap = true; rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC, PROT_READ | PROT_WRITE, MAP_SHARED, 0, 4095); @@ -156,7 +180,7 @@ void fail_test() assert(sc_mmap_err(&mmap) != NULL); fail_mmap = false; - mmap = (struct sc_mmap) {0}; + mmap = (struct sc_mmap){0}; fail_munmap = true; rc = sc_mmap_term(&mmap); assert(rc == -1); @@ -191,11 +215,26 @@ void fail_test() rc = sc_mmap_term(&mmap); assert(rc == 0); + + fail_posix_fallocate = 1; + fail_posix_fallocate_errno = EINTR; + rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC, + PROT_READ | PROT_WRITE, MAP_SHARED, 0, 4095); + assert(rc == 0); + assert(sc_mmap_term(&mmap) == 0); + + + fail_posix_fallocate = 1; + fail_posix_fallocate_errno = -1; + rc = sc_mmap_init(&mmap, "x.txt", O_RDWR | O_CREAT | O_TRUNC, + PROT_READ | PROT_WRITE, MAP_SHARED, 0, 4095); + assert(rc != 0); + + fail_posix_fallocate = UINT32_MAX; } #else void fail_test() { - } #endif diff --git a/option/option_test.c b/option/option_test.c index d902ebf..f013838 100644 --- a/option/option_test.c +++ b/option/option_test.c @@ -141,7 +141,51 @@ void test6() { char *value; int argc = 2; - char *argv[] = {"program", "-j9"}; + char *argv[] = {"program", "-s"}; + + struct sc_option opt = {.argv = argv, + .count = sizeof(options) / sizeof(struct sc_option_item), + .options = options}; + + for (int i = 1; i < argc; i++) { + char c = sc_option_at(&opt, i, &value); + switch (c) { + case 's': + break; + default: + assert(false); + break; + } + } +} + +void test7() +{ + char *value; + int argc = 2; + char *argv[] = {"program", "-j"}; + + struct sc_option opt = {.argv = argv, + .count = sizeof(options) / sizeof(struct sc_option_item), + .options = options}; + + for (int i = 1; i < argc; i++) { + char c = sc_option_at(&opt, i, &value); + switch (c) { + case '?': + break; + default: + assert(false); + break; + } + } +} + +void test8() +{ + char *value; + int argc = 2; + char *argv[] = {"program", "-sx"}; struct sc_option opt = {.argv = argv, .count = sizeof(options) / sizeof(struct sc_option_item), @@ -167,6 +211,8 @@ int main() test4(); test5(); test6(); + test7(); + test8(); return 0; } diff --git a/socket/sock_test.c b/socket/sock_test.c index 5cb6d47..c13114e 100644 --- a/socket/sock_test.c +++ b/socket/sock_test.c @@ -373,6 +373,10 @@ void test1() assert(sc_sock_connect(&sock, "d::1", "2131", "dsad", "50") != 0); assert(sc_sock_finish_connect(&sock) != 0); assert(sc_sock_connect(&sock, "dsadas", "2131", "::1", "50") != 0); + assert(sc_sock_connect(&sock, "dsadas", "2131", NULL, "50") != 0); + assert(sc_sock_connect(&sock, "dsadas", "2131", "s", NULL) != 0); + assert(sc_sock_connect(&sock, "127.0.01", "2131", "100.0.0.0", NULL) != 0); + assert(sc_sock_connect(&sock, "127.0.01", "2131", NULL, "9000") != 0); sc_sock_term(&sock); sc_sock_init(&sock, 0, false, SC_SOCK_INET);