aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/test_namestore_record_serialization.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2012-02-28 15:33:38 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2012-02-28 15:33:38 +0000
commitefdbca9f6d63d177280669211448683aa033d92b (patch)
tree074fab69a005aa8e1d4a5ded4b40a7a9b1ec1813 /src/namestore/test_namestore_record_serialization.c
parent30fc6be8600f0920803fa71233957dd7d1e9f058 (diff)
downloadgnunet-efdbca9f6d63d177280669211448683aa033d92b.tar.gz
gnunet-efdbca9f6d63d177280669211448683aa033d92b.zip
- record serialization + test
Diffstat (limited to 'src/namestore/test_namestore_record_serialization.c')
-rw-r--r--src/namestore/test_namestore_record_serialization.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/namestore/test_namestore_record_serialization.c b/src/namestore/test_namestore_record_serialization.c
new file mode 100644
index 000000000..5ea345bef
--- /dev/null
+++ b/src/namestore/test_namestore_record_serialization.c
@@ -0,0 +1,157 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
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 namestore/test_namestore_record_serialization.c
22 * @brief testcase for test_namestore_record_serialization.c
23 */
24#include "platform.h"
25#include "gnunet_common.h"
26#include "gnunet_namestore_service.h"
27#include "namestore.h"
28
29#define VERBOSE GNUNET_NO
30
31#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32
33static int res;
34
35static void
36run (void *cls, char *const *args, const char *cfgfile,
37 const struct GNUNET_CONFIGURATION_Handle *cfg)
38{
39 char * dest = NULL;
40 size_t len;
41 int c;
42 int elem = 0;
43
44 int rd_count = 3;
45 size_t data_len;
46 struct GNUNET_NAMESTORE_RecordData src[rd_count];
47 struct GNUNET_NAMESTORE_RecordData *dst = NULL;
48
49 memset(src, '\0', rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
50
51 data_len = 0;
52 for (c = 0; c < rd_count; c++)
53 {
54 src[c].record_type = c+1;
55 src[c].data_size = data_len;
56 src[c].data = GNUNET_malloc (data_len);
57
58 /* Setting data to data_len * record_type */
59 memset ((char *) src[c].data, 'a', data_len);
60 data_len += 10;
61 }
62 res = 0;
63
64 len = GNUNET_NAMESTORE_records_serialize (&dest, rd_count, src);
65 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serialized data len: %u\n",len);
66
67 GNUNET_assert (dest != NULL);
68
69 elem = GNUNET_NAMESTORE_records_deserialize(&dst, dest, len);
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserialized elements: %u\n",elem);
71
72 GNUNET_assert (elem == rd_count);
73 GNUNET_assert (dst != NULL);
74
75 for (c = 0; c < elem; c++)
76 {
77 if (src[c].data_size != dst[c].data_size)
78 {
79 GNUNET_break (0);
80 res = 1;
81 }
82 if (GNUNET_TIME_absolute_get_difference(src[c].expiration, dst[c].expiration).rel_value != GNUNET_TIME_relative_get_zero().rel_value)
83 {
84 GNUNET_break (0);
85 res = 1;
86 }
87 if (src[c].flags != dst[c].flags)
88 {
89 GNUNET_break (0);
90 res = 1;
91 }
92 if (src[c].record_type != dst[c].record_type)
93 {
94 GNUNET_break (0);
95 res = 1;
96 }
97
98 size_t data_size = src[c].data_size;
99 char data[data_size];
100 memset (data, 'a', data_size);
101 if (0 != memcmp (data, dst[c].data, data_size))
102 {
103 GNUNET_break (0);
104 res = 1;
105 }
106 if (0 != memcmp (data, src[c].data, data_size))
107 {
108 GNUNET_break (0);
109 res = 1;
110 }
111 if (0 != memcmp (src[c].data, dst[c].data, src[c].data_size))
112 {
113 GNUNET_break (0);
114 res = 1;
115 }
116
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c);
118 /* clean up */
119 GNUNET_free((char *) dst[c].data);
120 GNUNET_free((char *) src[c].data);
121 }
122 GNUNET_free (dest);
123 GNUNET_free (dst);
124}
125
126static int
127check ()
128{
129 static char *const argv[] = { "test_namestore_record_serialization",
130 "-c",
131 "test_namestore_api.conf",
132#if VERBOSE
133 "-L", "DEBUG",
134#endif
135 NULL
136 };
137 static struct GNUNET_GETOPT_CommandLineOption options[] = {
138 GNUNET_GETOPT_OPTION_END
139 };
140
141 res = 1;
142 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_record_serialization",
143 "nohelp", options, &run, &res);
144 return res;
145}
146
147int
148main (int argc, char *argv[])
149{
150 int ret;
151
152 ret = check ();
153
154 return ret;
155}
156
157/* end of test_namestore_record_serialization.c */