Skip to content

Commit 6a80c78

Browse files
committed
tests: runtime: in_opentelemetry_routing: enhance tests
This patch enhances runtime tests with additional context routing scenarios. Signed-off-by: Eduardo Silva <[email protected]>
1 parent 22bcf50 commit 6a80c78

File tree

1 file changed

+90
-15
lines changed

1 file changed

+90
-15
lines changed

tests/runtime/in_opentelemetry_routing.c

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,39 @@ struct test_ctx {
5757

5858
#define TEST_OUTPUT_DIR "otlp_routing_test_output"
5959

60-
/* Construct path to config file in source directory */
60+
/* Construct absolute path to config file */
6161
static char *get_config_path(const char *config_file)
6262
{
6363
char path[PATH_MAX];
6464
char *resolved;
6565
char *real_resolved;
6666
char cwd[PATH_MAX];
67+
size_t cwd_len;
68+
size_t config_file_len;
69+
int ret;
6770

68-
/* Try FLB_TESTS_DATA_PATH first (tests/runtime directory) */
71+
/* Try FLB_TESTS_DATA_PATH/data/routing/ first */
72+
snprintf(path, sizeof(path), "%s/data/routing/%s", FLB_TESTS_DATA_PATH, config_file);
73+
if (access(path, R_OK) == 0) {
74+
real_resolved = realpath(path, NULL);
75+
if (real_resolved) {
76+
resolved = flb_strdup(real_resolved);
77+
free(real_resolved);
78+
return resolved;
79+
}
80+
resolved = flb_strdup(path);
81+
return resolved;
82+
}
83+
84+
/* Try FLB_TESTS_DATA_PATH (tests/runtime directory) */
6985
snprintf(path, sizeof(path), "%s/%s", FLB_TESTS_DATA_PATH, config_file);
7086
if (access(path, R_OK) == 0) {
87+
real_resolved = realpath(path, NULL);
88+
if (real_resolved) {
89+
resolved = flb_strdup(real_resolved);
90+
free(real_resolved);
91+
return resolved;
92+
}
7193
resolved = flb_strdup(path);
7294
return resolved;
7395
}
@@ -87,10 +109,22 @@ static char *get_config_path(const char *config_file)
87109

88110
/* Try current working directory */
89111
if (getcwd(cwd, sizeof(cwd)) != NULL) {
90-
snprintf(path, sizeof(path), "%s/%s", cwd, config_file);
91-
if (access(path, R_OK) == 0) {
92-
resolved = flb_strdup(path);
93-
return resolved;
112+
cwd_len = strlen(cwd);
113+
config_file_len = strlen(config_file);
114+
if (cwd_len + 1 + config_file_len < sizeof(path)) {
115+
ret = snprintf(path, sizeof(path), "%s/%s", cwd, config_file);
116+
if (ret > 0 && (size_t)ret < sizeof(path)) {
117+
if (access(path, R_OK) == 0) {
118+
real_resolved = realpath(path, NULL);
119+
if (real_resolved) {
120+
resolved = flb_strdup(real_resolved);
121+
free(real_resolved);
122+
return resolved;
123+
}
124+
resolved = flb_strdup(path);
125+
return resolved;
126+
}
127+
}
94128
}
95129
}
96130

@@ -158,6 +192,8 @@ static struct test_ctx *test_ctx_create(const char *config_file)
158192
char *config_path;
159193
int ret;
160194
char cwd[PATH_MAX];
195+
size_t cwd_len;
196+
size_t test_dir_len;
161197

162198
ctx = flb_calloc(1, sizeof(struct test_ctx));
163199
if (!TEST_CHECK(ctx != NULL)) {
@@ -169,7 +205,17 @@ static struct test_ctx *test_ctx_create(const char *config_file)
169205

170206
/* Create output directory */
171207
if (getcwd(cwd, sizeof(cwd)) != NULL) {
172-
snprintf(ctx->output_dir, sizeof(ctx->output_dir), "%s/%s", cwd, TEST_OUTPUT_DIR);
208+
cwd_len = strlen(cwd);
209+
test_dir_len = strlen(TEST_OUTPUT_DIR);
210+
if (cwd_len + 1 + test_dir_len < sizeof(ctx->output_dir)) {
211+
ret = snprintf(ctx->output_dir, sizeof(ctx->output_dir), "%s/%s", cwd, TEST_OUTPUT_DIR);
212+
if (ret < 0 || (size_t)ret >= sizeof(ctx->output_dir)) {
213+
snprintf(ctx->output_dir, sizeof(ctx->output_dir), "./%s", TEST_OUTPUT_DIR);
214+
}
215+
}
216+
else {
217+
snprintf(ctx->output_dir, sizeof(ctx->output_dir), "./%s", TEST_OUTPUT_DIR);
218+
}
173219
}
174220
else {
175221
snprintf(ctx->output_dir, sizeof(ctx->output_dir), "./%s", TEST_OUTPUT_DIR);
@@ -270,10 +316,19 @@ static void cleanup_output_files(struct test_ctx *ctx, struct route_expectation
270316
{
271317
int i;
272318
char filepath[PATH_MAX];
319+
size_t dir_len;
320+
size_t file_len;
321+
int ret;
273322

274323
for (i = 0; i < count; i++) {
275-
snprintf(filepath, sizeof(filepath), "%s/%s", ctx->output_dir, expectations[i].output_file);
276-
unlink(filepath);
324+
dir_len = strlen(ctx->output_dir);
325+
file_len = strlen(expectations[i].output_file);
326+
if (dir_len + 1 + file_len < sizeof(filepath)) {
327+
ret = snprintf(filepath, sizeof(filepath), "%s/%s", ctx->output_dir, expectations[i].output_file);
328+
if (ret > 0 && (size_t)ret < sizeof(filepath)) {
329+
unlink(filepath);
330+
}
331+
}
277332
}
278333
}
279334

@@ -284,16 +339,36 @@ static int verify_expectations(struct route_expectation *expectations, int count
284339
int all_passed = 1;
285340
char filepath[PATH_MAX];
286341
int actual_count;
342+
struct route_expectation *exp;
343+
size_t dir_len;
344+
size_t file_len;
345+
int ret;
287346

288347
for (i = 0; i < count; i++) {
289-
struct route_expectation *exp = &expectations[i];
290-
291-
snprintf(filepath, sizeof(filepath), "%s/%s", ctx->output_dir, exp->output_file);
292-
actual_count = count_records_in_file(filepath);
348+
exp = &expectations[i];
349+
350+
dir_len = strlen(ctx->output_dir);
351+
file_len = strlen(exp->output_file);
352+
if (dir_len + 1 + file_len < sizeof(filepath)) {
353+
ret = snprintf(filepath, sizeof(filepath), "%s/%s", ctx->output_dir, exp->output_file);
354+
if (ret > 0 && (size_t)ret < sizeof(filepath)) {
355+
actual_count = count_records_in_file(filepath);
356+
}
357+
else {
358+
flb_error("[test] Route '%s': output file path too long", exp->route_name);
359+
actual_count = -1;
360+
}
361+
}
362+
else {
363+
flb_error("[test] Route '%s': output file path too long", exp->route_name);
364+
actual_count = -1;
365+
}
293366

294367
if (actual_count < 0) {
295-
flb_error("[test] Route '%s': failed to read output file: %s",
296-
exp->route_name, filepath);
368+
if (dir_len + 1 + file_len < sizeof(filepath)) {
369+
flb_error("[test] Route '%s': failed to read output file: %s",
370+
exp->route_name, filepath);
371+
}
297372
all_passed = 0;
298373
}
299374
else if (actual_count != exp->expected_count) {

0 commit comments

Comments
 (0)