Compare commits
6 Commits
test/autop
...
58c23c4089
| Author | SHA1 | Date | |
|---|---|---|---|
|
58c23c4089
|
|||
|
6f136bde88
|
|||
|
58dd89be40
|
|||
|
b2ce06923e
|
|||
|
9b292a6100
|
|||
|
a9c21ce487
|
3
.github/workflows/ci-code.yml
vendored
3
.github/workflows/ci-code.yml
vendored
@@ -50,9 +50,6 @@ jobs:
|
||||
run: |
|
||||
grep -P 'auto_(char|gchar|gcharv|guchar|jid|sqlite|gfd|FILE)[\w *]*;$' -r src && exit -1 || true
|
||||
|
||||
- name: Check CWE-134 format string vulnerabilities
|
||||
run: ./check-cwe134.sh
|
||||
|
||||
- name: Install clang-format
|
||||
run: |
|
||||
sudo apt-get update
|
||||
|
||||
@@ -144,16 +144,6 @@ scan-build make
|
||||
scan-view ...
|
||||
```
|
||||
|
||||
### Security checks
|
||||
|
||||
We have a static analyzer `check-cwe134.sh` that detects CWE-134 format string vulnerabilities. It runs automatically in CI but you can also run it locally:
|
||||
|
||||
```bash
|
||||
./check-cwe134.sh
|
||||
```
|
||||
|
||||
This checks for unsafe patterns where data could be passed directly as a format string to functions like `printf`, `cons_show`, etc. Never pass a raw string for formatting; use `"%s"` format specifier instead.
|
||||
|
||||
### Finding typos
|
||||
|
||||
We include a `.codespellrc` configuration file for `codespell` in the root directory.
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
FROM archlinux:latest
|
||||
FROM archlinux
|
||||
|
||||
ENV TERM=xterm
|
||||
ENV CC="ccache gcc"
|
||||
|
||||
RUN pacman -Syyu --noconfirm
|
||||
|
||||
RUN pacman -Syu --noconfirm
|
||||
# reflector is optional - if it fails due to network issues, continue with default mirrorlist
|
||||
RUN pacman -S --needed --noconfirm reflector && \
|
||||
(reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist || true)
|
||||
|
||||
@@ -168,6 +168,8 @@ 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 = \
|
||||
@@ -186,8 +188,6 @@ functionaltest_sources = \
|
||||
tests/functionaltests/test_muc.c tests/functionaltests/test_muc.h \
|
||||
tests/functionaltests/test_disconnect.c tests/functionaltests/test_disconnect.h \
|
||||
tests/functionaltests/test_lastactivity.c tests/functionaltests/test_lastactivity.h \
|
||||
tests/functionaltests/test_autoping.c tests/functionaltests/test_autoping.h \
|
||||
tests/functionaltests/test_disco.c tests/functionaltests/test_disco.h \
|
||||
tests/functionaltests/functionaltests.c
|
||||
|
||||
main_source = src/main.c
|
||||
|
||||
@@ -168,6 +168,10 @@ num_cores()
|
||||
# Run test failure detection verification first
|
||||
verify_test_failure_detection
|
||||
|
||||
# Run CWE-134 format string vulnerability check
|
||||
echo "=== Running CWE-134 security check ==="
|
||||
./check-cwe134.sh || { echo "CWE-134 check failed!"; exit 1; }
|
||||
|
||||
# Parse arguments
|
||||
COVERAGE_ONLY=no
|
||||
for arg in "$@"; do
|
||||
|
||||
@@ -77,8 +77,6 @@ _db_teardown(const char* ctx)
|
||||
}
|
||||
g_chatlog_database = NULL;
|
||||
}
|
||||
// Safe to call unconditionally; no-op if not initialized.
|
||||
// See: https://www.sqlite.org/c3ref/initialize.html
|
||||
sqlite3_shutdown();
|
||||
}
|
||||
|
||||
|
||||
@@ -148,8 +148,7 @@ create_input_window(void)
|
||||
* Fail gracefully instead of aborting in production.
|
||||
*/
|
||||
if (MB_CUR_MAX > PROF_MB_CUR_MAX) {
|
||||
log_error("Locale MB_CUR_MAX (%zu) exceeds compiled limit (%d)", (size_t)MB_CUR_MAX, PROF_MB_CUR_MAX);
|
||||
cons_show_error("Unsupported locale. Before running, execute in terminal: export LC_ALL=C.UTF-8");
|
||||
cons_show_error("Your locale's MB_CUR_MAX (%zu) exceeds PROF_MB_CUR_MAX (%d); input window disabled.", (size_t)MB_CUR_MAX, PROF_MB_CUR_MAX);
|
||||
return;
|
||||
}
|
||||
#ifdef NCURSES_REENTRANT
|
||||
@@ -168,7 +167,7 @@ create_input_window(void)
|
||||
|
||||
inp_win = newpad(1, INP_WIN_MAX);
|
||||
if (!inp_win) {
|
||||
log_error("Failed to allocate input window pad");
|
||||
// Failed to allocate input pad; leave inp_win NULL and avoid further use
|
||||
return;
|
||||
}
|
||||
wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));
|
||||
|
||||
@@ -40,8 +40,6 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#ifdef HAVE_NCURSESW_NCURSES_H
|
||||
#include <ncursesw/ncurses.h>
|
||||
#elif HAVE_NCURSES_H
|
||||
@@ -114,7 +112,6 @@ status_bar_init(void)
|
||||
int row = screen_statusbar_row();
|
||||
int cols = getmaxx(stdscr);
|
||||
if (cols <= 0) {
|
||||
log_warning("status_bar_init: invalid cols %d, defaulting to 1", cols);
|
||||
cols = 1;
|
||||
}
|
||||
statusbar_win = newwin(1, cols, row, 0);
|
||||
@@ -160,7 +157,6 @@ status_bar_resize(void)
|
||||
}
|
||||
int cols = getmaxx(stdscr);
|
||||
if (cols <= 0) {
|
||||
log_warning("status_bar_resize: invalid cols %d, defaulting to 1", cols);
|
||||
cols = 1;
|
||||
}
|
||||
werase(statusbar_win);
|
||||
|
||||
@@ -79,7 +79,15 @@ static void _win_print_wrapped(WINDOW* win, const char* const message, size_t in
|
||||
static int
|
||||
_check_subwin_width(int cols, int width)
|
||||
{
|
||||
return cols <= 1 ? 1 : CLAMP(width, 1, cols - 1);
|
||||
if (cols > 1) {
|
||||
if (width < 1)
|
||||
width = 1;
|
||||
if (width >= cols)
|
||||
width = cols - 1;
|
||||
} else {
|
||||
width = 1;
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
int
|
||||
|
||||
@@ -2532,6 +2532,9 @@ _disco_items_result_handler(xmpp_stanza_t* const stanza)
|
||||
}
|
||||
|
||||
xmpp_stanza_t* child = xmpp_stanza_get_children(query);
|
||||
if (child == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (child) {
|
||||
const char* stanza_name = xmpp_stanza_get_name(child);
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
* functional tests run less frequently than unit tests.
|
||||
*
|
||||
* Tests are organized into groups for better maintainability and parallel execution:
|
||||
* Group 1: Connect, Ping, Autoping (fast), Rooms, Software, Last Activity
|
||||
* Group 1: Connect, Ping, Rooms, Software
|
||||
* Group 2: Message, Receipts, Roster, Chat Session
|
||||
* Group 3: Presence, Disconnect, Autoping (slow)
|
||||
* Group 3: Presence, Disconnect
|
||||
* Group 4: MUC, Carbons
|
||||
*
|
||||
* Parallel execution:
|
||||
@@ -53,8 +53,6 @@
|
||||
#include "test_muc.h"
|
||||
#include "test_disconnect.h"
|
||||
#include "test_lastactivity.h"
|
||||
#include "test_autoping.h"
|
||||
#include "test_disco.h"
|
||||
|
||||
/* Macro to wrap each test with setup/teardown functions */
|
||||
#define PROF_FUNC_TEST(test) cmocka_unit_test_setup_teardown(test, init_prof_test, close_prof_test)
|
||||
@@ -111,16 +109,6 @@ main(int argc, char* argv[])
|
||||
/* Last Activity - XEP-0012 */
|
||||
PROF_FUNC_TEST(responds_to_last_activity_request),
|
||||
PROF_FUNC_TEST(last_activity_request_to_contact),
|
||||
|
||||
/* Autoping command tests - fast, no waiting */
|
||||
PROF_FUNC_TEST(autoping_set_interval),
|
||||
PROF_FUNC_TEST(autoping_set_zero_disables),
|
||||
PROF_FUNC_TEST(autoping_timeout_set),
|
||||
PROF_FUNC_TEST(autoping_timeout_zero_disables),
|
||||
|
||||
/* Autoping slow tests - require sleep for timer triggers (~2s each) */
|
||||
PROF_FUNC_TEST(autoping_sends_ping_after_interval),
|
||||
PROF_FUNC_TEST(autoping_server_not_supporting_ping),
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
@@ -155,7 +143,7 @@ main(int argc, char* argv[])
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
* GROUP 3: Presence, Disconnect, Disco
|
||||
* GROUP 3: Presence, Disconnect
|
||||
* Online/away/xa/dnd/chat status management
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group3_tests[] = {
|
||||
@@ -177,22 +165,6 @@ main(int argc, char* argv[])
|
||||
|
||||
/* Disconnect - clean session termination */
|
||||
PROF_FUNC_TEST(disconnect_ends_session),
|
||||
|
||||
/* Service Discovery - XEP-0030 */
|
||||
PROF_FUNC_TEST(disco_info_shows_identity),
|
||||
PROF_FUNC_TEST(disco_info_shows_features),
|
||||
PROF_FUNC_TEST(disco_info_to_server),
|
||||
PROF_FUNC_TEST(disco_info_to_jid),
|
||||
PROF_FUNC_TEST(disco_info_not_found),
|
||||
PROF_FUNC_TEST(disco_items_shows_items),
|
||||
PROF_FUNC_TEST(disco_items_empty_result),
|
||||
PROF_FUNC_TEST(disco_requires_connection),
|
||||
PROF_FUNC_TEST(disco_items_to_jid),
|
||||
PROF_FUNC_TEST(disco_info_empty_result),
|
||||
PROF_FUNC_TEST(disco_info_multiple_identities),
|
||||
PROF_FUNC_TEST(disco_info_without_name),
|
||||
PROF_FUNC_TEST(disco_items_without_name),
|
||||
PROF_FUNC_TEST(disco_info_service_unavailable),
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
@@ -240,9 +212,9 @@ main(int argc, char* argv[])
|
||||
const struct CMUnitTest* tests;
|
||||
size_t count;
|
||||
} groups[] = {
|
||||
{ "Group 1: Connect/Ping/Rooms/Software/Autoping", group1_tests, ARRAY_SIZE(group1_tests) },
|
||||
{ "Group 1: Connect/Ping/Rooms/Software", group1_tests, ARRAY_SIZE(group1_tests) },
|
||||
{ "Group 2: Message/Receipts/Roster/Session", group2_tests, ARRAY_SIZE(group2_tests) },
|
||||
{ "Group 3: Presence/Disconnect/Disco", group3_tests, ARRAY_SIZE(group3_tests) },
|
||||
{ "Group 3: Presence/Disconnect", group3_tests, ARRAY_SIZE(group3_tests) },
|
||||
{ "Group 4: MUC/Carbons", group4_tests, ARRAY_SIZE(group4_tests) },
|
||||
};
|
||||
const int num_groups = ARRAY_SIZE(groups);
|
||||
|
||||
@@ -362,6 +362,7 @@ close_prof_test(void **state)
|
||||
fd = 0;
|
||||
child_pid = 0;
|
||||
}
|
||||
_cleanup_dirs();
|
||||
|
||||
if (config_orig) {
|
||||
setenv("XDG_CONFIG_HOME", config_orig, 1);
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
#include <glib.h>
|
||||
#include "prof_cmocka.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stabber.h>
|
||||
|
||||
#include "proftest.h"
|
||||
|
||||
void
|
||||
autoping_set_interval(void** state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/autoping set 60");
|
||||
assert_true(prof_output_exact("Autoping interval set to 60 seconds."));
|
||||
}
|
||||
|
||||
void
|
||||
autoping_set_zero_disables(void** state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/autoping set 0");
|
||||
assert_true(prof_output_exact("Autoping disabled."));
|
||||
}
|
||||
|
||||
void
|
||||
autoping_timeout_set(void** state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/autoping timeout 30");
|
||||
assert_true(prof_output_exact("Autoping timeout set to 30 seconds."));
|
||||
}
|
||||
|
||||
void
|
||||
autoping_timeout_zero_disables(void** state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/autoping timeout 0");
|
||||
assert_true(prof_output_exact("Autoping timeout disabled."));
|
||||
}
|
||||
|
||||
void
|
||||
autoping_sends_ping_after_interval(void** state)
|
||||
{
|
||||
/*
|
||||
* This test verifies that autoping sends a ping IQ after the configured
|
||||
* interval. We set a short interval (1 second) and verify the ping is sent.
|
||||
*/
|
||||
|
||||
// Register disco#info response with ping support
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'>"
|
||||
"<identity category='server' type='im' name='Stabber'/>"
|
||||
"<feature var='urn:xmpp:ping'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
// Register ping response
|
||||
stbbr_for_query("urn:xmpp:ping",
|
||||
"<iq from='localhost' to='stabber@localhost/profanity' type='result'/>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
// Set short autoping interval
|
||||
prof_input("/autoping set 1");
|
||||
assert_true(prof_output_exact("Autoping interval set to 1 seconds."));
|
||||
|
||||
// Wait for autoping to trigger (interval + some buffer)
|
||||
sleep(2);
|
||||
|
||||
// Verify ping was sent (no 'to' attribute means server ping)
|
||||
assert_true(stbbr_received(
|
||||
"<iq id='*' type='get'>"
|
||||
"<ping xmlns='urn:xmpp:ping'/>"
|
||||
"</iq>"
|
||||
));
|
||||
}
|
||||
|
||||
void
|
||||
autoping_server_not_supporting_ping(void** state)
|
||||
{
|
||||
/*
|
||||
* When server doesn't support ping, autoping should show error.
|
||||
*/
|
||||
|
||||
// Register disco#info response WITHOUT ping support
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'>"
|
||||
"<identity category='server' type='im' name='Stabber'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
// Set short autoping interval
|
||||
prof_input("/autoping set 1");
|
||||
assert_true(prof_output_exact("Autoping interval set to 1 seconds."));
|
||||
|
||||
// Wait for autoping to trigger
|
||||
sleep(2);
|
||||
|
||||
// Should show error about ping not being supported
|
||||
assert_true(prof_output_regex("Server ping not supported"));
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
void autoping_set_interval(void** state);
|
||||
void autoping_set_zero_disables(void** state);
|
||||
void autoping_timeout_set(void** state);
|
||||
void autoping_timeout_zero_disables(void** state);
|
||||
void autoping_sends_ping_after_interval(void** state);
|
||||
void autoping_server_not_supporting_ping(void** state);
|
||||
@@ -1,420 +0,0 @@
|
||||
/*
|
||||
* test_disco.c
|
||||
*
|
||||
* Functional tests for /disco command (XEP-0030 Service Discovery).
|
||||
* Tests cover:
|
||||
* - /disco info [jid] - query entity capabilities and features
|
||||
* - /disco items [jid] - query entity items/services
|
||||
*
|
||||
* XEP-0030: https://xmpp.org/extensions/xep-0030.html
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include "prof_cmocka.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stabber.h>
|
||||
|
||||
#include "proftest.h"
|
||||
|
||||
void
|
||||
disco_info_shows_identity(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco info displays identity information correctly.
|
||||
* Identity includes: name, type, category
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'>"
|
||||
"<identity category='server' type='im' name='Prosody'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
|
||||
prof_input("/disco info");
|
||||
|
||||
prof_timeout(10);
|
||||
assert_true(prof_output_exact("Service discovery info for localhost"));
|
||||
assert_true(prof_output_exact("Identities"));
|
||||
assert_true(prof_output_regex("Prosody.*im.*server"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_info_shows_features(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco info displays feature list.
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'>"
|
||||
"<identity category='server' type='im' name='TestServer'/>"
|
||||
"<feature var='urn:xmpp:ping'/>"
|
||||
"<feature var='http://jabber.org/protocol/disco#info'/>"
|
||||
"<feature var='http://jabber.org/protocol/disco#items'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
|
||||
prof_input("/disco info");
|
||||
|
||||
prof_timeout(10);
|
||||
assert_true(prof_output_exact("Features:"));
|
||||
assert_true(prof_output_exact("urn:xmpp:ping"));
|
||||
assert_true(prof_output_exact("http://jabber.org/protocol/disco#info"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_info_to_server(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco info without arguments queries the server (domainpart).
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'>"
|
||||
"<identity category='server' type='im' name='LocalServer'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
|
||||
prof_input("/disco info");
|
||||
|
||||
/* Verify request was sent to server (localhost) */
|
||||
prof_timeout(10);
|
||||
assert_true(stbbr_received(
|
||||
"<iq id='*' to='localhost' type='get'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'/>"
|
||||
"</iq>"
|
||||
));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_info_to_jid(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco info <jid> queries the specified JID.
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='conference.localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'>"
|
||||
"<identity category='conference' type='text' name='MUC Service'/>"
|
||||
"<feature var='http://jabber.org/protocol/muc'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
|
||||
prof_input("/disco info conference.localhost");
|
||||
|
||||
prof_timeout(10);
|
||||
/* Verify request was sent to specified JID */
|
||||
assert_true(stbbr_received(
|
||||
"<iq id='*' to='conference.localhost' type='get'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'/>"
|
||||
"</iq>"
|
||||
));
|
||||
|
||||
assert_true(prof_output_exact("Service discovery info for conference.localhost"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_info_not_found(void **state)
|
||||
{
|
||||
/*
|
||||
* Test error handling when disco info returns item-not-found.
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='error' from='unknown.localhost'>"
|
||||
"<error type='cancel'>"
|
||||
"<item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
|
||||
"</error>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
|
||||
prof_input("/disco info unknown.localhost");
|
||||
|
||||
prof_timeout(10);
|
||||
assert_true(prof_output_regex("Service discovery failed.*item-not-found"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_items_shows_items(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco items displays items list with JID and name.
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#items",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#items'>"
|
||||
"<item jid='conference.localhost' name='Chat Rooms'/>"
|
||||
"<item jid='pubsub.localhost' name='Publish-Subscribe'/>"
|
||||
"<item jid='proxy.localhost' name='SOCKS5 Bytestreams'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
prof_input("/disco items");
|
||||
|
||||
prof_timeout(10);
|
||||
assert_true(prof_output_exact("Service discovery items for localhost:"));
|
||||
assert_true(prof_output_regex("conference.localhost.*Chat Rooms"));
|
||||
assert_true(prof_output_regex("pubsub.localhost.*Publish-Subscribe"));
|
||||
assert_true(prof_output_regex("proxy.localhost.*SOCKS5 Bytestreams"));
|
||||
|
||||
/* Verify IQ was sent with correct id */
|
||||
assert_true(stbbr_received(
|
||||
"<iq id='discoitemsreq' to='localhost' type='get'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#items'/>"
|
||||
"</iq>"
|
||||
));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_items_empty_result(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco items handles empty result gracefully.
|
||||
* Per XEP-0030: "if an entity has no associated items, it MUST return
|
||||
* an empty <query/> element (rather than an error)"
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#items",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#items'/>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
prof_input("/disco items");
|
||||
|
||||
prof_timeout(10);
|
||||
assert_true(prof_output_exact("No service discovery items for localhost"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_requires_connection(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco info and /disco items require an active connection.
|
||||
* First test without any connection, then after connect/disconnect.
|
||||
*/
|
||||
|
||||
/* Without connection */
|
||||
prof_input("/disconnect");
|
||||
assert_true(prof_output_exact("You are not currently connected."));
|
||||
|
||||
prof_input("/disco info");
|
||||
assert_true(prof_output_exact("You are not currently connected."));
|
||||
|
||||
prof_input("/disco items");
|
||||
assert_true(prof_output_exact("You are not currently connected."));
|
||||
|
||||
/* After connect and disconnect */
|
||||
prof_connect();
|
||||
|
||||
prof_input("/disconnect");
|
||||
assert_true(prof_output_exact("stabber@localhost logged out successfully."));
|
||||
|
||||
prof_input("/disco info");
|
||||
assert_true(prof_output_exact("You are not currently connected."));
|
||||
|
||||
prof_input("/disco items");
|
||||
assert_true(prof_output_exact("You are not currently connected."));
|
||||
}
|
||||
|
||||
void
|
||||
disco_items_to_jid(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco items <jid> queries the specified JID.
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#items",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='conference.localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#items'>"
|
||||
"<item jid='room1@conference.localhost' name='General Chat'/>"
|
||||
"<item jid='room2@conference.localhost' name='Support'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
prof_input("/disco items conference.localhost");
|
||||
|
||||
prof_timeout(10);
|
||||
/* Verify request was sent to specified JID */
|
||||
assert_true(stbbr_received(
|
||||
"<iq id='discoitemsreq' to='conference.localhost' type='get'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#items'/>"
|
||||
"</iq>"
|
||||
));
|
||||
|
||||
assert_true(prof_output_exact("Service discovery items for conference.localhost:"));
|
||||
assert_true(prof_output_regex("room1@conference.localhost.*General Chat"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_info_empty_result(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco info handles empty result (no identities/features).
|
||||
* This can happen with minimal server configurations.
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='minimal.localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'/>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
prof_input("/disco info minimal.localhost");
|
||||
|
||||
prof_timeout(10);
|
||||
/* Verify request was sent */
|
||||
assert_true(stbbr_received(
|
||||
"<iq id='*' to='minimal.localhost' type='get'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'/>"
|
||||
"</iq>"
|
||||
));
|
||||
/* Empty result should not crash and should not show "Service discovery info" */
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_info_multiple_identities(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco info displays multiple identities correctly.
|
||||
* Entities can have multiple identities (e.g., server + gateway).
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='gateway.localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'>"
|
||||
"<identity category='gateway' type='irc' name='IRC Gateway'/>"
|
||||
"<identity category='directory' type='chatroom' name='Room Directory'/>"
|
||||
"<identity category='automation' type='command-node' name='Ad-Hoc Commands'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
prof_input("/disco info gateway.localhost");
|
||||
|
||||
prof_timeout(10);
|
||||
assert_true(prof_output_exact("Service discovery info for gateway.localhost"));
|
||||
assert_true(prof_output_exact("Identities"));
|
||||
assert_true(prof_output_regex("IRC Gateway.*irc.*gateway"));
|
||||
assert_true(prof_output_regex("Room Directory.*chatroom.*directory"));
|
||||
assert_true(prof_output_regex("Ad-Hoc Commands.*command-node.*automation"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_info_without_name(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco info handles identity without name attribute.
|
||||
* Per XEP-0030: name is OPTIONAL, only category and type are REQUIRED.
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#info'>"
|
||||
"<identity category='server' type='im'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
prof_input("/disco info");
|
||||
|
||||
prof_timeout(10);
|
||||
assert_true(prof_output_exact("Service discovery info for localhost"));
|
||||
assert_true(prof_output_exact("Identities"));
|
||||
/* Should show type and category even without name */
|
||||
assert_true(prof_output_regex("im.*server"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_items_without_name(void **state)
|
||||
{
|
||||
/*
|
||||
* Test that /disco items handles items without name attribute.
|
||||
* Per XEP-0030: name is OPTIONAL for items, only jid is REQUIRED.
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#items",
|
||||
"<iq to='stabber@localhost/profanity' type='result' from='localhost'>"
|
||||
"<query xmlns='http://jabber.org/protocol/disco#items'>"
|
||||
"<item jid='conference.localhost'/>"
|
||||
"<item jid='pubsub.localhost' name='PubSub Service'/>"
|
||||
"<item jid='upload.localhost'/>"
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
prof_input("/disco items");
|
||||
|
||||
prof_timeout(10);
|
||||
assert_true(prof_output_exact("Service discovery items for localhost:"));
|
||||
/* Items without name should still display their JID */
|
||||
assert_true(prof_output_exact("conference.localhost"));
|
||||
assert_true(prof_output_regex("pubsub.localhost.*PubSub Service"));
|
||||
assert_true(prof_output_exact("upload.localhost"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
void
|
||||
disco_info_service_unavailable(void **state)
|
||||
{
|
||||
/*
|
||||
* Test error handling when disco info returns service-unavailable.
|
||||
*/
|
||||
stbbr_for_query("http://jabber.org/protocol/disco#info",
|
||||
"<iq to='stabber@localhost/profanity' type='error' from='offline.localhost'>"
|
||||
"<error type='cancel'>"
|
||||
"<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
|
||||
"</error>"
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
prof_connect();
|
||||
|
||||
prof_input("/disco info offline.localhost");
|
||||
|
||||
prof_timeout(10);
|
||||
assert_true(prof_output_regex("Service discovery failed.*service-unavailable"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
/* test_disco.h
|
||||
*
|
||||
* Functional tests for /disco command (XEP-0030 Service Discovery)
|
||||
*/
|
||||
|
||||
void disco_info_shows_identity(void **state);
|
||||
void disco_info_shows_features(void **state);
|
||||
void disco_info_to_server(void **state);
|
||||
void disco_info_to_jid(void **state);
|
||||
void disco_info_not_found(void **state);
|
||||
void disco_items_shows_items(void **state);
|
||||
void disco_items_empty_result(void **state);
|
||||
void disco_requires_connection(void **state);
|
||||
void disco_items_to_jid(void **state);
|
||||
void disco_info_empty_result(void **state);
|
||||
void disco_info_multiple_identities(void **state);
|
||||
void disco_info_without_name(void **state);
|
||||
void disco_items_without_name(void **state);
|
||||
void disco_info_service_unavailable(void **state);
|
||||
@@ -23,7 +23,7 @@ responds_to_last_activity_request(void **state)
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
// Verify that CProof responds with last activity info
|
||||
// Verify profanity responds with last activity info
|
||||
// The 'seconds' attribute indicates idle time
|
||||
assert_true(stbbr_received(
|
||||
"<iq id='last1' type='result' to='buddy1@localhost/mobile'>"
|
||||
|
||||
441
tests/unittests/test_autoping.c
Normal file
441
tests/unittests/test_autoping.c
Normal file
@@ -0,0 +1,441 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
20
tests/unittests/test_autoping.h
Normal file
20
tests/unittests/test_autoping.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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);
|
||||
261
tests/unittests/test_helpers.c
Normal file
261
tests/unittests/test_helpers.c
Normal file
@@ -0,0 +1,261 @@
|
||||
/*
|
||||
* 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"));
|
||||
}
|
||||
25
tests/unittests/test_helpers.h
Normal file
25
tests/unittests/test_helpers.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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,6 +36,8 @@
|
||||
#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)
|
||||
|
||||
@@ -656,6 +658,35 @@ 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