1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/**
* Just a very simple test suite, by hand, using assert()
*/
#include <xmpp/xmpp_parser.hpp>
#include <utils/encoding.hpp>
#include <config/config.hpp>
#include <bridge/colors.hpp>
#include <utils/split.hpp>
#include <xmpp/jid.hpp>
#include <string.h>
#include <iostream>
#include <vector>
#include <assert.h>
int main()
{
/**
* Encoding
*/
const char* valid = "C̡͔͕̩͙̽ͫ̈́ͥ̿̆ͧ̚r̸̩̘͍̻͖̆͆͛͊̉̕͡o͇͈̳̤̱̊̈͢q̻͍̦̮͕ͥͬͬ̽ͭ͌̾ͅǔ͉͕͇͚̙͉̭͉̇̽ȇ͈̮̼͍͔ͣ͊͞͝ͅ ͫ̾ͪ̓ͥ̆̋̔҉̢̦̠͈͔̖̲̯̦ụ̶̯͐̃̋ͮ͆͝n̬̱̭͇̻̱̰̖̤̏͛̏̿̑͟ë́͐҉̸̥̪͕̹̻̙͉̰ ̹̼̱̦̥ͩ͑̈́͑͝ͅt͍̥͈̹̝ͣ̃̔̈̔ͧ̕͝ḙ̸̖̟̙͙ͪ͢ų̯̞̼̲͓̻̞͛̃̀́b̮̰̗̩̰̊̆͗̾̎̆ͯ͌͝.̗̙͎̦ͫ̈́ͥ͌̈̓ͬ";
assert(utils::is_valid_utf8(valid) == true);
const char* invalid = "\xF0\x0F";
assert(utils::is_valid_utf8(invalid) == false);
const char* invalid2 = "\xFE\xFE\xFF\xFF";
assert(utils::is_valid_utf8(invalid2) == false);
std::string in = "coucou les copains ♥ ";
assert(utils::is_valid_utf8(in.c_str()) == true);
std::string res = utils::convert_to_utf8(in, "UTF-8");
assert(utils::is_valid_utf8(res.c_str()) == true && res == in);
std::string original_utf8("couc¥ou");
std::string original_latin1("couc\xa5ou");
// When converting back to utf-8
std::string from_latin1 = utils::convert_to_utf8(original_latin1.c_str(), "ISO-8859-1");
assert(from_latin1 == original_utf8);
// Check the behaviour when the decoding fails (here because we provide a
// wrong charset)
std::string from_ascii = utils::convert_to_utf8(original_latin1, "US-ASCII");
assert(from_ascii == "couc�ou");
/**
* Utils
*/
std::vector<std::string> splitted = utils::split("a::a", ':', false);
assert(splitted.size() == 2);
splitted = utils::split("a::a", ':', true);
assert(splitted.size() == 3);
assert(splitted[0] == "a");
assert(splitted[1] == "");
assert(splitted[2] == "a");
splitted = utils::split("\na", '\n', true);
assert(splitted.size() == 2);
assert(splitted[0] == "");
assert(splitted[1] == "a");
/**
* XML parsing
*/
XmppParser xml;
const std::string doc = "<stream xmlns='stream_ns'><stanza b='c'>inner<child1><grandchild/></child1><child2 xmlns='child2_ns'/>tail</stanza></stream>";
xml.add_stanza_callback([](const Stanza& stanza)
{
assert(stanza.get_name() == "stream_ns:stanza");
assert(stanza["b"] == "c");
assert(stanza.get_inner() == "inner");
assert(stanza.get_tail() == "");
assert(stanza.get_child("stream_ns:child1") != nullptr);
assert(stanza.get_child("stream_ns:child2") == nullptr);
assert(stanza.get_child("child2_ns:child2") != nullptr);
assert(stanza.get_child("child2_ns:child2")->get_tail() == "tail");
});
xml.feed(doc.data(), doc.size(), true);
/**
* XML escape/escape
*/
const std::string unescaped = "'coucou'<cc>/&\"gaga\"";
assert(xml_escape(unescaped) == "'coucou'<cc>/&"gaga"");
assert(xml_unescape(xml_escape(unescaped)) == unescaped);
/**
* JID parsing
*/
// Full JID
Jid jid1("♥@ツ.coucou/coucou@coucou/coucou");
std::cerr << jid1.local << " @ " << jid1.domain << " / " << jid1.resource << std::endl;
assert(jid1.local == "♥");
assert(jid1.domain == "ツ.coucou");
assert(jid1.resource == "coucou@coucou/coucou");
// Domain and resource
Jid jid2("ツ.coucou/coucou@coucou/coucou");
std::cerr << jid2.local << " @ " << jid2.domain << " / " << jid2.resource << std::endl;
assert(jid2.local == "");
assert(jid2.domain == "ツ.coucou");
assert(jid2.resource == "coucou@coucou/coucou");
/**
* Config
*/
Config::filename = "test.cfg";
Config::file_must_exist = false;
Config::set("coucou", "bonjour");
Config::close();
bool error = false;
try
{
Config::file_must_exist = true;
assert(Config::get("coucou", "") == "bonjour");
assert(Config::get("does not exist", "default") == "default");
Config::close();
}
catch (const std::ios::failure& e)
{
error = true;
}
assert(error == false);
return 0;
}
|