aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-06-04 19:03:43 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-06-04 19:03:43 +0000
commitdd715127aa919fdc66f1854d0db81560d5506d54 (patch)
tree4491b87b0e26d3e590c49299c3d0eeb4185f24ea /src/peerstore
parentd5aa4968b0307c3e89db1f6deb6f029d2874f626 (diff)
downloadgnunet-dd715127aa919fdc66f1854d0db81560d5506d54.tar.gz
gnunet-dd715127aa919fdc66f1854d0db81560d5506d54.zip
peerstore: db index + store test
Diffstat (limited to 'src/peerstore')
-rw-r--r--src/peerstore/Makefile.am8
-rw-r--r--src/peerstore/peerstore_api.c2
-rw-r--r--src/peerstore/plugin_peerstore_sqlite.c14
-rw-r--r--src/peerstore/test_peerstore_api.c144
-rw-r--r--src/peerstore/test_peerstore_api_store.c222
5 files changed, 239 insertions, 151 deletions
diff --git a/src/peerstore/Makefile.am b/src/peerstore/Makefile.am
index b7b7f3ea5..e65817569 100644
--- a/src/peerstore/Makefile.am
+++ b/src/peerstore/Makefile.am
@@ -65,16 +65,16 @@ libgnunet_plugin_peerstore_sqlite_la_DEPENDENCIES = \
65 libgnunetpeerstore.la 65 libgnunetpeerstore.la
66 66
67check_PROGRAMS = \ 67check_PROGRAMS = \
68 test_peerstore_api 68 test_peerstore_api_store
69 69
70if ENABLE_TEST_RUN 70if ENABLE_TEST_RUN
71AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH; 71AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;
72TESTS = $(check_PROGRAMS) 72TESTS = $(check_PROGRAMS)
73endif 73endif
74 74
75test_peerstore_api_SOURCES = \ 75test_peerstore_api_store_SOURCES = \
76 test_peerstore_api.c 76 test_peerstore_api_store.c
77test_peerstore_api_LDADD = \ 77test_peerstore_api_store_LDADD = \
78 $(top_builddir)/src/peerstore/libgnunetpeerstore.la \ 78 $(top_builddir)/src/peerstore/libgnunetpeerstore.la \
79 $(top_builddir)/src/testing/libgnunettesting.la \ 79 $(top_builddir)/src/testing/libgnunettesting.la \
80 $(top_builddir)/src/util/libgnunetutil.la 80 $(top_builddir)/src/util/libgnunetutil.la
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c
index 8797d7818..0143c58cc 100644
--- a/src/peerstore/peerstore_api.c
+++ b/src/peerstore/peerstore_api.c
@@ -423,7 +423,7 @@ GNUNET_PEERSTORE_store (struct GNUNET_PEERSTORE_Handle *h,
423 GNUNET_PEERSTORE_Continuation cont, 423 GNUNET_PEERSTORE_Continuation cont,
424 void *cont_cls) 424 void *cont_cls)
425{ 425{
426 struct GNUNET_MQ_Envelope *ev; //FIXME: add 'replace' flag in store function (similar to multihashmap) 426 struct GNUNET_MQ_Envelope *ev;
427 struct GNUNET_PEERSTORE_StoreContext *sc; 427 struct GNUNET_PEERSTORE_StoreContext *sc;
428 428
429 LOG (GNUNET_ERROR_TYPE_DEBUG, 429 LOG (GNUNET_ERROR_TYPE_DEBUG,
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c
index 38e72df51..3dd67024b 100644
--- a/src/peerstore/plugin_peerstore_sqlite.c
+++ b/src/peerstore/plugin_peerstore_sqlite.c
@@ -51,8 +51,6 @@
51 51
52#define LOG(kind,...) GNUNET_log_from (kind, "peerstore-sqlite", __VA_ARGS__) 52#define LOG(kind,...) GNUNET_log_from (kind, "peerstore-sqlite", __VA_ARGS__)
53 53
54//FIXME: Indexes
55
56/** 54/**
57 * Context for all functions in this plugin. 55 * Context for all functions in this plugin.
58 */ 56 */
@@ -459,6 +457,18 @@ database_setup (struct Plugin *plugin)
459 457
460 sqlite3_create_function(plugin->dbh, "UINT64_LT", 2, SQLITE_UTF8, NULL, &sqlite3_lessthan, NULL, NULL); 458 sqlite3_create_function(plugin->dbh, "UINT64_LT", 2, SQLITE_UTF8, NULL, &sqlite3_lessthan, NULL, NULL);
461 459
460 /* Create Indices */
461 if (SQLITE_OK !=
462 sqlite3_exec(plugin->dbh,
463 "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)",
464 NULL, NULL, NULL))
465 {
466 LOG (GNUNET_ERROR_TYPE_ERROR,
467 _("Unable to create indices: %s.\n"),
468 sqlite3_errmsg (plugin->dbh));
469 return GNUNET_SYSERR;
470 }
471
462 /* Prepare statements */ 472 /* Prepare statements */
463 473
464 sql_prepare (plugin->dbh, 474 sql_prepare (plugin->dbh,
diff --git a/src/peerstore/test_peerstore_api.c b/src/peerstore/test_peerstore_api.c
deleted file mode 100644
index e4f6225c6..000000000
--- a/src/peerstore/test_peerstore_api.c
+++ /dev/null
@@ -1,144 +0,0 @@
1/*
2 This file is part of GNUnet.
3 (C)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file peerstore/test_peerstore_api.c
22 * @brief testcase for peerstore_api.c
23 */
24#include "platform.h"
25#include "gnunet_util_lib.h"
26#include "gnunet_testing_lib.h"
27#include "gnunet_peerstore_service.h"
28#include <inttypes.h>
29
30//TODO: test single cycle of watch, store, iterate
31
32static int ok = 1;
33
34static int counter = 0;
35
36struct GNUNET_PEERSTORE_Handle *h;
37
38int iterate_cb (void *cls,
39 struct GNUNET_PEERSTORE_Record *record,
40 char *emsg)
41{
42 if(NULL != emsg)
43 {
44 printf("Error received: %s.\n", emsg);
45 return GNUNET_YES;
46 }
47 printf("Record:\n");
48 if(NULL == record)
49 {
50 GNUNET_assert(counter > 0);
51 counter = 0;
52 printf("END\n");
53 GNUNET_PEERSTORE_disconnect(h);
54 return GNUNET_YES;
55 }
56 printf("Sub system: %s\n", record->sub_system);
57 printf("Peer: %s\n", GNUNET_i2s (record->peer));
58 printf("Key: %s\n", record->key);
59 printf("Value: %.*s\n", (int)record->value_size, (char *)record->value);
60 printf("Expiry: %" PRIu64 "\n", record->expiry->abs_value_us);
61 counter ++;
62
63 return GNUNET_YES;
64}
65
66void store_cont(void *cls, int success)
67{
68 if(GNUNET_OK == success)
69 ok = 0;
70 else
71 ok = 1;
72 printf("Store success: %d\n", success);
73 GNUNET_PEERSTORE_iterate(h, "peerstore-test",
74 NULL,
75 NULL,
76 GNUNET_TIME_UNIT_FOREVER_REL,
77 &iterate_cb,
78 NULL);
79}
80
81int watch_cb (void *cls,
82 struct GNUNET_PEERSTORE_Record *record,
83 char *emsg)
84{
85 if(NULL != emsg)
86 {
87 printf("Error received: %s.\n", emsg);
88 return GNUNET_YES;
89 }
90
91 printf("Watch Record:\n");
92 printf("Sub system: %s\n", record->sub_system);
93 printf("Peer: %s\n", GNUNET_i2s (record->peer));
94 printf("Key: %s\n", record->key);
95 printf("Value: %.*s\n", (int)record->value_size, (char *)record->value);
96 printf("Expiry: %" PRIu64 "\n", record->expiry->abs_value_us);
97 return GNUNET_YES;
98}
99
100static void
101run (void *cls,
102 const struct GNUNET_CONFIGURATION_Handle *cfg,
103 struct GNUNET_TESTING_Peer *peer)
104{
105 struct GNUNET_PeerIdentity pid;
106 char *val = "peerstore-test-value";
107 size_t val_size = strlen(val);
108 struct GNUNET_TIME_Absolute expiry;
109
110 ok = 1;
111 memset (&pid, 32, sizeof (pid));
112 expiry = GNUNET_TIME_absolute_get();
113 h = GNUNET_PEERSTORE_connect(cfg);
114 GNUNET_assert(NULL != h);
115 GNUNET_PEERSTORE_watch(h,
116 "peerstore-test",
117 &pid,
118 "peerstore-test-key",
119 &watch_cb,
120 NULL);
121 GNUNET_PEERSTORE_store(h,
122 "peerstore-test",
123 &pid,
124 "peerstore-test-key",
125 val,
126 val_size,
127 expiry,
128 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
129 &store_cont,
130 NULL);
131}
132
133int
134main (int argc, char *argv[])
135{
136 if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerstore",
137 "peerstore",
138 "test_peerstore_api_data.conf",
139 &run, NULL))
140 return 1;
141 return ok;
142}
143
144/* end of test_peerstore_api.c */
diff --git a/src/peerstore/test_peerstore_api_store.c b/src/peerstore/test_peerstore_api_store.c
new file mode 100644
index 000000000..d3579947b
--- /dev/null
+++ b/src/peerstore/test_peerstore_api_store.c
@@ -0,0 +1,222 @@
1/*
2 This file is part of GNUnet.
3 (C)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20/**
21 * @file peerstore/test_peerstore_api.c
22 * @brief testcase for peerstore_api.c
23 */
24#include "platform.h"
25#include "gnunet_util_lib.h"
26#include "gnunet_testing_lib.h"
27#include "gnunet_peerstore_service.h"
28
29static int ok = 1;
30
31struct GNUNET_PEERSTORE_Handle *h;
32
33char *subsystem = "test_peerstore_api_store";
34struct GNUNET_PeerIdentity pid;
35char *key = "test_peerstore_api_store_key";
36char *val1 = "test_peerstore_api_store_val1";
37char *val2 = "test_peerstore_api_store_val2-";
38char *val3 = "test_peerstore_api_store_val3--";
39
40int count = 0;
41
42int test3_cont2(void *cls,
43 struct GNUNET_PEERSTORE_Record *record,
44 char *emsg)
45{
46 if(NULL != emsg)
47 return GNUNET_NO;
48 if(NULL != record)
49 {
50 GNUNET_assert((strlen(val3) + 1) == record->value_size);
51 GNUNET_assert(0 == strcmp((char *)val3, (char *)record->value));
52 count++;
53 return GNUNET_YES;
54 }
55 GNUNET_assert(count == 1);
56 ok = 0;
57 GNUNET_PEERSTORE_disconnect(h);
58 GNUNET_SCHEDULER_shutdown();
59 return GNUNET_YES;
60}
61
62static void
63test3_cont(void *cls, int success)
64{
65 if(GNUNET_YES != success)
66 return;
67 count = 0;
68 GNUNET_PEERSTORE_iterate(h,
69 subsystem,
70 &pid,
71 key,
72 GNUNET_TIME_UNIT_SECONDS,
73 &test3_cont2,
74 NULL);
75}
76
77/**
78 * Replace the previous 2 records
79 */
80static void
81test3()
82{
83 GNUNET_PEERSTORE_store(h,
84 subsystem,
85 &pid,
86 key,
87 val3,
88 strlen(val3) + 1,
89 GNUNET_TIME_UNIT_FOREVER_ABS,
90 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
91 &test3_cont,
92 NULL);
93}
94
95int test2_cont2(void *cls,
96 struct GNUNET_PEERSTORE_Record *record,
97 char *emsg)
98{
99 if(NULL != emsg)
100 return GNUNET_NO;
101 if(NULL != record)
102 {
103 GNUNET_assert(((strlen(val1) + 1) == record->value_size)
104 || ((strlen(val2) + 1) == record->value_size));
105 GNUNET_assert((0 == strcmp((char *)val1, (char *)record->value))
106 || (0 == strcmp((char *)val2, (char *)record->value)));
107 count++;
108 return GNUNET_YES;
109 }
110 GNUNET_assert(count == 2);
111 count = 0;
112 test3();
113 return GNUNET_YES;
114}
115
116static void
117test2_cont(void *cls, int success)
118{
119 if(GNUNET_YES != success)
120 return;
121 count = 0;
122 GNUNET_PEERSTORE_iterate(h,
123 subsystem,
124 &pid,
125 key,
126 GNUNET_TIME_UNIT_SECONDS,
127 &test2_cont2,
128 NULL);
129}
130
131/**
132 * Test storing a second value with the same key
133 */
134void test2()
135{
136 GNUNET_PEERSTORE_store(h,
137 subsystem,
138 &pid,
139 key,
140 val2,
141 strlen(val2) + 1,
142 GNUNET_TIME_UNIT_FOREVER_ABS,
143 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
144 &test2_cont,
145 NULL);
146}
147
148int test1_cont2(void *cls,
149 struct GNUNET_PEERSTORE_Record *record,
150 char *emsg)
151{
152 if(NULL != emsg)
153 return GNUNET_NO;
154 if(NULL != record)
155 {
156 GNUNET_assert((strlen(val1) + 1) == record->value_size);
157 GNUNET_assert(0 == strcmp((char *)val1, (char *)record->value));
158 count++;
159 return GNUNET_YES;
160 }
161 GNUNET_assert(count == 1);
162 count = 0;
163 test2();
164 return GNUNET_YES;
165}
166
167static void
168test1_cont(void *cls, int success)
169{
170 if(GNUNET_YES != success)
171 return;
172 count = 0;
173 GNUNET_PEERSTORE_iterate(h,
174 subsystem,
175 &pid,
176 key,
177 GNUNET_TIME_UNIT_SECONDS,
178 &test1_cont2,
179 NULL);
180}
181
182/**
183 * Store a single record
184 */
185static void
186test1()
187{
188 GNUNET_PEERSTORE_store(h,
189 subsystem,
190 &pid,
191 key,
192 val1,
193 strlen(val1) + 1,
194 GNUNET_TIME_UNIT_FOREVER_ABS,
195 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
196 &test1_cont,
197 NULL);
198}
199
200static void
201run (void *cls,
202 const struct GNUNET_CONFIGURATION_Handle *cfg,
203 struct GNUNET_TESTING_Peer *peer)
204{
205 h = GNUNET_PEERSTORE_connect(cfg);
206 GNUNET_assert(NULL != h);
207 memset (&pid, 1, sizeof (pid));
208 test1();
209}
210
211int
212main (int argc, char *argv[])
213{
214 if (0 != GNUNET_TESTING_service_run ("test-gnunet-peerstore",
215 "peerstore",
216 "test_peerstore_api_data.conf",
217 &run, NULL))
218 return 1;
219 return ok;
220}
221
222/* end of test_peerstore_api.c */