Files
cproof/tests/unittests/test_helpers.c
jabber.developer2 6f136bde88
All checks were successful
CI Code / Check spelling (pull_request) Successful in 20s
CI Code / Check coding style (pull_request) Successful in 35s
CI Code / Code Coverage (pull_request) Successful in 4m54s
CI Code / Linux (ubuntu) (pull_request) Successful in 6m23s
CI Code / Linux (debian) (pull_request) Successful in 9m12s
CI Code / Linux (arch) (pull_request) Successful in 11m12s
tests: add autoping, helpers and XEP functional tests
- Add 15 unit tests for autoping logic (XEP-0199)
- Add 10 helper tests for refactoring validation
- Add Last Activity functional tests (XEP-0012)
- Add MUC affiliation/kick tests (XEP-0045)
2026-02-05 00:01:16 +03:00

262 lines
7.5 KiB
C

/*
* test_helpers.c
* Unit tests for helper functions added in refactoring
*/
#include "prof_cmocka.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <glib.h>
/*
* Tests for _check_subwin_width logic (from src/ui/window.c)
* Since _check_subwin_width is static, we test the logic here
*/
static int
check_subwin_width(int cols, int width)
{
if (cols > 1) {
if (width < 1)
width = 1;
if (width >= cols)
width = cols - 1;
} else {
width = 1;
}
return width;
}
/* Test: width is clamped to minimum 1 when cols > 1 */
void
test_subwin_width_clamps_to_min(void** state)
{
assert_int_equal(check_subwin_width(80, 0), 1);
assert_int_equal(check_subwin_width(80, -5), 1);
assert_int_equal(check_subwin_width(100, -100), 1);
}
/* Test: width is clamped to cols-1 when >= cols */
void
test_subwin_width_clamps_to_max(void** state)
{
assert_int_equal(check_subwin_width(80, 80), 79);
assert_int_equal(check_subwin_width(80, 100), 79);
assert_int_equal(check_subwin_width(80, 1000), 79);
}
/* Test: valid width passes through unchanged */
void
test_subwin_width_valid_unchanged(void** state)
{
assert_int_equal(check_subwin_width(80, 20), 20);
assert_int_equal(check_subwin_width(80, 1), 1);
assert_int_equal(check_subwin_width(80, 79), 79);
assert_int_equal(check_subwin_width(100, 50), 50);
}
/* Test: edge case when cols <= 1 */
void
test_subwin_width_small_cols(void** state)
{
assert_int_equal(check_subwin_width(1, 50), 1);
assert_int_equal(check_subwin_width(0, 50), 1);
assert_int_equal(check_subwin_width(-1, 50), 1);
}
/*
* Tests for hash table iteration pattern
* Verifies that g_hash_table_iter produces same results as get_keys+list
*/
void
test_hash_table_iter_finds_all_keys(void** state)
{
GHashTable* ht = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert(ht, g_strdup("key1"), g_strdup("value1"));
g_hash_table_insert(ht, g_strdup("key2"), g_strdup("value2"));
g_hash_table_insert(ht, g_strdup("key3"), g_strdup("value3"));
GHashTableIter iter;
gpointer key, value;
int count = 0;
gboolean found_key1 = FALSE, found_key2 = FALSE, found_key3 = FALSE;
g_hash_table_iter_init(&iter, ht);
while (g_hash_table_iter_next(&iter, &key, &value)) {
count++;
if (strcmp((char*)key, "key1") == 0) found_key1 = TRUE;
if (strcmp((char*)key, "key2") == 0) found_key2 = TRUE;
if (strcmp((char*)key, "key3") == 0) found_key3 = TRUE;
}
assert_int_equal(count, 3);
assert_true(found_key1);
assert_true(found_key2);
assert_true(found_key3);
g_hash_table_destroy(ht);
}
void
test_hash_table_iter_empty_table(void** state)
{
GHashTable* ht = g_hash_table_new(g_str_hash, g_str_equal);
GHashTableIter iter;
gpointer key, value;
int count = 0;
g_hash_table_iter_init(&iter, ht);
while (g_hash_table_iter_next(&iter, &key, &value)) {
count++;
}
assert_int_equal(count, 0);
g_hash_table_destroy(ht);
}
void
test_hash_table_iter_early_exit(void** state)
{
GHashTable* ht = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert(ht, g_strdup("target"), g_strdup("found"));
g_hash_table_insert(ht, g_strdup("other1"), g_strdup("skip"));
g_hash_table_insert(ht, g_strdup("other2"), g_strdup("skip"));
GHashTableIter iter;
gpointer key, value;
const char* result = NULL;
g_hash_table_iter_init(&iter, ht);
while (g_hash_table_iter_next(&iter, &key, &value)) {
if (strcmp((char*)value, "found") == 0) {
result = (const char*)key;
break;
}
}
assert_non_null(result);
assert_string_equal(result, "target");
g_hash_table_destroy(ht);
}
/*
* Tests for nested hash table lookup (like features_by_jid)
*/
void
test_nested_hash_table_feature_lookup(void** state)
{
// Simulate conn.features_by_jid structure
GHashTable* features_by_jid = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, (GDestroyNotify)g_hash_table_destroy);
// Add jid1 with features
GHashTable* jid1_features = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
g_hash_table_insert(jid1_features, g_strdup("feature_a"), GINT_TO_POINTER(1));
g_hash_table_insert(jid1_features, g_strdup("feature_b"), GINT_TO_POINTER(1));
g_hash_table_insert(features_by_jid, g_strdup("jid1@server"), jid1_features);
// Add jid2 with different features
GHashTable* jid2_features = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
g_hash_table_insert(jid2_features, g_strdup("feature_c"), GINT_TO_POINTER(1));
g_hash_table_insert(features_by_jid, g_strdup("jid2@server"), jid2_features);
// Test: find feature_a (should be in jid1)
GHashTableIter iter;
gpointer key, value;
gboolean found_a = FALSE;
g_hash_table_iter_init(&iter, features_by_jid);
while (g_hash_table_iter_next(&iter, &key, &value)) {
GHashTable* features = (GHashTable*)value;
if (features && g_hash_table_lookup(features, "feature_a")) {
found_a = TRUE;
break;
}
}
assert_true(found_a);
// Test: find feature_c (should be in jid2)
gboolean found_c = FALSE;
g_hash_table_iter_init(&iter, features_by_jid);
while (g_hash_table_iter_next(&iter, &key, &value)) {
GHashTable* features = (GHashTable*)value;
if (features && g_hash_table_lookup(features, "feature_c")) {
found_c = TRUE;
break;
}
}
assert_true(found_c);
// Test: feature_x not found
gboolean found_x = FALSE;
g_hash_table_iter_init(&iter, features_by_jid);
while (g_hash_table_iter_next(&iter, &key, &value)) {
GHashTable* features = (GHashTable*)value;
if (features && g_hash_table_lookup(features, "feature_x")) {
found_x = TRUE;
break;
}
}
assert_false(found_x);
g_hash_table_destroy(features_by_jid);
}
/*
* Test for calloc array loop fix (connection.c queued_messages bug)
* The bug was: for (n = 0; n < len && array[n]; ++n)
* After calloc, array[n] is always NULL, so loop exits immediately
*/
void
test_calloc_array_iteration_bug(void** state)
{
int len = 5;
// Simulate the buggy pattern
char** array = calloc(len + 1, sizeof(char*));
int buggy_count = 0;
for (int n = 0; n < len && array[n]; ++n) {
buggy_count++;
}
// Bug: loop never executes because calloc zeros everything
assert_int_equal(buggy_count, 0);
// Correct pattern
int correct_count = 0;
for (int n = 0; n < len; ++n) {
correct_count++;
// In real code, we'd store values here: array[n] = get_value();
}
assert_int_equal(correct_count, 5);
free(array);
}
/*
* Test for format string safety
* Verifies that using "%s" format prevents interpretation of % in strings
*/
void
test_format_string_with_percent(void** state)
{
char buffer[256];
const char* dangerous_input = "test %s %n %x string";
// Safe: using %s format
int ret = snprintf(buffer, sizeof(buffer), "%s", dangerous_input);
assert_true(ret > 0);
assert_string_equal(buffer, dangerous_input);
// The string should be preserved exactly, including % characters
assert_non_null(strstr(buffer, "%s"));
assert_non_null(strstr(buffer, "%n"));
assert_non_null(strstr(buffer, "%x"));
}