summaryrefslogtreecommitdiff
path: root/src/util/test_anastasis_child_management.c
blob: c16670a721182c4073df7de64979fc1bf0708865 (plain)
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
/*
  This file is part of TALER
  Copyright (C) 2014-2020 Taler Systems SA

  TALER 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 3, or
  (at your option) any later version.

  TALER 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 TALER; see the file COPYING.  If not, see
  <http://www.gnu.org/licenses/>
*/

/**
 * @file lib/test_anastasis_child_management.c
 * @brief testcase to test the child management
 * @author Christian Grothoff
 * @author Dominik Meister
 */
#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include "anastasis_util_lib.h"


static struct ANASTASIS_ChildWaitHandle *cwh;

static int global_ret;

static struct GNUNET_OS_Process *pid;


static void
child_completed_callback (void *cls,
                          enum GNUNET_OS_ProcessStatusType type,
                          long unsigned int exit_code)
{
  cwh = NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Process extided with code: %lu \n",
              exit_code);
  FILE *file;
  char code[9];

  file = fopen ("child_management_test.txt", "r");
  if (NULL == file)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "could not find file: child_management_test.txt in %s:%u\n",
                __FILE__,
                __LINE__);
    global_ret = 1;
    return;
  }
  if (0 == fscanf (file,
                   "%8s",
                   code))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "could not read file: child_management_test.txt in %s:%u\n",
                __FILE__,
                __LINE__);
    global_ret = 1;
    return;
  }

  if (0 != strcmp ("12345678", code))
  {
    global_ret = 1;
    return;
  }
  GNUNET_OS_process_destroy (pid);
  pid = NULL;
  GNUNET_break (0 == unlink ("child_management_test.txt"));
  GNUNET_SCHEDULER_shutdown ();
  global_ret = 0;
}


static void
do_shutdown (void *cls)
{
  if (NULL != cwh)
  {
    ANASTASIS_wait_child_cancel (cwh);
    cwh = NULL;
  }
  if (NULL != pid)
  {
    GNUNET_assert (0 ==
                   GNUNET_OS_process_kill (pid,
                                           SIGKILL));
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_OS_process_wait (pid));
    GNUNET_OS_process_destroy (pid);
    pid = NULL;
  }
}


static void
test_child_management (void *cls)
{
  const char *command = "./child_management_test.sh";
  struct GNUNET_DISK_PipeHandle *p;
  struct GNUNET_DISK_FileHandle *out;

  (void) cls;
  p = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE);
  if (NULL == p)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                         "pipe");
    global_ret = 2;
    return;
  }
  pid = GNUNET_OS_start_process (0,
                                 p,
                                 NULL,
                                 NULL,
                                 command,
                                 command,
                                 "1234",
                                 "5678",
                                 NULL);
  if (NULL == pid)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
                         "fork");
    GNUNET_break (GNUNET_OK ==
                  GNUNET_DISK_pipe_close (p));
    global_ret = 1;
    return;
  }
  GNUNET_break (GNUNET_OK ==
                GNUNET_DISK_pipe_close_end (p,
                                            GNUNET_DISK_PIPE_END_READ));
  out = GNUNET_DISK_pipe_detach_end (p,
                                     GNUNET_DISK_PIPE_END_WRITE);
  GNUNET_assert (NULL != out);
  GNUNET_break (GNUNET_OK ==
                GNUNET_DISK_pipe_close (p));

  GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
                                 NULL);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"Alright");
  cwh = ANASTASIS_wait_child (pid,
                              &child_completed_callback,
                              cls);
  GNUNET_break (NULL != cwh);
  GNUNET_assert (5 ==
                 GNUNET_DISK_file_write (out,
                                         "Hello",
                                         5));
  GNUNET_break (GNUNET_OK ==
                GNUNET_DISK_file_close (out));
}


int
main (int argc,
      const char *const argv[])
{
  GNUNET_log_setup (argv[0],
                    "DEBUG",
                    NULL);
  GNUNET_SCHEDULER_run (&test_child_management,
                        NULL);
  return global_ret;
}


/* end of test_anastasis_child_management.c */