aboutsummaryrefslogtreecommitdiff
path: root/src/lib/gnsrecord/test_gnsrecord_serialization.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/gnsrecord/test_gnsrecord_serialization.c')
-rw-r--r--src/lib/gnsrecord/test_gnsrecord_serialization.c156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/lib/gnsrecord/test_gnsrecord_serialization.c b/src/lib/gnsrecord/test_gnsrecord_serialization.c
new file mode 100644
index 000000000..b06b3a0fe
--- /dev/null
+++ b/src/lib/gnsrecord/test_gnsrecord_serialization.c
@@ -0,0 +1,156 @@
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 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
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
30static int res;
31
32
33static void
34run (void *cls,
35 char *const *args,
36 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_GNSRECORD_Data src[rd_count];
45
46 memset (src, '\0', rd_count * sizeof(struct GNUNET_GNSRECORD_Data));
47
48 data_len = 0;
49 for (c = 0; c < rd_count; c++)
50 {
51 src[c].record_type = GNUNET_DNSPARSER_TYPE_TXT;
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_GNSRECORD_records_get_size (rd_count, src);
62 char rd_ser[len];
63 GNUNET_assert (len ==
64 GNUNET_GNSRECORD_records_serialize (rd_count,
65 src,
66 len,
67 rd_ser));
68
69 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70 "Serialized data len: %u\n",
71 (unsigned int) len);
72
73 GNUNET_assert (rd_ser != NULL);
74 {
75 struct GNUNET_GNSRECORD_Data dst[rd_count];
76 GNUNET_assert (GNUNET_OK ==
77 GNUNET_GNSRECORD_records_deserialize (len,
78 rd_ser,
79 rd_count,
80 dst));
81
82 GNUNET_assert (dst != NULL);
83
84 for (c = 0; c < rd_count; c++)
85 {
86 if (src[c].data_size != dst[c].data_size)
87 {
88 GNUNET_break (0);
89 res = 1;
90 }
91 if (src[c].expiration_time != dst[c].expiration_time)
92 {
93 GNUNET_break (0);
94 res = 1;
95 }
96 if (src[c].flags != dst[c].flags)
97 {
98 GNUNET_break (0);
99 res = 1;
100 }
101 if (src[c].record_type != dst[c].record_type)
102 {
103 GNUNET_break (0);
104 res = 1;
105 }
106
107 {
108 size_t data_size = src[c].data_size;
109 char data[data_size];
110
111 memset (data, 'a', data_size);
112 if (0 != memcmp (data, dst[c].data, data_size))
113 {
114 GNUNET_break (0);
115 res = 1;
116 }
117 if (0 != memcmp (data, src[c].data, data_size))
118 {
119 GNUNET_break (0);
120 res = 1;
121 }
122 if (0 != memcmp (src[c].data, dst[c].data, src[c].data_size))
123 {
124 GNUNET_break (0);
125 res = 1;
126 }
127 }
128 }
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Element [%i]: EQUAL\n", c);
130 }
131
132 for (c = 0; c < rd_count; c++)
133 {
134 GNUNET_free_nz ((void *) src[c].data);
135 }
136}
137
138
139int
140main (int argcx, char *argvx[])
141{
142 static char *const argv[] = { "test_gnsrecord_serialization",
143 NULL };
144 static struct GNUNET_GETOPT_CommandLineOption options[] = {
145 GNUNET_GETOPT_OPTION_END
146 };
147
148 res = 1;
149 GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
150 "test_namestore_record_serialization",
151 "nohelp", options, &run, &res);
152 return res;
153}
154
155
156/* end of test_gnsrecord_serialization.c */