Debian Codegraph @

xmpp-vala/tests/stanza.vala

raw
1 namespace Xmpp.Test {
2
3 class StanzaTest : Gee.TestCase {
4 public StanzaTest() {
5 base("Stanza");
6
7 add_async_test("node_one", (cb) => { test_node_one.begin(cb); });
8 add_async_test("typical_stream", (cb) => { test_typical_stream.begin(cb); });
9 add_async_test("ack_stream", (cb) => { test_ack_stream.begin(cb); });
10 add_test("get_attribute_(u)int", test_get_attribute_int);
11 }
12
13 private async void test_node_one(Gee.TestFinishedCallback cb) {
14 var node1 = new StanzaNode.build("test", "ns1_uri")
15 .add_self_xmlns()
16 .put_attribute("ns2", "ns2_uri", XMLNS_URI)
17 .put_attribute("bla", "blub")
18 .put_node(new StanzaNode.build("testaa", "ns2_uri")
19 .put_attribute("ns3", "ns3_uri", XMLNS_URI))
20 .put_node(new StanzaNode.build("testbb", "ns3_uri")
21 .add_self_xmlns());
22
23 var xml1 = node1.to_xml();
24 var node2 = yield new StanzaReader.for_string(xml1).read_node();
25 fail_if_not(node1.equals(node2));
26 fail_if_not_eq_str(node1.to_string(), node2.to_string());
27 cb();
28 }
29
30 private async void test_typical_stream(Gee.TestFinishedCallback cb) {
31 var stream = """
32 <?xml version='1.0' encoding='UTF-8'?>
33 <stream:stream
34 to='example.com'
35 xmlns='jabber:client'
36 xmlns:stream='http://etherx.jabber.org/streams'
37 version='1.0'>
38 <message from='[email protected]/churchyard'
39 to='[email protected]'
40 xml:lang='en'>
41 <body> I'll send a friar with speed, to Mantua, with my letters to thy lord.</body>
42 </message>
43 </stream:stream>
44 """;
45 var root_node_cmp = new StanzaNode.build("stream", "http://etherx.jabber.org/streams")
46 .put_attribute("to", "example.com")
47 .put_attribute("xmlns", "jabber:client")
48 .put_attribute("stream", "http://etherx.jabber.org/streams", XMLNS_URI)
49 .put_attribute("version", "1.0");
50 var node_cmp = new StanzaNode.build("message")
51 .put_attribute("from", "[email protected]/churchyard")
52 .put_attribute("to", "[email protected]")
53 .put_attribute("lang", "en", XML_URI)
54 .put_node(new StanzaNode.build("body")
55 .put_node(new StanzaNode.text(" I'll send a friar with speed, to Mantua, with my letters to thy lord.")));
56
57 var reader = new StanzaReader.for_string(stream);
58 fail_if_not_eq_node(root_node_cmp, yield reader.read_root_node());
59 fail_if_not_eq_node(node_cmp, yield reader.read_node());
60 yield reader.read_node();
61 yield fail_if_not_end_of_stream(reader);
62 cb();
63 }
64
65 private async void fail_if_not_end_of_stream(StanzaReader reader) {
66 try {
67 yield reader.read_node();
68 fail_if_reached("end of stream should be reached");
69 } catch (IOError.CLOSED e) {
70 return;
71 } catch (Error e) {
72 fail_if_reached("Unexpected error");
73 }
74 }
75
76 private async void test_ack_stream(Gee.TestFinishedCallback cb) {
77 var stream = """
78 <?xml version='1.0' encoding='UTF-8'?>
79 <stream:stream
80 to='example.com'
81 xmlns='jabber:client'
82 xmlns:stream='http://etherx.jabber.org/streams'
83 xmlns:ack='http://jabber.org/protocol/ack'
84 version='1.0'>
85 <stream:features>
86 <ack:ack/>
87 <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>
88 <required/>
89 </bind>
90 </stream:features>
91 <ack:r/>
92 </stream:stream>
93 """;
94 var root_node_cmp = new StanzaNode.build("stream", "http://etherx.jabber.org/streams")
95 .put_attribute("to", "example.com")
96 .put_attribute("xmlns", "jabber:client")
97 .put_attribute("stream", "http://etherx.jabber.org/streams", XMLNS_URI)
98 .put_attribute("ack", "http://jabber.org/protocol/ack", XMLNS_URI)
99 .put_attribute("version", "1.0");
100 var node_cmp = new StanzaNode.build("features", XmppStream.NS_URI)
101 .put_node(new StanzaNode.build("ack", "http://jabber.org/protocol/ack"))
102 .put_node(new StanzaNode.build("bind", "urn:ietf:params:xml:ns:xmpp-bind")
103 .add_self_xmlns()
104 .put_node(new StanzaNode.build("required", "urn:ietf:params:xml:ns:xmpp-bind")));
105 var node2_cmp = new StanzaNode.build("r", "http://jabber.org/protocol/ack");
106
107 var reader = new StanzaReader.for_string(stream);
108 fail_if_not_eq_node(root_node_cmp, yield reader.read_root_node());
109 fail_if_not_eq_node(node_cmp, yield reader.read_node());
110 fail_if_not_eq_node(node2_cmp, yield reader.read_node());
111 yield reader.read_node();
112 yield fail_if_not_end_of_stream(reader);
113 cb();
114 }
115
116 private void test_get_attribute_int() {
117 var stanza_node = new StanzaNode.build("test", "ns").add_self_xmlns().put_attribute("bar", "42");
118 fail_if_not_eq_int(stanza_node.get_attribute_int("bar", -2), 42);
119 fail_if_not_eq_uint(stanza_node.get_attribute_uint("bar", 3), 42);
120
121 stanza_node = new StanzaNode.build("test", "ns").add_self_xmlns().put_attribute("bar", "-42");
122 fail_if_not_eq_int(stanza_node.get_attribute_int("bar", -2), -42);
123 fail_if_not_eq_uint(stanza_node.get_attribute_uint("bar", 3), 3);
124
125 stanza_node = new StanzaNode.build("test", "ns").add_self_xmlns();
126 fail_if_not_eq_int(stanza_node.get_attribute_int("bar", -2), -2);
127 fail_if_not_eq_uint(stanza_node.get_attribute_uint("bar", 3), 3);
128
129 stanza_node = new StanzaNode.build("test", "ns").add_self_xmlns().put_attribute("bar", "0x42");
130 fail_if_not_eq_int(stanza_node.get_attribute_int("bar", -2), -2);
131 fail_if_not_eq_uint(stanza_node.get_attribute_uint("bar", 3), 3);
132
133 stanza_node = new StanzaNode.build("test", "ns").add_self_xmlns().put_attribute("bar", "str");
134 fail_if_not_eq_int(stanza_node.get_attribute_int("bar", -2), -2);
135 fail_if_not_eq_uint(stanza_node.get_attribute_uint("bar", 3), 3);
136
137 }
138
139 }
140
141 }