|
45 | 45 | #endif |
46 | 46 | #include <errno.h> |
47 | 47 | #include <limits.h> |
| 48 | +#include <wchar.h> /* wcscmp/wcslen/swprintf for the Windows user-PATH test (#2117) */ |
48 | 49 | #include <zlib.h> |
49 | 50 |
|
50 | 51 | /* Internal prompt seam used to restore process-global state after command |
@@ -14670,6 +14671,110 @@ TEST(cli_windows_update_hands_off_to_install_script) { |
14670 | 14671 | ASSERT_TRUE(preserved); |
14671 | 14672 | PASS(); |
14672 | 14673 | } |
| 14674 | + |
| 14675 | +/* #2117: install registers the install dir in the persistent current-user PATH |
| 14676 | + * via the wide registry API and de-duplicates on reinstall; uninstall must take |
| 14677 | + * exactly that entry back out and leave every other entry byte-for-byte intact, |
| 14678 | + * or the PATH grows without bound (a real Windows host reached ~8000 chars). |
| 14679 | + * This drives the actual query/append/remove logic through the hermetic |
| 14680 | + * CBM_TEST_WINDOWS_USER_PATH_RUN_ID seam, against a GUID-scoped scratch key |
| 14681 | + * under HKCU\Software\CodebaseMemoryMCP\Smoke — the developer's live |
| 14682 | + * HKCU\Environment\Path is never opened. The non-ASCII install dir doubles as |
| 14683 | + * the mojibake guard: a broken UTF-8->UTF-16 round-trip would fail the segment |
| 14684 | + * match and leak a duplicate instead of de-duplicating. |
| 14685 | + * |
| 14686 | + * Return codes mirror the internal enum: 0 = added/removed, 1 = already |
| 14687 | + * present / nothing to remove. */ |
| 14688 | +int cbm_cli_ensure_windows_user_path_for_test(const char *bin_dir, bool dry_run); |
| 14689 | +int cbm_cli_remove_windows_user_path_for_test(const char *bin_dir, bool dry_run); |
| 14690 | + |
| 14691 | +static const wchar_t k_cli_user_path_run_id[] = L"0123456789abcdef0123456789abcdef"; |
| 14692 | + |
| 14693 | +static int cli_test_read_smoke_path(HKEY key, wchar_t *out, DWORD out_bytes) { |
| 14694 | + DWORD type = 0; |
| 14695 | + DWORD bytes = out_bytes; |
| 14696 | + return RegQueryValueExW(key, L"Path", NULL, &type, (BYTE *)out, &bytes) == ERROR_SUCCESS ? 0 |
| 14697 | + : -1; |
| 14698 | +} |
| 14699 | + |
| 14700 | +static int cli_test_set_smoke_path(HKEY key, const wchar_t *value) { |
| 14701 | + DWORD bytes = (DWORD)((wcslen(value) + 1U) * sizeof(wchar_t)); |
| 14702 | + LONG rc = RegSetValueExW(key, L"Path", 0, REG_EXPAND_SZ, (const BYTE *)value, bytes); |
| 14703 | + return rc == ERROR_SUCCESS ? 0 : -1; |
| 14704 | +} |
| 14705 | + |
| 14706 | +TEST(cli_uninstall_removes_only_its_own_windows_user_path_entry_issue2117) { |
| 14707 | + if (!SetEnvironmentVariableW(L"CBM_TEST_WINDOWS_USER_PATH_RUN_ID", k_cli_user_path_run_id) || |
| 14708 | + !SetEnvironmentVariableW(L"SMOKE_TEMP_ROOT", L"C:\\Temp\\cbm-smoke") || |
| 14709 | + !SetEnvironmentVariableW(L"SMOKE_DOWNLOAD_URL", L"http://127.0.0.1:8765")) { |
| 14710 | + FAIL("could not arm the Windows user-PATH test seam"); |
| 14711 | + } |
| 14712 | + |
| 14713 | + wchar_t key_path[128]; |
| 14714 | + swprintf(key_path, sizeof(key_path) / sizeof(key_path[0]), |
| 14715 | + L"Software\\CodebaseMemoryMCP\\Smoke\\%ls", k_cli_user_path_run_id); |
| 14716 | + /* Start from a clean leaf so a prior aborted run cannot skew the result. */ |
| 14717 | + RegDeleteKeyW(HKEY_CURRENT_USER, key_path); |
| 14718 | + HKEY key = NULL; |
| 14719 | + if (RegCreateKeyExW(HKEY_CURRENT_USER, key_path, 0, NULL, REG_OPTION_NON_VOLATILE, |
| 14720 | + KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_WOW64_64KEY, NULL, &key, |
| 14721 | + NULL) != ERROR_SUCCESS) { |
| 14722 | + FAIL("could not create the scratch smoke registry key"); |
| 14723 | + } |
| 14724 | + DWORD sentinel_bytes = (DWORD)((wcslen(k_cli_user_path_run_id) + 1U) * sizeof(wchar_t)); |
| 14725 | + RegSetValueExW(key, L"CbmSmokeRunId", 0, REG_SZ, (const BYTE *)k_cli_user_path_run_id, |
| 14726 | + sentinel_bytes); |
| 14727 | + |
| 14728 | + /* Non-ASCII install dir (Omega mu epsilon-tonos gamma alpha). */ |
| 14729 | + wchar_t bin_dir_w[] = L"C:\\Users\\dev\\install_\u03A9\u03BC\u03AD\u03B3\u03B1\\bin"; |
| 14730 | + char bin_dir[512]; |
| 14731 | + WideCharToMultiByte(CP_UTF8, 0, bin_dir_w, -1, bin_dir, (int)sizeof(bin_dir), NULL, NULL); |
| 14732 | + |
| 14733 | + wchar_t readback[1024]; |
| 14734 | + wchar_t expected_appended[1024]; |
| 14735 | + swprintf(expected_appended, sizeof(expected_appended) / sizeof(expected_appended[0]), |
| 14736 | + L"C:\\Tools\\alpha;C:\\Tools\\omega;%ls", bin_dir_w); |
| 14737 | + wchar_t seeded_middle[1024]; |
| 14738 | + swprintf(seeded_middle, sizeof(seeded_middle) / sizeof(seeded_middle[0]), |
| 14739 | + L"C:\\Tools\\alpha;%ls;C:\\Tools\\omega", bin_dir_w); |
| 14740 | + |
| 14741 | + int ok = 1; |
| 14742 | + /* Scenario A — append to a PATH lacking our dir, de-dup, then remove. */ |
| 14743 | + ok &= cli_test_set_smoke_path(key, L"C:\\Tools\\alpha;C:\\Tools\\omega") == 0; |
| 14744 | + ok &= cbm_cli_ensure_windows_user_path_for_test(bin_dir, false) == 0; /* added */ |
| 14745 | + ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0; |
| 14746 | + ok &= wcscmp(readback, expected_appended) == 0; |
| 14747 | + /* Reinstall must not duplicate: the non-ASCII entry must round-trip-match. */ |
| 14748 | + ok &= cbm_cli_ensure_windows_user_path_for_test(bin_dir, false) == 1; /* already present */ |
| 14749 | + ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0; |
| 14750 | + ok &= wcscmp(readback, expected_appended) == 0; |
| 14751 | + /* Remove our entry — the two unrelated entries survive byte-for-byte. */ |
| 14752 | + ok &= cbm_cli_remove_windows_user_path_for_test(bin_dir, false) == 0; /* removed */ |
| 14753 | + ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0; |
| 14754 | + ok &= wcscmp(readback, L"C:\\Tools\\alpha;C:\\Tools\\omega") == 0; |
| 14755 | + /* Idempotent removal: nothing of ours left to take out. */ |
| 14756 | + ok &= cbm_cli_remove_windows_user_path_for_test(bin_dir, false) == 1; /* nothing to remove */ |
| 14757 | + ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0; |
| 14758 | + ok &= wcscmp(readback, L"C:\\Tools\\alpha;C:\\Tools\\omega") == 0; |
| 14759 | + |
| 14760 | + /* Scenario B — our entry in the MIDDLE: neighbours on both sides survive. */ |
| 14761 | + ok &= cli_test_set_smoke_path(key, seeded_middle) == 0; |
| 14762 | + ok &= cbm_cli_remove_windows_user_path_for_test(bin_dir, false) == 0; /* removed */ |
| 14763 | + ok &= cli_test_read_smoke_path(key, readback, sizeof(readback)) == 0; |
| 14764 | + ok &= wcscmp(readback, L"C:\\Tools\\alpha;C:\\Tools\\omega") == 0; |
| 14765 | + |
| 14766 | + /* Teardown: delete only our scratch leaf; clear the seam env. */ |
| 14767 | + RegCloseKey(key); |
| 14768 | + RegDeleteKeyW(HKEY_CURRENT_USER, key_path); |
| 14769 | + SetEnvironmentVariableW(L"CBM_TEST_WINDOWS_USER_PATH_RUN_ID", NULL); |
| 14770 | + SetEnvironmentVariableW(L"SMOKE_TEMP_ROOT", NULL); |
| 14771 | + SetEnvironmentVariableW(L"SMOKE_DOWNLOAD_URL", NULL); |
| 14772 | + |
| 14773 | + if (!ok) { |
| 14774 | + FAIL("uninstall must remove exactly its own user-PATH entry and leave others intact"); |
| 14775 | + } |
| 14776 | + PASS(); |
| 14777 | +} |
14673 | 14778 | #endif /* _WIN32 */ |
14674 | 14779 |
|
14675 | 14780 | /* ═══════════════════════════════════════════════════════════════════ |
@@ -14796,6 +14901,7 @@ SUITE(cli) { |
14796 | 14901 | RUN_TEST(cli_activation_guard_is_bypassed_for_dry_run_and_plan); |
14797 | 14902 | #ifdef _WIN32 |
14798 | 14903 | RUN_TEST(cli_windows_update_hands_off_to_install_script); |
| 14904 | + RUN_TEST(cli_uninstall_removes_only_its_own_windows_user_path_entry_issue2117); |
14799 | 14905 | #endif |
14800 | 14906 |
|
14801 | 14907 | /* Version (2 tests — selfupdate_test.go) */ |
|
0 commit comments