map,sock,str tests (#33)

* map, sock tests
This commit is contained in:
Tezc 2021-02-08 23:01:44 +03:00 committed by GitHub
parent 26a6cc95ea
commit 488f63265a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1342 additions and 33 deletions

View File

@ -42,6 +42,467 @@ static char *str_random(size_t size)
return dest;
}
void test_32()
{
uint32_t val;
struct sc_map_32 map;
assert(sc_map_init_32(&map, 0, 0));
sc_map_term_32(&map);
assert(sc_map_init_32(&map, 0, 1) == false);
assert(sc_map_init_32(&map, 0, 99) == false);
assert(sc_map_init_32(&map, 16, 94));
assert(sc_map_del_32(&map, 0, NULL) == false);
assert(sc_map_del_32(&map, 1, NULL) == false);
for (int i = 0; i < 14; i++) {
assert(sc_map_put_32(&map, i, i) == true);
}
for (int i = 100; i < 200; i++) {
assert(sc_map_del_32(&map,i, NULL) == false);
}
sc_map_clear_32(&map);
sc_map_clear_32(&map);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_32(&map, i, i) == true);
}
assert(sc_map_put_32(&map, 31, 15) == true);
assert(sc_map_put_32(&map, 15, 15) == true);
assert(sc_map_put_32(&map, 46, 15) == true);
assert(sc_map_get_32(&map, 19, &val) == false);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_32(&map, (5*i) + i, i) == true);
}
assert(sc_map_del_32(&map,4, NULL) == true);
assert(sc_map_del_32(&map,46, NULL) == true);
assert(sc_map_del_32(&map,15, NULL) == true);
sc_map_clear_32(&map);
for (int i = 1; i < 4; i++) {
assert(sc_map_put_32(&map, 16 * i, i) == true);
}
for (int i = 1; i < 4; i++) {
assert(sc_map_put_32(&map, 1024 * i, i) == true);
}
for (int i = 1; i < 4; i++) {
assert(sc_map_put_32(&map, 512 * i, i) == true);
}
assert(sc_map_del_32(&map, 512, NULL) == true);
assert(sc_map_del_32(&map, 1024, NULL) == true);
assert(sc_map_del_32(&map, 48, NULL) == true);
sc_map_term_32(&map);
}
void test_64()
{
uint64_t val;
struct sc_map_64 map;
assert(sc_map_init_64(&map, 0, 0));
sc_map_term_64(&map);
assert(sc_map_init_64(&map, 0, 1) == false);
assert(sc_map_init_64(&map, 0, 99) == false);
assert(sc_map_init_64(&map, 16, 94));
assert(sc_map_del_64(&map, 0, NULL) == false);
assert(sc_map_del_64(&map, 1, NULL) == false);
for (int i = 0; i < 14; i++) {
assert(sc_map_put_64(&map, i, i) == true);
}
for (int i = 100; i < 200; i++) {
assert(sc_map_del_64(&map,i, NULL) == false);
}
sc_map_clear_64(&map);
sc_map_clear_64(&map);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_64(&map, i, i) == true);
}
assert(sc_map_put_64(&map, 31, 15) == true);
assert(sc_map_put_64(&map, 15, 15) == true);
assert(sc_map_put_64(&map, 46, 15) == true);
assert(sc_map_get_64(&map, 19, &val) == false);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_64(&map, (5*i) + i, i) == true);
}
assert(sc_map_del_64(&map,4, NULL) == true);
assert(sc_map_del_64(&map,46, NULL) == true);
assert(sc_map_del_64(&map,15, NULL) == true);
sc_map_clear_64(&map);
for (int i = 1; i < 4; i++) {
assert(sc_map_put_64(&map, 16 * i, i) == true);
}
for (int i = 1; i < 4; i++) {
assert(sc_map_put_64(&map, 1024 * i, i) == true);
}
for (int i = 1; i < 4; i++) {
assert(sc_map_put_64(&map, 512 * i, i) == true);
}
assert(sc_map_del_64(&map, 512, NULL) == true);
assert(sc_map_del_64(&map, 1024, NULL) == true);
assert(sc_map_del_64(&map, 48, NULL) == true);
sc_map_term_64(&map);
}
void test_64v()
{
void* val;
struct sc_map_64v map;
assert(sc_map_init_64v(&map, 0, 0));
sc_map_term_64v(&map);
assert(sc_map_init_64v(&map, 0, 1) == false);
assert(sc_map_init_64v(&map, 0, 99) == false);
assert(sc_map_init_64v(&map, 16, 94));
assert(sc_map_del_64v(&map, 0, NULL) == false);
assert(sc_map_del_64v(&map, 1, NULL) == false);
for (int i = 0; i < 14; i++) {
assert(sc_map_put_64v(&map, i, NULL) == true);
}
for (int i = 100; i < 200; i++) {
assert(sc_map_del_64v(&map,i, NULL) == false);
}
sc_map_clear_64v(&map);
sc_map_clear_64v(&map);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_64v(&map, i, NULL) == true);
}
assert(sc_map_put_64v(&map, 31, NULL) == true);
assert(sc_map_put_64v(&map, 15, NULL) == true);
assert(sc_map_put_64v(&map, 46, NULL) == true);
assert(sc_map_get_64v(&map, 19, &val) == false);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_64v(&map, (5*i) + i, NULL) == true);
}
assert(sc_map_del_64v(&map,4, NULL) == true);
assert(sc_map_del_64v(&map,46, NULL) == true);
assert(sc_map_del_64v(&map,15, NULL) == true);
sc_map_clear_64v(&map);
for (int i = 1; i < 4; i++) {
assert(sc_map_put_64v(&map, 16 * i, NULL) == true);
}
for (int i = 1; i < 4; i++) {
assert(sc_map_put_64v(&map, 1024 * i, NULL) == true);
}
for (int i = 1; i < 4; i++) {
assert(sc_map_put_64v(&map, 512 * i, NULL) == true);
}
assert(sc_map_del_64v(&map, 512, NULL) == true);
assert(sc_map_del_64v(&map, 1024, NULL) == true);
assert(sc_map_del_64v(&map, 48, NULL) == true);
sc_map_term_64v(&map);
}
void test_64s()
{
const char* val;
struct sc_map_64s map;
assert(sc_map_init_64s(&map, 0, 0));
sc_map_term_64s(&map);
assert(sc_map_init_64s(&map, 0, 1) == false);
assert(sc_map_init_64s(&map, 0, 99) == false);
assert(sc_map_init_64s(&map, 16, 94));
assert(sc_map_del_64s(&map, 0, NULL) == false);
assert(sc_map_del_64s(&map, 1, NULL) == false);
for (int i = 0; i < 14; i++) {
assert(sc_map_put_64s(&map, i, NULL) == true);
}
for (int i = 100; i < 200; i++) {
assert(sc_map_del_64s(&map,i, NULL) == false);
}
sc_map_clear_64s(&map);
sc_map_clear_64s(&map);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_64s(&map, i, NULL) == true);
}
assert(sc_map_put_64s(&map, 31, NULL) == true);
assert(sc_map_put_64s(&map, 15, NULL) == true);
assert(sc_map_put_64s(&map, 46, NULL) == true);
assert(sc_map_get_64s(&map, 19, &val) == false);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_64s(&map, (5*i) + i, NULL) == true);
}
assert(sc_map_del_64s(&map,4, NULL) == true);
assert(sc_map_del_64s(&map,46, NULL) == true);
assert(sc_map_del_64s(&map,15, NULL) == true);
sc_map_clear_64s(&map);
for (int i = 1; i < 4; i++) {
assert(sc_map_put_64s(&map, 16 * i, NULL) == true);
}
for (int i = 1; i < 4; i++) {
assert(sc_map_put_64s(&map, 1024 * i, NULL) == true);
}
for (int i = 1; i < 4; i++) {
assert(sc_map_put_64s(&map, 512 * i, NULL) == true);
}
assert(sc_map_del_64s(&map, 512, NULL) == true);
assert(sc_map_del_64s(&map, 1024, NULL) == true);
assert(sc_map_del_64s(&map, 48, NULL) == true);
sc_map_term_64s(&map);
}
void test_str()
{
const char* arr = "abcdefghijklmnoprstuvyzabcdefghijklmnoprstuvyzabcdefghijklmnoprstuvyz";
const char* val;
struct sc_map_str map;
assert(sc_map_init_str(&map, 0, 0));
sc_map_term_str(&map);
assert(sc_map_init_str(&map, 0, 1) == false);
assert(sc_map_init_str(&map, 0, 99) == false);
assert(sc_map_init_str(&map, 16, 94));
assert(sc_map_del_str(&map, NULL, NULL) == false);
assert(sc_map_del_str(&map, "", NULL) == false);
for (int i = 0; i < 14; i++) {
assert(sc_map_put_str(&map, &arr[i], NULL) == true);
}
for (int i = 15; i < 30; i++) {
assert(sc_map_del_str(&map, &arr[i], NULL) == false);
}
sc_map_clear_str(&map);
sc_map_clear_str(&map);
assert(sc_map_put_str(&map, "h", NULL) == true);
assert(sc_map_put_str(&map, "z", NULL) == true);
assert(sc_map_get_str(&map, "13", &val) == false);
assert(sc_map_get_str(&map, NULL, &val) == false);
assert(sc_map_get_str(&map, "h", &val) == true);
assert(sc_map_get_str(&map, "z", &val) == true);
assert(sc_map_get_str(&map, "x", &val) == false);
assert(sc_map_put_str(&map, NULL, NULL) == true);
assert(sc_map_get_str(&map, NULL, &val) == true);
assert(sc_map_del_str(&map, NULL, &val) == true);
assert(sc_map_del_str(&map, "h", &val) == true);
assert(sc_map_del_str(&map, "13", &val) == false);
sc_map_clear_str(&map);
assert(sc_map_size_str(&map) == 0);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_str(&map, &arr[i], NULL) == true);
}
assert(sc_map_put_str(&map, &arr[15], NULL) == true);
assert(sc_map_put_str(&map, &arr[7], NULL) == true);
assert(sc_map_put_str(&map, &arr[9], NULL) == true);
assert(sc_map_get_str(&map, &arr[16], &val) == false);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_str(&map, &arr[(5*i) + i], NULL) == true);
}
assert(sc_map_del_str(&map,&arr[4], NULL) == true);
assert(sc_map_del_str(&map,&arr[6], NULL) == true);
assert(sc_map_del_str(&map,&arr[15], NULL) == true);
sc_map_clear_str(&map);
assert(sc_map_put_str(&map, "h", NULL) == true);
assert(sc_map_put_str(&map, "z", NULL) == true);
assert(sc_map_del_str(&map, "h", NULL) == true);
sc_map_clear_str(&map);
assert(sc_map_put_str(&map, "h", NULL) == true);
assert(sc_map_put_str(&map, "z", NULL) == true);
assert(sc_map_put_str(&map, "13", NULL) == true);
assert(sc_map_del_str(&map, "z", NULL) == true);
sc_map_term_str(&map);
}
void test_sv()
{
const char* arr = "abcdefghijklmnoprstuvyzabcdefghijklmnoprstuvyzabcdefghijklmnoprstuvyz";
void* val;
struct sc_map_sv map;
assert(sc_map_init_sv(&map, 0, 0));
sc_map_term_sv(&map);
assert(sc_map_init_sv(&map, 0, 1) == false);
assert(sc_map_init_sv(&map, 0, 99) == false);
assert(sc_map_init_sv(&map, 16, 94));
assert(sc_map_del_sv(&map, NULL, NULL) == false);
assert(sc_map_del_sv(&map, "", NULL) == false);
for (int i = 0; i < 14; i++) {
assert(sc_map_put_sv(&map, &arr[i], NULL) == true);
}
for (int i = 15; i < 30; i++) {
assert(sc_map_del_sv(&map, &arr[i], NULL) == false);
}
sc_map_clear_sv(&map);
sc_map_clear_sv(&map);
assert(sc_map_put_sv(&map, "h", NULL) == true);
assert(sc_map_put_sv(&map, "z", NULL) == true);
assert(sc_map_get_sv(&map, "13", &val) == false);
assert(sc_map_get_sv(&map, NULL, &val) == false);
assert(sc_map_get_sv(&map, "h", &val) == true);
assert(sc_map_get_sv(&map, "z", &val) == true);
assert(sc_map_get_sv(&map, "x", &val) == false);
assert(sc_map_put_sv(&map, NULL, NULL) == true);
assert(sc_map_get_sv(&map, NULL, &val) == true);
assert(sc_map_del_sv(&map, NULL, &val) == true);
assert(sc_map_del_sv(&map, "h", &val) == true);
assert(sc_map_del_sv(&map, "13", &val) == false);
sc_map_clear_sv(&map);
assert(sc_map_size_sv(&map) == 0);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_sv(&map, &arr[i], NULL) == true);
}
assert(sc_map_put_sv(&map, &arr[15], NULL) == true);
assert(sc_map_put_sv(&map, &arr[7], NULL) == true);
assert(sc_map_put_sv(&map, &arr[9], NULL) == true);
assert(sc_map_get_sv(&map, &arr[16], &val) == false);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_sv(&map, &arr[(5*i) + i], NULL) == true);
}
assert(sc_map_del_sv(&map,&arr[4], NULL) == true);
assert(sc_map_del_sv(&map,&arr[6], NULL) == true);
assert(sc_map_del_sv(&map,&arr[15], NULL) == true);
sc_map_clear_sv(&map);
assert(sc_map_put_sv(&map, "h", NULL) == true);
assert(sc_map_put_sv(&map, "z", NULL) == true);
assert(sc_map_del_sv(&map, "h", NULL) == true);
sc_map_clear_sv(&map);
assert(sc_map_put_sv(&map, "h", NULL) == true);
assert(sc_map_put_sv(&map, "z", NULL) == true);
assert(sc_map_put_sv(&map, "13", NULL) == true);
assert(sc_map_del_sv(&map, "z", NULL) == true);
sc_map_term_sv(&map);
}
void test_s64()
{
const char* arr = "abcdefghijklmnoprstuvyzabcdefghijklmnoprstuvyzabcdefghijklmnoprstuvyz";
uint64_t val;
struct sc_map_s64 map;
assert(sc_map_init_s64(&map, 0, 0));
sc_map_term_s64(&map);
assert(sc_map_init_s64(&map, 0, 1) == false);
assert(sc_map_init_s64(&map, 0, 99) == false);
assert(sc_map_init_s64(&map, 16, 94));
assert(sc_map_del_s64(&map, NULL, NULL) == false);
assert(sc_map_del_s64(&map, "", NULL) == false);
for (int i = 0; i < 14; i++) {
assert(sc_map_put_s64(&map, &arr[i], 0) == true);
}
for (int i = 15; i < 30; i++) {
assert(sc_map_del_s64(&map, &arr[i], NULL) == false);
}
sc_map_clear_s64(&map);
sc_map_clear_s64(&map);
assert(sc_map_put_s64(&map, "h", 0) == true);
assert(sc_map_put_s64(&map, "z", 0) == true);
assert(sc_map_get_s64(&map, "13", &val) == false);
assert(sc_map_get_s64(&map, NULL, &val) == false);
assert(sc_map_get_s64(&map, "h", &val) == true);
assert(sc_map_get_s64(&map, "z", &val) == true);
assert(sc_map_get_s64(&map, "x", &val) == false);
assert(sc_map_put_s64(&map, NULL, 0) == true);
assert(sc_map_get_s64(&map, NULL, &val) == true);
assert(sc_map_del_s64(&map, NULL, &val) == true);
assert(sc_map_del_s64(&map, "h", &val) == true);
assert(sc_map_del_s64(&map, "13", &val) == false);
sc_map_clear_s64(&map);
assert(sc_map_size_s64(&map) == 0);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_s64(&map, &arr[i], 0) == true);
}
assert(sc_map_put_s64(&map, &arr[15], 0) == true);
assert(sc_map_put_s64(&map, &arr[7], 0) == true);
assert(sc_map_put_s64(&map, &arr[9], 0) == true);
assert(sc_map_get_s64(&map, &arr[16], &val) == false);
for (int i = 0; i < 5; i++) {
assert(sc_map_put_s64(&map, &arr[(5*i) + i], 0) == true);
}
assert(sc_map_del_s64(&map,&arr[4], NULL) == true);
assert(sc_map_del_s64(&map,&arr[6], NULL) == true);
assert(sc_map_del_s64(&map,&arr[15], NULL) == true);
sc_map_clear_s64(&map);
assert(sc_map_put_s64(&map, "h", 0) == true);
assert(sc_map_put_s64(&map, "z", 0) == true);
assert(sc_map_del_s64(&map, "h", 0) == true);
sc_map_clear_s64(&map);
assert(sc_map_put_s64(&map, "h", 0) == true);
assert(sc_map_put_s64(&map, "z", 0) == true);
assert(sc_map_put_s64(&map, "13", 0) == true);
assert(sc_map_del_s64(&map, "z", 0) == true);
sc_map_term_s64(&map);
}
void test1()
{
struct sc_map_str map;
@ -523,7 +984,7 @@ void *__wrap_calloc(size_t n, size_t size)
return __real_calloc(n, size);
}
void fail_test()
void fail_test_32()
{
struct sc_map_32 map;
@ -549,8 +1010,225 @@ void fail_test()
sc_map_term_32(&map);
}
void fail_test_64()
{
struct sc_map_64 map;
fail_calloc = true;
assert(!sc_map_init_64(&map, 10, 0));
fail_calloc = false;
assert(sc_map_init_64(&map, 10, 0));
fail_calloc = true;
bool success = true;
for (int i = 0; i < 20; i++) {
success = sc_map_put_64(&map, i, i);
}
assert(!success);
fail_calloc = false;
assert(sc_map_put_64(&map, 44444, 44444));
for (size_t i = 0; i < SC_SIZE_MAX; i++) {
success = sc_map_put_64(&map, i, i);
}
assert(!success);
fail_calloc = false;
sc_map_term_64(&map);
}
void fail_test_64v()
{
struct sc_map_64v map;
fail_calloc = true;
assert(!sc_map_init_64v(&map, 10, 0));
fail_calloc = false;
assert(sc_map_init_64v(&map, 10, 0));
fail_calloc = true;
bool success = true;
for (int i = 0; i < 20; i++) {
success = sc_map_put_64v(&map, i, NULL);
}
assert(!success);
fail_calloc = false;
assert(sc_map_put_64v(&map, 44444, NULL));
for (size_t i = 0; i < SC_SIZE_MAX; i++) {
success = sc_map_put_64v(&map, i, NULL);
}
assert(!success);
fail_calloc = false;
sc_map_term_64v(&map);
}
void fail_test_64s()
{
struct sc_map_64s map;
fail_calloc = true;
assert(!sc_map_init_64s(&map, 10, 0));
fail_calloc = false;
assert(sc_map_init_64s(&map, 10, 0));
fail_calloc = true;
bool success = true;
for (int i = 0; i < 20; i++) {
success = sc_map_put_64s(&map, i, NULL);
}
assert(!success);
fail_calloc = false;
assert(sc_map_put_64s(&map, 44444, NULL));
for (size_t i = 0; i < SC_SIZE_MAX; i++) {
success = sc_map_put_64s(&map, i, NULL);
}
assert(!success);
fail_calloc = false;
sc_map_term_64s(&map);
}
void fail_test_str()
{
struct sc_map_str map;
const char* v;
const char* s = "abcdefghijklmnoprstuvyz10111213141516";
fail_calloc = true;
assert(!sc_map_init_str(&map, 10, 0));
fail_calloc = false;
assert(sc_map_init_str(&map, 10, 0));
fail_calloc = true;
bool success = true;
for (int i = 0; i < 20; i++) {
success = sc_map_put_str(&map, &s[i], NULL);
}
assert(!success);
fail_calloc = false;
assert(sc_map_put_str(&map, &s[21], NULL));
sc_map_clear_str(&map);
for (size_t i = 0; i < SC_SIZE_MAX; i++) {
char* c = str_random(32);
success = sc_map_put_str(&map,c, NULL);
if (!success) {
free(c);
break;
}
}
assert(!success);
fail_calloc = false;
sc_map_foreach_key(&map, v) {
free((void*) v);
}
sc_map_term_str(&map);
}
void fail_test_sv()
{
struct sc_map_sv map;
const char* v;
const char* s = "abcdefghijklmnoprstuvyz10111213141516";
fail_calloc = true;
assert(!sc_map_init_sv(&map, 10, 0));
fail_calloc = false;
assert(sc_map_init_sv(&map, 10, 0));
fail_calloc = true;
bool success = true;
for (int i = 0; i < 20; i++) {
success = sc_map_put_sv(&map, &s[i], NULL);
}
assert(!success);
fail_calloc = false;
assert(sc_map_put_sv(&map, &s[21], NULL));
sc_map_clear_sv(&map);
for (size_t i = 0; i < SC_SIZE_MAX; i++) {
char* c = str_random(32);
success = sc_map_put_sv(&map,c, NULL);
if (!success) {
free(c);
break;
}
}
assert(!success);
fail_calloc = false;
sc_map_foreach_key(&map, v) {
free((void*) v);
}
sc_map_term_sv(&map);
}
void fail_test_s64()
{
struct sc_map_s64 map;
const char* v;
const char* s = "abcdefghijklmnoprstuvyz10111213141516";
fail_calloc = true;
assert(!sc_map_init_s64(&map, 10, 0));
fail_calloc = false;
assert(sc_map_init_s64(&map, 10, 0));
fail_calloc = true;
bool success = true;
for (int i = 0; i < 20; i++) {
success = sc_map_put_s64(&map, &s[i], 0);
}
assert(!success);
fail_calloc = false;
assert(sc_map_put_s64(&map, &s[21], 0));
sc_map_clear_s64(&map);
for (size_t i = 0; i < SC_SIZE_MAX; i++) {
char* c = str_random(32);
success = sc_map_put_s64(&map,c, 0);
if (!success) {
free(c);
break;
}
}
assert(!success);
fail_calloc = false;
sc_map_foreach_key(&map, v) {
free((void*) v);
}
sc_map_term_s64(&map);
}
#else
void fail_test(void)
void fail_test_32(void)
{
}
void fail_test_64(void)
{
}
void fail_test_64v(void)
{
}
void fail_test_64s(void)
{
}
void fail_test_str(void)
{
}
void fail_test_sv(void)
{
}
void fail_test_s64(void)
{
}
#endif
@ -558,13 +1236,26 @@ void fail_test(void)
int main()
{
example();
fail_test();
fail_test_32();
fail_test_64();
fail_test_64v();
fail_test_64s();
fail_test_str();
fail_test_sv();
fail_test_s64();
test1();
test2();
test3();
test4();
test5();
test6();
test_32();
test_64();
test_64s();
test_64v();
test_str();
test_sv();
test_s64();
return 0;
}

