From 845b8afae2d1603cf82869dcf63171e5d78f5fe9 Mon Sep 17 00:00:00 2001 From: Lars Erik Wik Date: Tue, 25 Aug 2026 11:02:05 +0200 Subject: [PATCH 1/4] Fixed dubious argument order in RecordPkgOperationInChroot Each call to RecordPkgOperationInChroot passed version followed by architecture in the arguments. However, the function signature had this the other way around. To avoid any change in behavior, I changed the parameter names to match the arguments of the call sites. Signed-off-by: Lars Erik Wik (cherry picked from commit d85826807c7a6adfa24661477d5b0b4812b007b6) --- libpromises/changes_chroot.c | 2 +- libpromises/changes_chroot.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libpromises/changes_chroot.c b/libpromises/changes_chroot.c index 062b387027b..82fd1ff30bc 100644 --- a/libpromises/changes_chroot.c +++ b/libpromises/changes_chroot.c @@ -265,7 +265,7 @@ bool RecordFileEvaluatedInChroot(const char *path) return ret; } -bool RecordPkgOperationInChroot(const char *op, const char *name, const char *arch, const char *version) +bool RecordPkgOperationInChroot(const char *op, const char *name, const char *version, const char *arch) { assert(op != NULL); assert(name != NULL); diff --git a/libpromises/changes_chroot.h b/libpromises/changes_chroot.h index e17b25763a3..a325aaed31c 100644 --- a/libpromises/changes_chroot.h +++ b/libpromises/changes_chroot.h @@ -34,6 +34,6 @@ void PrepareChangesChroot(const char *path); bool RecordFileChangedInChroot(const char *path); bool RecordFileRenamedInChroot(const char *old_name, const char *new_name); bool RecordFileEvaluatedInChroot(const char *path); -bool RecordPkgOperationInChroot(const char *op, const char *name, const char *arch, const char *version); +bool RecordPkgOperationInChroot(const char *op, const char *name, const char *version, const char *arch); #endif /* CFENGINE_CHANGES_CHROOT_H */ From a276c0a0dde5acc1d703588755f51834c5b814c3 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 12:29:08 -0400 Subject: [PATCH 2/4] Fixed simulate mode swapping package version and architecture RecordPkgOperationInChroot() wrote the architecture before the version, the reverse of the order its readers expect. Ticket: none Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 (cherry picked from commit a5a6b6b6a51a40db2085073af6ddf96854ce4cfc) --- libpromises/changes_chroot.c | 3 ++- tests/unit/simulate_mode_test.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/libpromises/changes_chroot.c b/libpromises/changes_chroot.c index 82fd1ff30bc..bb81cf19bea 100644 --- a/libpromises/changes_chroot.c +++ b/libpromises/changes_chroot.c @@ -297,10 +297,11 @@ bool RecordPkgOperationInChroot(const char *op, const char *name, const char *ve return false; } + /* Record format: op,name,version,architecture */ CsvWriterField(csv_writer, op); CsvWriterField(csv_writer, name); - CsvWriterField(csv_writer, NULL_TO_EMPTY_STRING(arch)); CsvWriterField(csv_writer, NULL_TO_EMPTY_STRING(version)); + CsvWriterField(csv_writer, NULL_TO_EMPTY_STRING(arch)); CsvWriterNewRecord(csv_writer); CsvWriterClose(csv_writer); diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 662e2fe0e57..eb0aa4eb093 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -87,6 +87,21 @@ static void test_diff_install_cancels_removal(void) unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } +/* A package recorded with both a version and an architecture must be + * reported with each in its own place. */ +static void test_diff_version_and_architecture(void) +{ + assert_true(RecordPkgOperationInChroot(CHROOT_PKG_OPERATION_INSTALL, + "foo", "1.2.3", "x86_64")); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(strstr(output, "Package 'foo-x86_64 [1.2.3]' would be installed") != NULL); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + static void test_manifest_install_cancels_removal(void) { write_pkgs_ops("r,foo,,\r\n" @@ -111,6 +126,7 @@ int main() const UnitTest tests[] = { unit_test(test_diff_install_cancels_removal), + unit_test(test_diff_version_and_architecture), unit_test(test_manifest_install_cancels_removal), }; From 2c024071d9031510c40c997236d6c5d40756f84b Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 14:40:26 -0400 Subject: [PATCH 3/4] Fixed simulate diff mode not reporting a removal with a version A recorded removal with a version was only reported when it cancelled a recorded installation of the same version. Ticket: none Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 (cherry picked from commit eb6cf5f0476b8083609746e4a596687c1dfd80ee) --- cf-agent/simulate_mode.c | 2 +- tests/unit/simulate_mode_test.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index 1f5af93ed40..41989c42e55 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -931,7 +931,7 @@ bool DiffPkgOperations() { MapRemove(installed, name_arch); } - else + else if (inst_record != NULL) { /* Keeping the install message, the removal would make no change. */ insert_new_msg = false; diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index eb0aa4eb093..10b5513121a 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -27,6 +27,7 @@ #include /* SetChangesChroot(), ToChangesChroot() */ #include /* CHROOT_PKGS_OPS_FILE */ #include /* DeleteDirectoryTree() */ +#include /* StringContains() */ #include @@ -102,6 +103,36 @@ static void test_diff_version_and_architecture(void) unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } +/* A removal with a version and no install record to cancel is a removal to + * report. */ +static void test_diff_removal_with_version(void) +{ + write_pkgs_ops("r,foo,1.0,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [1.0]' would be removed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* A removal with a version that differs from the recorded installation would + * fail, so only the installation is reported. */ +static void test_diff_removal_with_other_version_keeps_install(void) +{ + write_pkgs_ops("i,foo,2.0,\r\n" + "r,foo,1.0,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [2.0]' would be installed")); + assert_false(StringContains(output, "would be removed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + static void test_manifest_install_cancels_removal(void) { write_pkgs_ops("r,foo,,\r\n" @@ -127,6 +158,8 @@ int main() { unit_test(test_diff_install_cancels_removal), unit_test(test_diff_version_and_architecture), + unit_test(test_diff_removal_with_version), + unit_test(test_diff_removal_with_other_version_keeps_install), unit_test(test_manifest_install_cancels_removal), }; From a0cd87ec524b7d4e5324a4e3328a5dda9f9315f1 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Tue, 10 Nov 2026 12:05:11 -0500 Subject: [PATCH 4/4] Fixed simulate diff mode aborting on repeated unversioned package installs DiffPkgOperations() compared the versions of two recorded installations of the same package without checking that both were set, which PkgVersionIsGreater() asserts. The diff report now keeps the record the way the manifest report does: a versioned record over an unversioned one, otherwise the higher version, otherwise the first one recorded. Ticket: none Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Fable 5.2 Co-Authored-By: Claude Opus 5.5 (cherry picked from commit ab0408f1f8f898e8f22b6a100d7106d46a4a3008) --- cf-agent/simulate_mode.c | 27 +++++++- tests/unit/simulate_mode_test.c | 114 ++++++++++++++++++++++++++++++-- 2 files changed, 135 insertions(+), 6 deletions(-) diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index 41989c42e55..51397b2625d 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -793,6 +793,31 @@ static inline char *GetPkgOperationMsg(ChrootPkgOperationCode op, const char *pk return msg; } +static bool PkgRecordShouldBeReplaced(const PkgOperationRecord *prev_record, + const char *new_ver) +{ + if (prev_record == NULL) + { + /* Nothing recorded yet */ + return true; + } + + if (NULL_OR_EMPTY(new_ver)) + { + /* An unversioned record never replaces an existing one */ + return false; + } + + if (NULL_OR_EMPTY(prev_record->pkg_ver)) + { + /* A versioned record is more specific than an unversioned one */ + return true; + } + + /* Both are versioned, keep the higher version */ + return PkgVersionIsGreater(new_ver, prev_record->pkg_ver); +} + bool DiffPkgOperations() { const char *pkgs_ops_csv_file = ToChangesChroot(CHROOT_PKGS_OPS_FILE); @@ -893,7 +918,7 @@ bool DiffPkgOperations() MapRemove(removed, name_arch); PkgOperationRecord *prev_record = MapGet(installed, name_arch); - if ((prev_record == NULL) || PkgVersionIsGreater(pkg_ver, prev_record->pkg_ver)) + if (PkgRecordShouldBeReplaced(prev_record, pkg_ver)) { char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_INSTALL, pkg_name, pkg_arch, pkg_ver); diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 10b5513121a..9aa8cc01a1e 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -82,8 +82,8 @@ static void test_diff_install_cancels_removal(void) char output[4096]; assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); - assert_true(strstr(output, "Package 'foo [1.2.3]' would be installed") != NULL); - assert_true(strstr(output, "would be removed") == NULL); + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be installed")); + assert_false(StringContains(output, "would be removed")); unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } @@ -98,7 +98,7 @@ static void test_diff_version_and_architecture(void) char output[4096]; assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); - assert_true(strstr(output, "Package 'foo-x86_64 [1.2.3]' would be installed") != NULL); + assert_true(StringContains(output, "Package 'foo-x86_64 [1.2.3]' would be installed")); unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } @@ -141,8 +141,106 @@ static void test_manifest_install_cancels_removal(void) char output[4096]; assert_true(call_with_captured_stdout(&ManifestPkgOperations, output, sizeof(output))); - assert_true(strstr(output, "Package 'foo [1.2.3]' would be present") != NULL); - assert_true(strstr(output, "would be absent") == NULL); + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be present")); + assert_false(StringContains(output, "would be absent")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* Two recorded installations of the same package without a version must be + * reported once, not abort on comparing the empty versions. */ +static void test_diff_repeated_unversioned_install(void) +{ + write_pkgs_ops("i,foo,,\r\n" + "i,foo,,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo' would be installed")); + /* Reported once: the report has no other line. */ + assert_int_equal(StringCountTokens(output, strlen(output), "\n"), 1); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* A versioned installation record is more specific than an unversioned one + * of the same package, so it is the one to report. */ +static void test_diff_unversioned_then_versioned_install(void) +{ + write_pkgs_ops("i,foo,,\r\n" + "i,foo,1.2.3,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be installed")); + assert_false(StringContains(output, "Package 'foo' would be installed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* An unversioned installation record must never replace an existing + * versioned installation record of the same package. */ +static void test_diff_versioned_then_unversioned_install(void) +{ + write_pkgs_ops("i,foo,1.2.3,\r\n" + "i,foo,,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be installed")); + assert_false(StringContains(output, "Package 'foo' would be installed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* When two installations are recorded with different versions and the first + * has the higher version, the first record is kept. */ +static void test_diff_install_higher_version_first_keeps_first(void) +{ + write_pkgs_ops("i,foo,2.0.0,\r\n" + "i,foo,1.0.0,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [2.0.0]' would be installed")); + assert_false(StringContains(output, "Package 'foo [1.0.0]' would be installed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* When two installations are recorded with different versions and the last + * has the higher version, the last record replaces the first. */ +static void test_diff_install_higher_version_last_keeps_last(void) +{ + write_pkgs_ops("i,foo,1.0.0,\r\n" + "i,foo,2.0.0,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [2.0.0]' would be installed")); + assert_false(StringContains(output, "Package 'foo [1.0.0]' would be installed")); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* Two recorded installations of the same package with the same version + * must be reported once. */ +static void test_diff_repeated_versioned_install(void) +{ + write_pkgs_ops("i,foo,1.2.3,\r\n" + "i,foo,1.2.3,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(StringContains(output, "Package 'foo [1.2.3]' would be installed")); + /* Reported once: the report has no other line. */ + assert_int_equal(StringCountTokens(output, strlen(output), "\n"), 1); unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } @@ -161,6 +259,12 @@ int main() unit_test(test_diff_removal_with_version), unit_test(test_diff_removal_with_other_version_keeps_install), unit_test(test_manifest_install_cancels_removal), + unit_test(test_diff_repeated_unversioned_install), + unit_test(test_diff_unversioned_then_versioned_install), + unit_test(test_diff_versioned_then_unversioned_install), + unit_test(test_diff_install_higher_version_first_keeps_first), + unit_test(test_diff_install_higher_version_last_keeps_last), + unit_test(test_diff_repeated_versioned_install), }; int ret = run_tests(tests);