renaming GNUNET_TIME_relative_get_forever and GNUNET_TIME_absolute_get_forever method...
[oweals/gnunet.git] / src / namestore / test_namestore_record_serialization.c
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
33 static int res;
34
35 static void
36 run (void *cls, char *const *args, const char *cfgfile,
37      const struct GNUNET_CONFIGURATION_Handle *cfg)
38 {
39   size_t len;
40   int c;
41
42   int rd_count = 3;
43   size_t data_len;
44   struct GNUNET_NAMESTORE_RecordData src[rd_count];
45
46   memset(src, '\0', rd_count * sizeof (struct GNUNET_NAMESTORE_RecordData));
47
48   data_len = 0;
49   for (c = 0; c < rd_count; c++)
50   {
51     src[c].record_type = c+1;
52     src[c].data_size = data_len;
53     src[c].data = GNUNET_malloc (data_len);
54
55     /* Setting data to data_len * record_type */
56     memset ((char *) src[c].data, 'a', data_len);
57     data_len += 10;
58   }
59   res = 0;
60
61   len = GNUNET_NAMESTORE_records_get_size(rd_count, src);
62   char rd_ser[len];
63   GNUNET_assert (len == GNUNET_NAMESTORE_records_serialize(rd_count, src, len, rd_ser));
64
65   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serialized data len: %u\n",len);
66
67   GNUNET_assert (rd_ser != NULL);
68
69   struct GNUNET_NAMESTORE_RecordData dst[rd_count];
70   GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_records_deserialize (len, rd_ser, rd_count, dst));
71
72   GNUNET_assert (dst != NULL);
73
74   for (c = 0; c < rd_count; c++)
75   {
76     if (src[c].data_size != dst[c].data_size)
77     {
78       GNUNET_break (0);
79       res = 1;
80     }
81     if (0 != GNUNET_TIME_absolute_get_difference(src[c].expiration, dst[c].expiration).rel_value)
82     {
83       GNUNET_break (0);
84       res = 1;
85     }
86     if (src[c].flags != dst[c].flags)
87     {
88       GNUNET_break (0);
89       res = 1;
90     }
91     if (src[c].record_type != dst[c].record_type)
92     {
93       GNUNET_break (0);
94       res = 1;
95     }
96
97     size_t data_size = src[c].data_size;
98     char data[data_size];
99     memset (data, 'a', data_size);
100     if (0 != memcmp (data, dst[c].data, data_size))
101     {
102       GNUNET_break (0);
103       res = 1;
104     }
105     if (0 != memcmp (data, src[c].data, data_size))
106     {
107       GNUNET_break (0);
108       res = 1;
109     }
110     if (0 != memcmp (src[c].data, dst[c].data, src[c].data_size))
111     {
112       GNUNET_break (0);
113       res = 1;
114     }
115
116     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c);
117   }
118
119   for (c = 0; c < rd_count; c++)
120   {
121     GNUNET_free ((void *)src[c].data);
122   }
123 }
124
125 static int
126 check ()
127 {
128   static char *const argv[] = { "test_namestore_record_serialization",
129     "-c",
130     "test_namestore_api.conf",
131 #if VERBOSE
132     "-L", "DEBUG",
133 #endif
134     NULL
135   };
136   static struct GNUNET_GETOPT_CommandLineOption options[] = {
137     GNUNET_GETOPT_OPTION_END
138   };
139
140   res = 1;
141   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_record_serialization",
142                       "nohelp", options, &run, &res);
143   return res;
144 }
145
146 int
147 main (int argc, char *argv[])
148 {
149   int ret;
150
151   ret = check ();
152
153   return ret;
154 }
155
156 /* end of test_namestore_record_serialization.c */