Close file descriptor on error handling.
[oweals/gnunet.git] / src / gnsrecord / test_gnsrecord_serialization.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file gnsrecord/test_gnsrecord_serialization.c
22  * @brief testcase for gnsrecord_serialization.c
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_gnsrecord_lib.h"
27
28 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
29
30 static int res;
31
32
33 static void
34 run (void *cls, char *const *args, const char *cfgfile,
35      const struct GNUNET_CONFIGURATION_Handle *cfg)
36 {
37   size_t len;
38   int c;
39
40   int rd_count = 3;
41   size_t data_len;
42   struct GNUNET_GNSRECORD_Data src[rd_count];
43
44   memset(src, '\0', rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
45
46   data_len = 0;
47   for (c = 0; c < rd_count; c++)
48   {
49     src[c].record_type = c+1;
50     src[c].data_size = data_len;
51     src[c].data = GNUNET_malloc (data_len);
52
53     /* Setting data to data_len * record_type */
54     memset ((char *) src[c].data, 'a', data_len);
55     data_len += 10;
56   }
57   res = 0;
58
59   len = GNUNET_GNSRECORD_records_get_size (rd_count, src);
60   char rd_ser[len];
61   GNUNET_assert (len ==
62                  GNUNET_GNSRECORD_records_serialize (rd_count,
63                                                      src,
64                                                      len,
65                                                      rd_ser));
66
67   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
68               "Serialized data len: %u\n",
69               (unsigned int) len);
70
71   GNUNET_assert (rd_ser != NULL);
72
73   struct GNUNET_GNSRECORD_Data dst[rd_count];
74   GNUNET_assert (GNUNET_OK ==
75                  GNUNET_GNSRECORD_records_deserialize (len,
76                                                        rd_ser,
77                                                        rd_count,
78                                                        dst));
79
80   GNUNET_assert (dst != NULL);
81
82   for (c = 0; c < rd_count; c++)
83   {
84     if (src[c].data_size != dst[c].data_size)
85     {
86       GNUNET_break (0);
87       res = 1;
88     }
89     if (src[c].expiration_time != dst[c].expiration_time)
90     {
91       GNUNET_break (0);
92       res = 1;
93     }
94     if (src[c].flags != dst[c].flags)
95     {
96       GNUNET_break (0);
97       res = 1;
98     }
99     if (src[c].record_type != dst[c].record_type)
100     {
101       GNUNET_break (0);
102       res = 1;
103     }
104
105     size_t data_size = src[c].data_size;
106     char data[data_size];
107     memset (data, 'a', data_size);
108     if (0 != memcmp (data, dst[c].data, data_size))
109     {
110       GNUNET_break (0);
111       res = 1;
112     }
113     if (0 != memcmp (data, src[c].data, data_size))
114     {
115       GNUNET_break (0);
116       res = 1;
117     }
118     if (0 != memcmp (src[c].data, dst[c].data, src[c].data_size))
119     {
120       GNUNET_break (0);
121       res = 1;
122     }
123
124     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c);
125   }
126
127   for (c = 0; c < rd_count; c++)
128   {
129     GNUNET_free ((void *)src[c].data);
130   }
131 }
132
133
134 int
135 main (int argcx, char *argvx[])
136 {
137   static char *const argv[] = { "test_gnsrecord_serialization",
138     NULL
139   };
140   static struct GNUNET_GETOPT_CommandLineOption options[] = {
141     GNUNET_GETOPT_OPTION_END
142   };
143
144   res = 1;
145   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_record_serialization",
146                       "nohelp", options, &run, &res);
147   return res;
148 }
149
150 /* end of test_gnsrecord_serialization.c */