Compare commits
2 Commits
58c23c4089
...
7d7615f10d
| Author | SHA1 | Date | |
|---|---|---|---|
|
7d7615f10d
|
|||
|
b772bd8c86
|
@@ -168,8 +168,6 @@ unittest_sources = \
|
||||
tests/unittests/test_callbacks.c tests/unittests/test_callbacks.h \
|
||||
tests/unittests/test_plugins_disco.c tests/unittests/test_plugins_disco.h \
|
||||
tests/unittests/test_forced_encryption.c tests/unittests/test_forced_encryption.h \
|
||||
tests/unittests/test_helpers.c tests/unittests/test_helpers.h \
|
||||
tests/unittests/test_autoping.c tests/unittests/test_autoping.h \
|
||||
tests/unittests/unittests.c
|
||||
|
||||
functionaltest_sources = \
|
||||
|
||||
@@ -1,441 +0,0 @@
|
||||
/*
|
||||
* test_autoping.c
|
||||
* Unit tests for autoping functionality (XEP-0199)
|
||||
*
|
||||
* Tests the autoping logic including:
|
||||
* - Timer state management
|
||||
* - Timeout checking
|
||||
* - Connection state validation
|
||||
*/
|
||||
|
||||
#include "prof_cmocka.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
|
||||
/*
|
||||
* Since autoping functions use static variables and are tightly coupled
|
||||
* to the connection and xmpp libraries, we test the logic patterns here
|
||||
* rather than calling the actual functions.
|
||||
*
|
||||
* These tests validate the correctness of the autoping algorithm:
|
||||
* - State machine for ping wait
|
||||
* - Timeout calculation
|
||||
* - Connection state checks
|
||||
*/
|
||||
|
||||
/* Connection status enum (isolated from xmpp.h to avoid conflicts) */
|
||||
typedef enum {
|
||||
CONN_DISCONNECTED,
|
||||
CONN_CONNECTED,
|
||||
CONN_CONNECTING
|
||||
} ConnStatus;
|
||||
|
||||
/* Test structure simulating autoping state */
|
||||
typedef struct {
|
||||
gboolean wait;
|
||||
GTimer* timer;
|
||||
int timeout_seconds;
|
||||
ConnStatus conn_status;
|
||||
} AutopingState;
|
||||
|
||||
static AutopingState*
|
||||
autoping_state_new(void)
|
||||
{
|
||||
AutopingState* state = calloc(1, sizeof(AutopingState));
|
||||
state->wait = FALSE;
|
||||
state->timer = NULL;
|
||||
state->timeout_seconds = 30;
|
||||
state->conn_status = CONN_DISCONNECTED;
|
||||
return state;
|
||||
}
|
||||
|
||||
static void
|
||||
autoping_state_free(AutopingState* state)
|
||||
{
|
||||
if (state) {
|
||||
if (state->timer) {
|
||||
g_timer_destroy(state->timer);
|
||||
}
|
||||
free(state);
|
||||
}
|
||||
}
|
||||
|
||||
/* Simulates iq_autoping_timer_cancel logic */
|
||||
static void
|
||||
autoping_cancel(AutopingState* state)
|
||||
{
|
||||
state->wait = FALSE;
|
||||
if (state->timer) {
|
||||
g_timer_destroy(state->timer);
|
||||
state->timer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Simulates ping send logic */
|
||||
static gboolean
|
||||
autoping_send(AutopingState* state)
|
||||
{
|
||||
if (state->conn_status != CONN_CONNECTED) {
|
||||
return FALSE;
|
||||
}
|
||||
if (state->wait) {
|
||||
/* Already waiting for pong */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
state->wait = TRUE;
|
||||
if (state->timer) {
|
||||
g_timer_destroy(state->timer);
|
||||
}
|
||||
state->timer = g_timer_new();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* Simulates iq_autoping_check logic */
|
||||
typedef enum {
|
||||
AUTOPING_OK,
|
||||
AUTOPING_NOT_CONNECTED,
|
||||
AUTOPING_NOT_WAITING,
|
||||
AUTOPING_NO_TIMER,
|
||||
AUTOPING_TIMEOUT
|
||||
} AutopingCheckResult;
|
||||
|
||||
static AutopingCheckResult
|
||||
autoping_check(AutopingState* state)
|
||||
{
|
||||
if (state->conn_status != CONN_CONNECTED) {
|
||||
return AUTOPING_NOT_CONNECTED;
|
||||
}
|
||||
if (state->wait == FALSE) {
|
||||
return AUTOPING_NOT_WAITING;
|
||||
}
|
||||
if (state->timer == NULL) {
|
||||
return AUTOPING_NO_TIMER;
|
||||
}
|
||||
|
||||
gdouble elapsed = g_timer_elapsed(state->timer, NULL);
|
||||
unsigned long seconds_elapsed = (unsigned long)(elapsed * 1.0);
|
||||
|
||||
if (state->timeout_seconds > 0 && seconds_elapsed >= (unsigned long)state->timeout_seconds) {
|
||||
return AUTOPING_TIMEOUT;
|
||||
}
|
||||
|
||||
return AUTOPING_OK;
|
||||
}
|
||||
|
||||
/* Simulates timer extend logic (reset timer on activity) */
|
||||
static void
|
||||
autoping_extend(AutopingState* state)
|
||||
{
|
||||
if (state->timer) {
|
||||
g_timer_start(state->timer);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_cancel resets state correctly
|
||||
*/
|
||||
void
|
||||
test_autoping_cancel_resets_state(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
/* Setup: send a ping */
|
||||
autoping_send(as);
|
||||
assert_true(as->wait);
|
||||
assert_non_null(as->timer);
|
||||
|
||||
/* Cancel should reset everything */
|
||||
autoping_cancel(as);
|
||||
assert_false(as->wait);
|
||||
assert_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_send fails when disconnected
|
||||
*/
|
||||
void
|
||||
test_autoping_send_fails_when_disconnected(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_DISCONNECTED;
|
||||
|
||||
gboolean result = autoping_send(as);
|
||||
|
||||
assert_false(result);
|
||||
assert_false(as->wait);
|
||||
assert_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_send succeeds when connected
|
||||
*/
|
||||
void
|
||||
test_autoping_send_succeeds_when_connected(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
gboolean result = autoping_send(as);
|
||||
|
||||
assert_true(result);
|
||||
assert_true(as->wait);
|
||||
assert_non_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_send returns false if already waiting
|
||||
*/
|
||||
void
|
||||
test_autoping_send_fails_if_already_waiting(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
/* First send succeeds */
|
||||
gboolean result1 = autoping_send(as);
|
||||
assert_true(result1);
|
||||
|
||||
/* Second send fails - already waiting */
|
||||
gboolean result2 = autoping_send(as);
|
||||
assert_false(result2);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns NOT_CONNECTED when disconnected
|
||||
*/
|
||||
void
|
||||
test_autoping_check_not_connected(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_DISCONNECTED;
|
||||
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_NOT_CONNECTED);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns NOT_WAITING when not waiting for pong
|
||||
*/
|
||||
void
|
||||
test_autoping_check_not_waiting(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->wait = FALSE;
|
||||
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_NOT_WAITING);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns NO_TIMER when timer is null
|
||||
*/
|
||||
void
|
||||
test_autoping_check_no_timer(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->wait = TRUE;
|
||||
as->timer = NULL;
|
||||
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_NO_TIMER);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns OK when within timeout
|
||||
*/
|
||||
void
|
||||
test_autoping_check_ok_within_timeout(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->timeout_seconds = 30;
|
||||
|
||||
autoping_send(as);
|
||||
|
||||
/* Immediately after send, should be OK */
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_OK);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns OK when timeout is 0 (disabled)
|
||||
*/
|
||||
void
|
||||
test_autoping_check_ok_when_timeout_disabled(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->timeout_seconds = 0; /* Disabled */
|
||||
|
||||
autoping_send(as);
|
||||
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_OK);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: timer extend resets the timer
|
||||
*/
|
||||
void
|
||||
test_autoping_timer_extend_resets_timer(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
autoping_send(as);
|
||||
|
||||
/* Wait a tiny bit */
|
||||
g_usleep(10000); /* 10ms */
|
||||
|
||||
gdouble elapsed_before = g_timer_elapsed(as->timer, NULL);
|
||||
|
||||
/* Extend should reset timer */
|
||||
autoping_extend(as);
|
||||
|
||||
gdouble elapsed_after = g_timer_elapsed(as->timer, NULL);
|
||||
|
||||
/* After extend, elapsed time should be very small */
|
||||
assert_true(elapsed_after < elapsed_before);
|
||||
assert_true(elapsed_after < 0.005); /* Less than 5ms */
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: timer extend does nothing when timer is null
|
||||
*/
|
||||
void
|
||||
test_autoping_timer_extend_noop_without_timer(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->timer = NULL;
|
||||
|
||||
/* Should not crash */
|
||||
autoping_extend(as);
|
||||
|
||||
assert_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: pong received cancels wait state
|
||||
*/
|
||||
void
|
||||
test_autoping_pong_received_cancels_wait(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
/* Send ping */
|
||||
autoping_send(as);
|
||||
assert_true(as->wait);
|
||||
|
||||
/* Simulate pong received - calls cancel */
|
||||
autoping_cancel(as);
|
||||
|
||||
assert_false(as->wait);
|
||||
assert_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: multiple pings can be sent after pong received
|
||||
*/
|
||||
void
|
||||
test_autoping_can_send_after_pong(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
/* First ping */
|
||||
gboolean result1 = autoping_send(as);
|
||||
assert_true(result1);
|
||||
|
||||
/* Pong received */
|
||||
autoping_cancel(as);
|
||||
|
||||
/* Second ping should succeed */
|
||||
gboolean result2 = autoping_send(as);
|
||||
assert_true(result2);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: connection status changes affect behavior
|
||||
*/
|
||||
void
|
||||
test_autoping_respects_connection_state_changes(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
|
||||
/* Start disconnected */
|
||||
as->conn_status = CONN_DISCONNECTED;
|
||||
assert_false(autoping_send(as));
|
||||
|
||||
/* Connect */
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
assert_true(autoping_send(as));
|
||||
|
||||
/* Disconnect - check should handle gracefully */
|
||||
as->conn_status = CONN_DISCONNECTED;
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
assert_int_equal(result, AUTOPING_NOT_CONNECTED);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: timeout with very short timeout value
|
||||
*/
|
||||
void
|
||||
test_autoping_timeout_detection(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->timeout_seconds = 1; /* 1 second timeout */
|
||||
|
||||
autoping_send(as);
|
||||
|
||||
/* Immediately should be OK */
|
||||
AutopingCheckResult result1 = autoping_check(as);
|
||||
assert_int_equal(result1, AUTOPING_OK);
|
||||
|
||||
/* Manipulate timer to simulate timeout */
|
||||
/* We can't easily wait 1 second in a unit test, so we test the logic */
|
||||
/* by checking that elapsed time is calculated correctly */
|
||||
gdouble elapsed = g_timer_elapsed(as->timer, NULL);
|
||||
assert_true(elapsed < 1.0);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
* test_autoping.h
|
||||
* Header for autoping unit tests
|
||||
*/
|
||||
|
||||
void test_autoping_cancel_resets_state(void** state);
|
||||
void test_autoping_send_fails_when_disconnected(void** state);
|
||||
void test_autoping_send_succeeds_when_connected(void** state);
|
||||
void test_autoping_send_fails_if_already_waiting(void** state);
|
||||
void test_autoping_check_not_connected(void** state);
|
||||
void test_autoping_check_not_waiting(void** state);
|
||||
void test_autoping_check_no_timer(void** state);
|
||||
void test_autoping_check_ok_within_timeout(void** state);
|
||||
void test_autoping_check_ok_when_timeout_disabled(void** state);
|
||||
void test_autoping_timer_extend_resets_timer(void** state);
|
||||
void test_autoping_timer_extend_noop_without_timer(void** state);
|
||||
void test_autoping_pong_received_cancels_wait(void** state);
|
||||
void test_autoping_can_send_after_pong(void** state);
|
||||
void test_autoping_respects_connection_state_changes(void** state);
|
||||
void test_autoping_timeout_detection(void** state);
|
||||
@@ -1,261 +0,0 @@
|
||||
/*
|
||||
* 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"));
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* test_helpers.h
|
||||
* Header for helper function tests
|
||||
*/
|
||||
|
||||
#ifndef TEST_HELPERS_H
|
||||
#define TEST_HELPERS_H
|
||||
|
||||
/* Subwindow width clamping tests */
|
||||
void test_subwin_width_clamps_to_min(void** state);
|
||||
void test_subwin_width_clamps_to_max(void** state);
|
||||
void test_subwin_width_valid_unchanged(void** state);
|
||||
void test_subwin_width_small_cols(void** state);
|
||||
|
||||
/* Hash table iteration tests */
|
||||
void test_hash_table_iter_finds_all_keys(void** state);
|
||||
void test_hash_table_iter_empty_table(void** state);
|
||||
void test_hash_table_iter_early_exit(void** state);
|
||||
void test_nested_hash_table_feature_lookup(void** state);
|
||||
|
||||
/* Bug fix verification tests */
|
||||
void test_calloc_array_iteration_bug(void** state);
|
||||
void test_format_string_with_percent(void** state);
|
||||
|
||||
#endif
|
||||
@@ -36,8 +36,6 @@
|
||||
#include "test_form.h"
|
||||
#include "test_callbacks.h"
|
||||
#include "test_plugins_disco.h"
|
||||
#include "test_helpers.h"
|
||||
#include "test_autoping.h"
|
||||
|
||||
#define muc_unit_test(f) cmocka_unit_test_setup_teardown(f, muc_before_test, muc_after_test)
|
||||
|
||||
@@ -658,35 +656,6 @@ main(int argc, char* argv[])
|
||||
cmocka_unit_test_setup_teardown(test_allow_unencrypted_message_confirms_on_second_attempt, load_preferences, close_preferences),
|
||||
cmocka_unit_test_setup_teardown(test_cmd_force_encryption_invalid_policy, load_preferences, close_preferences),
|
||||
cmocka_unit_test_setup_teardown(test_allow_unencrypted_message_invalid_mode, load_preferences, close_preferences),
|
||||
|
||||
// Helper function tests
|
||||
cmocka_unit_test(test_subwin_width_clamps_to_min),
|
||||
cmocka_unit_test(test_subwin_width_clamps_to_max),
|
||||
cmocka_unit_test(test_subwin_width_valid_unchanged),
|
||||
cmocka_unit_test(test_subwin_width_small_cols),
|
||||
cmocka_unit_test(test_hash_table_iter_finds_all_keys),
|
||||
cmocka_unit_test(test_hash_table_iter_empty_table),
|
||||
cmocka_unit_test(test_hash_table_iter_early_exit),
|
||||
cmocka_unit_test(test_nested_hash_table_feature_lookup),
|
||||
cmocka_unit_test(test_calloc_array_iteration_bug),
|
||||
cmocka_unit_test(test_format_string_with_percent),
|
||||
|
||||
// Autoping tests (XEP-0199)
|
||||
cmocka_unit_test(test_autoping_cancel_resets_state),
|
||||
cmocka_unit_test(test_autoping_send_fails_when_disconnected),
|
||||
cmocka_unit_test(test_autoping_send_succeeds_when_connected),
|
||||
cmocka_unit_test(test_autoping_send_fails_if_already_waiting),
|
||||
cmocka_unit_test(test_autoping_check_not_connected),
|
||||
cmocka_unit_test(test_autoping_check_not_waiting),
|
||||
cmocka_unit_test(test_autoping_check_no_timer),
|
||||
cmocka_unit_test(test_autoping_check_ok_within_timeout),
|
||||
cmocka_unit_test(test_autoping_check_ok_when_timeout_disabled),
|
||||
cmocka_unit_test(test_autoping_timer_extend_resets_timer),
|
||||
cmocka_unit_test(test_autoping_timer_extend_noop_without_timer),
|
||||
cmocka_unit_test(test_autoping_pong_received_cancels_wait),
|
||||
cmocka_unit_test(test_autoping_can_send_after_pong),
|
||||
cmocka_unit_test(test_autoping_respects_connection_state_changes),
|
||||
cmocka_unit_test(test_autoping_timeout_detection),
|
||||
};
|
||||
return cmocka_run_group_tests(all_tests, NULL, NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user