aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.c
blob: 3406355c537d1c092ec86119540ad3ab2881a4da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/*
      This file is part of GNUnet
      (C) 2008, 2009 Christian Grothoff (and other contributing authors)

      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
      by the Free Software Foundation; either version 2, or (at your
      option) any later version.

      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      General Public License for more details.

      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      Boston, MA 02111-1307, USA.
 */

/**
 * @file testing/testing.c
 * @brief convenience API for writing testcases for GNUnet
 *        Many testcases need to start and stop gnunetd,
 *        and this library is supposed to make that easier
 *        for TESTCASES.  Normal programs should always
 *        use functions from gnunet_{util,arm}_lib.h.  This API is
 *        ONLY for writing testcases!
 * @author Christian Grothoff
 *
 * TODO:
 * - modify configuration to allow 2087-connections from
 *   controlling host (otherwise shutdown won't work)
 * 
 */
#include "platform.h"
#include "gnunet_arm_service.h"
#include "gnunet_core_service.h"
#include "gnunet_constants.h"
#include "gnunet_testing_lib.h"
#include "gnunet_transport_service.h"

#define DEBUG_TESTING GNUNET_YES

/**
 * How long do we wait after starting gnunet-service-arm
 * for the core service to be alive?
 */
#define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)

/**
 * How many times are we willing to try to 
 * wait for "scp" or "gnunet-service-arm" to
 * complete (waitpid) before giving up?
 */
#define MAX_EXEC_WAIT_RUNS 50

/**
 * Phases of starting GNUnet on a system.
 */
enum StartPhase
  {
    SP_COPYING,
    SP_COPIED,
    SP_START_ARMING,
    SP_START_CORE,
    SP_START_DONE,
    SP_CLEANUP,
    SP_CONFIG_UPDATE
  };


/**
 * Handle for a GNUnet daemon (technically a set of
 * daemons; the handle is really for the master ARM
 * daemon) started by the testing library.
 */
struct GNUNET_TESTING_Daemon
{
  /**
   * Our scheduler.
   */
  struct GNUNET_SCHEDULER_Handle *sched;

  /**
   * Our configuration.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Host to run GNUnet on.
   */
  char *hostname;

  /**
   * Username we are using.
   */
  char *username;

  /**
   * Name of the configuration file
   */
  char *cfgfile;

  /**
   * Function to call when the peer is running.
   */
  GNUNET_TESTING_NotifyDaemonRunning cb;

  /**
   * Closure for cb.
   */
  void *cb_cls;

  /**
   * Arguments from "daemon_stop" call.
   */
  GNUNET_TESTING_NotifyCompletion dead_cb;

  /**
   * Closure for 'dead_cb'.
   */
  void *dead_cb_cls;

  /**
   * Arguments from "daemon_stop" call.
   */
  GNUNET_TESTING_NotifyCompletion update_cb;

  /**
   * Closure for 'update_cb'.
   */
  void *update_cb_cls;

  /**
   * Identity of this peer (once started).
   */
  struct GNUNET_PeerIdentity id;

  /**
   * Flag to indicate that we've already been asked
   * to terminate (but could not because some action
   * was still pending).
   */
  int dead;

  /**
   * PID of the process that we started last.
   */
  pid_t pid;

  /**
   * How many iterations have we been waiting for
   * the started process to complete?
   */
  unsigned int wait_runs;

  /**
   * In which phase are we during the start of
   * this process?
   */
  enum StartPhase phase;

  /**
   * ID of the current task.
   */
  GNUNET_SCHEDULER_TaskIdentifier task;

  /**
   * Handle to the server.
   */ 
  struct GNUNET_CORE_Handle * server;
};


/**
 * Function called after GNUNET_CORE_connect has succeeded
 * (or failed for good).  Note that the private key of the
 * peer is intentionally not exposed here; if you need it,
 * your process should try to read the private key file
 * directly (which should work if you are authorized...).
 *
 * @param cls closure
 * @param server handle to the server, NULL if we failed
 * @param my_identity ID of this peer, NULL if we failed
 * @param publicKey public key of this peer, NULL if we failed
 */
