t0212: test URL redacting in EVENT format

In the added tests cases, skip testing the `GIT_TRACE2_REDACT=0` case because we would need to exactly model the full JSON event stream like we did in the preceding basic tests and I do not think it is worth it. Furthermore, the Trace2 routines print the same content in normal, perf, or event format, and in t0210 and t0211 we already tested the basic functionality, so no need to repeat it here. In this test, we use the test-helper to unit test each of the event messages where URLs can appear and confirm that they are redacted in each event. Signed-off-by: Jeff Hostetler <jeffhostetler@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Nov 22, 2023 at 19:18 UTC 16fa3eebc0b92dc2aa5d875c2f1338644e3c5b99
2 files changed +95
t/helper/test-trace2.c
+55
@@ -412,6 +412,56 @@ static int ut_201counter(int argc, const char **argv)
412 return 0;
413 }
414
415 +static int ut_300redact_start(int argc, const char **argv)
416 +{
417 + if (!argc)
418 + die("expect <argv...>");
419 +
420 + trace2_cmd_start(argv);
421 +
422 + return 0;
423 +}
424 +
425 +static int ut_301redact_child_start(int argc, const char **argv)
426 +{
427 + struct child_process cmd = CHILD_PROCESS_INIT;
428 + int k;
429 +
430 + if (!argc)
431 + die("expect <argv...>");
432 +
433 + for (k = 0; argv[k]; k++)
434 + strvec_push(&cmd.args, argv[k]);
435 +
436 + trace2_child_start(&cmd);
437 +
438 + strvec_clear(&cmd.args);
439 +
440 + return 0;
441 +}
442 +
443 +static int ut_302redact_exec(int argc, const char **argv)
444 +{
445 + if (!argc)
446 + die("expect <exe> <argv...>");
447 +
448 + trace2_exec(argv[0], &argv[1]);
449 +
450 + return 0;
451 +}
452 +
453 +static int ut_303redact_def_param(int argc, const char **argv)
454 +{
455 + struct key_value_info kvi = KVI_INIT;
456 +
457 + if (argc < 2)
458 + die("expect <key> <value>");
459 +
460 + trace2_def_param(argv[0], argv[1], &kvi);
461 +
462 + return 0;
463 +}
464 +
465 /*
466 * Usage:
467 * test-tool trace2 <ut_name_1> <ut_usage_1>
@@ -438,6 +488,11 @@ static struct unit_test ut_table[] = {
488
489 { ut_200counter, "200counter", "<v1> [<v2> [<v3> [...]]]" },
490 { ut_201counter, "201counter", "<v1> <v2> <threads>" },
491 +
492 + { ut_300redact_start, "300redact_start", "<argv...>" },
493 + { ut_301redact_child_start, "301redact_child_start", "<argv...>" },
494 + { ut_302redact_exec, "302redact_exec", "<exe> <argv...>" },
495 + { ut_303redact_def_param, "303redact_def_param", "<key> <value>" },
496 };
497 /* clang-format on */
498
t/t0212-trace2-event.sh
+40
@@ -323,4 +323,44 @@ test_expect_success 'discard traces when there are too many files' '
323 head -n2 trace_target_dir/git-trace2-discard | tail -n1 | grep \"event\":\"too_many_files\"
324 '
325
326 +# In the following "...redact..." tests, skip testing the GIT_TRACE2_REDACT=0
327 +# case because we would need to exactly model the full JSON event stream like
328 +# we did in the basic tests above and I do not think it is worth it.
329 +
330 +test_expect_success 'unsafe URLs are redacted by default in cmd_start events' '
331 + test_when_finished \
332 + "rm -r trace.event" &&
333 +
334 + GIT_TRACE2_EVENT="$(pwd)/trace.event" \
335 + test-tool trace2 300redact_start git clone https://user:pwd@example.com/ clone2 &&
336 + ! grep user:pwd trace.event
337 +'
338 +
339 +test_expect_success 'unsafe URLs are redacted by default in child_start events' '
340 + test_when_finished \
341 + "rm -r trace.event" &&
342 +
343 + GIT_TRACE2_EVENT="$(pwd)/trace.event" \
344 + test-tool trace2 301redact_child_start git clone https://user:pwd@example.com/ clone2 &&
345 + ! grep user:pwd trace.event
346 +'
347 +
348 +test_expect_success 'unsafe URLs are redacted by default in exec events' '
349 + test_when_finished \
350 + "rm -r trace.event" &&
351 +
352 + GIT_TRACE2_EVENT="$(pwd)/trace.event" \
353 + test-tool trace2 302redact_exec git clone https://user:pwd@example.com/ clone2 &&
354 + ! grep user:pwd trace.event
355 +'
356 +
357 +test_expect_success 'unsafe URLs are redacted by default in def_param events' '
358 + test_when_finished \
359 + "rm -r trace.event" &&
360 +
361 + GIT_TRACE2_EVENT="$(pwd)/trace.event" \
362 + test-tool trace2 303redact_def_param url https://user:pwd@example.com/ &&
363 + ! grep user:pwd trace.event
364 +'
365 +
366 test_done