glitch in the license text detected by hyazinthe, thank you!
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /**
16  * @file gnsrecord/test_gnsrecord_serialization.c
17  * @brief testcase for gnsrecord_serialization.c
18  */
19 #include "platform.h"
20 #include "gnunet_util_lib.h"
21 #include "gnunet_gnsrecord_lib.h"
22 #include "gnunet_dnsparser_lib.h"
23
24 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
25
26 static int res;
27
28
29 static void
30 run (void *cls,
31      char *const *args,
32      const char *cfgfile,
33      const struct GNUNET_CONFIGURATION_Handle *cfg)
34 {
35   size_t len;
36   int c;
37
38   int rd_count = 3;
39   size_t data_len;
40   struct GNUNET_GNSRECORD_Data src[rd_count];
41
42   memset(src, '\0', rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
43
44   data_len = 0;
45   for (c = 0; c < rd_count; c++)
46   {
47     src[c].record_type = GNUNET_DNSPARSER_TYPE_TXT;
48     src[c].data_size = data_len;
49     src[c].data = GNUNET_malloc (data_len);
50
51     /* Setting data to data_len * record_type */
52     memset ((char *) src[c].data, 'a', data_len);
53     data_len += 10;
54   }
55   res = 0;
56
57   len = GNUNET_GNSRECORD_records_get_size (rd_count, src);
58   char rd_ser[len];
59   GNUNET_assert (len ==
60                  GNUNET_GNSRECORD_records_serialize (rd_count,
61                                                      src,
62                                                      len,
63                                                      rd_ser));
64
65   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
66               "Serialized data len: %u\n",
67               (unsigned int) len);
68
69   GNUNET_assert (rd_ser != NULL);
70   {
71     struct GNUNET_GNSRECORD_Data dst[rd_count];
72     GNUNET_assert (GNUNET_OK ==
73                    GNUNET_GNSRECORD_records_deserialize (len,
74                                                          rd_ser,
75                                                          rd_count,
76                                                          dst));
77
78     GNUNET_assert (dst != NULL);
79
80     for (c = 0; c < rd_count; c++)
81     {
82       if (src[c].data_size != dst[c].data_size)
83       {
84         GNUNET_break (0);
85         res = 1;
86       }
87       if (src[c].expiration_time != dst[c].expiration_time)
88       {
89         GNUNET_break (0);
90         res = 1;
91       }
92       if (src[c].flags != dst[c].flags)
93       {
94         GNUNET_break (0);
95         res = 1;
96       }
97       if (src[c].record_type != dst[c].record_type)
98       {
99         GNUNET_break (0);
100         res = 1;
101       }
102
103       {
104         size_t data_size = src[c].data_size;
105         char data[data_size];
106
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     }
125     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c);
126   }
127
128   for (c = 0; c < rd_count; c++)
129   {
130     GNUNET_free ((void *)src[c].data);
131   }
132 }
133
134
135 int
136 main (int argcx, char *argvx[])
137 {
138   static char *const argv[] = { "test_gnsrecord_serialization",
139     NULL
140   };
141   static struct GNUNET_GETOPT_CommandLineOption options[] = {
142     GNUNET_GETOPT_OPTION_END
143   };
144
145   res = 1;
146   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test_namestore_record_serialization",
147                       "nohelp", options, &run, &res);
148   return res;
149 }
150
151 /* end of test_gnsrecord_serialization.c */