2005-01-26 Havoc Pennington <hp@redhat.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 2.1
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-userdb.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"))
144     return DBUS_AUTH_STATE_AUTHENTICATED;
145   else
146     return -1;
147 }
148
149 static const char*
150 auth_state_to_string (DBusAuthState state)
151 {
152   switch (state)
153     {
154     case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
155       return "WAITING_FOR_INPUT";
156     case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
157       return "WAITING_FOR_MEMORY";
158     case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
159       return "HAVE_BYTES_TO_SEND";
160     case DBUS_AUTH_STATE_NEED_DISCONNECT:
161       return "NEED_DISCONNECT";
162     case DBUS_AUTH_STATE_AUTHENTICATED:
163       return "AUTHENTICATED";
164     }
165
166   return "unknown";
167 }
168
169 static char **
170 split_string (DBusString *str)
171 {
172   int i, j, k, count, end;
173   char **array;
174
175   end = _dbus_string_get_length (str);
176
177   i = 0;
178   _dbus_string_skip_blank (str, i, &i);
179   for (count = 0; i < end; count++)
180     {
181       _dbus_string_find_blank (str, i, &i);
182       _dbus_string_skip_blank (str, i, &i);
183     }
184
185   array = dbus_new0 (char *, count + 1);
186   if (array == NULL)
187     return NULL;
188
189   i = 0;
190   _dbus_string_skip_blank (str, i, &i);
191   for (k = 0; k < count; k++)
192     {
193       _dbus_string_find_blank (str, i, &j);
194
195       array[k] = dbus_malloc (j - i + 1);
196       if (array[k] == NULL)
197         {
198           dbus_free_string_array (array);
199           return NULL;
200         }
201       memcpy (array[k],
202               _dbus_string_get_const_data_len (str, i, j - i), j - i);
203       array[k][j - i] = '\0';
204
205       _dbus_string_skip_blank (str, j, &i);
206     }
207   array[k] = NULL;
208
209   return array;
210 }
211
212 /**
213  * Runs an "auth script" which is a script for testing the
214  * authentication protocol. Scripts send and receive data, and then
215  * include assertions about the state of both ends of the connection
216  * after processing the data. A script succeeds if these assertions
217  * hold.
218  *
219  * @param filename the file containing the script to run
220  * @returns #TRUE if the script succeeds, #FALSE otherwise
221  */
222 dbus_bool_t
223 _dbus_auth_script_run (const DBusString *filename)
224 {
225   DBusString file;
226   DBusError error;
227   DBusString line;
228   dbus_bool_t retval;
229   int line_no;
230   DBusAuth *auth;
231   DBusString from_auth;
232   DBusAuthState state;
233   DBusString context;
234   
235   retval = FALSE;
236   auth = NULL;
237
238   _dbus_string_init_const (&context, "org_freedesktop_test");
239   
240   if (!_dbus_string_init (&file))
241     return FALSE;
242
243   if (!_dbus_string_init (&line))
244     {
245       _dbus_string_free (&file);
246       return FALSE;
247     }
248
249   if (!_dbus_string_init (&from_auth))
250     {
251       _dbus_string_free (&file);
252       _dbus_string_free (&line);
253       return FALSE;
254     }
255
256   dbus_error_init (&error);
257   if (!_dbus_file_get_contents (&file, filename, &error))    {
258       _dbus_warn ("Getting contents of %s failed: %s\n",
259                   _dbus_string_get_const_data (filename), error.message);
260       dbus_error_free (&error);
261       goto out;
262     }
263
264   state = DBUS_AUTH_STATE_NEED_DISCONNECT;
265   line_no = 0;
266  next_iteration:
267   while (_dbus_string_pop_line (&file, &line))
268     {      
269       line_no += 1;
270
271       _dbus_string_delete_leading_blanks (&line);
272
273       if (auth != NULL)
274         {
275           while ((state = _dbus_auth_do_work (auth)) ==
276                  DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
277             {
278               const DBusString *tmp;
279               if (_dbus_auth_get_bytes_to_send (auth, &tmp))
280                 {
281                   int count = _dbus_string_get_length (tmp);
282
283                   if (_dbus_string_copy (tmp, 0, &from_auth,
284                                          _dbus_string_get_length (&from_auth)))
285                     _dbus_auth_bytes_sent (auth, count);
286                 }
287             }
288         }
289       
290       if (_dbus_string_get_length (&line) == 0)
291         {
292           /* empty line */
293           goto next_iteration;
294         }
295       else if (_dbus_string_starts_with_c_str (&line,
296                                                "#"))
297         {
298           /* Ignore this comment */
299           goto next_iteration;
300         }
301       else if (_dbus_string_starts_with_c_str (&line,
302                                                "CLIENT"))
303         {
304           DBusCredentials creds;
305           
306           if (auth != NULL)
307             {
308               _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
309               goto out;
310             }
311
312           auth = _dbus_auth_client_new ();
313           if (auth == NULL)
314             {
315               _dbus_warn ("no memory to create DBusAuth\n");
316               goto out;
317             }
318
319           /* test ref/unref */
320           _dbus_auth_ref (auth);
321           _dbus_auth_unref (auth);
322           
323           _dbus_credentials_from_current_process (&creds);
324           _dbus_auth_set_credentials (auth, &creds);
325         }
326       else if (_dbus_string_starts_with_c_str (&line,
327                                                "SERVER"))
328         {
329           DBusCredentials creds;
330           
331           if (auth != NULL)
332             {
333               _dbus_warn ("already created a DBusAuth (CLIENT or SERVER given twice)\n");
334               goto out;
335             }
336
337           auth = _dbus_auth_server_new ();
338           if (auth == NULL)
339             {
340               _dbus_warn ("no memory to create DBusAuth\n");
341               goto out;
342             }
343
344           /* test ref/unref */
345           _dbus_auth_ref (auth);
346           _dbus_auth_unref (auth);
347           
348           _dbus_credentials_from_current_process (&creds);
349           _dbus_auth_set_credentials (auth, &creds);
350           _dbus_auth_set_context (auth, &context);
351         }
352       else if (auth == NULL)
353         {
354           _dbus_warn ("must specify CLIENT or SERVER\n");
355           goto out;
356
357         }
358       else if (_dbus_string_starts_with_c_str (&line,
359                                                "NO_CREDENTIALS"))
360         {
361           DBusCredentials creds = { -1, -1, -1 };
362           _dbus_auth_set_credentials (auth, &creds);
363         }
364       else if (_dbus_string_starts_with_c_str (&line,
365                                                "ROOT_CREDENTIALS"))
366         {
367           DBusCredentials creds = { -1, 0, 0 };
368           _dbus_auth_set_credentials (auth, &creds);          
369         }
370       else if (_dbus_string_starts_with_c_str (&line,
371                                                "SILLY_CREDENTIALS"))
372         {
373           DBusCredentials creds = { -1, 4312, 1232 };
374           _dbus_auth_set_credentials (auth, &creds);          
375         }
376       else if (_dbus_string_starts_with_c_str (&line,
377                                                "ALLOWED_MECHS"))
378         {
379           char **mechs;
380
381           _dbus_string_delete_first_word (&line);
382           mechs = split_string (&line);
383           _dbus_auth_set_mechanisms (auth, (const char **) mechs);
384           dbus_free_string_array (mechs);
385         }
386       else if (_dbus_string_starts_with_c_str (&line,
387                                                "SEND"))
388         {
389           DBusString to_send;
390           
391           _dbus_string_delete_first_word (&line);
392
393           if (!_dbus_string_init (&to_send))
394             {
395               _dbus_warn ("no memory to allocate string\n");
396               goto out;
397             }
398
399           if (!append_quoted_string (&to_send, &line))
400             {
401               _dbus_warn ("failed to append quoted string line %d\n",
402                           line_no);
403               _dbus_string_free (&to_send);
404               goto out;
405             }
406
407           _dbus_verbose ("Sending '%s'\n", _dbus_string_get_const_data (&to_send));
408           
409           if (!_dbus_string_append (&to_send, "\r\n"))
410             {
411               _dbus_warn ("failed to append \r\n from line %d\n",
412                           line_no);
413               _dbus_string_free (&to_send);
414               goto out;
415             }
416
417           /* Replace USERID_HEX with our username in hex */
418           {
419             int where;
420             
421             if (_dbus_string_find (&to_send, 0,
422                                    "USERID_HEX", &where))
423               {
424                 DBusString username;
425
426                 if (!_dbus_string_init (&username))
427                   {
428                     _dbus_warn ("no memory for userid\n");
429                     _dbus_string_free (&to_send);
430                     goto out;
431                   }
432
433                 if (!_dbus_string_append_uint (&username,
434                                                _dbus_getuid ()))
435                   {
436                     _dbus_warn ("no memory for userid\n");
437                     _dbus_string_free (&username);
438                     _dbus_string_free (&to_send);
439                     goto out;
440                   }
441
442                 _dbus_string_delete (&to_send, where, strlen ("USERID_HEX"));
443                 
444                 if (!_dbus_string_hex_encode (&username, 0,
445                                               &to_send, where))
446                   {
447                     _dbus_warn ("no memory to subst USERID_HEX\n");
448                     _dbus_string_free (&username);
449                     _dbus_string_free (&to_send);
450                     goto out;
451                   }
452
453                 _dbus_string_free (&username);
454               }
455             else if (_dbus_string_find (&to_send, 0,
456                                         "USERNAME_HEX", &where))
457               {
458                 DBusString username;
459                 const DBusString *u;
460                 
461                 if (!_dbus_string_init (&username))
462                   {
463                     _dbus_warn ("no memory for username\n");
464                     _dbus_string_free (&to_send);
465                     goto out;
466                   }
467
468                 if (!_dbus_username_from_current_process (&u) ||
469                     !_dbus_string_copy (u, 0, &username,
470                                         _dbus_string_get_length (&username)))
471                   {
472                     _dbus_warn ("no memory for username\n");
473                     _dbus_string_free (&username);
474                     _dbus_string_free (&to_send);
475                     goto out;
476                   }
477
478                 _dbus_string_delete (&to_send, where, strlen ("USERNAME_HEX"));
479                 
480                 if (!_dbus_string_hex_encode (&username, 0,
481                                               &to_send, where))
482                   {
483                     _dbus_warn ("no memory to subst USERNAME_HEX\n");
484                     _dbus_string_free (&username);
485                     _dbus_string_free (&to_send);
486                     goto out;
487                   }
488
489                 _dbus_string_free (&username);
490               }
491           }
492
493           {
494             DBusString *buffer;
495
496             _dbus_auth_get_buffer (auth, &buffer);
497             if (!_dbus_string_copy (&to_send, 0,
498                                     buffer, _dbus_string_get_length (buffer)))
499               {
500                 _dbus_warn ("not enough memory to call bytes_received, or can't add bytes to auth object already in end state\n");
501                 _dbus_string_free (&to_send);
502                 _dbus_auth_return_buffer (auth, buffer, 0);
503                 goto out;
504               }
505
506             _dbus_auth_return_buffer (auth, buffer, _dbus_string_get_length (&to_send));
507           }
508           
509           _dbus_string_free (&to_send);
510         }
511       else if (_dbus_string_starts_with_c_str (&line,
512                                                "EXPECT_STATE"))
513         {
514           DBusAuthState expected;
515           
516           _dbus_string_delete_first_word (&line);
517
518           expected = auth_state_from_string (&line);
519           if (expected < 0)
520             {
521               _dbus_warn ("bad auth state given to EXPECT_STATE\n");
522               goto parse_failed;
523             }
524
525           if (expected != state)
526             {
527               _dbus_warn ("expected auth state %s but got %s on line %d\n",
528                           auth_state_to_string (expected),
529                           auth_state_to_string (state),
530                           line_no);
531               goto out;
532             }
533         }
534       else if (_dbus_string_starts_with_c_str (&line,
535                                                "EXPECT_COMMAND"))
536         {
537           DBusString received;
538           
539           _dbus_string_delete_first_word (&line);
540
541           if (!_dbus_string_init (&received))
542             {
543               _dbus_warn ("no mem to allocate string received\n");
544               goto out;
545             }
546
547           if (!_dbus_string_pop_line (&from_auth, &received))
548             {
549               _dbus_warn ("no line popped from the DBusAuth being tested, expected command %s on line %d\n",
550                           _dbus_string_get_const_data (&line), line_no);
551               _dbus_string_free (&received);
552               goto out;
553             }
554
555           if (!same_first_word (&received, &line))
556             {
557               _dbus_warn ("line %d expected command '%s' and got '%s'\n",
558                           line_no,
559                           _dbus_string_get_const_data (&line),
560                           _dbus_string_get_const_data (&received));
561               _dbus_string_free (&received);
562               goto out;
563             }
564           
565           _dbus_string_free (&received);
566         }
567       else if (_dbus_string_starts_with_c_str (&line,
568                                                "EXPECT_UNUSED"))
569         {
570           DBusString expected;
571           const DBusString *unused;
572           
573           _dbus_string_delete_first_word (&line);
574
575           if (!_dbus_string_init (&expected))
576             {
577               _dbus_warn ("no mem to allocate string expected\n");
578               goto out;
579             }
580
581           if (!append_quoted_string (&expected, &line))
582             {
583               _dbus_warn ("failed to append quoted string line %d\n",
584                           line_no);
585               _dbus_string_free (&expected);
586               goto out;
587             }
588
589           _dbus_auth_get_unused_bytes (auth, &unused);
590           
591           if (_dbus_string_equal (&expected, unused))
592             {
593               _dbus_auth_delete_unused_bytes (auth);
594               _dbus_string_free (&expected);
595             }
596           else
597             {
598               _dbus_warn ("Expected unused bytes '%s' and have '%s'\n",
599                           _dbus_string_get_const_data (&expected),
600                           _dbus_string_get_const_data (unused));
601               _dbus_string_free (&expected);
602               goto out;
603             }
604         }
605       else if (_dbus_string_starts_with_c_str (&line,
606                                                "EXPECT"))
607         {
608           DBusString expected;
609           
610           _dbus_string_delete_first_word (&line);
611
612           if (!_dbus_string_init (&expected))
613             {
614               _dbus_warn ("no mem to allocate string expected\n");
615               goto out;
616             }
617
618           if (!append_quoted_string (&expected, &line))
619             {
620               _dbus_warn ("failed to append quoted string line %d\n",
621                           line_no);
622               _dbus_string_free (&expected);
623               goto out;
624             }
625
626           if (_dbus_string_equal_len (&expected, &from_auth,
627                                       _dbus_string_get_length (&expected)))
628             {
629               _dbus_string_delete (&from_auth, 0,
630                                    _dbus_string_get_length (&expected));
631               _dbus_string_free (&expected);
632             }
633           else
634             {
635               _dbus_warn ("Expected exact string '%s' and have '%s'\n",
636                           _dbus_string_get_const_data (&expected),
637                           _dbus_string_get_const_data (&from_auth));
638               _dbus_string_free (&expected);
639               goto out;
640             }
641         }
642       else
643         goto parse_failed;
644
645       goto next_iteration; /* skip parse_failed */
646       
647     parse_failed:
648       {
649         _dbus_warn ("couldn't process line %d \"%s\"\n",
650                     line_no, _dbus_string_get_const_data (&line));
651         goto out;
652       }
653     }
654
655   if (auth != NULL &&
656       state == DBUS_AUTH_STATE_AUTHENTICATED)
657     {
658       const DBusString *unused;
659
660       _dbus_auth_get_unused_bytes (auth, &unused);
661
662       if (_dbus_string_get_length (unused) > 0)
663         {
664           _dbus_warn ("did not expect unused bytes (scripts must specify explicitly if they are expected)\n");
665           goto out;
666         }
667     }
668
669   if (_dbus_string_get_length (&from_auth) > 0)
670     {
671       _dbus_warn ("script did not have EXPECT_ statements for all the data received from the DBusAuth\n");
672       _dbus_warn ("Leftover data: %s\n", _dbus_string_get_const_data (&from_auth));
673       goto out;
674     }
675   
676   retval = TRUE;
677   
678  out:
679   if (auth)
680     _dbus_auth_unref (auth);
681
682   _dbus_string_free (&file);
683   _dbus_string_free (&line);
684   _dbus_string_free (&from_auth);
685   
686   return retval;
687 }
688
689 /** @} */
690 #endif /* DBUS_BUILD_TESTS */