aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore/test_peerstore_api_store.c
blob: 978009dbbb8fb18911c3f58ff7a6d9d403314301 (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
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
/*
     This file is part of GNUnet.
     Copyright (C) GNUnet e.V.

     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 3, 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., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file peerstore/test_peerstore_api_store.c
 * @brief testcase for peerstore store operation
 */
#include "platform.h"
#include "gnunet_peerstore_service.h"
#include "gnunet_testing_lib.h"


static int ok = 1;

static struct GNUNET_PEERSTORE_Handle *h;

static char *subsystem = "test_peerstore_api_store";
static struct GNUNET_PeerIdentity pid;
static char *key = "test_peerstore_api_store_key";
static char *val1 = "test_peerstore_api_store_val1";
static char *val2 = "test_peerstore_api_store_val2-";
static char *val3 = "test_peerstore_api_store_val3--";

static int count = 0;


static void
test3_cont2 (void *cls,
             const struct GNUNET_PEERSTORE_Record *record,
             const char *emsg)
{
  if (NULL != emsg)
    return;
  if (NULL != record)
  {
    GNUNET_assert ((strlen (val3) + 1) == record->value_size);
    GNUNET_assert (0 == strcmp ((char *) val3,
                                (char *) record->value));
    count++;
    return;
  }
  GNUNET_assert (count == 1);
  ok = 0;
  GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
  GNUNET_SCHEDULER_shutdown ();
}


static void
test3_cont (void *cls,
            int success)
{
  if (GNUNET_YES != success)
    return;
  count = 0;
  GNUNET_PEERSTORE_iterate (h,
                            subsystem,
                            &pid,
                            key,
                            GNUNET_TIME_UNIT_SECONDS,
                            &test3_cont2,
                            NULL);
}


/**
 * Replace the previous 2 records
 */
static void
test3 ()
{
  GNUNET_PEERSTORE_store (h,
                          subsystem,
                          &pid,
                          key,
                          val3,
                          strlen (val3) + 1,
                          GNUNET_TIME_UNIT_FOREVER_ABS,
                          GNUNET_PEERSTORE_STOREOPTION_REPLACE,
                          &test3_cont,
                          NULL);
}


static void
test2_cont2 (void *cls,
             const struct GNUNET_PEERSTORE_Record *record,
             const char *emsg)
{
  if (NULL != emsg)
    return;
  if (NULL != record)
  {
    GNUNET_assert (((strlen (val1) + 1) == record->value_size) ||
                   ((strlen (val2) + 1) == record->value_size));
    GNUNET_assert ((0 == strcmp ((char *) val1, (char *) record->value)) ||
                   (0 == strcmp ((char *) val2, (char *) record->value)));
    count++;
    return;
  }
  GNUNET_assert (count == 2);
  count = 0;
  test3 ();
}


static void
test2_cont (void *cls, int success)
{
  if (GNUNET_YES != success)
    return;
  count = 0;
  GNUNET_PEERSTORE_iterate (h,
                            subsystem,
                            &pid, key,
                            GNUNET_TIME_UNIT_SECONDS,
                            &test2_cont2, NULL);
}


/**
 * Test storing a second value with the same key
 */
void
test2 ()
{
  GNUNET_PEERSTORE_store (h,
                          subsystem,
                          &pid,
                          key,
                          val2,
                          strlen (val2) + 1,
                          GNUNET_TIME_UNIT_FOREVER_ABS,
                          GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
                          &test2_cont,
                          NULL);
}


static void
test1_cont2 (void *cls,
             const struct GNUNET_PEERSTORE_Record *record,
             const char *emsg)
{
  if (NULL != emsg)
    return;
  if (NULL != record)
  {
    GNUNET_assert ((strlen (val1) + 1) == record->value_size);
    GNUNET_assert (0 == strcmp ((char *) val1, (char *) record->value));
    count++;
    return;
  }
  GNUNET_assert (count == 1);
  count = 0;
  test2 ();
}


static void
test1_cont (void *cls, int success)
{
  if (GNUNET_YES != success)
    return;
  count = 0;
  GNUNET_PEERSTORE_iterate (h,
                            subsystem,
                            &pid,
                            key,
                            GNUNET_TIME_UNIT_SECONDS,
                            &test1_cont2,
                            NULL);
}


/**
 * Store a single record
 */
static void
test1 ()
{
  GNUNET_PEERSTORE_store (h,
                          subsystem,
                          &pid,
                          key,
                          val1,
                          strlen (val1) + 1,
                          GNUNET_TIME_UNIT_FOREVER_ABS,
                          GNUNET_PEERSTORE_STOREOPTION_REPLACE,
                          &test1_cont,
                          NULL);
}


static void
run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
     struct GNUNET_TESTING_Peer *peer)
{
  h = GNUNET_PEERSTORE_connect (cfg);
  GNUNET_assert (NULL != h);
  memset (&pid, 1, sizeof (pid));
  test1 ();
}


int
main (int argc, char *argv[])
{
  if (0 !=
      GNUNET_TESTING_service_run ("test-gnunet-peerstore",
                                  "peerstore",
                                  "test_peerstore_api_data.conf",
                                  &run, NULL))
    return 1;
  return ok;
}

/* end of test_peerstore_api_store.c */