static void
testing_init (void *cls,
	      struct GNUNET_CORE_Handle * server,
	      const struct GNUNET_PeerIdentity *
	      my_identity,
	      const struct
	      GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
	      publicKey)
{
  struct GNUNET_TESTING_Daemon *d = cls;
  GNUNET_TESTING_NotifyDaemonRunning cb;

  d->phase = SP_START_DONE;
  cb = d->cb;
  d->cb = NULL;
  if (server == NULL)
    {
      cb (d->cb_cls, NULL, d->cfg, d,
	  _("Failed to connect to core service\n"));
      if (GNUNET_YES == d->dead)
	GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
      return;
    }
#if DEBUG_TESTING
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Successfully started peer `%4s'.\n",
	      GNUNET_i2s(my_identity));
#endif
  d->id = *my_identity;
  if (GNUNET_YES == d->dead)
    GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
  else
    cb (d->cb_cls, my_identity, d->cfg, d, NULL);
  d->server = server;
}


/**
 * Finite-state machine for starting GNUnet.
 *
 * @param cls our "struct GNUNET_TESTING_Daemon"
 * @param tc unused
 */
static void
start_fsm (void *cls,
	   const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  static struct GNUNET_CORE_MessageHandler no_handlers[] =
    { { NULL, 0, 0 } };
  struct GNUNET_TESTING_Daemon * d = cls;
  GNUNET_TESTING_NotifyDaemonRunning cb;
  enum GNUNET_OS_ProcessStatusType type;
  unsigned long code;
  char *dst;
 
  d->task = GNUNET_SCHEDULER_NO_TASK;
  switch (d->phase)
    {
    case SP_COPYING:
      /* confirm copying complete */
      if (GNUNET_OK != 
	  GNUNET_OS_process_status (d->pid,
				    &type,
				    &code))
	{
	  d->wait_runs++;
	  if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
	    {
	      cb = d->cb;
	      d->cb = NULL;
	      cb (d->cb_cls,
		  NULL,
		  d->cfg,
		  d,
		  _("`scp' does not seem to terminate.\n"));
	      return;
	    }
	  /* wait some more */
	  d->task
	    = GNUNET_SCHEDULER_add_delayed (d->sched, 
					    GNUNET_NO,
					    GNUNET_SCHEDULER_PRIORITY_KEEP,
					    GNUNET_SCHEDULER_NO_TASK,
					    GNUNET_CONSTANTS_EXEC_WAIT,
					    &start_fsm,
					    d);
	  return;
	}
      if ( (type != GNUNET_OS_PROCESS_EXITED) ||
	   (code != 0) )
	{
	  cb = d->cb;
	  d->cb = NULL;
	  cb (d->cb_cls,
	      NULL,
	      d->cfg,
	      d,
	      _("`scp' did not complete cleanly.\n"));	  
	  return;
	}	  
#if DEBUG_TESTING
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		  "Successfully copied configuration file.\n");
#endif
      d->phase = SP_COPIED;
      /* fall-through */
    case SP_COPIED:
      /* start GNUnet on remote host */
      if (NULL == d->hostname)        
	{
	  d->pid = GNUNET_OS_start_process ("gnunet-service-arm",
					    "gnunet-service-arm",
					    "-c",
					    d->cfgfile,
					    "-d",
					    NULL);
	}
      else
	{
	  if (d->username != NULL)
	    GNUNET_asprintf (&dst,
			     "%s@%s",
			     d->username,
			     d->hostname);
	  else
	    dst = GNUNET_strdup (d->hostname);
	  d->pid = GNUNET_OS_start_process ("ssh",
					    "ssh",
					    dst,
					    "gnunet-service-arm",
					    "-c",
					    d->cfgfile,
					    "-d",
					    NULL);
	  GNUNET_free (dst);
	}
      if (-1 == d->pid)
	{
	  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		      _("Could not start `%s' process to start GNUnet.\n"),
		      (NULL == d->hostname) ? "gnunet-service-arm" : "ssh");
	  cb = d->cb;
	  d->cb = NULL;
	  cb (d->cb_cls,
	      NULL,
	      d->cfg,
	      d,
	      (NULL == d->hostname) 
	      ? _("Failed to start `gnunet-service-arm' process.\n") 
	      : _("Failed to start `ssh' process.\n"));
	}      
      d->phase = SP_START_ARMING;
      d->wait_runs = 0;
      break;     
    case SP_START_ARMING:
      if (GNUNET_OK != 
	  GNUNET_OS_process_status (d->pid,
				    &type,
				    &code))
	{
	  d->wait_runs++;
	  if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
	    {
	      cb = d->cb;
	      d->cb = NULL;
	      cb (d->cb_cls,
		  NULL,
		  d->cfg,
		  d,
		  (NULL == d->hostname) 
		  ? _("`gnunet-service-arm' does not seem to terminate.\n") 
		  : _("`ssh' does not seem to terminate.\n"));
	      return;
	    }
	  /* wait some more */
	  d->task
	    = GNUNET_SCHEDULER_add_delayed (d->sched, 
					    GNUNET_NO,
					    GNUNET_SCHEDULER_PRIORITY_KEEP,
					    GNUNET_SCHEDULER_NO_TASK,
					    GNUNET_CONSTANTS_EXEC_WAIT,
					    &start_fsm,
					    d);
	  return;
	}
