aboutsummaryrefslogtreecommitdiff
path: root/src/service/messenger/messenger_api_cmd_start_service.c
blob: f764d31c2f3470bdbfb8e64ef877e5085483f603 (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
/*
   This file is part of GNUnet
   Copyright (C) 2023--2024 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   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
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file messenger_api_cmd_start_service.c
 * @brief cmd to start a messenger service.
 * @author Tobias Frisch
 */

#include "gnunet_util_lib.h"
#include "gnunet_testing_ng_lib.h"
#include "gnunet_testing_netjail_lib.h"
#include "gnunet_transport_testing_ng_lib.h"
#include "gnunet_messenger_service.h"
#include "messenger-testing-cmds.h"

static void
on_message_cb (void *cls,
               struct GNUNET_MESSENGER_Room *room,
               const struct GNUNET_MESSENGER_Contact *sender,
               const struct GNUNET_MESSENGER_Contact *recipient,
               const struct GNUNET_MESSENGER_Message *message,
               const struct GNUNET_HashCode *hash,
               enum GNUNET_MESSENGER_MessageFlags flags)
{
  struct GNUNET_MESSENGER_StartServiceState *sss = cls;

  const struct GNUNET_HashCode *key = GNUNET_MESSENGER_room_get_key (room);
  struct GNUNET_MESSENGER_RoomState *rs;

  rs = GNUNET_CONTAINER_multihashmap_get (sss->rooms, key);
  if (! rs)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Testing library failed to find room state\n");
    GNUNET_TESTING_interpreter_fail (sss->is);
    return;
  }

  if (GNUNET_MESSENGER_KIND_PEER != message->header.kind)
    return;

  if (GNUNET_OK != GNUNET_CONTAINER_multipeermap_put (rs->doors,
                                                      &(message->body.peer.peer)
                                                      ,
                                                      NULL,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Testing library failed to register peer identity as found door\n");
    GNUNET_TESTING_interpreter_fail (sss->is);
    return;
  }
}


static void
start_service_run (void *cls,
                   struct GNUNET_TESTING_Interpreter *is)
{
  struct GNUNET_MESSENGER_StartServiceState *sss = cls;

  sss->is = is;

  const struct GNUNET_TESTING_Command *peer_cmd;
  peer_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
                                                        sss->peer_label);

  const struct GNUNET_TESTING_StartPeerState *sps;
  GNUNET_TRANSPORT_TESTING_get_trait_state (peer_cmd, &sps);

  const struct GNUNET_TESTING_Command *system_cmd;
  system_cmd = GNUNET_TESTING_interpreter_lookup_command (is,
                                                          sss->system_label);

  const struct GNUNET_TESTING_System *tl_system;
  GNUNET_TESTING_get_trait_test_system (system_cmd,
                                        &tl_system);

  sss->tl_system = tl_system;

  sss->msg = GNUNET_MESSENGER_connect (sps->cfg, NULL, NULL, on_message_cb,
                                       sss);
  if (! sss->msg)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Testing library failed to connect to messenger service\n");
    GNUNET_TESTING_interpreter_fail (is);
    return;
  }

  sss->rooms = GNUNET_CONTAINER_multihashmap_create (
    sss->topology->stage_amount, GNUNET_NO);
}


static void
start_service_cleanup (void *cls)
{
  struct GNUNET_MESSENGER_StartServiceState *sss = cls;

  GNUNET_free (sss->system_label);
  GNUNET_free (sss->peer_label);
  GNUNET_free (sss);
}


static enum GNUNET_GenericReturnValue
start_service_traits (void *cls,
                      const void **ret,
                      const char *trait,
                      unsigned int index)
{
  struct GNUNET_MESSENGER_StartServiceState *sss = cls;

  struct GNUNET_TESTING_Trait traits[] = {
    GNUNET_MESSENGER_make_trait_state ((void *) sss),
    GNUNET_TESTING_trait_end ()
  };

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


struct GNUNET_TESTING_Command
GNUNET_MESSENGER_cmd_start_service (const char *label,
                                    const char *peer_label,
                                    const char *system_label,
                                    struct GNUNET_MESSENGER_TestStageTopology *
                                    topology,
                                    unsigned int peer_index)
{
  struct GNUNET_MESSENGER_StartServiceState *sss;

  sss = GNUNET_new (struct GNUNET_MESSENGER_StartServiceState);
  sss->peer_label = GNUNET_strdup (peer_label);
  sss->system_label = GNUNET_strdup (system_label);
  sss->topology = topology;

  sss->is = NULL;
  sss->tl_system = NULL;
  sss->msg = NULL;
  sss->rooms = NULL;
  sss->peer_index = peer_index;
  sss->stage_index = 0;

  return GNUNET_TESTING_command_new (sss,
                                     label,
                                     &start_service_run,
                                     &start_service_cleanup,
                                     &start_service_traits);
}