Skip to content

Commit

Permalink
Improved the test coverage of socket
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Feb 1, 2018
1 parent 16c9f6c commit 13dba19
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
24 changes: 24 additions & 0 deletions tests/b6b_test_inet_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ int main()
assert(b6b_list_next(b6b_list_first(interp.fg->_))->o->f < UINT16_MAX);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp localhost 2924}",
33) == B6B_OK);
assert(b6b_call_copy(&interp,
"{$inet.client tcp localhost 2924}",
33) == B6B_OK);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp localhost 2924}",
33) == B6B_OK);
assert(b6b_call_copy(&interp,
"{$inet.client tcp localhosf 2924}",
33) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp 127.0.0.1 2924}",
Expand All @@ -119,5 +137,11 @@ int main()
33) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.client tcp 255.255.255.255 2924}",
39) == B6B_ERR);
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}
64 changes: 62 additions & 2 deletions tests/b6b_test_inet_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ int main()
struct b6b_interp interp;
struct sockaddr_in dst = {.sin_family = AF_INET},
src = {.sin_family = AF_INET};
struct sockaddr_in6 dst6 = {
.sin6_family = AF_INET6,
.sin6_addr = IN6ADDR_LOOPBACK_INIT
}, src6 = {
.sin6_family = AF_INET6,
.sin6_addr = IN6ADDR_LOOPBACK_INIT
};
char buf[4];
int s;

src.sin_port = htons(2923);
src.sin_port = src6.sin6_port = htons(2923);
src.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
dst.sin_port = htons(2924);
dst.sin_port = dst6.sin6_port = htons(2924);
dst.sin_addr.s_addr = src.sin_addr.s_addr;

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
Expand Down Expand Up @@ -73,6 +80,24 @@ int main()
33) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp 1.2.3.4 2924}",
33) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp localhost 2924}",
33) == B6B_OK);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp localhosf 2924}",
33) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$inet.server tcp 127.0.0.1 2924}",
Expand Down Expand Up @@ -152,6 +177,41 @@ int main()
b6b_interp_destroy(&interp);


assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$global a [$inet.server udp ::1 2924]}",
39) == B6B_OK);
s = socket(AF_INET6, SOCK_DGRAM, 0);
assert(s >= 0);
assert(sendto(s,
"abcd",
4,
0,
(const struct sockaddr *)&dst6,
sizeof(dst6)) == 4);
assert(sendto(s,
"efgh",
4,
0,
(const struct sockaddr *)&dst6,
sizeof(dst6)) == 4);
assert(b6b_call_copy(&interp, "{$a read}", 9) == B6B_OK);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 4);
assert(strcmp(interp.fg->_->s, "abcd") == 0);
assert(b6b_call_copy(&interp, "{$a peer}", 9) == B6B_OK);
assert(b6b_as_list(interp.fg->_));
assert(!b6b_list_empty(interp.fg->_));
assert(b6b_as_str(b6b_list_first(interp.fg->_)->o));
assert(b6b_list_first(interp.fg->_)->o->slen == sizeof("::1") - 1);
assert(strcmp(b6b_list_first(interp.fg->_)->o->s, "::1") == 0);
assert(b6b_list_next(b6b_list_first(interp.fg->_)));
assert(b6b_as_float(b6b_list_next(b6b_list_first(interp.fg->_))->o));
assert(b6b_list_next(b6b_list_first(interp.fg->_))->o->f > 0);
assert(b6b_list_next(b6b_list_first(interp.fg->_))->o->f < USHRT_MAX);
assert(close(s) == 0);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$global a [$inet.server udp 127.0.0.1 2924]}",
Expand Down
26 changes: 25 additions & 1 deletion tests/b6b_test_un_pair.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of b6b.
*
* Copyright 2017 Dima Krasner
* Copyright 2017, 2018 Dima Krasner
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,12 +19,27 @@
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#ifndef __SANITIZE_ADDRESS__
# include <sys/time.h>
# include <sys/resource.h>
#endif

#include <b6b.h>

int main()
{
struct b6b_interp interp;
#ifndef __SANITIZE_ADDRESS__
struct rlimit rlim;
#endif

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$un.pair}", 35) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$un.pair streaf}", 17) == B6B_ERR);
b6b_interp_destroy(&interp);

/* bi-directional reading and writing should succeed */
assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
Expand Down Expand Up @@ -103,5 +118,14 @@ int main()
assert(strcmp(b6b_list_next(b6b_list_first(interp.fg->_))->o->s, "cd") == 0);
b6b_interp_destroy(&interp);

#ifndef __SANITIZE_ADDRESS__
assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
rlim.rlim_cur = 1;
assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$un.pair dgram}", 16) == B6B_ERR);
b6b_interp_destroy(&interp);
#endif

return EXIT_SUCCESS;
}
14 changes: 14 additions & 0 deletions tests/b6b_test_un_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,23 @@ int main()
b6b_interp_destroy(&interp);

#ifndef __SANITIZE_ADDRESS__
assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$global s [$un.server stream /tmp/x]}",
38) == B6B_OK);
assert(b6b_call_copy(&interp,
"{$global c [$un.client stream /tmp/x]}",
38) == B6B_OK);

assert(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
rlim.rlim_cur = 1;
assert(setrlimit(RLIMIT_NOFILE, &rlim) == 0);

assert(b6b_call_copy(&interp, "{$s accept}", 11) == B6B_OK);
assert(b6b_as_list(interp.fg->_));
assert(b6b_list_empty(interp.fg->_));
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$un.server stream /tmp/x}", 26) == B6B_ERR);
b6b_interp_destroy(&interp);
Expand Down

0 comments on commit 13dba19

Please sign in to comment.