#if DEBUG_TESTING
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		  "Successfully started `%s'.\n",
		  "gnunet-service-arm");
#endif
      d->phase = SP_START_CORE;
      GNUNET_CORE_connect (d->sched,
			   d->cfg,
			   ARM_START_WAIT,
			   d,
			   &testing_init,
			   NULL, NULL, NULL, 
			   NULL, GNUNET_NO,
			   NULL, GNUNET_NO,
			   no_handlers);     
      break;
    case SP_START_CORE:
      GNUNET_break (0);
      break;
    case SP_START_DONE:
      GNUNET_break (0);
      break;
    case SP_CLEANUP:
      /* confirm copying complete */
      if (GNUNET_OK != 
	  GNUNET_OS_process_status (d->pid,
				    &type,
				    &code))
	{
	  d->wait_runs++;
	  if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
	    {
	      d->dead_cb (d->dead_cb_cls,
			  _("`ssh' does not seem to terminate.\n"));
	      GNUNET_free (d->cfgfile);
	      GNUNET_free_non_null (d->hostname);
	      GNUNET_free_non_null (d->username);
	      GNUNET_free (d);
	      return;
	    }
	  /* wait some more */
	  d->task
	    = GNUNET_SCHEDULER_add_delayed (d->sched, 
					    GNUNET_NO,
					    GNUNET_SCHEDULER_PRIORITY_KEEP,
					    GNUNET_SCHEDULER_NO_TASK,
					    GNUNET_CONSTANTS_EXEC_WAIT,
					    &start_fsm,
					    d);
	  return;
	}
      if ( (type != GNUNET_OS_PROCESS_EXITED) ||
	   (code != 0) )
	{
	  d->dead_cb (d->dead_cb_cls,
		      _("`sshp' did not complete cleanly.\n"));	  
	  GNUNET_free (d->cfgfile);
	  GNUNET_free_non_null (d->hostname);
	  GNUNET_free_non_null (d->username);
	  GNUNET_free (d);
	  return;
	}	 
      GNUNET_free (d->cfgfile);
      GNUNET_free_non_null (d->hostname);
      GNUNET_free_non_null (d->username);
      d->dead_cb (d->dead_cb_cls, NULL);
      GNUNET_free (d);
      break;
    case SP_CONFIG_UPDATE:
      /* confirm copying complete */
      if (GNUNET_OK != 
	  GNUNET_OS_process_status (d->pid,
				    &type,
				    &code))
	{
	  d->wait_runs++;
	  if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
	    {
	      cb = d->cb;
	      d->cb = NULL;
	      cb (d->cb_cls,
		  NULL,
		  d->cfg,
		  d,
		  _("`scp' does not seem to terminate.\n"));
	      return;
	    }
	  /* wait some more */
	  d->task
	    = GNUNET_SCHEDULER_add_delayed (d->sched, 
					    GNUNET_NO,
					    GNUNET_SCHEDULER_PRIORITY_KEEP,
					    GNUNET_SCHEDULER_NO_TASK,
					    GNUNET_CONSTANTS_EXEC_WAIT,
					    &start_fsm,
					    d);
	  return;
	}
      if ( (type != GNUNET_OS_PROCESS_EXITED) ||
	   (code != 0) )
	{
	  d->update_cb (d->update_cb_cls,
			_("`scp' did not complete cleanly.\n"));	  
	  return;
	}	  
