Fixed offline contacts in OCS2007. Also other minor details.
[siplcs.git] / src / tests.c
blob3cda51761af088b790040fb6fcd5f63bcb7aa9eb
1 /**
2 * @file tests.c
4 * pidgin-sipe
6 * Copyright (C) 2008 Novell, Inc.
8 * Implemented with reference to the follow documentation:
9 * - http://davenport.sourceforge.net/ntlm.html
10 * - MS-NLMP: http://msdn.microsoft.com/en-us/library/cc207842.aspx
11 * - MS-SIP : http://msdn.microsoft.com/en-us/library/cc246115.aspx
13 * Build and run with `make tests`
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <glib.h>
31 #include <stdlib.h>
32 #include <zlib.h>
34 #include "sipe-sign.h"
35 #include "sip-ntlm.c"
36 #include "sipe.h"
38 #include "uuid.h"
40 static int successes = 0;
41 static int failures = 0;
43 void assert_equal(char * expected, char * got, int len, gboolean stringify)
45 gchar * res = got;
46 gchar to_str[len*2];
48 if (stringify) {
49 int i, j;
50 for (i = 0, j = 0; i < len; i++, j+=2) {
51 g_sprintf(&to_str[j], "%02X", (got[i]&0xff));
53 len *= 2;
54 res = to_str;
57 printf("expected: %s\n", expected);
58 printf("received: %s\n", res);
60 if (strncmp(expected, res, len) == 0) {
61 successes++;
62 printf("PASSED\n");
63 } else {
64 failures++;
65 printf("FAILED\n");
69 int main()
71 printf ("Starting Tests\n");
73 // Initialization that Pidgin would normally do
74 g_type_init();
75 purple_signals_init();
76 purple_util_init();
77 purple_debug_init();
78 purple_dbus_init();
79 purple_ciphers_init();
81 /* These tests are from the MS-SIPE document */
83 char * password = "Password";
84 char * user = "User";
85 char * domain = "Domain";
86 char nonce [] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
87 char exported_session_key[] = {0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
89 printf ("\nTesting LMOWFv1()\n");
90 char response_key_lm [16];
91 LMOWFv1 (password, user, domain, response_key_lm);
92 assert_equal("E52CAC67419A9A224A3B108F3FA6CB6D", response_key_lm, 16, TRUE);
94 printf ("\nTesting LM Response Generation\n");
95 char lm_challenge_response [24];
96 DESL (response_key_lm, nonce, lm_challenge_response);
97 assert_equal("98DEF7B87F88AA5DAFE2DF779688A172DEF11C7D5CCDEF13", lm_challenge_response, 24, TRUE);
99 printf ("\n\nTesting NTOWFv1()\n");
100 char response_key_nt [16];
101 NTOWFv1 (password, user, domain, response_key_nt);
102 assert_equal("A4F49C406510BDCAB6824EE7C30FD852", response_key_nt, 16, TRUE);
104 printf ("\nTesting NT Response Generation\n");
105 char nt_challenge_response [24];
106 DESL (response_key_nt, nonce, nt_challenge_response);
107 assert_equal("67C43011F30298A2AD35ECE64F16331C44BDBED927841F94", nt_challenge_response, 24, TRUE);
109 printf ("\n\nTesting Session Base Key and Key Exchange Generation\n");
110 char session_base_key [16];
111 MD4(response_key_nt, 16, session_base_key);
112 char key_exchange_key [16];
113 KXKEY(session_base_key, lm_challenge_response, key_exchange_key);
114 assert_equal("D87262B0CDE4B1CB7499BECCCDF10784", session_base_key, 16, TRUE);
115 assert_equal("D87262B0CDE4B1CB7499BECCCDF10784", key_exchange_key, 16, TRUE);
117 printf ("\n\nTesting Encrypted Session Key Generation\n");
118 char encrypted_random_session_key [16];
119 RC4K (key_exchange_key, exported_session_key, encrypted_random_session_key);
120 assert_equal("518822B1B3F350C8958682ECBB3E3CB7", encrypted_random_session_key, 16, TRUE);
122 /* End tests from the MS-SIPE document */
124 // Test from http://davenport.sourceforge.net/ntlm.html#ntlm1Signing
125 printf ("\n\nTesting Signature Algorithm\n");
126 char sk [] = {0x01, 0x02, 0x03, 0x04, 0x05, 0xe5, 0x38, 0xb0};
127 assert_equal (
128 "0100000078010900397420FE0E5A0F89",
129 purple_ntlm_gen_signature ("jCIFS", sk, 0x00090178, 0, 8),
130 32, FALSE
133 // Verify signature of SIPE message received from OCS 2007 after authenticating with pidgin-sipe
134 printf ("\n\nTesting MS-SIPE Example Message Signing\n");
135 char * msg1 = "<NTLM><0878F41B><1><SIP Communications Service><ocs1.ocs.provo.novell.com><8592g5DCBa1694i5887m0D0Bt2247b3F38xAE9Fx><3><REGISTER><sip:gabriel@ocs.provo.novell.com><2947328781><B816D65C2300A32CFA6D371F2AF537FD><900><200>";
136 char exported_session_key2 [] = { 0x5F, 0x02, 0x91, 0x53, 0xBC, 0x02, 0x50, 0x58, 0x96, 0x95, 0x48, 0x61, 0x5E, 0x70, 0x99, 0xBA };
137 assert_equal (
138 "0100000000000000BF2E52667DDF6DED",
139 purple_ntlm_gen_signature(msg1, exported_session_key2, 0, 100, 16),
140 32, FALSE
143 // Verify parsing of message and signature verification
144 printf ("\n\nTesting MS-SIPE Example Message Parsing, Signing, and Verification\n");
145 char * msg2 = "SIP/2.0 200 OK\r\nms-keep-alive: UAS; tcp=no; hop-hop=yes; end-end=no; timeout=300\r\nAuthentication-Info: NTLM rspauth=\"0100000000000000BF2E52667DDF6DED\", srand=\"0878F41B\", snum=\"1\", opaque=\"4452DFB0\", qop=\"auth\", targetname=\"ocs1.ocs.provo.novell.com\", realm=\"SIP Communications Service\"\r\nFrom: \"Gabriel Burt\"<sip:gabriel@ocs.provo.novell.com>;tag=2947328781;epid=1234567890\r\nTo: <sip:gabriel@ocs.provo.novell.com>;tag=B816D65C2300A32CFA6D371F2AF537FD\r\nCall-ID: 8592g5DCBa1694i5887m0D0Bt2247b3F38xAE9Fx\r\nCSeq: 3 REGISTER\r\nVia: SIP/2.0/TLS 164.99.194.49:10409;branch=z9hG4bKE0E37DBAF252C3255BAD;received=164.99.195.20;ms-received-port=10409;ms-received-cid=1E00\r\nContact: <sip:164.99.195.20:10409;transport=tls;ms-received-cid=1E00>;expires=900\r\nExpires: 900\r\nAllow-Events: vnd-microsoft-provisioning,vnd-microsoft-roaming-contacts,vnd-microsoft-roaming-ACL,presence,presence.wpending,vnd-microsoft-roaming-self,vnd-microsoft-provisioning-v2\r\nSupported: adhoclist\r\nServer: RTC/3.0\r\nSupported: com.microsoft.msrtc.presence\r\nContent-Length: 0\r\n\r\n";
146 struct sipmsg * msg = sipmsg_parse_msg(msg2);
147 struct sipmsg_breakdown msgbd;
148 msgbd.msg = msg;
149 sipmsg_breakdown_parse(&msgbd, "SIP Communications Service", "ocs1.ocs.provo.novell.com");
150 gchar * msg_str = sipmsg_breakdown_get_string(&msgbd);
151 gchar * sig = purple_ntlm_sipe_signature_make (msg_str, exported_session_key2);
152 sipmsg_breakdown_free(&msgbd);
153 assert_equal ("0100000000000000BF2E52667DDF6DED", sig, 32, FALSE);
154 printf("purple_ntlm_verify_signature result = %i\n", purple_ntlm_verify_signature (sig, "0100000000000000BF2E52667DDF6DED"));
157 /* begin tests from MS-SIPRE */
159 const char *testEpid = "01010101";
160 const char *expectedUUID = "4b1682a8-f968-5701-83fc-7c6741dc6697";
161 char *calcUUID = generateUUIDfromEPID(testEpid);
163 printf("\n\nTesting MS-SIPRE uuid derivation\n");
165 assert_equal(expectedUUID, calcUUID, strlen(expectedUUID), FALSE);
166 g_free(calcUUID);
168 guchar addr[6];
169 long mac = mac_addr_sys(addr);
170 gchar nmac[6];
172 int i,j;
173 for (i = 0,j=0; i < 6; i++,j+=2) {
174 g_sprintf(&nmac[j], "%02X", addr[i]);
177 printf("Mac: %s\n", g_strdup(nmac));
179 /* end tests from MS-SIPRE */
181 printf ("\nFinished With Tests; %d successs %d failures\n", successes, failures);