Skip to content

Commit 613d0da

Browse files
committed
tests: runtime: Add test cases for contaminated percent characters
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent f9eb113 commit 613d0da

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

tests/runtime/out_file.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ void flb_test_file_delim_ltsv(void);
2626
void flb_test_file_label_delim(void);
2727
void flb_test_file_template(void);
2828
void flb_test_file_mkdir(void);
29+
void flb_test_file_literal_percent(void);
2930
void flb_test_file_dynamic_path_file(void);
3031
void flb_test_file_dynamic_timestamp(void);
32+
void flb_test_file_dynamic_percent_values(void);
3133
void flb_test_file_dynamic_requires_fallback(void);
3234
void flb_test_file_dynamic_missing_fallback(void);
3335
void flb_test_file_dynamic_unsafe_fallback(void);
@@ -40,8 +42,10 @@ TEST_LIST = {
4042
{"path", flb_test_file_path},
4143
{"path_file", flb_test_file_path_file},
4244
{"mkdir", flb_test_file_mkdir},
45+
{"literal_percent", flb_test_file_literal_percent},
4346
{"dynamic_path_file", flb_test_file_dynamic_path_file},
4447
{"dynamic_timestamp", flb_test_file_dynamic_timestamp},
48+
{"dynamic_percent_values", flb_test_file_dynamic_percent_values},
4549
{"dynamic_requires_fallback", flb_test_file_dynamic_requires_fallback},
4650
{"dynamic_missing_fallback", flb_test_file_dynamic_missing_fallback},
4751
{"dynamic_unsafe_fallback", flb_test_file_dynamic_unsafe_fallback},
@@ -548,6 +552,63 @@ void flb_test_file_path_file(void)
548552
#define JSON_DYNAMIC_LIMIT \
549553
"[1448403340,{\"hostname\":\"host1\"}]" \
550554
"[1448403341,{\"hostname\":\"host2\"}]"
555+
#define JSON_DYNAMIC_PERCENT "[1448403340,{\"proxy_name\":\"proxy%m\"}]"
556+
557+
void flb_test_file_literal_percent(void)
558+
{
559+
int ret;
560+
int bytes;
561+
char *p = JSON_BASIC;
562+
char file[256];
563+
char fallback[256];
564+
flb_ctx_t *ctx;
565+
int in_ffd;
566+
int out_ffd;
567+
FILE *fp;
568+
569+
snprintf(file, sizeof(file), "%s/metrics%%used.log", TEST_LOGPATH);
570+
snprintf(fallback, sizeof(fallback), "%s/fallback.log", TEST_LOGPATH);
571+
remove(file);
572+
remove(fallback);
573+
flb_test_rmdir(TEST_LOGPATH);
574+
575+
ctx = flb_create();
576+
flb_service_set(ctx, "Flush", "1", "Grace", "1", "Log_Level", "error", NULL);
577+
578+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
579+
TEST_CHECK(in_ffd >= 0);
580+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
581+
582+
out_ffd = flb_output(ctx, (char *) "file", NULL);
583+
TEST_CHECK(out_ffd >= 0);
584+
flb_output_set(ctx, out_ffd, "match", "test", NULL);
585+
flb_output_set(ctx, out_ffd, "path", TEST_LOGPATH, NULL);
586+
flb_output_set(ctx, out_ffd, "file", "metrics%used.log", NULL);
587+
flb_output_set(ctx, out_ffd, "fallback_path", TEST_LOGPATH, NULL);
588+
flb_output_set(ctx, out_ffd, "fallback_file", "fallback.log", NULL);
589+
flb_output_set(ctx, out_ffd, "mkdir", "true", NULL);
590+
591+
ret = flb_start(ctx);
592+
TEST_CHECK(ret == 0);
593+
594+
bytes = flb_lib_push(ctx, in_ffd, p, strlen(p));
595+
TEST_CHECK(bytes == strlen(p));
596+
ret = wait_for_file(file, 1, TEST_TIMEOUT);
597+
TEST_CHECK(ret == 0);
598+
599+
flb_stop(ctx);
600+
flb_destroy(ctx);
601+
602+
fp = fopen(file, "r");
603+
TEST_CHECK(fp != NULL);
604+
if (fp != NULL) {
605+
fclose(fp);
606+
}
607+
TEST_CHECK(access(fallback, F_OK) != 0);
608+
remove(file);
609+
remove(fallback);
610+
flb_test_rmdir(TEST_LOGPATH);
611+
}
551612

552613
void flb_test_file_dynamic_path_file(void)
553614
{
@@ -588,6 +649,7 @@ void flb_test_file_dynamic_path_file(void)
588649
flb_output_set(ctx, out_ffd, "match", "test", NULL);
589650
flb_output_set(ctx, out_ffd, "path", TEST_LOGPATH "/$TAG/$proxy_name", NULL);
590651
flb_output_set(ctx, out_ffd, "file", "file.%Y%m%d", NULL);
652+
flb_output_set(ctx, out_ffd, "enable_strftime", "true", NULL);
591653
flb_output_set(ctx, out_ffd, "fallback_path", TEST_LOGPATH, NULL);
592654
flb_output_set(ctx, out_ffd, "fallback_file", "metrics.log", NULL);
593655
flb_output_set(ctx, out_ffd, "mkdir", "true", NULL);
@@ -642,6 +704,7 @@ void flb_test_file_dynamic_timestamp(void)
642704
flb_output_set(ctx, out_ffd, "match", "test", NULL);
643705
flb_output_set(ctx, out_ffd, "path", TEST_LOGPATH, NULL);
644706
flb_output_set(ctx, out_ffd, "file", "events.%Y%m%d.log", NULL);
707+
flb_output_set(ctx, out_ffd, "enable_strftime", "true", NULL);
645708
flb_output_set(ctx, out_ffd, "fallback_path", TEST_LOGPATH, NULL);
646709
flb_output_set(ctx, out_ffd, "fallback_file", "fallback.log", NULL);
647710
flb_output_set(ctx, out_ffd, "mkdir", "true", NULL);
@@ -662,6 +725,66 @@ void flb_test_file_dynamic_timestamp(void)
662725
flb_test_rmdir(TEST_LOGPATH);
663726
}
664727

728+
void flb_test_file_dynamic_percent_values(void)
729+
{
730+
int ret;
731+
int bytes;
732+
char *p = JSON_DYNAMIC_PERCENT;
733+
char path[256];
734+
char proxy_dir[256];
735+
char tag_dir[256];
736+
char year_dir[256];
737+
flb_ctx_t *ctx;
738+
int in_ffd;
739+
int out_ffd;
740+
741+
snprintf(year_dir, sizeof(year_dir), "%s/2015", TEST_LOGPATH);
742+
snprintf(tag_dir, sizeof(tag_dir), "%s/test%%d", year_dir);
743+
snprintf(proxy_dir, sizeof(proxy_dir), "%s/proxy%%m", tag_dir);
744+
snprintf(path, sizeof(path), "%s/file.20151124", proxy_dir);
745+
remove(path);
746+
flb_test_rmdir(proxy_dir);
747+
flb_test_rmdir(tag_dir);
748+
flb_test_rmdir(year_dir);
749+
flb_test_rmdir(TEST_LOGPATH);
750+
751+
ctx = flb_create();
752+
flb_service_set(ctx, "Flush", "1", "Grace", "1",
753+
"Log_Level", "error", NULL);
754+
755+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
756+
TEST_CHECK(in_ffd >= 0);
757+
flb_input_set(ctx, in_ffd, "tag", "test%d", NULL);
758+
759+
out_ffd = flb_output(ctx, (char *) "file", NULL);
760+
TEST_CHECK(out_ffd >= 0);
761+
flb_output_set(ctx, out_ffd, "match", "*", NULL);
762+
flb_output_set(ctx, out_ffd, "path",
763+
TEST_LOGPATH "/%Y/$TAG/$proxy_name", NULL);
764+
flb_output_set(ctx, out_ffd, "file", "file.%Y%m%d", NULL);
765+
flb_output_set(ctx, out_ffd, "enable_strftime", "true", NULL);
766+
flb_output_set(ctx, out_ffd, "fallback_path", TEST_LOGPATH, NULL);
767+
flb_output_set(ctx, out_ffd, "fallback_file", "fallback.log", NULL);
768+
flb_output_set(ctx, out_ffd, "mkdir", "true", NULL);
769+
770+
ret = flb_start(ctx);
771+
TEST_CHECK(ret == 0);
772+
773+
bytes = flb_lib_push(ctx, in_ffd, p, strlen(p));
774+
TEST_CHECK(bytes == strlen(p));
775+
ret = wait_for_file(path, 1, TEST_TIMEOUT);
776+
TEST_CHECK(ret == 0);
777+
778+
flb_stop(ctx);
779+
flb_destroy(ctx);
780+
781+
remove(path);
782+
flb_test_rmdir(proxy_dir);
783+
flb_test_rmdir(tag_dir);
784+
flb_test_rmdir(year_dir);
785+
flb_test_rmdir(TEST_LOGPATH);
786+
}
787+
665788
void flb_test_file_dynamic_requires_fallback(void)
666789
{
667790
int ret;

0 commit comments

Comments
 (0)