#if DEBUG_TESTING
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		  "Successfully copied configuration file.\n");
#endif
      d->update_cb (d->update_cb_cls, NULL);
      d->phase = SP_START_DONE;
      break;
    }
}


/**
 * Starts a GNUnet daemon.  GNUnet must be installed on the target
 * system and available in the PATH.  The machine must furthermore be
 * reachable via "ssh" (unless the hostname is "NULL") without the
 * need to enter a password.
 *
 * @param sched scheduler to use 
 * @param cfg configuration to use
 * @param hostname name of the machine where to run GNUnet
 *        (use NULL for localhost).
 * @param cb function to call with the result
 * @param cb_cls closure for cb
 * @return handle to the daemon (actual start will be completed asynchronously)
 */
struct GNUNET_TESTING_Daemon *
GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
			     struct GNUNET_CONFIGURATION_Handle *cfg,
			     const char *hostname,
			     GNUNET_TESTING_NotifyDaemonRunning cb,
			     void *cb_cls)
{
  struct GNUNET_TESTING_Daemon * ret;
  char *arg;
  char *username;

  ret = GNUNET_malloc (sizeof(struct GNUNET_TESTING_Daemon));
  ret->sched = sched;
  ret->cfg = cfg;
  ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
  ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
  if (NULL == ret->cfgfile)
    {						
      GNUNET_free_non_null (ret->hostname);
      GNUNET_free (ret);
      return NULL;
    }
  ret->cb = cb;
  ret->cb_cls = cb_cls;
  /* 1) write configuration to temporary file */
  if (GNUNET_OK != 
      GNUNET_CONFIGURATION_write (cfg,
				  ret->cfgfile))
    {
      if (0 != UNLINK (ret->cfgfile))
	  GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
				    "unlink",
				    ret->cfgfile);
      GNUNET_free_non_null (ret->hostname);
      GNUNET_free (ret->cfgfile);
      GNUNET_free (ret);
      return NULL;
    }
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (cfg,
					     "TESTING",
					     "USERNAME",
					     &username))
    {
      if (NULL != getenv ("USER"))
	username = GNUNET_strdup (getenv("USER"));
      else
	username = NULL;
    }
  ret->username = username;

  /* 2) copy file to remote host */  
  if (NULL != hostname)
    {
      ret->phase = SP_COPYING;
      if (NULL != username)
	GNUNET_asprintf (&arg,
			 "%s@%s:%s", 
			 username,
			 hostname,
			 ret->cfgfile);
      else
	GNUNET_asprintf (&arg,
			 "%s:%s", 
			 hostname,
			 ret->cfgfile);
      ret->pid = GNUNET_OS_start_process ("scp",
					  "scp",
					  ret->cfgfile,
					  arg,
					  NULL);
      GNUNET_free (arg);
      if (-1 == ret->pid)
	{
	  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		      _("Could not start `%s' process to copy configuration file.\n"),
		      "scp");
	  if (0 != UNLINK (ret->cfgfile))
	    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
				      "unlink",
				      ret->cfgfile);
	  GNUNET_free_non_null (ret->hostname);
	  GNUNET_free_non_null (ret->username);
	  GNUNET_free (ret->cfgfile);
	  GNUNET_free (ret);
 	  return NULL;
	}
      ret->task
	= GNUNET_SCHEDULER_add_delayed (sched, 
					GNUNET_NO,
					GNUNET_SCHEDULER_PRIORITY_KEEP,
					GNUNET_SCHEDULER_NO_TASK,
					GNUNET_CONSTANTS_EXEC_WAIT,
					&start_fsm,
					ret);
      return ret;
    }
  ret->phase = SP_COPIED;
  GNUNET_SCHEDULER_add_continuation (sched,
				     GNUNET_NO,
				     &start_fsm,
				     ret,
				     GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  return ret;
}


/**
 * Stops a GNUnet daemon.
 *
 * @param d the daemon that should be stopped
 * @param cb function called once the daemon was stopped
 * @param cb_cls closure for cb
 */
void GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
				 GNUNET_TESTING_NotifyCompletion cb,
				 void * cb_cls)
{
  struct GNUNET_CLIENT_Connection *cc;
  char *dst;

  if (NULL != d->cb)
    {
      d->dead = GNUNET_YES;
      d->dead_cb = cb;
      d->dead_cb_cls = cb_cls;
      return;
    }
  if (d->phase == SP_CONFIG_UPDATE)
    {
      GNUNET_SCHEDULER_cancel (d->sched,
			       d->task);
      d->phase = SP_START_DONE;
    }
  if (d->server != NULL)
    {
      GNUNET_CORE_disconnect (d->server);
      d->server = NULL;
    }
  /* shutdown ARM process (will also terminate others) */
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      _("Terminating peer `%4s'\n"),
	      GNUNET_i2s(&d->id));
  cc = GNUNET_CLIENT_connect (d->sched,
			      "arm",
			      d->cfg);
  GNUNET_CLIENT_service_shutdown (cc);
  GNUNET_CLIENT_disconnect (cc);
  
  /* state clean up and notifications */
  if (0 != UNLINK (d->cfgfile))
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
			      "unlink",
			      d->cfgfile);
  if (d->hostname != NULL)
    {
      if (NULL != d->username)
	GNUNET_asprintf (&dst,
			 "%s@%s",
			 d->username,
			 d->hostname);
      else
	dst = GNUNET_strdup (d->hostname);
      d->pid = GNUNET_OS_start_process ("ssh",
					"ssh",
					dst,
					"rm",
					d->cfgfile,
					NULL);
      GNUNET_free (dst);
      if (-1 == d->pid)
	{
	  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		      _("Could not start `%s' process to delete configuration file.\n"),
		      "ssh");
	  GNUNET_free (d->cfgfile);
	  GNUNET_free_non_null (d->hostname);
	  GNUNET_free_non_null (d->username);
	  GNUNET_free (d);
	  cb (cb_cls, _("Error cleaning up configuration file.\n"));
	  return;
	}
      d->phase = SP_CLEANUP;
      d->dead_cb = cb;
      d->dead_cb_cls = cb_cls;
      d->task
	= GNUNET_SCHEDULER_add_delayed (d->sched, 
					GNUNET_NO,
					GNUNET_SCHEDULER_PRIORITY_KEEP,
					GNUNET_SCHEDULER_NO_TASK,
					GNUNET_CONSTANTS_EXEC_WAIT,
					&start_fsm,
					d);
      return;
    }
  GNUNET_free (d->cfgfile);
  GNUNET_free_non_null (d->hostname);
  GNUNET_free_non_null (d->username);
  GNUNET_free (d);
  cb (cb_cls, NULL);
}


/**
 * Changes the configuration of a GNUnet daemon.
 *
 * @param d the daemon that should be modified
 * @param cfg the new configuration for the daemon
 * @param cb function called once the configuration was changed
 * @param cb_cls closure for cb
 */
void GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
					struct GNUNET_CONFIGURATION_Handle *cfg,
					GNUNET_TESTING_NotifyCompletion cb,
					void * cb_cls)
{
  char *arg;

  if (d->phase != SP_START_DONE)
    {
      cb (cb_cls,
	  _("Peer not yet running, can not change configuration at this point."));
      return;      
    }

  /* 1) write configuration to temporary file */
  if (GNUNET_OK != 
      GNUNET_CONFIGURATION_write (cfg,
				  d->cfgfile))
    {
      cb (cb_cls,
	  _("Failed to write new configuration to disk."));
      return;
    }

  /* 2) copy file to remote host (if necessary) */  
  if (NULL == d->hostname)
    {
      /* signal success */
      cb (cb_cls, NULL); 
      return;
    }
  d->phase = SP_CONFIG_UPDATE;
  if (NULL != d->username)
    GNUNET_asprintf (&arg,
		     "%s@%s:%s", 
		     d->username,
		     d->hostname,
		     d->cfgfile);
  else
    GNUNET_asprintf (&arg,
		     "%s:%s", 
		     d->hostname,
		     d->cfgfile);
  d->pid = GNUNET_OS_start_process ("scp",
				    "scp",
				    d->cfgfile,
				    arg,
				    NULL);
  GNUNET_free (arg);
  if (-1 == d->pid)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  _("Could not start `%s' process to copy configuration file.\n"),
		  "scp");
      cb (cb_cls,
	  _("Failed to copy new configuration to remote machine."));
      d->phase = SP_START_DONE;
      return;
    }
  d->update_cb = cb;
  d->update_cb_cls = cb_cls;
  d->task
    = GNUNET_SCHEDULER_add_delayed (d->sched, 
				    GNUNET_NO,
				    GNUNET_SCHEDULER_PRIORITY_KEEP,
				    GNUNET_SCHEDULER_NO_TASK,
				    GNUNET_CONSTANTS_EXEC_WAIT,
				    &start_fsm,
				    d);
}


