|
20 | 20 | #include <stdlib.h> |
21 | 21 | #include <string.h> |
22 | 22 | #include <time.h> |
| 23 | +#include <limits.h> |
| 24 | + |
| 25 | +/* glibc's _FORTIFY_SOURCE realpath() aborts unless the output buffer is at |
| 26 | + * least PATH_MAX bytes, independent of the actual path length. */ |
| 27 | +#ifndef PATH_MAX |
| 28 | +#define PATH_MAX 4096 |
| 29 | +#endif |
23 | 30 |
|
24 | 31 | /* Sleep before rewriting a fixture so its mtime_ns strictly increases and the |
25 | 32 | * incremental change classifier reliably sees the edit (10 ms). */ |
@@ -651,10 +658,114 @@ TEST(index_not_indexed_by_design_reported) { |
651 | 658 | PASS(); |
652 | 659 | } |
653 | 660 |
|
| 661 | +/* INV(relative-repo-path, #794): index_repository with repo_path "." must |
| 662 | + * canonicalize to the absolute path BEFORE anything derives from it. On |
| 663 | + * v0.8.1 the literal "." flowed through: the project was named "root", the |
| 664 | + * projects table stored root_path="." verbatim, the post-dump integrity |
| 665 | + * check flagged bad_root_path and AUTO-DELETED the whole DB — while the |
| 666 | + * response still claimed status "indexed". The guard asserts the three |
| 667 | + * things that failure mode breaks: a path-derived project name (not |
| 668 | + * "root"), an absolute stored root_path, and a DB that persists and stays |
| 669 | + * queryable after the run. */ |
| 670 | +TEST(index_relative_repo_path_canonicalized) { |
| 671 | +#ifdef _WIN32 |
| 672 | + /* realpath/chdir are POSIX; the assertions are POSIX-path-shaped. The |
| 673 | + * #794 canonicalization logic under guard is platform-independent, so |
| 674 | + * the four Unix legs carry this regression guard. */ |
| 675 | + SKIP_PLATFORM("POSIX-only guard (realpath/chdir); #794 logic is platform-independent"); |
| 676 | +#else |
| 677 | + RProj lp; |
| 678 | + memset(&lp, 0, sizeof(lp)); |
| 679 | + snprintf(lp.tmpdir, sizeof(lp.tmpdir), "/tmp/cbm_resil_XXXXXX"); |
| 680 | + if (!cbm_mkdtemp(lp.tmpdir)) { |
| 681 | + FAIL("mkdtemp failed"); |
| 682 | + } |
| 683 | + rh_to_fwd_slashes(lp.tmpdir); |
| 684 | + ri_write_text(lp.tmpdir, "good.py", "def alpha():\n return 1\n"); |
| 685 | + |
| 686 | + /* Resolve the canonical form of tmpdir (macOS: /tmp -> /private/tmp) so |
| 687 | + * the derived-name and stored-root assertions compare like with like. */ |
| 688 | + char canon[PATH_MAX]; |
| 689 | + if (!realpath(lp.tmpdir, canon)) { |
| 690 | + snprintf(canon, sizeof(canon), "%s", lp.tmpdir); |
| 691 | + } |
| 692 | + lp.project = cbm_project_name_from_path(canon); |
| 693 | + if (!lp.project) { |
| 694 | + FAIL("project name derivation failed"); |
| 695 | + } |
| 696 | + |
| 697 | + const char *home = getenv("HOME"); |
| 698 | + if (!home) { |
| 699 | + home = "/tmp"; |
| 700 | + } |
| 701 | + char cache_dir[512]; |
| 702 | + snprintf(cache_dir, sizeof(cache_dir), "%s/.cache/codebase-memory-mcp", home); |
| 703 | + cbm_mkdir(cache_dir); |
| 704 | + snprintf(lp.dbpath, sizeof(lp.dbpath), "%s/%s.db", cache_dir, lp.project); |
| 705 | + unlink(lp.dbpath); |
| 706 | + |
| 707 | + lp.srv = cbm_mcp_server_new(NULL); |
| 708 | + if (!lp.srv) { |
| 709 | + FAIL("server alloc failed"); |
| 710 | + } |
| 711 | + |
| 712 | + /* chdir into the repo and index "." — restore cwd immediately after. */ |
| 713 | + char oldcwd[PATH_MAX]; |
| 714 | + if (!getcwd(oldcwd, sizeof(oldcwd))) { |
| 715 | + FAIL("getcwd failed"); |
| 716 | + } |
| 717 | + if (chdir(lp.tmpdir) != 0) { |
| 718 | + FAIL("chdir failed"); |
| 719 | + } |
| 720 | + char *resp = cbm_mcp_handle_tool(lp.srv, "index_repository", "{\"repo_path\":\".\"}"); |
| 721 | + int rc_back = chdir(oldcwd); |
| 722 | + if (rc_back != 0) { |
| 723 | + free(resp); |
| 724 | + FAIL("chdir back failed"); |
| 725 | + } |
| 726 | + ASSERT_NOT_NULL(resp); |
| 727 | + |
| 728 | + /* The response names the path-derived project, never the literal-dot |
| 729 | + * artifact "root". */ |
| 730 | + ASSERT_NULL(strstr(resp, "\"project\":\"root\"")); |
| 731 | + ASSERT_NOT_NULL(strstr(resp, lp.project)); |
| 732 | + ASSERT_NOT_NULL(strstr(resp, "\"status\":\"indexed\"")); |
| 733 | + free(resp); |
| 734 | + |
| 735 | + /* The DB survived the run (v0.8.1 auto-deleted it here)... */ |
| 736 | + cbm_store_t *store = cbm_store_open_path(lp.dbpath); |
| 737 | + if (!store) { |
| 738 | + FAIL("db missing after relative-path index (auto-clean regression)"); |
| 739 | + } |
| 740 | + |
| 741 | + /* ...with an ABSOLUTE canonical root_path in the projects table... */ |
| 742 | + cbm_project_t info = {0}; |
| 743 | + ASSERT_EQ(cbm_store_get_project(store, lp.project, &info), CBM_STORE_OK); |
| 744 | + ASSERT_NOT_NULL(info.root_path); |
| 745 | + ASSERT_TRUE(info.root_path[0] == '/'); |
| 746 | + ASSERT_STR_EQ(info.root_path, canon); |
| 747 | + free((char *)info.name); |
| 748 | + free((char *)info.indexed_at); |
| 749 | + free((char *)info.root_path); |
| 750 | + |
| 751 | + /* ...and it stays queryable. */ |
| 752 | + cbm_node_t *hits = NULL; |
| 753 | + int hit_count = 0; |
| 754 | + ASSERT_EQ(cbm_store_find_nodes_by_name(store, lp.project, "alpha", &hits, &hit_count), |
| 755 | + CBM_STORE_OK); |
| 756 | + ASSERT_GTE(hit_count, 1); |
| 757 | + cbm_store_free_nodes(hits, hit_count); |
| 758 | + |
| 759 | + rh_cleanup(&lp, store); |
| 760 | + PASS(); |
| 761 | +#endif /* !_WIN32 */ |
| 762 | +} |
| 763 | + |
654 | 764 | SUITE(index_resilience) { |
655 | 765 | RUN_TEST(index_oversized_file_reported); |
656 | 766 | RUN_TEST(index_clean_run_no_logfile); |
657 | 767 | RUN_TEST(index_parse_partial_reported); |
658 | 768 | RUN_TEST(index_parse_partial_clears_on_fix); |
659 | 769 | RUN_TEST(index_not_indexed_by_design_reported); |
| 770 | + RUN_TEST(index_relative_repo_path_canonicalized); |
660 | 771 | } |
0 commit comments