Skip to content

Commit 2105327

Browse files
authored
CDRIVER-4493 Override host platform, os, flags, compiler info for testing (#1355)
Fix test failures where a large platform string caused handshake truncation
1 parent 7c49ae2 commit 2105327

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

src/libmongoc/tests/test-mongoc-handshake.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,31 @@ _get_handshake_document (bool default_append)
371371
return handshake_doc;
372372
}
373373

374+
/* Override host info with plausible but short strings to avoid any
375+
* truncation */
376+
static void
377+
_override_host_platform_os (void)
378+
{
379+
_reset_handshake ();
380+
mongoc_handshake_t *md = _mongoc_handshake_get ();
381+
bson_free (md->os_type);
382+
md->os_type = bson_strdup ("Linux");
383+
bson_free (md->os_name);
384+
md->os_name = bson_strdup ("mongoc");
385+
bson_free (md->os_architecture);
386+
md->os_architecture = bson_strdup ("x86_64");
387+
bson_free (md->driver_name);
388+
md->driver_name = bson_strdup ("test_e");
389+
bson_free (md->driver_version);
390+
md->driver_version = bson_strdup ("1.25.0");
391+
bson_free (md->platform);
392+
md->platform = bson_strdup ("posix=1234");
393+
bson_free (md->compiler_info);
394+
md->compiler_info = bson_strdup ("CC=GCC");
395+
bson_free (md->flags);
396+
md->flags = bson_strdup ("CFLAGS=\"-fPIE\"");
397+
}
398+
374399
// erase all FaaS variables used in testing
375400
static void
376401
clear_faas_env (void)
@@ -391,7 +416,7 @@ clear_faas_env (void)
391416
static void
392417
test_mongoc_handshake_data_append_success (void)
393418
{
394-
_reset_handshake ();
419+
_override_host_platform_os ();
395420
bson_t *doc = _get_handshake_document (true);
396421
bson_iter_t md_iter;
397422
bson_iter_init (&md_iter, doc);
@@ -412,7 +437,7 @@ test_valid_aws_lambda (void *test_ctx)
412437
ASSERT (_mongoc_setenv ("AWS_REGION", "us-east-2"));
413438
ASSERT (_mongoc_setenv ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "1024"));
414439

415-
_reset_handshake ();
440+
_override_host_platform_os ();
416441
bson_t *doc = _get_handshake_document (true);
417442
_handshake_check_required_fields (doc);
418443
_handshake_check_env (doc, default_memory_mb, 0, "us-east-2");
@@ -444,7 +469,7 @@ test_valid_aws_and_vercel (void *test_ctx)
444469
ASSERT (_mongoc_setenv ("VERCEL", "1"));
445470
ASSERT (_mongoc_setenv ("VERCEL_REGION", "cdg1"));
446471

447-
_reset_handshake ();
472+
_override_host_platform_os ();
448473
bson_t *doc = _get_handshake_document (true);
449474
_handshake_check_required_fields (doc);
450475
_handshake_check_env (doc, 0, 0, "cdg1");
@@ -463,8 +488,9 @@ test_valid_aws (void *test_ctx)
463488
ASSERT (_mongoc_setenv ("AWS_REGION", "us-east-2"));
464489
ASSERT (_mongoc_setenv ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "1024"));
465490

466-
_reset_handshake ();
491+
_override_host_platform_os ();
467492
bson_t *doc = _get_handshake_document (true);
493+
468494
_handshake_check_required_fields (doc);
469495
_handshake_check_env (doc, default_memory_mb, 0, "us-east-2");
470496
_handshake_check_env_name (doc, "aws.lambda");
@@ -486,7 +512,7 @@ test_valid_azure (void *test_ctx)
486512
BSON_UNUSED (test_ctx);
487513
ASSERT (_mongoc_setenv ("FUNCTIONS_WORKER_RUNTIME", "node"));
488514

489-
_reset_handshake ();
515+
_override_host_platform_os ();
490516
bson_t *doc = _get_handshake_document (true);
491517
_handshake_check_required_fields (doc);
492518
_handshake_check_env (doc, 0, 0, NULL);
@@ -506,7 +532,7 @@ test_valid_gcp (void *test_ctx)
506532
ASSERT (_mongoc_setenv ("FUNCTION_TIMEOUT_SEC", "60"));
507533
ASSERT (_mongoc_setenv ("FUNCTION_REGION", "us-central1"));
508534

509-
_reset_handshake ();
535+
_override_host_platform_os ();
510536
bson_t *doc = _get_handshake_document (true);
511537
_handshake_check_required_fields (doc);
512538
_handshake_check_env (
@@ -525,7 +551,7 @@ test_valid_vercel (void *test_ctx)
525551
ASSERT (_mongoc_setenv ("VERCEL", "1"));
526552
ASSERT (_mongoc_setenv ("VERCEL_REGION", "cdg1"));
527553

528-
_reset_handshake ();
554+
_override_host_platform_os ();
529555
bson_t *doc = _get_handshake_document (true);
530556
_handshake_check_required_fields (doc);
531557
_handshake_check_env (doc, 0, 0, "cdg1");
@@ -544,7 +570,7 @@ test_multiple_faas (void *test_ctx)
544570
ASSERT (_mongoc_setenv ("AWS_EXECUTION_ENV", "AWS_Lambda_java8"));
545571
ASSERT (_mongoc_setenv ("FUNCTIONS_WORKER_RUNTIME", "node"));
546572

547-
_reset_handshake ();
573+
_override_host_platform_os ();
548574
bson_t *doc = _get_handshake_document (true);
549575
_handshake_check_required_fields (doc);
550576

@@ -566,7 +592,7 @@ test_truncate_region (void *test_ctx)
566592
long_region[region_len - 1] = '\0';
567593
ASSERT (_mongoc_setenv ("AWS_REGION", long_region));
568594

569-
_reset_handshake ();
595+
_override_host_platform_os ();
570596
bson_t *doc = _get_handshake_document (true);
571597
_handshake_check_required_fields (doc);
572598
_handshake_check_env_name (doc, "aws.lambda");
@@ -593,7 +619,7 @@ test_wrong_types (void *test_ctx)
593619
ASSERT (_mongoc_setenv ("AWS_EXECUTION_ENV", "AWS_Lambda_java8"));
594620
ASSERT (_mongoc_setenv ("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "big"));
595621

596-
_reset_handshake ();
622+
_override_host_platform_os ();
597623
bson_t *doc = _get_handshake_document (true);
598624
_handshake_check_required_fields (doc);
599625
_handshake_check_env_name (doc, "aws.lambda");
@@ -617,7 +643,7 @@ test_aws_not_lambda (void *test_ctx)
617643
// Entire env field must be omitted with non-lambda AWS
618644
ASSERT (_mongoc_setenv ("AWS_EXECUTION_ENV", "EC2"));
619645

620-
_reset_handshake ();
646+
_override_host_platform_os ();
621647
bson_t *doc = _get_handshake_document (true);
622648
_handshake_check_required_fields (doc);
623649

0 commit comments

Comments
 (0)