struct ConnectContext
{
  struct GNUNET_TESTING_Daemon *d1;
  struct GNUNET_TESTING_Daemon *d2;
  struct GNUNET_TRANSPORT_Handle *d1th;
  struct GNUNET_TRANSPORT_Handle *d2th;
  struct GNUNET_TIME_Absolute timeout;
  GNUNET_TESTING_NotifyCompletion cb;
  void *cb_cls;
};


/**
 * Success, connection is up.  Signal client our success.
 */
static size_t
transmit_ready (void *cls, size_t size, void *buf)
{
  struct ConnectContext *ctx = cls;
  if (buf == NULL)
    ctx->cb (ctx->cb_cls, _("Peers failed to connect"));
  else
    ctx->cb (ctx->cb_cls, NULL);
  GNUNET_free (ctx);
  return 0;
}


/**
 * Receive the HELLO from one peer, give it to the other
 * and ask them to connect.
 * 
 * @param cls "struct ConnectContext"
 * @param latency how fast is the connection
 * @param peer ID of peer giving us the HELLO
 * @param message HELLO message of peer
 */
static void
process_hello (void *cls,
               struct GNUNET_TIME_Relative latency,
               const struct GNUNET_PeerIdentity *peer,
               const struct GNUNET_MessageHeader *message)
{
  struct ConnectContext *ctx = cls;

  if (peer == NULL)
    {
      /* signal error */
      ctx->cb (ctx->cb_cls,
	       _("Failed to receive `HELLO' from peer\n"));
      GNUNET_TRANSPORT_disconnect (ctx->d1th);
      GNUNET_TRANSPORT_disconnect (ctx->d2th);
      GNUNET_free (ctx);
      return;
    }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received `%s' from transport service of `%4s'\n",
              "HELLO", GNUNET_i2s (peer));
  GNUNET_assert (message != NULL);
  GNUNET_TRANSPORT_offer_hello (ctx->d2th, message);
  GNUNET_CORE_notify_transmit_ready (ctx->d2->server,
				     0,
				     GNUNET_TIME_absolute_get_remaining (ctx->timeout),
				     &ctx->d1->id,
				     sizeof (struct GNUNET_MessageHeader),
				     &transmit_ready, ctx);
}


/**
 * Establish a connection between two GNUnet daemons.
 *
 * @param d1 handle for the first daemon
 * @param d2 handle for the second daemon
 * @param timeout how long is the connection attempt
 *        allowed to take?
 * @param cb function to call at the end
 * @param cb_cls closure for cb
 */
void GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
				     struct GNUNET_TESTING_Daemon *d2,
				     struct GNUNET_TIME_Relative timeout,
				     GNUNET_TESTING_NotifyCompletion cb,
				     void *cb_cls)
{
  struct ConnectContext *ctx;

  if ( (d1->server == NULL) ||
       (d2->server == NULL) )
    {
      cb (cb_cls, _("Peers are not fully running yet, can not connect!\n"));
      return;
    }
  ctx = GNUNET_malloc (sizeof(struct ConnectContext));
  ctx->d1 = d1;
  ctx->d2 = d2;
  ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  ctx->cb = cb;
  ctx->cb_cls = cb_cls;
  ctx->d1th = GNUNET_TRANSPORT_connect (d1->sched, d1->cfg, d1, NULL, NULL, NULL);
  if (ctx->d1th == NULL)
    {
      GNUNET_free (ctx);
      cb (cb_cls, _("Failed to connect to transport service!\n"));
      return;
    }
  ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched, d2->cfg, d2, NULL, NULL, NULL);
  if (ctx->d2th == NULL)
    {
      GNUNET_TRANSPORT_disconnect (ctx->d1th);
      GNUNET_free (ctx);
      cb (cb_cls, _("Failed to connect to transport service!\n"));
      return;
    }
  GNUNET_TRANSPORT_get_hello (ctx->d1th, 
			      timeout,
			      &process_hello, 
			      ctx);
}


/* end of testing.c */