|
36 | 36 | #include <sys/stat.h> |
37 | 37 | #include <unistd.h> |
38 | 38 | #ifndef _WIN32 |
| 39 | +#include <sys/socket.h> |
| 40 | +#include <sys/un.h> |
39 | 41 | #include <sys/wait.h> |
40 | 42 | #endif |
41 | 43 | #ifdef __APPLE__ |
@@ -1341,6 +1343,122 @@ TEST(cli_activation_quiesce_does_not_wait_on_bootstrap_startup) { |
1341 | 1343 | ASSERT_TRUE(event_order); |
1342 | 1344 | PASS(); |
1343 | 1345 | } |
| 1346 | + |
| 1347 | +TEST(cli_install_recovers_markerless_stale_rendezvous) { |
| 1348 | + char tmpdir[256]; |
| 1349 | + snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-markerless-rendezvous-XXXXXX"); |
| 1350 | + if (!cbm_mkdtemp(tmpdir)) { |
| 1351 | + FAIL("cbm_mkdtemp failed"); |
| 1352 | + } |
| 1353 | + char runtime_parent[512]; |
| 1354 | + char cache_dir[512]; |
| 1355 | + char install_dir[512]; |
| 1356 | + char target_path[640]; |
| 1357 | + char activation_log[640]; |
| 1358 | + snprintf(runtime_parent, sizeof(runtime_parent), "%s/runtime", tmpdir); |
| 1359 | + snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir); |
| 1360 | + snprintf(install_dir, sizeof(install_dir), "%s/custom/bin", tmpdir); |
| 1361 | + snprintf(target_path, sizeof(target_path), "%s/codebase-memory-mcp", install_dir); |
| 1362 | + snprintf(activation_log, sizeof(activation_log), "%s/logs/activation-events.ndjson", cache_dir); |
| 1363 | + if (test_mkdirp(runtime_parent) != 0) { |
| 1364 | + test_rmdir_r(tmpdir); |
| 1365 | + FAIL("runtime parent setup failed"); |
| 1366 | + } |
| 1367 | + |
| 1368 | + char *old_home = NULL; |
| 1369 | + char *old_cache = NULL; |
| 1370 | + cli_activation_save_env(&old_home, &old_cache); |
| 1371 | + const char *shell = getenv("SHELL"); |
| 1372 | + char *old_shell = shell ? strdup(shell) : NULL; |
| 1373 | + cbm_setenv("HOME", tmpdir, 1); |
| 1374 | + cbm_setenv("SHELL", "/bin/zsh", 1); |
| 1375 | + cbm_setenv("CBM_CACHE_DIR", cache_dir, 1); |
| 1376 | + |
| 1377 | + cbm_daemon_ipc_endpoint_t *endpoint = cbm_daemon_bootstrap_endpoint_new(runtime_parent); |
| 1378 | + const char *socket_path = endpoint ? cbm_daemon_ipc_endpoint_address(endpoint) : NULL; |
| 1379 | + char anchor_path[1024] = {0}; |
| 1380 | + size_t socket_length = socket_path ? strlen(socket_path) : 0; |
| 1381 | + bool anchor_path_ok = |
| 1382 | + socket_length > 5 && strcmp(socket_path + socket_length - 5, ".sock") == 0; |
| 1383 | + if (anchor_path_ok) { |
| 1384 | + int written = snprintf(anchor_path, sizeof(anchor_path), "%.*s.anc", |
| 1385 | + (int)(socket_length - 5), socket_path); |
| 1386 | + anchor_path_ok = written > 0 && written < (int)sizeof(anchor_path); |
| 1387 | + } |
| 1388 | + struct sockaddr_un address; |
| 1389 | + memset(&address, 0, sizeof(address)); |
| 1390 | + address.sun_family = AF_UNIX; |
| 1391 | + size_t address_length = socket_path ? strlen(socket_path) : 0; |
| 1392 | + bool address_ok = address_length > 0 && address_length < sizeof(address.sun_path); |
| 1393 | + if (address_ok) { |
| 1394 | + memcpy(address.sun_path, socket_path, address_length + 1); |
| 1395 | + } |
| 1396 | +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) |
| 1397 | + address.sun_len = (uint8_t)(offsetof(struct sockaddr_un, sun_path) + address_length + 1); |
| 1398 | +#endif |
| 1399 | + socklen_t sockaddr_length = |
| 1400 | + (socklen_t)(offsetof(struct sockaddr_un, sun_path) + address_length + 1); |
| 1401 | + int raw_listener = address_ok && anchor_path_ok ? socket(AF_UNIX, SOCK_STREAM, 0) : -1; |
| 1402 | + struct stat socket_status = {0}; |
| 1403 | + struct stat anchor_status = {0}; |
| 1404 | + bool orphan_created = |
| 1405 | + raw_listener >= 0 && |
| 1406 | + bind(raw_listener, (const struct sockaddr *)&address, sockaddr_length) == 0 && |
| 1407 | + chmod(socket_path, 0600) == 0 && listen(raw_listener, 1) == 0 && |
| 1408 | + link(socket_path, anchor_path) == 0 && lstat(socket_path, &socket_status) == 0 && |
| 1409 | + lstat(anchor_path, &anchor_status) == 0 && S_ISSOCK(socket_status.st_mode) && |
| 1410 | + S_ISSOCK(anchor_status.st_mode) && socket_status.st_nlink == 2 && |
| 1411 | + anchor_status.st_nlink == 2 && socket_status.st_dev == anchor_status.st_dev && |
| 1412 | + socket_status.st_ino == anchor_status.st_ino; |
| 1413 | + if (raw_listener >= 0) { |
| 1414 | + (void)close(raw_listener); |
| 1415 | + } |
| 1416 | + |
| 1417 | + cbm_cli_set_activation_runtime_parent_for_test(runtime_parent); |
| 1418 | + char dir_arg[640]; |
| 1419 | + snprintf(dir_arg, sizeof(dir_arg), "--dir=%s", install_dir); |
| 1420 | + char *install_argv[] = {"--force", "--skip-config", "--yes", dir_arg}; |
| 1421 | + int install_rc = orphan_created ? cli_test_cmd_install(4, install_argv) : -1; |
| 1422 | + cbm_cli_set_activation_runtime_parent_for_test(g_cli_suite_runtime_parent); |
| 1423 | + cbm_set_auto_answer_for_test(0); |
| 1424 | + |
| 1425 | + struct stat target_status = {0}; |
| 1426 | + struct stat log_status = {0}; |
| 1427 | + struct stat absent_status = {0}; |
| 1428 | + bool target_exists = stat(target_path, &target_status) == 0; |
| 1429 | + bool log_private = stat(activation_log, &log_status) == 0 && (log_status.st_mode & 0077) == 0; |
| 1430 | + const char *events = read_test_file(activation_log); |
| 1431 | + const char *requested = events ? strstr(events, "\"phase\":\"requested\"") : NULL; |
| 1432 | + const char *stopped = events ? strstr(events, "\"phase\":\"daemon_stopped\"") : NULL; |
| 1433 | + const char *completed = events ? strstr(events, "\"phase\":\"completed\"") : NULL; |
| 1434 | + bool event_order = |
| 1435 | + requested && stopped && completed && requested < stopped && stopped < completed; |
| 1436 | + errno = 0; |
| 1437 | + bool socket_removed = socket_path && lstat(socket_path, &absent_status) != 0 && errno == ENOENT; |
| 1438 | + errno = 0; |
| 1439 | + bool anchor_removed = lstat(anchor_path, &absent_status) != 0 && errno == ENOENT; |
| 1440 | + |
| 1441 | + if (old_shell) { |
| 1442 | + cbm_setenv("SHELL", old_shell, 1); |
| 1443 | + } else { |
| 1444 | + cbm_unsetenv("SHELL"); |
| 1445 | + } |
| 1446 | + free(old_shell); |
| 1447 | + cli_activation_restore_env(old_home, old_cache); |
| 1448 | + cbm_daemon_ipc_endpoint_free(endpoint); |
| 1449 | + test_rmdir_r(tmpdir); |
| 1450 | + |
| 1451 | + ASSERT_TRUE(anchor_path_ok); |
| 1452 | + ASSERT_TRUE(address_ok); |
| 1453 | + ASSERT_TRUE(orphan_created); |
| 1454 | + ASSERT_EQ(install_rc, 0); |
| 1455 | + ASSERT_TRUE(target_exists); |
| 1456 | + ASSERT_TRUE(log_private); |
| 1457 | + ASSERT_TRUE(event_order); |
| 1458 | + ASSERT_TRUE(socket_removed); |
| 1459 | + ASSERT_TRUE(anchor_removed); |
| 1460 | + PASS(); |
| 1461 | +} |
1344 | 1462 | #endif |
1345 | 1463 |
|
1346 | 1464 | TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) { |
@@ -14401,6 +14519,7 @@ SUITE(cli) { |
14401 | 14519 | #ifndef _WIN32 |
14402 | 14520 | RUN_TEST(cli_activation_cleanup_failure_fail_stops_before_lease_release); |
14403 | 14521 | RUN_TEST(cli_activation_quiesce_does_not_wait_on_bootstrap_startup); |
| 14522 | + RUN_TEST(cli_install_recovers_markerless_stale_rendezvous); |
14404 | 14523 | #endif |
14405 | 14524 | RUN_TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary); |
14406 | 14525 | RUN_TEST(cli_install_dir_and_skip_config_stage_first_install_safely); |
|
0 commit comments