2003-04-01 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-auth-script.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-auth-script.c Test DBusAuth using a special script file (internal to D-BUS implementation)
3  * 
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23 #include <config.h>
24
25 #ifdef DBUS_BUILD_TESTS
26
27 #include "dbus-auth-script.h"
28 #include "dbus-auth.h"
29 #include "dbus-string.h"
30 #include "dbus-hash.h"
31 #include "dbus-internals.h"
32 #include "dbus-marshal.h"
33
34 /**
35  * @defgroup DBusAuthScript code for running unit test scripts for DBusAuth
36  * @ingroup  DBusInternals
37  * @brief DBusAuth unit test scripting
38  *
39  * The code in here is used for unit testing, it loads
40  * up a script that tests DBusAuth.
41  *
42  * @{
43  */
44
45 /* this is slightly different from the other append_quoted_string
46  * in dbus-message-builder.c
47  */
48 static dbus_bool_t
49 append_quoted_string (DBusString       *dest,
50                       const DBusString *quoted)
51 {
52   dbus_bool_t in_quotes = FALSE;
53   dbus_bool_t in_backslash = FALSE;
54   int i;
55
56   i = 0;
57   while (i < _dbus_string_get_length (quoted))
58     {
59       unsigned char b;
60
61       b = _dbus_string_get_byte (quoted, i);
62
63       if (in_backslash)
64         {
65           unsigned char a;
66           
67           if (b == 'r')
68             a = '\r';
69           else if (b == 'n')
70             a = '\n';
71           else if (b == '\\')
72             a = '\\';
73           else
74             {
75               _dbus_warn ("bad backslashed byte %c\n", b);
76               return FALSE;
77             }
78
79           if (!_dbus_string_append_byte (dest, a))
80             return FALSE;
81           
82           in_backslash = FALSE;
83         }
84       else if (b == '\\')
85         {
86           in_backslash = TRUE;
87         }
88       else if (in_quotes)
89         {
90           if (b == '\'')
91             in_quotes = FALSE;
92           else
93             {
94               if (!_dbus_string_append_byte (dest, b))
95                 return FALSE;
96             }
97         }
98       else
99         {
100           if (b == '\'')
101             in_quotes = TRUE;
102           else if (b == ' ' || b == '\n' || b == '\t')
103             break; /* end on whitespace if not quoted */
104           else
105             {
106               if (!_dbus_string_append_byte (dest, b))
107                 return FALSE;
108             }
109         }
110       
111       ++i;
112     }
113
114   return TRUE;
115 }
116
117 static dbus_bool_t
118 same_first_word (const DBusString *a,
119                  const DBusString *b)
120 {
121   int first_a_blank, first_b_blank;
122
123   _dbus_string_find_blank (a, 0, &first_a_blank);
124   _dbus_string_find_blank (b, 0, &first_b_blank);
125
126   if (first_a_blank != first_b_blank)
127     return FALSE;
128
129   return _dbus_string_equal_len (a, b, first_a_blank);
130 }
131
132 static DBusAuthState
133 auth_state_from_string (const DBusString *str)
134
135   if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_INPUT"))
136     return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
137   else if (_dbus_string_starts_with_c_str (str, "WAITING_FOR_MEMORY"))
138     return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
139   else if (_dbus_string_starts_with_c_str (str, "HAVE_BYTES_TO_SEND"))
140     return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
141   else if (_dbus_string_starts_with_c_str (str, "NEED_DISCONNECT"))
142     return DBUS_AUTH_STATE_NEED_DISCONNECT;
143   else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED_WITH_UNUSED_BYTES"))
144     return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
145   else if (_dbus_string_starts_with_c_str (str, "AUTHENTICATED"))
146     return DBUS_AUTH_STATE_AUTHENTICATED;
147   else
148     return -1;
149 }
150
151 static const char*
152 auth_state_to_string (DBusAuthState state)
153 {
154   switch (state)
155     {
156     case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
157       return "WAITING_FOR_INPUT";
158     case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
159       return "WAITING_FOR_MEMORY";
160     case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
161       return "HAVE_BYTES_TO_SEND";
162     case DBUS_AUTH_STATE_NEED_DISCONNECT:
163       return "NEED_DISCONNECT";
164     case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
165       return "AUTHENTICATED_WITH_UNUSED_BYTES";
166     case DBUS_AUTH_STATE_AUTHENTICATED:
167       return "AUTHENTICATED";
168     }
169
170   return "unknown";
171 }
172
173 /**
174  * Runs an "auth script" which is a script for testing the
175  * authentication protocol. Scripts send and receive data, and then
176  * include assertions about the state of both ends of the connection
177  * after processing the data. A script succeeds if these assertions
178  * hold.
179  *
180  * @param filename the file containing the script to run
181  * @returns #TRUE if the script succeeds, #FALSE otherwise
182  */
183 dbus_bool_t
184 _dbus_auth_script_run (const DBusString *filename)
185 {
186   DBusString file;
187   DBusError error;
188   DBusString line;
189   dbus_bool_t retval;
190   int line_no;
191   DBusAuth *auth;
192   DBusString from_auth;
193   DBusAuthState state;
194   DBusString context;
195   
196   retval = FALSE;
197   auth = NULL;
198
199   _dbus_string_init_const (&context, "org_freedesktop_test");
200   
201   if (!_dbus_string_init (&file))
202     return FALSE;
203
204   if (!_dbus_string_init (&line))
205     {
206       _dbus_string_free (&file);
207       return FALSE;
208     }
209
210   if (!_dbus_string_init (&from_auth))
211     {
212       _dbus_string_free (&file);
213       _dbus_string_free (&line);
214       return FALSE;
215     }
216
217   dbus_error_init (&error);
218   if (!_dbus_file_get_contents (&file, filename, &error))    {
219       _dbus_warn ("Getting contents of %s failed: %s\n",
220                   _dbus_string_get_const_data (filename), error.message);
221       dbus_error_free (&error);
222       goto out;
223     }
224
225   state = DBUS_AUTH_STATE_NEED_DISCONNECT;
226   line_no = 0;
227  next_iteration:
228   while (_dbus_string_pop_line (&file, &line))
229     {      
230       line_no += 1;
231
232       _dbus_string_delete_leading_blanks (&line);
233
234       if (auth != NULL)
235         {
236           while ((state = _dbus_auth_do_work (auth)) ==
237                  DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
238             {
239               const DBusString *tmp;
240               if (_dbus_auth_get_bytes_to_send (auth, &tmp))
241                 {
242                   int count = _dbus_string_get_length (tmp);
243
244                   if (_dbus_string_copy (tmp, 0, &from_auth,
245                                          _dbus_string_get_length (&from_auth)))
246                     _dbus_auth_bytes_sent (auth, count);
247                 }
248             }
249         }
250       
251       if (_dbus_string_get_length (&line) == 0)
252         {
253           /* empty line */
254           goto next_iteration;
255         }
256       else if (_dbus_string_starts_with_c_str (&line,
257                                                "#"))
258         {
259           /* Ignore this comment */
260           goto next_iteration;
261         }
262       else if (_dbus_string_starts_with_c_str (&line,
263                                                "CLIENT"))
264         {
265           DBusCredentials creds;
266           
267           if (auth != NULL)
268             {
269               _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
270               goto out;
271             }
272
273           auth = _dbus_auth_client_new ();
274           if (auth == NULL)
275             {
276               _dbus_warn ("no memory to create DBusAuth\n");
277               goto out;
278             }
279
280           _dbus_credentials_from_current_process (&creds);
281           _dbus_auth_set_credentials (auth, &creds);
282         }
283       else if (_dbus_string_starts_with_c_str (&line,
284                                                "SERVER"))
285         {
286           DBusCredentials creds;
287           
288           if (auth != NULL)
289             {
290               _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
291               goto out;
292             }
293
294           auth = _dbus_auth_server_new ();
295           if (auth == NULL)
296             {
297               _dbus_warn ("no memory to create DBusAuth\n");
298               goto out;
299             }
300
301           _dbus_credentials_from_current_process (&creds);
302           _dbus_auth_set_credentials (auth, &creds);
303           _dbus_auth_set_context (auth, &context);
304         }
305       else if (auth == NULL)
306         {
307           _dbus_warn ("must specify CLIENT or SERVER\n");
308           goto out;
309
310         }
311       else if (_dbus_string_starts_with_c_str (&line,
312                                                "NO_CREDENTIALS"))
313         {
314           DBusCredentials creds = { -1, -1, -1 };
315           _dbus_auth_set_credentials (auth, &creds);
316         }
317       else if (_dbus_string_starts_with_c_str (&line,
318                                                "ROOT_CREDENTIALS"))
319         {
320           DBusCredentials creds = { -1, 0, 0 };
321           _dbus_auth_set_credentials (auth, &creds);          
322         }
323       else if (_dbus_string_starts_with_c_str (&line,
324                                                "SILLY_CREDENTIALS"))
325         {
326           DBusCredentials creds = { -1, 4312, 1232 };
327           _dbus_auth_set_credentials (auth, &creds);          
328         }
329       else if (_dbus_string_starts_with_c_str (&line,
330                                                "SEND"))
331         {
332           DBusString to_send;
333           
334           _dbus_string_delete_first_word (&line);
335
336           if (!_dbus_string_init (&to_send))
337             {
338               _dbus_warn ("no memory to allocate string\n");
339               goto out;
340             }
341
342           if (!append_quoted_string (&to_send, &line))
343             {
344               _dbus_warn ("failed to append quoted string line %d\n",
345                           line_no);
346               _dbus_string_free (&to_send);
347               goto out;
348             }
349
350           _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
351           
352           if (!_dbus_string_append (&to_send, "\r\n"))
353             {
354               _dbus_warn ("failed to append \r\n from line %d\n",
355                           line_no);
356               _dbus_string_free (&to_send);
357               goto out;
358             }
359
360           /* Replace USERID_BASE64 with our username in base64 */
361           {
362             int where;
363             
364             if (_dbus_string_find (&to_send, 0,
365                                    "USERID_BASE64", &where))
366               {
367                 DBusString username;
368
369                 if (!_dbus_string_init (&username))
370                   {
371                     _dbus_warn ("no memory for userid\n");
372                     _dbus_string_free (&to_send);
373                     goto out;
374                   }
375
376                 if (!_dbus_string_append_our_uid (&username))
377                   {
378                     _dbus_warn ("no memory for userid\n");
379                     _dbus_string_free (&username);
380                     _dbus_string_free (&to_send);
381                     goto out;
382                   }
383
384                 _dbus_string_delete (&to_send, where, strlen ("USERID_BASE64"));
385                 
386                 if (!_dbus_string_base64_encode (&username, 0,
387                                                  &to_send, where))
388                   {
389                     _dbus_warn ("no memory to subst USERID_BASE64\n");
390                     _dbus_string_free (&username);
391                     _dbus_string_free (&to_send);
392                     goto out;
393                   }
394
395                 _dbus_string_free (&username);
396               }
397             else if (_dbus_string_find (&to_send, 0,
398                                         "USERNAME_BASE64", &where))
399               {
400                 DBusString username;
401                 const DBusString *u;
402                 
403                 if (!_dbus_string_init (&username))
404                   {
405                     _dbus_warn ("no memory for username\n");
406                     _dbus_string_free (&to_send);
407                     goto out;
408                   }
409
410                 if (!_dbus_user_info_from_current_process (&u, NULL, NULL) ||
411                     !_dbus_string_copy (u, 0, &username,
412                                         _dbus_string_get_length (&username)))
413                   {
414                     _dbus_warn ("no memory for username\n");
415                     _dbus_string_free (&username);
416                     _dbus_string_free (&to_send);
417                     goto out;
418                   }
419
420                 _dbus_string_delete (&to_send, where, strlen ("USERNAME_BASE64"));
421                 
422                 if (!_dbus_string_base64_encode (&username, 0,
423                                                  &to_send, where))
424                   {
425                     _dbus_warn ("no memory to subst USERNAME_BASE64\n");
426                     _dbus_string_free (&username);
427                     _dbus_string_free (&to_send);
428                     goto out;
429                   }
430
431                 _dbus_string_free (&username);
432               }
433           }
434
435           {
436             DBusString *buffer;
437
438             _dbus_auth_get_buffer (auth, &buffer);
439             if (!_dbus_string_copy (&to_send, 0,
440                                     buffer, _dbus_string_get_length (buffer)))
441               {
442                 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
443                 _dbus_string_free (&to_send);
444                 _dbus_auth_return_buffer (auth, buffer, 0);
445                 goto out;
446               }
447
448             _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
449           }
450           
451           _dbus_string_free (&to_send);
452         }
453       else if (_dbus_string_starts_with_c_str (&line,
454                                                "EXPECT_STATE"))
455         {
456           DBusAuthState expected;
457           
458           _dbus_string_delete_first_word (&line);
459
460           expected = auth_state_from_string (&line);
461           if (expected < 0)
462             {
463               _dbus_warn ("bad auth state given to EXPECT_STATE\n");
464               goto parse_failed;
465             }
466
467           if (expected != state)
468             {
469               _dbus_warn ("expected auth state %s but got %s on line %d\n",
470                           auth_state_to_string (expected),
471                           auth_state_to_string (state),
472                           line_no);
473               goto out;
474             }
475         }
476       else if (_dbus_string_starts_with_c_str (&line,
477                                                "EXPECT_COMMAND"))
478         {
479           DBusString received;
480           
481           _dbus_string_delete_first_word (&line);
482
483           if (!_dbus_string_init (&received))
484             {
485               _dbus_warn ("no mem to allocate string received\n");
486               goto out;
487             }
488
489           if (!_dbus_string_pop_line (&from_auth, &received))
490             {
491               _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
492                           _dbus_string_get_const_data (&line), line_no);
493               _dbus_string_free (&received);
494               goto out;
495             }
496
497           if (!same_first_word (&received, &line))
498             {
499               _dbus_warn ("line %d expected command '%s' and got '%s'\n",
500                           line_no,
501                           _dbus_string_get_const_data (&line),
502                           _dbus_string_get_const_data (&received));
503               _dbus_string_free (&received);
504               goto out;
505             }
506           
507           _dbus_string_free (&received);
508         }
509       else if (_dbus_string_starts_with_c_str (&line,
510                                                "EXPECT_UNUSED"))
511         {
512           DBusString expected;
513           const DBusString *unused;
514           
515           _dbus_string_delete_first_word (&line);
516
517           if (!_dbus_string_init (&expected))
518             {
519               _dbus_warn ("no mem to allocate string expected\n");
520               goto out;
521             }
522
523           if (!append_quoted_string (&expected, &line))
524             {
525               _dbus_warn ("failed to append quoted string line %d\n",
526                           line_no);
527               _dbus_string_free (&expected);
528               goto out;
529             }
530
531           _dbus_auth_get_unused_bytes (auth, &unused);
532           
533           if (_dbus_string_equal (&expected, unused))
534             {
535               _dbus_auth_delete_unused_bytes (auth);
536               _dbus_string_free (&expected);
537             }
538           else
539             {
540               _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
541                           _dbus_string_get_const_data (&expected),
542                           _dbus_string_get_const_data (unused));
543               _dbus_string_free (&expected);
544               goto out;
545             }
546         }
547       else if (_dbus_string_starts_with_c_str (&line,
548                                                "EXPECT"))
549         {
550           DBusString expected;
551           
552           _dbus_string_delete_first_word (&line);
553
554           if (!_dbus_string_init (&expected))
555             {
556               _dbus_warn ("no mem to allocate string expected\n");
557               goto out;
558             }
559
560           if (!append_quoted_string (&expected, &line))
561             {
562               _dbus_warn ("failed to append quoted string line %d\n",
563                           line_no);
564               _dbus_string_free (&expected);
565               goto out;
566             }
567
568           if (_dbus_string_equal_len (&expected, &from_auth,
569                                       _dbus_string_get_length (&expected)))
570             {
571               _dbus_string_delete (&from_auth, 0,
572                                    _dbus_string_get_length (&expected));
573               _dbus_string_free (&expected);
574             }
575           else
576             {
577               _dbus_warn ("Expected exact string '%s' and have '%s'\n",
578                           _dbus_string_get_const_data (&expected),
579                           _dbus_string_get_const_data (&from_auth));
580               _dbus_string_free (&expected);
581               goto out;
582             }
583         }
584       else
585         goto parse_failed;
586
587       goto next_iteration; /* skip parse_failed */
588       
589     parse_failed:
590       {
591         _dbus_warn ("couldn't process line %d \"%s\"\n",
592                     line_no, _dbus_string_get_const_data (&line));
593         goto out;
594       }
595     }
596
597   if (auth != NULL &&
598       state == DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES)
599     {
600       _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
601       goto out;
602     }
603
604   if (_dbus_string_get_length (&from_auth) > 0)
605     {
606       _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
607       _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
608       goto out;
609     }
610   
611   retval = TRUE;
612   
613  out:
614   if (auth)
615     _dbus_auth_unref (auth);
616
617   _dbus_string_free (&file);
618   _dbus_string_free (&line);
619   _dbus_string_free (&from_auth);
620   
621   return retval;
622 }
623
624 /** @} */
625 #endif /* DBUS_BUILD_TESTS */