summaryrefslogtreecommitdiff
path: root/src/testing/testing_cmd_policy_create.c
blob: 62ad71fcb78fdf689ef152c72b907275b15d33e8 (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
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
/*
  This file is part of Anastasis
  Copyright (C) 2020 Anastasis SARL

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

  Anastasis 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file testing/testing_cmd_policy_create.c
 * @brief command to execute the anastasis secret share service
 * @author Christian Grothoff
 * @author Dennis Neufeld
 * @author Dominik Meister
 */

#include "platform.h"
#include "anastasis_testing_lib.h"
#include <taler/taler_util.h>
#include <taler/taler_testing_lib.h>


/**
 * State for a "policy create" CMD.
 */
struct PolicyCreateState
{
  /**
   * The interpreter state.
   */
  struct TALER_TESTING_Interpreter *is;

  /**
   * Label of this command.
   */
  const char *label;

  /**
   * References to upload commands of previous truth uploads.
   */
  const char **cmd_label_array;

  /**
   * Length of array of command labels (cmd_label_array).
   */
  unsigned int cmd_label_array_length;

  /**
   * Policy object
   */
  struct ANASTASIS_Policy *policy;
};


/**
 * Run a "policy create" CMD.
 *
 * @param cls closure.
 * @param cmd command currently being run.
 * @param is interpreter state.
 */
static void
policy_create_run (void *cls,
                   const struct TALER_TESTING_Command *cmd,
                   struct TALER_TESTING_Interpreter *is)
{
  struct PolicyCreateState *pcs = cls;
  const struct ANASTASIS_Truth *truths[pcs->cmd_label_array_length];

  GNUNET_assert (pcs->cmd_label_array_length > 0);
  GNUNET_assert (NULL != pcs->cmd_label_array);
  pcs->is = is;
  if (NULL != pcs->cmd_label_array)
  {
    for (unsigned int i = 0; i < pcs->cmd_label_array_length; i++)
    {
      const struct TALER_TESTING_Command *ref;
      const struct ANASTASIS_Truth *truth;

      ref = TALER_TESTING_interpreter_lookup_command (is,
                                                      pcs->cmd_label_array[i]);
      if (NULL == ref)
      {
        GNUNET_break (0);
        TALER_TESTING_interpreter_fail (pcs->is);
        return;
      }
      if (GNUNET_OK !=
          ANASTASIS_TESTING_get_trait_truth (ref,
                                             0,
                                             &truth))
      {
        GNUNET_break (0);
        TALER_TESTING_interpreter_fail (pcs->is);
        return;
      }
      GNUNET_assert (NULL != truth);
      truths[i] = truth;
    }
  }

  pcs->policy = ANASTASIS_policy_create (truths,
                                         pcs->cmd_label_array_length);

  if (NULL == pcs->policy)
  {
    GNUNET_break (0);
    TALER_TESTING_interpreter_fail (pcs->is);
    return;
  }
  TALER_TESTING_interpreter_next (pcs->is);
}


/**
 * Free the state of a "policy create" CMD, and possibly
 * cancel it if it did not complete.
 *
 * @param cls closure.
 * @param cmd command being freed.
 */
static void
policy_create_cleanup (void *cls,
                       const struct TALER_TESTING_Command *cmd)
{
  struct PolicyCreateState *pcs = cls;

  GNUNET_free (pcs->cmd_label_array);
  if (NULL != pcs->policy)
  {
    ANASTASIS_policy_destroy (pcs->policy);
    pcs->policy = NULL;
  }
  GNUNET_free (pcs);
}


/**
 * Offer internal data to other commands.
 *
 * @param cls closure
 * @param[out] ret result (could be anything)
 * @param trait name of the trait
 * @param index index number of the object to extract.
 * @return #GNUNET_OK on success
 */
static int
policy_create_traits (void *cls,
                      const void **ret,
                      const char *trait,
                      unsigned int index)
{
  struct PolicyCreateState *pcs = cls;
  struct TALER_TESTING_Trait traits[] = {
    ANASTASIS_TESTING_make_trait_policy (0,
                                         pcs->policy),
    TALER_TESTING_trait_end ()
  };

  return TALER_TESTING_get_trait (traits,
                                  ret,
                                  trait,
                                  index);
}


struct TALER_TESTING_Command
ANASTASIS_TESTING_cmd_policy_create (const char *label,
                                     ...)
{
  struct PolicyCreateState *pcs;
  va_list ap;
  const char *truth_upload_cmd;

  pcs = GNUNET_new (struct PolicyCreateState);
  pcs->label = label;

  va_start (ap,
            label);
  while (NULL != (truth_upload_cmd = va_arg (ap, const char *)))
  {
    GNUNET_array_append (pcs->cmd_label_array,
                         pcs->cmd_label_array_length,
                         truth_upload_cmd);
  }
  va_end (ap);
  {
    struct TALER_TESTING_Command cmd = {
      .cls = pcs,
      .label = label,
      .run = &policy_create_run,
      .cleanup = &policy_create_cleanup,
      .traits = &policy_create_traits
    };

    return cmd;
  }
}


/* end of testing_cmd_policy_create.c */