Files
cproof/tests/functionaltests/test_disco.c
jabber.developer2 24e1dac354 test(disco): add comprehensive XEP-0030 functional tests
Add 7 new tests for /disco command:
- disco_items_to_jid: query items to specific JID
- disco_info_empty_result: handle empty disco#info response
- disco_info_multiple_identities: multiple identity elements
- disco_info_without_name: identity without optional name attr
- disco_items_without_name: items without optional name attr
- disco_info_service_unavailable: error handling for info
- disco_items_error_handling: error handling for items (XEP-0030 §7)

The disco_items_error_handling test documents a bug where disco#items
errors are silently ignored (unlike disco#info which handles them).
This violates XEP-0030 Section 7 which requires error feedback to user.
2026-03-03 16:57:35 +01:00

446 lines
14 KiB
C

/*
* 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();
}
void
disco_items_error_handling(void **state)
{
/*
* Test that /disco items displays error responses to user.
* Per XEP-0030 Section 7: errors like service-unavailable must be shown.
*/
stbbr_for_query("http://jabber.org/protocol/disco#items",
"<iq to='stabber@localhost/profanity' type='error' from='broken.localhost'>"
"<error type='cancel'>"
"<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>"
"</error>"
"</iq>"
);
prof_connect();
prof_input("/disco items broken.localhost");
prof_timeout(10);
/* Per XEP-0030, error should be displayed to user */
assert_true(prof_output_regex("Service discovery failed.*service-unavailable"));
prof_timeout_reset();
}