Skip to content

Commit

Permalink
Merge pull request #61 from douzzer/20231207-replace_rule_transaction…
Browse files Browse the repository at this point in the history
…ally

20231207-replace_rule_transactionally
  • Loading branch information
JacobBarthelmeh authored Dec 8, 2023
2 parents 464e92d + afa0c1e commit 9f739b2
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA

SHELL := /bin/bash
SHELL := bash

ifeq "$(V)" "1"
override undefine VERY_QUIET
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_wolfsentry_options_h.awk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/awk -f
#!/usr/bin/env -S awk -f

# build_wolfsentry_options_h.awk
#
Expand Down
2 changes: 1 addition & 1 deletion scripts/convert_copyright_boilerplate.awk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/awk -f
#!/usr/bin/env -S awk -f

# update_copyright_boilerplate.awk
#
Expand Down
2 changes: 1 addition & 1 deletion scripts/pre-push.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

# wolfsentry/scripts/pre-push.sh
#
Expand Down
162 changes: 162 additions & 0 deletions tests/unittests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,70 @@ TEST_SKIP(test_rw_locks)

#ifdef TEST_STATIC_ROUTES

static wolfsentry_errcode_t replace_rule_transactionally(
WOLFSENTRY_CONTEXT_ARGS_IN,
const char *event_label,
int event_label_len,
const struct wolfsentry_sockaddr *del_remote,
const struct wolfsentry_sockaddr *del_local,
wolfsentry_route_flags_t del_flags,
const struct wolfsentry_sockaddr *ins_remote,
const struct wolfsentry_sockaddr *ins_local,
wolfsentry_route_flags_t ins_flags,
wolfsentry_ent_id_t *ins_id,
wolfsentry_action_res_t *action_results)
{
wolfsentry_errcode_t ret;
struct wolfsentry_context *new_wolfsentry_ctx = NULL;
int n_deleted;

WOLFSENTRY_PROMOTABLE_OR_RETURN();

ret = wolfsentry_context_clone(
WOLFSENTRY_CONTEXT_ARGS_OUT,
&new_wolfsentry_ctx,
WOLFSENTRY_CLONE_FLAG_NONE);
if (ret < 0)
goto out;

ret = wolfsentry_route_delete(
WOLFSENTRY_CONTEXT_ARGS_OUT_EX(new_wolfsentry_ctx),
NULL /* caller_arg */,
del_remote,
del_local,
del_flags,
event_label,
event_label_len,
action_results,
&n_deleted);
if (ret < 0)
goto out;

ret = wolfsentry_route_insert(
WOLFSENTRY_CONTEXT_ARGS_OUT_EX(new_wolfsentry_ctx),
NULL /* caller_arg */,
ins_remote,
ins_local,
ins_flags,
event_label,
event_label_len,
ins_id,
action_results);
if (ret < 0)
goto out;

ret = wolfsentry_context_exchange(WOLFSENTRY_CONTEXT_ARGS_OUT, new_wolfsentry_ctx);

out:

if (new_wolfsentry_ctx != NULL) {
WOLFSENTRY_WARN_ON_FAILURE(wolfsentry_context_free(WOLFSENTRY_CONTEXT_ARGS_OUT_EX(&new_wolfsentry_ctx)));
}


WOLFSENTRY_ERROR_UNLOCK_AND_RERETURN(ret);
}

static int test_static_routes(void) {

struct wolfsentry_context *wolfsentry;
Expand Down Expand Up @@ -2131,6 +2195,104 @@ static int test_static_routes(void) {

/* leave the route in the table, to be cleaned up by wolfsentry_shutdown(). */

{
struct {
struct wolfsentry_sockaddr sa;
byte addr_buf[4];
} alt_remote, alt_local;
wolfsentry_route_flags_t alt_flags = WOLFSENTRY_ROUTE_FLAG_TCPLIKE_PORT_NUMBERS;
wolfsentry_ent_id_t ins_id;

memcpy(&alt_remote, &remote, sizeof alt_remote);
memcpy(alt_remote.sa.addr,"\4\5\6\7",sizeof remote.addr_buf);
memcpy(&alt_local, &local, sizeof alt_local);
alt_flags = flags;
WOLFSENTRY_SET_BITS(alt_flags, WOLFSENTRY_ROUTE_FLAG_PENALTYBOXED);
WOLFSENTRY_CLEAR_BITS(alt_flags, WOLFSENTRY_ROUTE_FLAG_GREENLISTED);

WOLFSENTRY_EXIT_ON_FAILURE(
replace_rule_transactionally(
WOLFSENTRY_CONTEXT_ARGS_OUT,
NULL /* event_label */,
0 /* event_label_len */,
&remote.sa,
&local.sa,
flags,
&alt_remote.sa,
&alt_local.sa,
alt_flags,
&ins_id,
&action_results));

WOLFSENTRY_EXIT_ON_FAILURE(
replace_rule_transactionally(
WOLFSENTRY_CONTEXT_ARGS_OUT,
NULL /* event_label */,
0 /* event_label_len */,
&alt_remote.sa,
&alt_local.sa,
alt_flags,
&remote.sa,
&local.sa,
flags,
&ins_id,
&action_results));

WOLFSENTRY_EXIT_UNLESS_EXPECTED_FAILURE(
ITEM_NOT_FOUND,
replace_rule_transactionally(
WOLFSENTRY_CONTEXT_ARGS_OUT,
NULL /* event_label */,
0 /* event_label_len */,
&alt_remote.sa,
&alt_local.sa,
alt_flags,
&remote.sa,
&local.sa,
flags,
&ins_id,
&action_results));

WOLFSENTRY_EXIT_ON_FAILURE(
replace_rule_transactionally(
WOLFSENTRY_CONTEXT_ARGS_OUT,
NULL /* event_label */,
0 /* event_label_len */,
&remote.sa,
&local.sa,
flags,
&alt_remote.sa,
&alt_local.sa,
alt_flags,
&ins_id,
&action_results));

WOLFSENTRY_EXIT_ON_FAILURE(
wolfsentry_route_delete(
WOLFSENTRY_CONTEXT_ARGS_OUT,
NULL /* caller_arg */,
&alt_remote.sa,
&alt_local.sa,
alt_flags,
NULL /* event_label */,
0 /* event_label_len */,
&action_results,
&n_deleted));

WOLFSENTRY_EXIT_UNLESS_EXPECTED_FAILURE(
ITEM_NOT_FOUND,
wolfsentry_route_delete(
WOLFSENTRY_CONTEXT_ARGS_OUT,
NULL /* caller_arg */,
&remote.sa,
&local.sa,
flags,
NULL /* event_label */,
0 /* event_label_len */,
&action_results,
&n_deleted));
}

printf("all subtests succeeded -- %d distinct ents inserted and deleted.\n",wolfsentry->mk_id_cb_state.id_counter);

WOLFSENTRY_EXIT_ON_FAILURE(wolfsentry_shutdown(WOLFSENTRY_CONTEXT_ARGS_OUT_EX(&wolfsentry)));
Expand Down

0 comments on commit 9f739b2

Please sign in to comment.