Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Gustavo Sousa <gustavo.sousa@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: Lucas De Marchi <lucas.demarchi@intel.com>,
	Gustavo Sousa <gustavo.sousa@intel.com>
Subject: [PATCH i-g-t v4 4/6] igt_hook: Implement and use set_fake_argv() in test
Date: Wed, 14 Aug 2024 17:47:59 -0300	[thread overview]
Message-ID: <20240814204822.95283-5-gustavo.sousa@intel.com> (raw)
In-Reply-To: <20240814204822.95283-1-gustavo.sousa@intel.com>

In upcoming changes, fake_argv will need to allow a variable number of
elements. As such, implement the variadic function set_fake_argv() to
allow that.

v2:
  - Replace "while" loop with the more concise "for" loop. (Lucas)

Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com> # v1
Signed-off-by: Gustavo Sousa <gustavo.sousa@intel.com>
---
 lib/tests/igt_hook_integration.c | 46 +++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 10 deletions(-)

diff --git a/lib/tests/igt_hook_integration.c b/lib/tests/igt_hook_integration.c
index f5ba25e92897..25328f975de8 100644
--- a/lib/tests/igt_hook_integration.c
+++ b/lib/tests/igt_hook_integration.c
@@ -3,6 +3,7 @@
  * Copyright(c) 2024 Intel Corporation. All rights reserved.
  */
 
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
@@ -11,11 +12,9 @@
 
 #include "igt_tests_common.h"
 
-char prog[] = "igt_hook_integration";
-char hook_opt[] = "--hook";
-char hook_str[128];
-char *fake_argv[] = {prog, hook_opt, hook_str};
-int fake_argc = sizeof(fake_argv) / sizeof(fake_argv[0]);
+char fake_argv_buffer[1024];
+char *fake_argv[64];
+int fake_argc;
 
 #define ENV_ARRAY(evt_name, fullname_suffix, subtest, dyn_subtest, result) \
 { \
@@ -51,6 +50,29 @@ const char *post_test_env[] = TEST_ENV("post-test", "FAIL");
 
 #define num_env_vars (sizeof(pre_test_env) / sizeof(pre_test_env[0]))
 
+static void set_fake_argv(const char *arg0, ...)
+{
+	va_list ap;
+	const char *arg;
+	size_t buf_size;
+
+	fake_argc = 0;
+	buf_size = 0;
+
+	va_start(ap, arg0);
+
+	for (arg = arg0; arg != NULL; arg = va_arg(ap, typeof(arg))) {
+		internal_assert(buf_size + strlen(arg) < sizeof(fake_argv_buffer));
+		internal_assert((size_t)fake_argc < sizeof(fake_argv) / sizeof(fake_argv[0]));
+
+		strcpy(fake_argv_buffer + buf_size, arg);
+		fake_argv[fake_argc++] = fake_argv_buffer + buf_size;
+		buf_size += strlen(arg) + 1;
+	}
+
+	va_end(ap);
+}
+
 __noreturn static void fake_main(void)
 {
 	igt_subtest_init(fake_argc, fake_argv);
@@ -85,7 +107,7 @@ static void test_invalid_hook_str(void)
 	static char err[4096];
 	int errfd;
 
-	sprintf(hook_str, "invalid-event:echo hello");
+	set_fake_argv("igt_hook_integration", "--hook", "invalid-event:echo hello", NULL);
 
 	pid = do_fork_bg_with_pipes(fake_main, NULL, &errfd);
 
@@ -196,6 +218,7 @@ static void run_tests_and_match_env(const char *evt_descriptors, const char **ex
 	int ret;
 	int pipefd[2];
 	pid_t pid;
+	char hook_str[128];
 	FILE *f;
 
 	ret = pipe(pipefd);
@@ -204,10 +227,13 @@ static void run_tests_and_match_env(const char *evt_descriptors, const char **ex
 	/* Use grep to filter only env var set by us. This should ensure that
 	 * writing to the pipe will not block due to capacity, since we only
 	 * read from the pipe after the shell command is done. */
-	sprintf(hook_str,
-		"%1$s:printenv -0 | grep -z ^IGT_HOOK >&%2$d; printf -- ---\\\\00 >&%2$d",
-		evt_descriptors,
-		pipefd[1]);
+	ret = snprintf(hook_str, sizeof(hook_str),
+		     "%1$s:printenv -0 | grep -z ^IGT_HOOK >&%2$d; printf -- ---\\\\00 >&%2$d",
+		     evt_descriptors,
+		     pipefd[1]);
+	internal_assert(0 < ret && ret < sizeof(hook_str));
+
+	set_fake_argv("igt_hook_integration", "--hook", hook_str, NULL);
 
 	pid = do_fork_bg_with_pipes(fake_main, NULL, NULL);
 	internal_assert(safe_wait(pid, &ret) != -1);
-- 
2.46.0


  parent reply	other threads:[~2024-08-14 20:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-14 20:47 [PATCH i-g-t v4 0/6] Add support for hook script Gustavo Sousa
2024-08-14 20:47 ` [PATCH i-g-t v4 1/6] igt_hook: Add feature Gustavo Sousa
2024-08-14 20:47 ` [PATCH i-g-t v4 2/6] runner: Make it easier to extend argv Gustavo Sousa
2024-08-14 20:47 ` [PATCH i-g-t v4 3/6] runner: Add option --hook Gustavo Sousa
2024-08-14 20:47 ` Gustavo Sousa [this message]
2024-08-14 20:48 ` [PATCH i-g-t v4 5/6] igt_hook: Allow multiple hook descriptors Gustavo Sousa
2024-08-14 20:48 ` [PATCH i-g-t v4 6/6] runner: Allow multiple --hook options Gustavo Sousa
2024-08-14 22:30 ` ✓ CI.xeBAT: success for Add support for hook script (rev4) Patchwork
2024-08-14 22:41 ` ✓ Fi.CI.BAT: " Patchwork
2024-08-16  5:33 ` ✗ Fi.CI.IGT: failure " Patchwork
2024-08-21 16:49   ` Matt Roper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240814204822.95283-5-gustavo.sousa@intel.com \
    --to=gustavo.sousa@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=lucas.demarchi@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox