Files
biboumi/tests/hash_disco.cpp
T
2026-09-24 22:07:38 -05:00

107 lines
4.3 KiB
C++

#include "catch2/catch_test_macros.hpp"
#include <xmpp/hash_disco.hpp>
#include <xmpp/xmpp_parser.hpp>
#include <xmpp/xmpp_stanza.hpp>
// WARN: This generation might be wrong!!! These test might be wrong!!!
TEST_CASE("Hash disco 1") {
XmppParser xml;
const std::string doc =
R"(<stream><iq type='result'><query xmlns="http://jabber.org/protocol/disco#info">
<identity xmlns='' category="client" name="BombusMod" type="mobile"/>
<feature xmlns='' var="http://jabber.org/protocol/si"/>
<feature xmlns='' var="http://jabber.org/protocol/bytestreams"/>
<feature xmlns='' var="http://jabber.org/protocol/chatstates"/>
<feature xmlns='' var="http://jabber.org/protocol/disco#info"/>
<feature xmlns='' var="http://jabber.org/protocol/disco#items"/>
<feature xmlns='' var="urn:xmpp:ping"/>
<feature xmlns='' var="jabber:iq:time"/>
<feature xmlns='' var="jabber:iq:privacy"/>
<feature xmlns='' var="jabber:iq:version"/>
<feature xmlns='' var="http://jabber.org/protocol/rosterx"/>
<feature xmlns='' var="urn:xmpp:time"/>
<feature xmlns='' var="jabber:x:oob"/>
<feature xmlns='' var="http://jabber.org/protocol/ibb"/>
<feature xmlns='' var="http://jabber.org/protocol/si/profile/file-transfer"/>
<feature xmlns='' var="urn:xmpp:receipts"/>
<feature xmlns='' var="jabber:iq:roster"/>
<feature xmlns='' var="jabber:iq:last"/>
</query></iq></stream>)";
auto check_stanza = [](const Stanza &stanza) {
const std::string hash = Xmpp::hash_disco_sha1(stanza);
CHECK(hash == "GRREviyyjLzK2wK4QLX5NNF9FmQ=");
};
xml.add_stanza_callback([check_stanza](const Stanza &stanza) {
check_stanza(stanza);
// Do the same checks on a copy of that stanza.
Stanza copy(stanza);
check_stanza(copy);
// And do the same checks on moved-constructed stanza
Stanza moved(std::move(copy));
});
CHECK(doc.size() <= std::numeric_limits<int>::max());
xml.feed(doc.data(), static_cast<int>(doc.size()), true);
}
// Just one more test case to make sure we aren't always generating the same string
TEST_CASE("Hash disco 2") {
XmppParser xml;
const std::string doc =
R"(<stream><iq type='result'><query xmlns='http://jabber.org/protocol/disco#info'>
<identity xmlns='' xml:lang='el' category='client' name='Ψ 0.11' type='pc'/>
<identity xmlns='' xml:lang='en' category='client' name='Psi 0.11' type='mobile'/>
<feature xmlns='' var='http://jabber.org/protocol/caps'/>
<feature xmlns='' var='http://jabber.org/protocol/disco#info'/>
<feature xmlns='' var='http://jabber.org/protocol/disco#items'/>
<feature xmlns='' var='http://jabber.org/protocol/muc'/>
</query></iq></stream>)";
auto check_stanza = [](const Stanza &stanza) {
const std::string hash = Xmpp::hash_disco_sha1(stanza);
CHECK(hash == "ga7DGXbUk3tyt5T/WMlqa5qM0zM=");
};
xml.add_stanza_callback([check_stanza](const Stanza &stanza) {
check_stanza(stanza);
// Do the same checks on a copy of that stanza.
Stanza copy(stanza);
check_stanza(copy);
// And do the same checks on moved-constructed stanza
Stanza moved(std::move(copy));
});
CHECK(doc.size() <= std::numeric_limits<int>::max());
xml.feed(doc.data(), static_cast<int>(doc.size()), true);
}
TEST_CASE("Hash disco 2 different order") {
XmppParser xml;
const std::string doc =
R"(<stream><iq type='result'><query xmlns='http://jabber.org/protocol/disco#info'>
<identity xmlns='' xml:lang='en' category='client' name='Psi 0.11' type='mobile'/>
<identity xmlns='' xml:lang='el' category='client' name='Ψ 0.11' type='pc'/>
<feature xmlns='' var='http://jabber.org/protocol/disco#info'/>
<feature xmlns='' var='http://jabber.org/protocol/caps'/>
<feature xmlns='' var='http://jabber.org/protocol/disco#items'/>
<feature xmlns='' var='http://jabber.org/protocol/muc'/>
</query></iq></stream>)";
auto check_stanza = [](const Stanza &stanza) {
const std::string hash = Xmpp::hash_disco_sha1(stanza);
CHECK(hash == "ga7DGXbUk3tyt5T/WMlqa5qM0zM=");
};
xml.add_stanza_callback([check_stanza](const Stanza &stanza) {
check_stanza(stanza);
// Do the same checks on a copy of that stanza.
Stanza copy(stanza);
check_stanza(copy);
// And do the same checks on moved-constructed stanza
Stanza moved(std::move(copy));
});
CHECK(doc.size() <= std::numeric_limits<int>::max());
xml.feed(doc.data(), static_cast<int>(doc.size()), true);
}