aboutsummaryrefslogtreecommitdiff
path: root/src/lib/hello/test_hello-uri.c
blob: 9f3179270a12975448cbfb38146cbdd9a3423b9d (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
/*
     This file is part of GNUnet.
     Copyright (C) 2022 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 hello/test_hello-uri.c
 * @brief test for helper library for handling URI-based HELLOs
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_signatures.h"
#include "gnunet_util_lib.h"
#include "gnunet_hello_uri_lib.h"


/**
 * Check for expected URIs.
 *
 * @param cls a `unsigned int*`, bitmask set to found URIs
 * @param uri URI to check for
 */
static void
check_uris (void *cls,
            const struct GNUNET_PeerIdentity *pid,
            const char *uri)
{
  unsigned int *found = cls;

  if (0 == strcmp (uri,
                   "test://address"))
    *found |= 1;
  else if (0 == strcmp (uri,
                        "test://more"))
    *found |= 2;
  else
    *found = (unsigned int) -1;
}


int
main (int argc,
      char *argv[])
{
  struct GNUNET_PeerIdentity pid;
  struct GNUNET_HELLO_Builder *b;
  struct GNUNET_CRYPTO_EddsaPrivateKey priv;

  GNUNET_log_setup ("test-hell-uri",
                    "WARNING",
                    NULL);
  GNUNET_CRYPTO_eddsa_key_create (&priv);
  GNUNET_CRYPTO_eddsa_key_get_public (&priv,
                                      &pid.public_key);
  b = GNUNET_HELLO_builder_new (&pid);
  GNUNET_assert (GNUNET_SYSERR ==
                 GNUNET_HELLO_builder_add_address (b,
                                                   "invalid"));
  GNUNET_assert (GNUNET_SYSERR ==
                 GNUNET_HELLO_builder_add_address (b,
                                                   "i%v://bla"));
  GNUNET_assert (GNUNET_SYSERR ==
                 GNUNET_HELLO_builder_add_address (b,
                                                   "://empty"));
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_HELLO_builder_add_address (b,
                                                   "test://address"));
  GNUNET_assert (GNUNET_NO ==
                 GNUNET_HELLO_builder_add_address (b,
                                                   "test://address"));
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_HELLO_builder_add_address (b,
                                                   "test://more"));
  {
    void *block;
    size_t block_size = 0;
    struct GNUNET_HELLO_Builder *b2;
    const struct GNUNET_PeerIdentity *p2;
    unsigned int found;

    GNUNET_assert (GNUNET_NO ==
                   GNUNET_HELLO_builder_to_block (b,
                                                  &priv,
                                                  NULL,
                                                  &block_size,
                                                  GNUNET_TIME_UNIT_FOREVER_REL));
    GNUNET_assert (GNUNET_NO ==
                   GNUNET_HELLO_builder_to_block (b,
                                                  &priv,
                                                  NULL,
                                                  &block_size,
                                                  GNUNET_TIME_UNIT_FOREVER_REL));
    GNUNET_assert (0 != block_size);
    block = GNUNET_malloc (block_size);
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_HELLO_builder_to_block (b,
                                                  &priv,
                                                  block,
                                                  &block_size,
                                                  GNUNET_TIME_UNIT_FOREVER_REL));
    b2 = GNUNET_HELLO_builder_from_block (block,
                                          block_size);
    GNUNET_free (block);
    GNUNET_assert (NULL != b2);
    found = 0;
    p2 = GNUNET_HELLO_builder_iterate (b2,
                                       &check_uris,
                                       &found);
    GNUNET_assert (3 == found);
    GNUNET_assert (0 ==
                   GNUNET_memcmp (p2,
                                  &pid));
    GNUNET_HELLO_builder_free (b2);
  }

  {
    char *url;
    struct GNUNET_HELLO_Builder *b2;
    const struct GNUNET_PeerIdentity *p2;
    unsigned int found;

    url = GNUNET_HELLO_builder_to_url (b,
                                       &priv);
    b2 = GNUNET_HELLO_builder_from_url (url);
    GNUNET_free (url);
    GNUNET_assert (NULL != b2);
    found = 0;
    p2 = GNUNET_HELLO_builder_iterate (b2,
                                       &check_uris,
                                       &found);
    GNUNET_assert (3 == found);
    GNUNET_assert (0 ==
                   GNUNET_memcmp (p2,
                                  &pid));
    GNUNET_HELLO_builder_free (b2);
  }

  {
    struct GNUNET_MQ_Envelope *env;
    struct GNUNET_HELLO_Builder *b2;
    const struct GNUNET_PeerIdentity *p2;
    unsigned int found;

    env = GNUNET_HELLO_builder_to_env (b,
                                       &priv,
                                       GNUNET_TIME_UNIT_FOREVER_REL);
    b2 = GNUNET_HELLO_builder_from_msg (GNUNET_MQ_env_get_msg (env));
    GNUNET_free (env);
    GNUNET_assert (NULL != b2);
    found = 0;
    p2 = GNUNET_HELLO_builder_iterate (b2,
                                       &check_uris,
                                       &found);
    GNUNET_assert (3 == found);
    GNUNET_assert (0 ==
                   GNUNET_memcmp (p2,
                                  &pid));
    GNUNET_HELLO_builder_free (b2);
  }

  GNUNET_HELLO_builder_free (b);

  GNUNET_CRYPTO_mpi_print_unsigned (priv.d,
                                    sizeof (priv.d),
                                    GCRYMPI_CONST_ONE);
  priv.d[0] &= 248;
  priv.d[31] &= 127;
  priv.d[31] |= 64;
  {
    char *buf;

    buf = GNUNET_STRINGS_data_to_string_alloc (&priv,
                                               sizeof (priv));
    fprintf (stderr,
             "PK: %s\n",
             buf);
    GNUNET_free (buf);
  }
  GNUNET_CRYPTO_eddsa_key_get_public (&priv,
                                      &pid.public_key);
  b = GNUNET_HELLO_builder_new (&pid);
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_HELLO_builder_add_address (b,
                                                   "a://first"));
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_HELLO_builder_add_address (b,
                                                   "b://second"));
  {
    char *url;

    url = GNUNET_HELLO_builder_to_url (b,
                                       &priv);
    fprintf (stderr,
             "TV: %s\n",
             url);
    GNUNET_free (url);
  }
  GNUNET_HELLO_builder_free (b);

  return 0;
}