Skip to content

Commit

Permalink
Added tests for inet.client
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Feb 1, 2018
1 parent 0300441 commit 16c9f6c
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 5 deletions.
123 changes: 123 additions & 0 deletions tests/b6b_test_inet_client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* This file is part of b6b.
*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include <b6b.h>

int main()
{
struct b6b_interp interp;

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp, "{$inet.client}", 14) == 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}", 18) == 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 127.0.0.1}",
28) == B6B_ERR);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$local s [$inet.server tcp 127.0.0.1 2924]}",
44) == B6B_OK);
assert(b6b_call_copy(&interp,
"{$local c [$inet.client tcp 127.0.0.1 2924]}",
44) == B6B_OK);
assert(b6b_call_copy(&interp, "{$c 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("127.0.0.1") - 1);
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 == 2924);
assert(!b6b_list_next(b6b_list_next(b6b_list_first(interp.fg->_))));
assert(b6b_call_copy(&interp,
"{$local a [$list.index [$s accept] 0 ]}",
39) == B6B_OK);
assert(b6b_call_copy(&interp, "{$c write abcd}", 15) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 4);
assert(b6b_call_copy(&interp, "{$c write efgh}", 15) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 4);
assert(b6b_call_copy(&interp, "{$a read}", 9) == B6B_OK);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 8);
assert(strcmp(interp.fg->_->s, "abcdefgh") == 0);
assert(b6b_call_copy(&interp, "{$a write ijkl}", 15) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 4);
assert(b6b_call_copy(&interp, "{$a write mnop}", 15) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 4);
assert(b6b_call_copy(&interp, "{$c read}", 9) == B6B_OK);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 8);
assert(strcmp(interp.fg->_->s, "ijklmnop") == 0);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
assert(b6b_call_copy(&interp,
"{$local s [$inet.server udp 127.0.0.1 2924]}",
44) == B6B_OK);
assert(b6b_call_copy(&interp,
"{$local c [$inet.client udp 127.0.0.1 2924]}",
44) == B6B_OK);
assert(b6b_call_copy(&interp, "{$s peer}", 9) == B6B_ERR);
assert(b6b_call_copy(&interp, "{$c write abcd}", 15) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 4);
assert(b6b_call_copy(&interp, "{$c write efgh}", 15) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 4);
assert(b6b_call_copy(&interp, "{$s 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, "{$s 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("127.0.0.1") - 1);
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 < 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 127.0.0.1 2924}",
33) == B6B_OK);
assert(b6b_call_copy(&interp,
"{$inet.client tcf 127.0.0.1 2924}",
33) == B6B_ERR);
b6b_interp_destroy(&interp);

return EXIT_SUCCESS;
}
15 changes: 13 additions & 2 deletions tests/b6b_test_un_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,19 @@ int main()
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_OK);
assert(b6b_call_copy(&interp, "{$un.client dgram /tmp/x}", 25) == B6B_ERR);
assert(b6b_call_copy(&interp,
"{$local s [$un.server dgram /tmp/x]}",
36) == B6B_OK);
assert(b6b_call_copy(&interp,
"{$local c [$un.client dgram /tmp/x]}",
36) == B6B_OK);
assert(b6b_call_copy(&interp, "{$c write abcd}", 15) == B6B_OK);
assert(b6b_as_float(interp.fg->_));
assert(interp.fg->_->f == 4);
assert(b6b_call_copy(&interp, "{$s read}", 9) == B6B_OK);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 4);
assert(strcmp(interp.fg->_->s, "abcd") == 0);
b6b_interp_destroy(&interp);

assert(b6b_interp_new_argv(&interp, 0, NULL, B6B_OPT_TRACE));
Expand Down
5 changes: 3 additions & 2 deletions tests/b6b_test_un_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ int main()
assert(s1 >= 0);
assert(connect(s1, (const struct sockaddr *)&dst, sizeof(dst)) == 0);
assert(send(s1, "abcd", 4, 0) == 4);
assert(send(s1, "efgh", 4, 0) == 4);
assert(b6b_call_copy(&interp,
"{[$list.index [$_ accept] 0] read}",
34) == B6B_OK);
assert(b6b_as_str(interp.fg->_));
assert(interp.fg->_->slen == 4);
assert(strcmp(interp.fg->_->s, "abcd") == 0);
assert(interp.fg->_->slen == 8);
assert(strcmp(interp.fg->_->s, "abcdefgh") == 0);
assert(close(s1) == 0);
b6b_interp_destroy(&interp);

Expand Down
3 changes: 2 additions & 1 deletion tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ stream_tests = [
['sh', 'slow', 10],
['un_server', 'quick', 5],
['un_client', 'quick', 5],
['inet_server', 'quick', 5]
['inet_server', 'quick', 5],
['inet_client', 'quick', 5]
]

binding_tests = []
Expand Down

0 comments on commit 16c9f6c

Please sign in to comment.