purple: rename Iface -> Interface in 3.x.x API
[siplcs.git] / src / core / sipe-sipmsg-tests.c
blob8704361fb47c8fffe7243c1ffbdeea4c1a00c040
1 /**
2 * @file sipe-sipmsg-tests.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2019 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Some non-NTLM tests code was factored out from sipe-sec-ntlm-test.c:
25 *------------- Copyright notices from "sipe-sec-ntlm-tests.c" -------------
26 * Copyright (C) 2011-2016 SIPE Project <http://sipe.sourceforge.net/>
27 * Copyright (C) 2010 pier11 <pier11@operamail.com>
28 * Copyright (C) 2008 Novell, Inc.
29 *------------- Copyright notices from "sipe-sec-ntlm-tests.c" -------------
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <stdint.h>
40 #include <glib.h>
42 #include "sipe-common.h"
43 #include "sipe-backend.h"
44 #include "sipe-crypt.h"
45 #include "sipe-mime.h"
46 #include "sipe-rtf.h"
47 #include "sipe-sign.h"
48 #include "sipe-utils.h"
49 #include "sip-transport.h"
50 #include "sipmsg.h"
52 #include "uuid.h"
55 * Stubs
57 gboolean sipe_backend_debug_enabled(void)
59 return(TRUE);
62 void sipe_backend_debug_literal(sipe_debug_level level,
63 const gchar *msg)
65 printf("DEBUG(%d): %s\n", level, msg);
68 void sipe_backend_debug(sipe_debug_level level,
69 const gchar *format,
70 ...)
72 va_list ap;
73 gchar *newformat = g_strdup_printf("DEBUG(%d): %s\n", level, format);
75 va_start(ap, format);
76 vprintf(newformat, ap);
77 va_end(ap);
79 g_free(newformat);
82 gchar *sipe_backend_markup_css_property(SIPE_UNUSED_PARAMETER const gchar *style,
83 SIPE_UNUSED_PARAMETER const gchar *option)
85 return(NULL);
88 void sipe_mime_parts_foreach(SIPE_UNUSED_PARAMETER const gchar *type,
89 SIPE_UNUSED_PARAMETER const gchar *body,
90 SIPE_UNUSED_PARAMETER sipe_mime_parts_cb callback,
91 SIPE_UNUSED_PARAMETER gpointer user_data)
95 gchar *sipe_rtf_to_html(SIPE_UNUSED_PARAMETER const gchar *rtf)
97 return(NULL);
100 const gchar *sip_transport_epid(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private)
102 return(NULL);
105 const gchar *sip_transport_ip_address(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private)
107 return(NULL);
110 /* needed when linking against NSS */
111 void md4sum(const uint8_t *data, uint32_t length, uint8_t *digest);
112 void md4sum(SIPE_UNUSED_PARAMETER const uint8_t *data,
113 SIPE_UNUSED_PARAMETER uint32_t length,
114 SIPE_UNUSED_PARAMETER uint8_t *digest)
119 * Tester code
121 static guint succeeded = 0;
122 static guint failed = 0;
124 static void assert_equal(const char *expected, const gchar *got)
126 if (sipe_strequal(expected, got)) {
127 succeeded++;
128 } else {
129 printf("FAILED: %s\n %s\n", got, expected);
130 failed++;
134 static void msg_tests(void) {
135 /* Address parsing */
137 const gchar *responses[] = {
138 "SIP/2.0 200 OK\r\n"
139 "From: \"J.D. User\" <sip:foo.bar@company.com>;something else\r\n"
140 "To: \"Test Recipient\" <SIP:test@recipient.com>\r\n"
141 "X-Some-Header: <SiP:joe.header@test.com>\r\n"
142 "Content-Length: 0\r\n"
143 "\r\n",
144 "SIP/2.0 200 OK\r\n"
145 "From: sip:foo.bar@company.com;something else\r\n"
146 "To: SIP:test@recipient.com\r\n"
147 "X-Some-Header: SiP:joe.header@test.com\r\n"
148 "Content-Length: 0\r\n"
149 "\r\n",
150 NULL
152 const gchar **resp;
154 for (resp = responses; *resp; resp++) {
155 struct sipmsg *msg = sipmsg_parse_msg(*resp);
156 gchar *address;
158 address = sipmsg_parse_from_address(msg);
159 assert_equal("sip:foo.bar@company.com", address);
160 g_free(address);
162 address = sipmsg_parse_to_address(msg);
163 assert_equal("SIP:test@recipient.com", address);
164 g_free(address);
166 address = sipmsg_parse_address_from_header(msg, "x-some-header");
167 assert_equal("SiP:joe.header@test.com", address);
168 g_free(address);
170 sipmsg_free(msg);
174 /* P-Asserted-Identity parsing */
176 const struct {
177 const gchar *header;
178 const gchar *sip_uri;
179 const gchar *tel_uri;
180 } testcases[] = {
182 "\"Cullen Jennings\" <sip:fluffy@cisco.com>",
183 "sip:fluffy@cisco.com",
184 NULL
187 "tel:+14085264000",
188 NULL,
189 "tel:+14085264000"
192 "\"Lunch, Lucas\" <sip:llucas@cisco.com>,<tel:+420123456;ext=88463>",
193 "sip:llucas@cisco.com",
194 "tel:+420123456;ext=88463"
197 NULL,
198 NULL,
199 NULL
201 }, *testcase;
203 for (testcase = testcases; testcase->header; testcase++) {
204 gchar *sip_uri = NULL;
205 gchar *tel_uri = NULL;
207 sipmsg_parse_p_asserted_identity(testcase->header,
208 &sip_uri,
209 &tel_uri);
210 assert_equal(testcase->sip_uri, sip_uri);
211 assert_equal(testcase->tel_uri, tel_uri);
213 g_free(tel_uri);
214 g_free(sip_uri);
218 /* Test Authentication Algorithm's v4 Signature String */
220 const gchar *response =
221 "SIP/2.0 180 Ringing\r\n"
222 "Authentication-Info: NTLM rspauth=\"010000003EA8D688BA51D5CD64000000\", srand=\"1B6D47A1\", snum=\"11\", opaque=\"357E6F72\", qop=\"auth\", targetname=\"LOC-COMPANYT-FE03.COMPANY.COM\", realm=\"SIP Communications Service\"\r\n"
223 "Via: SIP/2.0/tls 192.168.44.10:50230;received=10.117.245.254;ms-received-port=50230;ms-received-cid=37ABE00\r\n"
224 "FROM: \"Sender\"<sip:sender@company.com>;tag=2420628112;epid=54392f1bbf01\r\n"
225 "TO: \"recipient\"<sip:recipient@company.com>;tag=7aee15546a;epid=3102EB8BD1\r\n"
226 "CSEQ: 1 INVITE\r\n"
227 "CALL-ID: 41CEg82ECa0AC8i3DD7mE673t9CF4b19DAxF780x\r\n"
228 "RECORD-ROUTE: <sip:LOC-COMPANYT-OCSR2P01.COMPANY.COM:5061;transport=tls;ms-fe=LOC-COMPANYT-FE03.COMPANY.COM;opaque=state:F:T:Eu:Ci.R37abe00;lr;ms-route-sig=gdOGgL7NiL3hv_oBc0NdrJOxZk_r-8naq-k_DtpgAA>\r\n"
229 "CONTACT: <sip:recipient@company.com;opaque=user:epid:-gLwenLTVVqy-Ak8TJn1ZAAA;gruu>;text;audio;video\r\n"
230 "CONTENT-LENGTH: 0\r\n"
231 "SUPPORTED: gruu-10\r\n"
232 "ALLOW: UPDATE\r\n"
233 "P-ASSERTED-IDENTITY: \"recipient\"<SIP:recipient@company.com>\r\n"
234 "SERVER: RTCC/3.5.0.0 MCXService/3.5.0.0 communicator.NOKIAS60R2.JVP.EN_US/1.0.6875.0\r\n"
235 "\r\n";
236 const gchar *response_sig = "<NTLM><1B6D47A1><11><SIP Communications Service><LOC-COMPANYT-FE03.COMPANY.COM><41CEg82ECa0AC8i3DD7mE673t9CF4b19DAxF780x><1><INVITE><sip:sender@company.com><2420628112><sip:recipient@company.com><7aee15546a><SIP:recipient@company.com><><><180>";
237 struct sipmsg_breakdown msgbd;
238 gchar *msg_str;
240 msgbd.msg = sipmsg_parse_msg(response);
241 sipmsg_breakdown_parse(&msgbd,
242 "SIP Communications Service",
243 "LOC-COMPANYT-FE03.COMPANY.COM",
244 NULL);
245 msg_str = sipmsg_breakdown_get_string(4, &msgbd);
247 assert_equal(response_sig, msg_str);
249 g_free(msg_str);
250 sipmsg_free(msgbd.msg);
251 sipmsg_breakdown_free(&msgbd);
254 /* Test parsing of address fields where URIs wrapped in "<...>" */
256 const gchar *response =
257 "SIP/2.0 180 Ringing\r\n"
258 "Authentication-Info: NTLM rspauth=\"010000003EA8D688BA51D5CD64000000\", srand=\"1B6D47A1\", snum=\"11\", opaque=\"357E6F72\", qop=\"auth\", targetname=\"bar\", realm=\"foo\"\r\n"
259 "From: sip:sender@company.com;tag=2420628112;epid=54392f1bbf01\r\n"
260 "To: sip:recipient@company.com;tag=7aee15546a;epid=3102EB8BD1\r\n"
261 "CSeq: 1 INVITE\r\n"
262 "Call-ID: 41CEg82ECa0AC8i3DD7mE673t9CF4b19DAxF780x\r\n"
263 "Content-Length: 0\r\n"
264 "P-Asserted-Identity: <SIP:recipient@company.com>\r\n"
265 "\r\n";
266 const gchar *response_sig = "<NTLM><1B6D47A1><11><foo><bar><41CEg82ECa0AC8i3DD7mE673t9CF4b19DAxF780x><1><INVITE><sip:sender@company.com><2420628112><sip:recipient@company.com><7aee15546a><SIP:recipient@company.com><><><180>";
267 struct sipmsg_breakdown msgbd;
268 gchar *msg_str;
270 msgbd.msg = sipmsg_parse_msg(response);
271 sipmsg_breakdown_parse(&msgbd,
272 "foo",
273 "bar",
274 NULL);
275 msg_str = sipmsg_breakdown_get_string(4, &msgbd);
277 assert_equal(response_sig, msg_str);
279 g_free(msg_str);
280 sipmsg_free(msgbd.msg);
281 sipmsg_breakdown_free(&msgbd);
284 /* UUID tests - begin tests from MS-SIPRE */
286 const char *testEpid = "01010101";
287 const char *expectedUUID = "4b1682a8-f968-5701-83fc-7c6741dc6697";
288 gchar *gotUUID = generateUUIDfromEPID(testEpid);
290 assert_equal(expectedUUID, gotUUID);
292 g_free(gotUUID);
296 int main(SIPE_UNUSED_PARAMETER int argc,
297 SIPE_UNUSED_PARAMETER char *argv[])
299 /* Initialization for crypto backend (test mode) */
300 sipe_crypto_init(FALSE);
302 msg_tests();
304 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
305 return(failed);
309 Local Variables:
310 mode: c
311 c-file-style: "bsd"
312 indent-tabs-mode: t
313 tab-width: 8
314 End: