1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5 #include <stdlib.h>
6
7 #include "xmpp/jid.h"
8
9 void
create_jid_from_null_returns_null(void ** state)10 create_jid_from_null_returns_null(void** state)
11 {
12 Jid* result = jid_create(NULL);
13 assert_null(result);
14 }
15
16 void
create_jid_from_empty_string_returns_null(void ** state)17 create_jid_from_empty_string_returns_null(void** state)
18 {
19 Jid* result = jid_create("");
20 assert_null(result);
21 }
22
23 void
create_jid_from_full_returns_full(void ** state)24 create_jid_from_full_returns_full(void** state)
25 {
26 Jid* result = jid_create("myuser@mydomain/laptop");
27 assert_string_equal("myuser@mydomain/laptop", result->fulljid);
28 jid_destroy(result);
29 }
30
31 void
create_jid_from_full_returns_bare(void ** state)32 create_jid_from_full_returns_bare(void** state)
33 {
34 Jid* result = jid_create("myuser@mydomain/laptop");
35 assert_string_equal("myuser@mydomain", result->barejid);
36 jid_destroy(result);
37 }
38
39 void
create_jid_from_full_returns_resourcepart(void ** state)40 create_jid_from_full_returns_resourcepart(void** state)
41 {
42 Jid* result = jid_create("myuser@mydomain/laptop");
43 assert_string_equal("laptop", result->resourcepart);
44 jid_destroy(result);
45 }
46
47 void
create_jid_from_full_returns_localpart(void ** state)48 create_jid_from_full_returns_localpart(void** state)
49 {
50 Jid* result = jid_create("myuser@mydomain/laptop");
51 assert_string_equal("myuser", result->localpart);
52 jid_destroy(result);
53 }
54
55 void
create_jid_from_full_returns_domainpart(void ** state)56 create_jid_from_full_returns_domainpart(void** state)
57 {
58 Jid* result = jid_create("myuser@mydomain/laptop");
59 assert_string_equal("mydomain", result->domainpart);
60 jid_destroy(result);
61 }
62
63 void
create_jid_from_full_nolocal_returns_full(void ** state)64 create_jid_from_full_nolocal_returns_full(void** state)
65 {
66 Jid* result = jid_create("mydomain/laptop");
67 assert_string_equal("mydomain/laptop", result->fulljid);
68 jid_destroy(result);
69 }
70
71 void
create_jid_from_full_nolocal_returns_bare(void ** state)72 create_jid_from_full_nolocal_returns_bare(void** state)
73 {
74 Jid* result = jid_create("mydomain/laptop");
75 assert_string_equal("mydomain", result->barejid);
76 jid_destroy(result);
77 }
78
79 void
create_jid_from_full_nolocal_returns_resourcepart(void ** state)80 create_jid_from_full_nolocal_returns_resourcepart(void** state)
81 {
82 Jid* result = jid_create("mydomain/laptop");
83 assert_string_equal("laptop", result->resourcepart);
84 jid_destroy(result);
85 }
86
87 void
create_jid_from_full_nolocal_returns_domainpart(void ** state)88 create_jid_from_full_nolocal_returns_domainpart(void** state)
89 {
90 Jid* result = jid_create("mydomain/laptop");
91 assert_string_equal("mydomain", result->domainpart);
92 jid_destroy(result);
93 }
94
95 void
create_jid_from_full_nolocal_returns_null_localpart(void ** state)96 create_jid_from_full_nolocal_returns_null_localpart(void** state)
97 {
98 Jid* result = jid_create("mydomain/laptop");
99 assert_null(result->localpart);
100 jid_destroy(result);
101 }
102
103 void
create_jid_from_bare_returns_null_full(void ** state)104 create_jid_from_bare_returns_null_full(void** state)
105 {
106 Jid* result = jid_create("myuser@mydomain");
107 assert_null(result->fulljid);
108 jid_destroy(result);
109 }
110
111 void
create_jid_from_bare_returns_null_resource(void ** state)112 create_jid_from_bare_returns_null_resource(void** state)
113 {
114 Jid* result = jid_create("myuser@mydomain");
115 assert_null(result->resourcepart);
116 jid_destroy(result);
117 }
118
119 void
create_jid_from_bare_returns_bare(void ** state)120 create_jid_from_bare_returns_bare(void** state)
121 {
122 Jid* result = jid_create("myuser@mydomain");
123 assert_string_equal("myuser@mydomain", result->barejid);
124 jid_destroy(result);
125 }
126
127 void
create_jid_from_bare_returns_localpart(void ** state)128 create_jid_from_bare_returns_localpart(void** state)
129 {
130 Jid* result = jid_create("myuser@mydomain");
131 assert_string_equal("myuser", result->localpart);
132 jid_destroy(result);
133 }
134
135 void
create_jid_from_bare_returns_domainpart(void ** state)136 create_jid_from_bare_returns_domainpart(void** state)
137 {
138 Jid* result = jid_create("myuser@mydomain");
139 assert_string_equal("mydomain", result->domainpart);
140 jid_destroy(result);
141 }
142
143 void
create_room_jid_returns_room(void ** state)144 create_room_jid_returns_room(void** state)
145 {
146 Jid* result = jid_create_from_bare_and_resource("room@conference.domain.org", "myname");
147
148 assert_string_equal("room@conference.domain.org", result->barejid);
149 jid_destroy(result);
150 }
151
152 void
create_room_jid_returns_nick(void ** state)153 create_room_jid_returns_nick(void** state)
154 {
155 Jid* result = jid_create_from_bare_and_resource("room@conference.domain.org", "myname");
156
157 assert_string_equal("myname", result->resourcepart);
158 jid_destroy(result);
159 }
160
161 void
create_with_slash_in_resource(void ** state)162 create_with_slash_in_resource(void** state)
163 {
164 Jid* result = jid_create("room@conference.domain.org/my/nick");
165
166 assert_string_equal("room", result->localpart);
167 assert_string_equal("conference.domain.org", result->domainpart);
168 assert_string_equal("my/nick", result->resourcepart);
169 assert_string_equal("room@conference.domain.org", result->barejid);
170 assert_string_equal("room@conference.domain.org/my/nick", result->fulljid);
171
172 jid_destroy(result);
173 }
174
175 void
create_with_at_in_resource(void ** state)176 create_with_at_in_resource(void** state)
177 {
178 Jid* result = jid_create("room@conference.domain.org/my@nick");
179
180 assert_string_equal("room", result->localpart);
181 assert_string_equal("conference.domain.org", result->domainpart);
182 assert_string_equal("my@nick", result->resourcepart);
183 assert_string_equal("room@conference.domain.org", result->barejid);
184 assert_string_equal("room@conference.domain.org/my@nick", result->fulljid);
185
186 jid_destroy(result);
187 }
188
189 void
create_with_at_and_slash_in_resource(void ** state)190 create_with_at_and_slash_in_resource(void** state)
191 {
192 Jid* result = jid_create("room@conference.domain.org/my@nick/something");
193
194 assert_string_equal("room", result->localpart);
195 assert_string_equal("conference.domain.org", result->domainpart);
196 assert_string_equal("my@nick/something", result->resourcepart);
197 assert_string_equal("room@conference.domain.org", result->barejid);
198 assert_string_equal("room@conference.domain.org/my@nick/something", result->fulljid);
199
200 jid_destroy(result);
201 }
202
203 void
create_full_with_trailing_slash(void ** state)204 create_full_with_trailing_slash(void** state)
205 {
206 Jid* result = jid_create("room@conference.domain.org/nick/");
207
208 assert_string_equal("room", result->localpart);
209 assert_string_equal("conference.domain.org", result->domainpart);
210 assert_string_equal("nick/", result->resourcepart);
211 assert_string_equal("room@conference.domain.org", result->barejid);
212 assert_string_equal("room@conference.domain.org/nick/", result->fulljid);
213
214 jid_destroy(result);
215 }
216
217 void
returns_fulljid_when_exists(void ** state)218 returns_fulljid_when_exists(void** state)
219 {
220 Jid* jid = jid_create("localpart@domainpart/resourcepart");
221
222 char* result = jid_fulljid_or_barejid(jid);
223
224 assert_string_equal("localpart@domainpart/resourcepart", result);
225
226 jid_destroy(jid);
227 }
228
229 void
returns_barejid_when_fulljid_not_exists(void ** state)230 returns_barejid_when_fulljid_not_exists(void** state)
231 {
232 Jid* jid = jid_create("localpart@domainpart");
233
234 char* result = jid_fulljid_or_barejid(jid);
235
236 assert_string_equal("localpart@domainpart", result);
237
238 jid_destroy(jid);
239 }
240