Skip to content

Commit 67ae3b0

Browse files
cosmo0920edsiper
authored andcommitted
in_forward: tests: Add fail-close around test cases for authentication
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 5bf6923 commit 67ae3b0

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

tests/runtime/in_forward.c

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,148 @@ void flb_test_threaded_forward_issue_10946()
903903
flb_destroy(ctx);
904904
}
905905

906+
static flb_ctx_t *fw_make_ctx_with_forward(int *in_ffd_out, int *out_ffd_out)
907+
{
908+
struct flb_lib_out_cb cb = {0};
909+
flb_ctx_t *ctx;
910+
int in_ffd, out_ffd, ret;
911+
912+
ctx = flb_create();
913+
TEST_CHECK(ctx != NULL);
914+
if (!ctx) { return NULL; }
915+
916+
flb_service_set(ctx,
917+
"Flush", "0.200000000",
918+
"Grace", "1",
919+
"Log_Level", "error",
920+
NULL);
921+
922+
/* forward input */
923+
in_ffd = flb_input(ctx, (char *) "forward", NULL);
924+
TEST_CHECK(in_ffd >= 0);
925+
if (in_ffd < 0) { flb_destroy(ctx); return NULL; }
926+
927+
/* lib output: count only (no payload check) */
928+
cb.cb = cb_count_only;
929+
cb.data = NULL;
930+
out_ffd = flb_output(ctx, (char *) "lib", (void *) &cb);
931+
TEST_CHECK(out_ffd >= 0);
932+
if (out_ffd < 0) {
933+
flb_destroy(ctx);
934+
return NULL;
935+
}
936+
ret = flb_output_set(ctx, out_ffd,
937+
"match", "*",
938+
"format", "json",
939+
NULL);
940+
TEST_CHECK(ret == 0);
941+
942+
if (in_ffd_out) *in_ffd_out = in_ffd;
943+
if (out_ffd_out) *out_ffd_out = out_ffd;
944+
return ctx;
945+
}
946+
947+
/* 1) users-only => must fail to start (fail-close) */
948+
void flb_test_fw_auth_users_only_fail_start()
949+
{
950+
flb_ctx_t *ctx;
951+
int in_ffd, out_ffd, ret;
952+
953+
ctx = fw_make_ctx_with_forward(&in_ffd, &out_ffd);
954+
TEST_CHECK(ctx != NULL);
955+
if (!ctx) {
956+
return;
957+
}
958+
959+
ret = flb_input_set(ctx, in_ffd,
960+
"tag", "test",
961+
"security.users", "alice s3cr3t",
962+
NULL);
963+
TEST_CHECK(ret == 0);
964+
965+
ret = flb_start(ctx);
966+
TEST_CHECK(ret != 0);
967+
if (ret == 0) {
968+
TEST_MSG("users-only config unexpectedly started; fail-close not enforced");
969+
flb_stop(ctx);
970+
}
971+
flb_destroy(ctx);
972+
}
973+
974+
/* 2) empty_shared_key + users => start OK */
975+
void flb_test_fw_auth_empty_shared_key_plus_users_start_ok()
976+
{
977+
flb_ctx_t *ctx;
978+
int in_ffd, out_ffd, ret;
979+
980+
ctx = fw_make_ctx_with_forward(&in_ffd, &out_ffd);
981+
TEST_CHECK(ctx != NULL);
982+
if (!ctx) { return; }
983+
984+
ret = flb_input_set(ctx, in_ffd,
985+
"tag", "test",
986+
"empty_shared_key", "true",
987+
"security.users", "alice s3cr3t",
988+
NULL);
989+
TEST_CHECK(ret == 0);
990+
991+
ret = flb_start(ctx);
992+
TEST_CHECK(ret == 0);
993+
if (ret == 0) {
994+
flb_stop(ctx);
995+
}
996+
flb_destroy(ctx);
997+
}
998+
999+
/* 3) shared_key only => start OK (backward compatible) */
1000+
void flb_test_fw_auth_shared_key_only_start_ok()
1001+
{
1002+
flb_ctx_t *ctx;
1003+
int in_ffd, out_ffd, ret;
1004+
1005+
ctx = fw_make_ctx_with_forward(&in_ffd, &out_ffd);
1006+
TEST_CHECK(ctx != NULL);
1007+
if (!ctx) { return; }
1008+
1009+
ret = flb_input_set(ctx, in_ffd,
1010+
"tag", "test",
1011+
"shared_key", "k",
1012+
NULL);
1013+
TEST_CHECK(ret == 0);
1014+
1015+
ret = flb_start(ctx);
1016+
TEST_CHECK(ret == 0);
1017+
if (ret == 0) {
1018+
flb_stop(ctx);
1019+
}
1020+
flb_destroy(ctx);
1021+
}
1022+
1023+
/* 4) shared_key + users => start OK (both checks) */
1024+
void flb_test_fw_auth_shared_key_plus_users_start_ok()
1025+
{
1026+
flb_ctx_t *ctx;
1027+
int in_ffd, out_ffd, ret;
1028+
1029+
ctx = fw_make_ctx_with_forward(&in_ffd, &out_ffd);
1030+
TEST_CHECK(ctx != NULL);
1031+
if (!ctx) { return; }
1032+
1033+
ret = flb_input_set(ctx, in_ffd,
1034+
"tag", "test",
1035+
"shared_key", "k",
1036+
"security.users", "alice s3cr3t",
1037+
NULL);
1038+
TEST_CHECK(ret == 0);
1039+
1040+
ret = flb_start(ctx);
1041+
TEST_CHECK(ret == 0);
1042+
if (ret == 0) {
1043+
flb_stop(ctx);
1044+
}
1045+
flb_destroy(ctx);
1046+
}
1047+
9061048
TEST_LIST = {
9071049
{"forward", flb_test_forward},
9081050
{"forward_port", flb_test_forward_port},
@@ -914,5 +1056,9 @@ TEST_LIST = {
9141056
{"forward_gzip", flb_test_forward_gzip},
9151057
{"forward_zstd", flb_test_forward_zstd},
9161058
{"issue_10946", flb_test_threaded_forward_issue_10946},
1059+
{"fw_auth_users_only_fail_start", flb_test_fw_auth_users_only_fail_start},
1060+
{"fw_auth_empty_shared_key_plus_users_start_ok", flb_test_fw_auth_empty_shared_key_plus_users_start_ok},
1061+
{"fw_auth_shared_key_only_start_ok", flb_test_fw_auth_shared_key_only_start_ok},
1062+
{"fw_auth_shared_key_plus_users_start_ok", flb_test_fw_auth_shared_key_plus_users_start_ok},
9171063
{NULL, NULL}
9181064
};

0 commit comments

Comments
 (0)