View File

@ -23,7 +23,7 @@ enable_testing()
add_executable(${PROJECT_NAME}_test sock_test.c sc_sock.c)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_SIZE_MAX=1400000ul)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_SIZE_MAX=300 -Dsc_fcntl=test_fcntl)
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
@ -33,7 +33,10 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
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=pipe
-Wl,--wrap=close,--wrap=malloc,--wrap=realloc,--wrap=epoll_create1)
-Wl,--wrap=close,--wrap=malloc,--wrap=realloc,--wrap=epoll_create1
-Wl,--wrap=setsockopt,--wrap=listen,--wrap=fcntl,--wrap=socket
-Wl,--wrap=inet_ntop,--wrap=send,--wrap=recv,--wrap=connect
-Wl,--wrap=write,--wrap=read,--wrap=epoll_ctl)
endif ()
endif ()

View File

@ -26,7 +26,6 @@
#include "sc_sock.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
@ -34,10 +33,14 @@
#include <stdlib.h>
#include <string.h>
#ifndef SC_SIZE_MAX
#define SC_SIZE_MAX INT32_MAX
#endif
#if defined(_WIN32) || defined(_WIN64)
#include <Ws2tcpip.h>
#include <afunix.h>
#include <assert.h>
#pragma warning(disable : 4996)
#define sc_close(n) closesocket(n)
@ -340,7 +343,7 @@ int sc_sock_finish_connect(struct sc_sock *sock)
static int sc_sock_connect_unix(struct sc_sock *sock, const char *addr)
{
int rc, err;
int rc;
const size_t len = strlen(addr);
struct sockaddr_un addr_un = {.sun_family = AF_UNIX};
@ -352,10 +355,6 @@ static int sc_sock_connect_unix(struct sc_sock *sock, const char *addr)
rc = connect(sock->fdt.fd, (struct sockaddr *) &addr_un, sizeof(addr_un));
if (rc != 0) {
err = sc_sock_err();
if (!sock->blocking && ((err == SC_EINTR || err == SC_EINPROGRESS))) {
return 0;
}
sc_sock_errstr(sock, 0);
}
@ -483,10 +482,6 @@ error_unix:
goto end;
}
if (p == NULL) {
goto error;
}
error:
sc_sock_errstr(sock, 0);
error_gai:
@ -502,14 +497,12 @@ int sc_sock_send(struct sc_sock *sock, char *buf, int len, int flags)
{
int n;
assert(len > 0);
if (len <= 0) {
return len;
}
retry:
n = send(sock->fdt.fd, buf, (size_t) len, flags);
n = (int) send(sock->fdt.fd, buf, (size_t) len, flags);
if (n == SC_ERR) {
int err = sc_sock_err();
if (err == SC_EINTR) {
@ -531,14 +524,12 @@ int sc_sock_recv(struct sc_sock *sock, char *buf, int len, int flags)
{
int n;
assert(len > 0);
if (len <= 0) {
return len;
}
retry:
n = recv(sock->fdt.fd, buf, (size_t) len, flags);
n = (int) recv(sock->fdt.fd, buf, (size_t) len, flags);
if (n == 0) {
return SC_SOCK_ERROR;
} else if (n == SC_ERR) {
@ -621,6 +612,7 @@ int sc_sock_listen(struct sc_sock *sock, const char *host, const char *port)
rc = listen(sock->fdt.fd, 4096);
if (rc != 0) {
sc_sock_errstr(sock, 0);
sc_sock_close(sock);
}
return rc == 0 ? 0 : -1;
@ -675,10 +667,6 @@ static const char *sc_sock_print_storage(struct sc_sock *sock,
addr_un = (struct sockaddr_un *) storage;
snprintf(buf, len, "%s", addr_un->sun_path);
break;
default:
snprintf(buf, len, "Unknown family : %d \n", storage->ss_family);
break;
}
return buf;
@ -995,7 +983,7 @@ static int sc_sock_poll_expand(struct sc_sock_poll *p)
void *ev;
if (p->count == p->cap) {
if (p->cap >= INT32_MAX / 2) {
if (p->cap >= SC_SIZE_MAX / 2) {
goto error;
}
@ -1172,7 +1160,7 @@ static int sc_sock_poll_expand(struct sc_sock_poll *p)
void *ev;
if (p->count == p->cap) {
if (p->cap >= INT32_MAX / 2) {
if (p->cap >= SC_SIZE_MAX / 2) {
goto error;
}
@ -1353,7 +1341,7 @@ static int sc_sock_poll_expand(struct sc_sock_poll* p)
struct pollfd* ev = NULL;
if (p->count == p->cap) {
if (p->cap >= INT32_MAX / 2) {
if (p->cap >= SC_SIZE_MAX / 2) {
goto error;
}

View File

@ -1,3 +1,4 @@
#define _XOPEN_SOURCE 700
#include "sc_sock.h"
@ -24,7 +25,9 @@ struct sc_thread
#else
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@ -351,8 +354,9 @@ void test_unix()
void test1()
{
int rc;
char tmp[5];
struct sc_sock sock;
struct sc_sock sock, client, in;
sc_sock_init(&sock, 0, false, SC_SOCK_INET);
assert(sc_sock_connect(&sock, "3127.0.0.1", "2131", NULL, NULL) != 0);
@ -367,8 +371,26 @@ 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);
sc_sock_term(&sock);
sc_sock_init(&sock, 0, false, SC_SOCK_INET);
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
rc = sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL);
assert(rc != SC_SOCK_ERROR);
sleep(2);
rc = sc_sock_accept(&sock, &in);
if (rc != 0) {
printf("%d, %s \n", rc, sc_sock_error(&sock));
assert(true);
}
assert(sc_sock_finish_connect(&client) == 0);
assert(sc_sock_term(&sock) == 0);
assert(sc_sock_term(&client) == 0);
assert(sc_sock_term(&in) == 0);
}
void test_poll_mass(void)
@ -445,6 +467,74 @@ void test_pipe(void)
#ifdef SC_HAVE_WRAP
struct sc_mutex
{
pthread_mutex_t mtx;
};
int test_done = 0;
struct sc_mutex mutex;
int sc_mutex_init(struct sc_mutex *mtx)
{
int rc, rv;
pthread_mutexattr_t attr;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
mtx->mtx = mut;
// May fail on OOM
rc = pthread_mutexattr_init(&attr);
if (rc != 0) {
return -1;
}
// This won't fail as long as we pass correct params.
rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
assert (rc == 0);
// May fail on OOM
rc = pthread_mutex_init(&mtx->mtx, &attr);
// This won't fail as long as we pass correct param.
rv = pthread_mutexattr_destroy(&attr);
assert(rv == 0);
return rc != 0 ? -1 : 0;
}
int sc_mutex_term(struct sc_mutex *mtx)
{
int rc;
rc = pthread_mutex_destroy(&mtx->mtx);
return rc != 0 ? -1 : 0;
}
void sc_mutex_lock(struct sc_mutex *mtx)
{
int rc;
// This won't fail as long as we pass correct param.
rc = pthread_mutex_lock(&mtx->mtx);
if (rc != 0) {
printf("%s \n", strerror(rc));
fprintf(stderr, "%s \n", strerror(rc));
fflush(stderr);
fflush(stdout);
}
assert(rc == 0);
}
void sc_mutex_unlock(struct sc_mutex *mtx)
{
int rc;
// This won't fail as long as we pass correct param.
rc = pthread_mutex_unlock(&mtx->mtx);
assert(rc == 0);
}
bool fail_malloc = false;
void *__real_malloc(size_t n);
void *__wrap_malloc(size_t n)
@ -483,6 +573,7 @@ int __real_close(int fd);
int __wrap_close(int fd)
{
if (fail_close) {
__real_close(fd);
return -1;
}
@ -500,12 +591,199 @@ int __wrap_pipe(int __pipedes[2])
return __real_pipe(__pipedes);
}
int fail_setsockopt = INT32_MAX;
int __real_setsockopt(int fd, int level, int optname, const void *optval,
socklen_t optlen);
int __wrap_setsockopt(int fd, int level, int optname, const void *optval,
socklen_t optlen)
{
sc_mutex_lock(&mutex);
if (--fail_setsockopt <= 0) {
sc_mutex_unlock(&mutex);
return -1;
}
sc_mutex_unlock(&mutex);
return __real_setsockopt(fd, level, optname, optval, optlen);
}
int fail_listen = false;
int __real_listen(int fd, int n);
int __wrap_listen(int fd, int n)
{
if (fail_listen) {
return -1;
}
return __real_listen(fd, n);
}
int fail_fcntl = INT32_MAX;
int __real_fcntl(int fd, int cmd, ...);
int __wrap_fcntl(int fd, int cmd, ...)
{
(void) fd;
(void) cmd;
if (test_done) {
va_list va;
va_start(va, cmd);
void* t = va_arg(va, void*);
return __real_fcntl(fd, cmd, t);
}
sc_mutex_lock(&mutex);
if (--fail_fcntl <= 0) {
sc_mutex_unlock(&mutex);
return -1;
}
if (cmd == F_GETFL) {
sc_mutex_unlock(&mutex);
return __real_fcntl(fd, F_GETFL, 0);
}
if (cmd == F_SETFL) {
va_list va;
va_start(va, cmd);
int x = va_arg(va, int);
sc_mutex_unlock(&mutex);
return __real_fcntl(fd, F_SETFL, x);
}
sc_mutex_unlock(&mutex);
return -1;
}
int fail_socket = false;
int __real_socket(int domain, int type, int protocol);
int __wrap_socket(int domain, int type, int protocol)
{
if (fail_socket) {
return -1;
}
return __real_socket(domain, type, protocol);
}
int fail_inet_ntop = INT32_MAX;
const char *__real_inet_ntop(int af, const void *restrict cp,
char *restrict buf, socklen_t len);
const char *__wrap_inet_ntop(int af, const void *restrict cp,
char *restrict buf, socklen_t len)
{
sc_mutex_lock(&mutex);
if (--fail_inet_ntop <= 0) {
sc_mutex_unlock(&mutex);
return NULL;
}
sc_mutex_unlock(&mutex);
return __real_inet_ntop(af, cp, buf, len);
}
int fail_connect = 0;
int fail_connect_err = 0;
int __real_connect(int fd, const struct sockaddr *addr, socklen_t len);
int __wrap_connect(int fd, const struct sockaddr *addr, socklen_t len)
{
if (fail_connect) {
errno = fail_connect_err;
return -1;
}
return __real_connect(fd, addr, len);
}
int fail_send = INT32_MAX;
int fail_send_err = 0;
int fail_send_errno = 0;
ssize_t __real_send(int fd, const void *buf, size_t n, int flags);
ssize_t __wrap_send(int fd, const void *buf, size_t n, int flags)
{
if (--fail_send <= 0) {
errno = (errno == EINTR) ? EINVAL : fail_send_errno;
return fail_send_err;
}
return __real_send(fd, buf, n, flags);
}
int fail_write = INT32_MAX;
int fail_write_err = 0;
int fail_write_errno = 0;
ssize_t __real_write(int fd, const void *buf, size_t n);
ssize_t __wrap_write(int fd, const void *buf, size_t n)
{
if (--fail_write <= 0) {
errno = (errno == EINTR) ? EINVAL : fail_write_errno;
return fail_write_err;
}
return __real_write(fd, buf, n);
}
int fail_recv = INT32_MAX;
int fail_recv_err = 0;
int fail_recv_errno = 0;
ssize_t __real_recv(int fd, void *buf, size_t n, int flags);
ssize_t __wrap_recv(int fd, void *buf, size_t n, int flags)
{
if (--fail_recv <= 0) {
errno = (errno == EINTR) ? EINVAL : fail_recv_errno;
return fail_recv_err;
}
return __real_recv(fd, buf, n, flags);
}
int fail_read = INT32_MAX;
int fail_read_err = 0;
int fail_read_errno = 0;
ssize_t __real_read(int fd, void *buf, size_t n, int flags);
ssize_t __wrap_read(int fd, void *buf, size_t n, int flags)
{
if (--fail_read <= 0) {
errno = (errno == EINTR) ? EINVAL : fail_read_errno;
return fail_read_err;
}
return __real_read(fd, buf, n, flags);
}
int fail_epoll_ctl = false;
int __real_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
int __wrap_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
{
if (fail_epoll_ctl) {
return -1;
}
return __real_epoll_ctl(epfd, op, fd, event);
}
void poll_fail_test()
{
bool fail;
int i;
struct sc_sock sock;
struct sc_sock_poll poll;
struct sc_sock_pipe pipe[200];
struct sc_sock_pipe pipe[301];
assert(sc_sock_poll_init(&poll) == 0);
sc_sock_init(&sock, 0, true, AF_INET);
fail_epoll_ctl = true;
assert(sc_sock_poll_add(&poll, &sock.fdt, SC_SOCK_READ, NULL) == -1);
fail_epoll_ctl = false;
sc_sock_term(&sock);
sc_sock_poll_term(&poll);
assert(sc_sock_poll_init(&poll) == 0);
sc_sock_init(&sock, 0, true, AF_INET);
sock.fdt.op = SC_SOCK_READ;
assert(sc_sock_poll_del(&poll, &sock.fdt, SC_SOCK_READ, NULL) == -1);
sc_sock_term(&sock);
sc_sock_poll_term(&poll);
fail_malloc = true;
assert(sc_sock_poll_init(&poll) == -1);
@ -639,6 +917,22 @@ void poll_fail_test()
assert(poll.count == 0);
assert(sc_sock_poll_term(&poll) == 0);
fail_realloc = false;
assert(sc_sock_poll_init(&poll) == 0);
for (i = 0; i < SC_SIZE_MAX + 1; i++) {
assert(sc_sock_pipe_init(&pipe[i], 0) == 0);
fail = sc_sock_poll_add(&poll, &pipe[i].fdt, SC_SOCK_READ, NULL);
if (fail) {
break;
}
}
assert(fail);
for (int j = 0; j <= i; j++) {
assert(sc_sock_poll_del(&poll, &pipe[j].fdt, SC_SOCK_READ, NULL) == 0);
assert(sc_sock_pipe_term(&pipe[j]) == 0);
}
assert(poll.count == 0);
assert(sc_sock_poll_term(&poll) == 0);
}
void pipe_fail_test()
@ -652,10 +946,331 @@ void pipe_fail_test()
fail_close = true;
assert(sc_sock_pipe_term(&pipe) == -1);
fail_close = false;
assert(sc_sock_pipe_term(&pipe) == 0);
assert(sc_sock_pipe_init(&pipe, 0) == 0);
fail_write = 1;
fail_write_err = -1;
fail_write_errno = EINTR;
assert(sc_sock_pipe_write(&pipe, NULL, 10) == -1);
sc_sock_pipe_term(&pipe);
fail_write = INT32_MAX;
fail_write_err = 0;
fail_write_errno = 0;
assert(sc_sock_pipe_init(&pipe, 0) == 0);
fail_read = 1;
fail_read_err = -1;
fail_read_errno = EINTR;
assert(sc_sock_pipe_read(&pipe, NULL, 10) == -1);
sc_sock_pipe_term(&pipe);
fail_read = INT32_MAX;
fail_read_err = 0;
fail_read_errno = 0;
}
void sock_fail_test()
{
struct sc_sock sock;
struct sc_sock sock2;
struct sc_sock sock3;
sc_sock_init(&sock, 0, true, SC_SOCK_INET);
sc_sock_listen(&sock, "127.0.0.1", "8080");
fail_close = true;
assert(sc_sock_term(&sock) == -1);
assert(*sc_sock_error(&sock));
fail_close = false;
sc_sock_init(&sock, 0, true, SC_SOCK_INET);
fail_setsockopt = 1;
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == -1);
fail_setsockopt = 2;
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == -1);
fail_setsockopt = 3;
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == -1);
fail_setsockopt = 4;
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == -1);
fail_setsockopt = 5;
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == 0);
assert(sc_sock_term(&sock) == 0);
sc_sock_init(&sock, 0, true, SC_SOCK_UNIX);
fail_setsockopt = 1;
assert(sc_sock_listen(&sock, "/tmp/x", NULL) == -1);
fail_setsockopt = 2;
assert(sc_sock_listen(&sock, "/tmp/x", NULL) == -1);
fail_setsockopt = 3;
assert(sc_sock_listen(&sock, "/tmp/x", NULL) == 0);
assert(sc_sock_term(&sock) == 0);
fail_setsockopt = INT32_MAX;
sc_sock_init(&sock, 0, true, SC_SOCK_INET);
fail_listen = 1;
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == -1);
fail_listen = 0;
assert(sc_sock_accept(&sock, &sock2) == -1);
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == 0);
fail_setsockopt = INT32_MAX;
sc_sock_init(&sock2, 0, true, SC_SOCK_INET);
assert(sc_sock_connect(&sock2, "127.0.0.1", "8080", NULL, NULL) == 0);
fail_setsockopt = 1;
assert(sc_sock_accept(&sock, &sock3) == -1);
fail_setsockopt = INT32_MAX;
sc_sock_term(&sock);
sc_sock_term(&sock2);
assert(sc_sock_recv(&sock, NULL, -33, 0) == -33);
assert(sc_sock_send(&sock, NULL, -33, 0) == -33);
sc_sock_init(&sock, 0, true, SC_SOCK_INET6);
fail_setsockopt = 1;
assert(sc_sock_listen(&sock, "::1", "8080") == -1);
assert(sc_sock_term(&sock) == 0);
fail_setsockopt = INT32_MAX;
assert(sc_sock_set_blocking(&sock, true) == -1);
sc_sock_init(&sock, 0, true, SC_SOCK_INET);
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == 0);
fail_fcntl = 2;
assert(sc_sock_set_blocking(&sock, true) == -1);
fail_fcntl = INT32_MAX;
sc_sock_term(&sock);
sc_sock_init(&sock, 0, true, SC_SOCK_INET);
fail_fcntl = 1;
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == -1);
assert(sc_sock_term(&sock) == 0);
fail_fcntl = INT32_MAX;
sc_sock_init(&sock, 0, true, SC_SOCK_UNIX);
assert(sc_sock_connect(
&sock,
"/tmp/"
"longlonglonglolonglonglonglonglonglonglonglonglonglonglongl"
"onglonglonglonglonglonglongnglonglonglonglonglong",
"", NULL, NULL) == -1);
sc_sock_term(&sock);
sc_sock_init(&sock, 0, true, SC_SOCK_INET);
fail_socket = 1;
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == -1);
assert(sc_sock_connect(&sock, "127.0.0.1", "8080", NULL, NULL) == -1);
assert(sc_sock_term(&sock) == 0);
sc_sock_init(&sock, 0, true, SC_SOCK_UNIX);
fail_socket = 1;
assert(sc_sock_listen(&sock, "/tmp/x", "8080") == -1);
assert(sc_sock_connect(&sock, "/tmp/x", "8080", NULL, NULL) == -1);
assert(sc_sock_term(&sock) == 0);
fail_socket = 0;
}
void sock_fail_test2()
{
int rc;
struct sc_sock sock, client, in;
sc_sock_init(&sock, 0, false, SC_SOCK_INET);
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
rc = sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL);
assert(rc != SC_SOCK_ERROR);
sleep(2);
fail_fcntl = 1;
assert(sc_sock_accept(&sock, &in) == -1);
fail_fcntl = INT32_MAX;
assert(sc_sock_term(&sock) == 0);
assert(sc_sock_term(&client) == 0);
assert(sc_sock_term(&in) == 0);
sc_sock_init(&sock, 0, false, SC_SOCK_INET);
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
rc = sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL);
assert(rc != SC_SOCK_ERROR);
sleep(2);
fail_setsockopt = 1;
assert(sc_sock_accept(&sock, &in) == -1);
fail_setsockopt = INT32_MAX;
assert(sc_sock_term(&sock) == 0);
assert(sc_sock_term(&client) == 0);
assert(sc_sock_term(&in) == 0);
sc_sock_init(&sock, 0, false, SC_SOCK_INET);
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
rc = sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL);
assert(rc != SC_SOCK_ERROR);
sleep(2);
fail_setsockopt = 2;
assert(sc_sock_accept(&sock, &in) == -1);
fail_setsockopt = INT32_MAX;
assert(sc_sock_term(&sock) == 0);
assert(sc_sock_term(&client) == 0);
assert(sc_sock_term(&in) == 0);
sc_sock_init(&sock, 0, false, SC_SOCK_INET);
assert(sc_sock_listen(&sock, "127.0.0.1", "8080") == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
rc = sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL);
assert(rc != SC_SOCK_ERROR);
sleep(2);
fail_setsockopt = 3;
assert(sc_sock_accept(&sock, &in) == -1);
fail_setsockopt = INT32_MAX;
assert(sc_sock_term(&sock) == 0);
assert(sc_sock_term(&client) == 0);
assert(sc_sock_term(&in) == 0);
sc_sock_init(&sock, 0, false, SC_SOCK_UNIX);
assert(sc_sock_listen(&sock, "/tmp/x", "8080") == 0);
sc_sock_init(&client, 0, false, SC_SOCK_UNIX);
fail_setsockopt = 1;
rc = sc_sock_connect(&client, "/tmp/x", "8080", NULL, NULL);
assert(rc == SC_SOCK_ERROR);
fail_setsockopt = INT32_MAX;
assert(sc_sock_term(&sock) == 0);
assert(sc_sock_term(&client) == 0);
assert(sc_sock_term(&in) == 0);
sc_sock_init(&sock, 0, false, SC_SOCK_UNIX);
assert(sc_sock_listen(&sock, "/tmp/x", "8080") == 0);
sc_sock_init(&client, 0, false, SC_SOCK_UNIX);
fail_setsockopt = 2;
rc = sc_sock_connect(&client, "/tmp/x", "8080", NULL, NULL);
assert(rc == SC_SOCK_ERROR);
fail_setsockopt = INT32_MAX;
assert(sc_sock_term(&sock) == 0);
assert(sc_sock_term(&client) == 0);
assert(sc_sock_term(&in) == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_setsockopt = 1;
rc = sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL);
assert(rc == SC_SOCK_ERROR);
fail_setsockopt = INT32_MAX;
assert(sc_sock_term(&client) == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_setsockopt = 2;
rc = sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL);
assert(rc == SC_SOCK_ERROR);
fail_setsockopt = INT32_MAX;
assert(sc_sock_term(&client) == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_setsockopt = 3;
rc = sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL);
assert(rc == SC_SOCK_ERROR);
fail_setsockopt = INT32_MAX;
assert(sc_sock_term(&client) == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_setsockopt = 4;
rc = sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL);
assert(rc == SC_SOCK_ERROR);
fail_setsockopt = INT32_MAX;
assert(sc_sock_term(&client) == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
sc_sock_listen(&client, "127.0.0.1", "8080");
fail_inet_ntop = 1;
sc_sock_print(&client, NULL, 0);
fail_inet_ntop = INT32_MAX;
assert(sc_sock_term(&client) == 0);
}
void sock_fail_test3()
{
struct sc_sock client;
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_send = 1;
fail_send_errno = EAGAIN;
fail_send_err = -1;
assert(sc_sock_send(&client, NULL, 10, 0) == SC_SOCK_WANT_WRITE);
sc_sock_term(&client);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_send = 1;
fail_send_err = -1;
fail_send_errno = EINTR;
assert(sc_sock_send(&client, NULL, 10, 0) == SC_SOCK_ERROR);
sc_sock_term(&client);
fail_send = INT32_MAX;
fail_send_err = 0;
fail_send_errno = 0;
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_recv = 1;
fail_recv_errno = EAGAIN;
fail_recv_err = -1;
assert(sc_sock_recv(&client, NULL, 10, 0) == SC_SOCK_WANT_READ);
sc_sock_term(&client);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_recv = 1;
fail_recv_err = -1;
fail_recv_errno = EINTR;
assert(sc_sock_recv(&client, NULL, 10, 0) == SC_SOCK_ERROR);
sc_sock_term(&client);
fail_recv = INT32_MAX;
fail_recv_err = 0;
fail_recv_errno = 0;
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_connect_err = EINPROGRESS;
fail_connect = -1;
assert(sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL) ==
SC_SOCK_WANT_WRITE);
fail_connect_err = 0;
fail_connect = 0;
assert(sc_sock_term(&client) == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_connect_err = -999;
fail_connect = -1;
assert(sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL) == -1);
fail_connect_err = 0;
fail_connect = 0;
assert(sc_sock_term(&client) == 0);
sc_sock_init(&client, 0, false, SC_SOCK_INET);
fail_connect_err = EAGAIN;
fail_connect = -1;
assert(sc_sock_connect(&client, "127.0.0.1", "8080", NULL, NULL) ==
SC_SOCK_WANT_WRITE);
fail_connect_err = 0;
fail_connect = 0;
assert(sc_sock_term(&client) == 0);
sc_sock_init(&client, 0, false, SC_SOCK_UNIX);
fail_connect_err = EINPROGRESS;
fail_connect = -1;
assert(sc_sock_connect(&client, "/tmp/x", "8080", NULL, NULL) == -1);
fail_connect_err = 0;
fail_connect = 0;
assert(sc_sock_term(&client) == 0);
}
#else
void sock_fail_test()
{
}
void sock_fail_test2()
{
}
void sock_fail_test3()
{
}
void poll_fail_test()
{
}
@ -843,6 +1458,10 @@ void test_err()
int main()
{
#ifdef SC_HAVE_WRAP
assert(sc_mutex_init(&mutex) == 0);
#endif
#if defined(_WIN32) || defined(_WIN64)
WSADATA data;
@ -850,7 +1469,6 @@ int main()
assert(rc == 0);
assert(LOBYTE(data.wVersion) == 2 && HIBYTE(data.wVersion) == 2);
#endif
test1();
test_ip4();
@ -864,6 +1482,9 @@ int main()
test_pipe();
pipe_fail_test();
poll_fail_test();
sock_fail_test();
sock_fail_test2();
sock_fail_test3();
test_poll();
test_err();
test_poll_mass();
@ -872,5 +1493,11 @@ int main()
rc = WSACleanup();
assert(rc == 0);
#endif
#ifdef SC_HAVE_WRAP
assert(sc_mutex_term(&mutex) == 0);
test_done = 1;
#endif
return 0;
}