diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2012-02-28 15:33:38 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2012-02-28 15:33:38 +0000 |
commit | efdbca9f6d63d177280669211448683aa033d92b (patch) | |
tree | 074fab69a005aa8e1d4a5ded4b40a7a9b1ec1813 /src | |
parent | 30fc6be8600f0920803fa71233957dd7d1e9f058 (diff) | |
download | gnunet-efdbca9f6d63d177280669211448683aa033d92b.tar.gz gnunet-efdbca9f6d63d177280669211448683aa033d92b.zip |
- record serialization + test
Diffstat (limited to 'src')
-rw-r--r-- | src/namestore/Makefile.am | 18 | ||||
-rw-r--r-- | src/namestore/gnunet-service-namestore.c | 74 | ||||
-rw-r--r-- | src/namestore/hostkey | bin | 0 -> 913 bytes | |||
-rw-r--r-- | src/namestore/namestore.h | 36 | ||||
-rw-r--r-- | src/namestore/namestore_common.c | 159 | ||||
-rw-r--r-- | src/namestore/test_namestore_record_serialization.c | 157 |
6 files changed, 430 insertions, 14 deletions
diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am index 622c445d9..2e9a99d5f 100644 --- a/src/namestore/Makefile.am +++ b/src/namestore/Makefile.am | |||
@@ -26,7 +26,9 @@ check_PROGRAMS = \ | |||
26 | $(SQLITE_TESTS) | 26 | $(SQLITE_TESTS) |
27 | 27 | ||
28 | if HAVE_EXPERIMENTAL | 28 | if HAVE_EXPERIMENTAL |
29 | check_PROGRAMS += test_namestore_api test_namestore_api_zone_iteration | 29 | check_PROGRAMS += test_namestore_api \ |
30 | test_namestore_api_zone_iteration \ | ||
31 | test_namestore_record_serialization | ||
30 | endif | 32 | endif |
31 | 33 | ||
32 | lib_LTLIBRARIES = \ | 34 | lib_LTLIBRARIES = \ |
@@ -46,7 +48,8 @@ bin_PROGRAMS = \ | |||
46 | gnunet-service-namestore | 48 | gnunet-service-namestore |
47 | 49 | ||
48 | gnunet_service_namestore_SOURCES = \ | 50 | gnunet_service_namestore_SOURCES = \ |
49 | gnunet-service-namestore.c | 51 | gnunet-service-namestore.c \ |
52 | namestore_common.c | ||
50 | gnunet_service_namestore_LDADD = \ | 53 | gnunet_service_namestore_LDADD = \ |
51 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | 54 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ |
52 | $(top_builddir)/src/util/libgnunetutil.la \ | 55 | $(top_builddir)/src/util/libgnunetutil.la \ |
@@ -80,10 +83,17 @@ test_namestore_api_zone_iteration_LDADD = \ | |||
80 | $(top_builddir)/src/util/libgnunetutil.la \ | 83 | $(top_builddir)/src/util/libgnunetutil.la \ |
81 | $(top_builddir)/src/namestore/libgnunetnamestore.la | 84 | $(top_builddir)/src/namestore/libgnunetnamestore.la |
82 | 85 | ||
86 | test_namestore_record_serialization_SOURCES = \ | ||
87 | test_namestore_record_serialization.c \ | ||
88 | namestore_common.c | ||
89 | test_namestore_record_serialization_LDADD = \ | ||
90 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
91 | $(top_builddir)/src/namestore/libgnunetnamestore.la | ||
92 | |||
83 | EXTRA_DIST = \ | 93 | EXTRA_DIST = \ |
84 | test_namestore_api.conf \ | 94 | test_namestore_api.conf \ |
85 | test_plugin_namestore_sqlite.conf | 95 | test_plugin_namestore_sqlite.conf\ |
86 | # hostkey | 96 | hostkey |
87 | 97 | ||
88 | 98 | ||
89 | test_plugin_namestore_sqlite_SOURCES = \ | 99 | test_plugin_namestore_sqlite_SOURCES = \ |
diff --git a/src/namestore/gnunet-service-namestore.c b/src/namestore/gnunet-service-namestore.c index 747726fd0..8f969371b 100644 --- a/src/namestore/gnunet-service-namestore.c +++ b/src/namestore/gnunet-service-namestore.c | |||
@@ -192,6 +192,8 @@ struct LookupNameContext | |||
192 | }; | 192 | }; |
193 | 193 | ||
194 | 194 | ||
195 | |||
196 | |||
195 | static void | 197 | static void |
196 | handle_lookup_name_it (void *cls, | 198 | handle_lookup_name_it (void *cls, |
197 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key, | 199 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key, |
@@ -578,6 +580,22 @@ static void handle_record_remove (void *cls, | |||
578 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | 580 | GNUNET_SERVER_receive_done (client, GNUNET_OK); |
579 | } | 581 | } |
580 | 582 | ||
583 | struct ZoneIterationProcResult | ||
584 | { | ||
585 | int have_zone_key; | ||
586 | struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded zone_key; | ||
587 | |||
588 | int have_signature; | ||
589 | struct GNUNET_CRYPTO_RsaSignature signature; | ||
590 | struct GNUNET_TIME_Absolute expire; | ||
591 | |||
592 | int have_name; | ||
593 | char name[256]; | ||
594 | |||
595 | unsigned int rd_count; | ||
596 | char *rd_ser; | ||
597 | }; | ||
598 | |||
581 | 599 | ||
582 | void zone_iteration_proc (void *cls, | 600 | void zone_iteration_proc (void *cls, |
583 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key, | 601 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key, |
@@ -587,15 +605,40 @@ void zone_iteration_proc (void *cls, | |||
587 | const struct GNUNET_NAMESTORE_RecordData *rd, | 605 | const struct GNUNET_NAMESTORE_RecordData *rd, |
588 | const struct GNUNET_CRYPTO_RsaSignature *signature) | 606 | const struct GNUNET_CRYPTO_RsaSignature *signature) |
589 | { | 607 | { |
590 | struct ZoneIterationResponseMessage zir_msg; | 608 | struct ZoneIterationProcResult *zipr = cls; |
591 | struct GNUNET_NAMESTORE_ZoneIteration * zi = cls; | 609 | size_t len; |
610 | if (zone_key != NULL) | ||
611 | { | ||
612 | zipr->zone_key = *zone_key; | ||
613 | zipr->have_zone_key = GNUNET_YES; | ||
614 | } | ||
615 | else | ||
616 | zipr->have_zone_key = GNUNET_NO; | ||
592 | 617 | ||
593 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE"); | 618 | zipr->expire = expire; |
594 | zir_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE); | ||
595 | zir_msg.op_id = htonl(zi->op_id); | ||
596 | zir_msg.header.size = htons (sizeof (struct ZoneIterationResponseMessage)); | ||
597 | 619 | ||
598 | GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO); | 620 | if (name != NULL) |
621 | { | ||
622 | memcpy (zipr->name, name, strlen(name) + 1); | ||
623 | zipr->have_name = GNUNET_YES; | ||
624 | } | ||
625 | else | ||
626 | zipr->have_name = GNUNET_NO; | ||
627 | |||
628 | zipr->rd_count = rd_count; | ||
629 | |||
630 | if (signature != NULL) | ||
631 | { | ||
632 | zipr->signature = *signature; | ||
633 | zipr->have_signature = GNUNET_YES; | ||
634 | } | ||
635 | else | ||
636 | zipr->have_signature = GNUNET_NO; | ||
637 | |||
638 | if ((rd_count > 0) && (rd != NULL)) | ||
639 | { | ||
640 | len = GNUNET_NAMESTORE_records_serialize (&zipr->rd_ser, rd_count, rd); | ||
641 | } | ||
599 | } | 642 | } |
600 | 643 | ||
601 | static void handle_iteration_start (void *cls, | 644 | static void handle_iteration_start (void *cls, |
@@ -607,6 +650,8 @@ static void handle_iteration_start (void *cls, | |||
607 | struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message; | 650 | struct ZoneIterationStartMessage * zis_msg = (struct ZoneIterationStartMessage *) message; |
608 | struct GNUNET_NAMESTORE_Client *nc; | 651 | struct GNUNET_NAMESTORE_Client *nc; |
609 | struct GNUNET_NAMESTORE_ZoneIteration *zi; | 652 | struct GNUNET_NAMESTORE_ZoneIteration *zi; |
653 | struct ZoneIterationResponseMessage zir_msg; | ||
654 | struct ZoneIterationProcResult zipr; | ||
610 | int res; | 655 | int res; |
611 | 656 | ||
612 | nc = client_lookup(client); | 657 | nc = client_lookup(client); |
@@ -625,8 +670,16 @@ static void handle_iteration_start (void *cls, | |||
625 | 670 | ||
626 | GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi); | 671 | GNUNET_CONTAINER_DLL_insert (nc->op_head, nc->op_tail, zi); |
627 | 672 | ||
628 | res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, zi); | 673 | res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, &zipr); |
629 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "iterate_records: %i\n", res); | 674 | |
675 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "ZONE_ITERATION_RESPONSE"); | ||
676 | zir_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_RESPONSE); | ||
677 | zir_msg.op_id = htonl(zi->op_id); | ||
678 | zir_msg.header.size = htons (sizeof (struct ZoneIterationResponseMessage)); | ||
679 | |||
680 | GNUNET_SERVER_notification_context_unicast (snc, zi->client->client, (const struct GNUNET_MessageHeader *) &zir_msg, GNUNET_NO); | ||
681 | |||
682 | |||
630 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | 683 | GNUNET_SERVER_receive_done (client, GNUNET_OK); |
631 | } | 684 | } |
632 | 685 | ||
@@ -679,6 +732,7 @@ static void handle_iteration_next (void *cls, | |||
679 | struct GNUNET_NAMESTORE_ZoneIteration *zi; | 732 | struct GNUNET_NAMESTORE_ZoneIteration *zi; |
680 | struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message; | 733 | struct ZoneIterationStopMessage * zis_msg = (struct ZoneIterationStopMessage *) message; |
681 | uint32_t id; | 734 | uint32_t id; |
735 | int res; | ||
682 | 736 | ||
683 | nc = client_lookup(client); | 737 | nc = client_lookup(client); |
684 | if (nc == NULL) | 738 | if (nc == NULL) |
@@ -702,7 +756,7 @@ static void handle_iteration_next (void *cls, | |||
702 | } | 756 | } |
703 | 757 | ||
704 | zi->offset++; | 758 | zi->offset++; |
705 | res = GSN_database->iterate_records (GSN_database->cls, &zis_msg->zone, NULL, zi->offset , &zone_iteration_proc, zi); | 759 | res = GSN_database->iterate_records (GSN_database->cls, &zi->zone, NULL, zi->offset , &zone_iteration_proc, zi); |
706 | } | 760 | } |
707 | 761 | ||
708 | 762 | ||
diff --git a/src/namestore/hostkey b/src/namestore/hostkey new file mode 100644 index 000000000..eac1d1e2f --- /dev/null +++ b/src/namestore/hostkey | |||
Binary files differ | |||
diff --git a/src/namestore/namestore.h b/src/namestore/namestore.h index 04eaa61fc..5b6a24ca6 100644 --- a/src/namestore/namestore.h +++ b/src/namestore/namestore.h | |||
@@ -43,6 +43,42 @@ | |||
43 | #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT 441 | 43 | #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT 441 |
44 | #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP 442 | 44 | #define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP 442 |
45 | 45 | ||
46 | size_t | ||
47 | GNUNET_NAMESTORE_records_serialize (char ** dest, | ||
48 | unsigned int rd_count, | ||
49 | const struct GNUNET_NAMESTORE_RecordData *rd); | ||
50 | |||
51 | int | ||
52 | GNUNET_NAMESTORE_records_deserialize ( struct GNUNET_NAMESTORE_RecordData **dest, char *src, size_t len); | ||
53 | |||
54 | /** | ||
55 | * A GNS record serialized for network transmission. | ||
56 | * layout is [struct GNUNET_NAMESTORE_NetworkRecord][char[data_size] data] | ||
57 | */ | ||
58 | struct GNUNET_NAMESTORE_NetworkRecord | ||
59 | { | ||
60 | /** | ||
61 | * Expiration time for the DNS record. | ||
62 | */ | ||
63 | struct GNUNET_TIME_AbsoluteNBO expiration; | ||
64 | |||
65 | /** | ||
66 | * Number of bytes in 'data'. | ||
67 | */ | ||
68 | uint32_t data_size; | ||
69 | |||
70 | /** | ||
71 | * Type of the GNS/DNS record. | ||
72 | */ | ||
73 | uint32_t record_type; | ||
74 | |||
75 | /** | ||
76 | * Flags for the record. | ||
77 | */ | ||
78 | uint32_t flags; | ||
79 | }; | ||
80 | |||
81 | |||
46 | 82 | ||
47 | GNUNET_NETWORK_STRUCT_BEGIN | 83 | GNUNET_NETWORK_STRUCT_BEGIN |
48 | /** | 84 | /** |
diff --git a/src/namestore/namestore_common.c b/src/namestore/namestore_common.c new file mode 100644 index 000000000..37f0eab0b --- /dev/null +++ b/src/namestore/namestore_common.c | |||
@@ -0,0 +1,159 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 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 | /** | ||
22 | * @file namestore/namestore_common.c | ||
23 | * @brief API to access the NAMESTORE service | ||
24 | * @author Martin Schanzenbach | ||
25 | * @author Matthias Wachs | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_constants.h" | ||
31 | #include "gnunet_arm_service.h" | ||
32 | #include "gnunet_namestore_service.h" | ||
33 | #include "namestore.h" | ||
34 | #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING | ||
35 | |||
36 | #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__) | ||
37 | /** | ||
38 | * Serialize an array of GNUNET_NAMESTORE_RecordData *rd to transmit over the | ||
39 | * network | ||
40 | * | ||
41 | * @param dest where to write the serialized data | ||
42 | * @param rd_count number of elements in array | ||
43 | * @param rd array | ||
44 | * | ||
45 | * @return number of bytes written to destination dest | ||
46 | */ | ||
47 | size_t | ||
48 | GNUNET_NAMESTORE_records_serialize (char ** dest, | ||
49 | unsigned int rd_count, | ||
50 | const struct GNUNET_NAMESTORE_RecordData *rd) | ||
51 | { | ||
52 | //size_t len = 0; | ||
53 | struct GNUNET_NAMESTORE_NetworkRecord * nr; | ||
54 | char * d = (*dest); | ||
55 | int c = 0; | ||
56 | int offset; | ||
57 | |||
58 | |||
59 | size_t total_len = rd_count * sizeof (struct GNUNET_NAMESTORE_NetworkRecord); | ||
60 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Struct size: %u\n", total_len); | ||
61 | |||
62 | /* figure out total len required */ | ||
63 | for (c = 0; c < rd_count; c ++) | ||
64 | { | ||
65 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data size record[%i] : %u\n", c, rd[c].data_size); | ||
66 | total_len += rd[c].data_size; | ||
67 | } | ||
68 | |||
69 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serializing %i records with total length of %llu\n", rd_count, total_len); | ||
70 | |||
71 | (*dest) = GNUNET_malloc (total_len); | ||
72 | d = (*dest); | ||
73 | |||
74 | /* copy records */ | ||
75 | offset = 0; | ||
76 | |||
77 | for (c = 0; c < rd_count; c ++) | ||
78 | { | ||
79 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Serialized record [%i]: data_size %i\n", c,rd[c].data_size); | ||
80 | |||
81 | nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &d[offset]; | ||
82 | nr->data_size = htonl (rd[c].data_size); | ||
83 | nr->flags = htonl (rd[c].flags); | ||
84 | nr->record_type = htonl (rd[c].record_type); | ||
85 | nr->expiration = GNUNET_TIME_absolute_hton(rd[c].expiration); | ||
86 | |||
87 | /*put data here */ | ||
88 | offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord); | ||
89 | memcpy (&d[offset], rd[c].data, rd[c].data_size); | ||
90 | offset += rd[c].data_size; | ||
91 | } | ||
92 | |||
93 | GNUNET_assert (offset == total_len); | ||
94 | return total_len; | ||
95 | } | ||
96 | |||
97 | |||
98 | /** | ||
99 | * Deserialize an array of GNUNET_NAMESTORE_RecordData *rd after transmission | ||
100 | * over the network | ||
101 | * | ||
102 | * @param source where to read the data to deserialize | ||
103 | * @param rd_count number of elements in array | ||
104 | * @param rd array | ||
105 | * | ||
106 | * @return number of elements deserialized | ||
107 | */ | ||
108 | int | ||
109 | GNUNET_NAMESTORE_records_deserialize ( struct GNUNET_NAMESTORE_RecordData **dest, char *src, size_t len) | ||
110 | { | ||
111 | struct GNUNET_NAMESTORE_NetworkRecord * nr; | ||
112 | struct GNUNET_NAMESTORE_RecordData *d = (*dest); | ||
113 | int elements; | ||
114 | size_t offset; | ||
115 | uint32_t data_size; | ||
116 | int c; | ||
117 | |||
118 | offset = 0; | ||
119 | elements = 0; | ||
120 | while (offset < len) | ||
121 | { | ||
122 | nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &src[offset]; | ||
123 | offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord); | ||
124 | |||
125 | data_size = ntohl (nr->data_size); | ||
126 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Datasize record[%i]: %u\n", elements, data_size); | ||
127 | offset += data_size; | ||
128 | elements ++; | ||
129 | } | ||
130 | |||
131 | GNUNET_assert (len == offset); | ||
132 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserializing %i records with total length of %u\n", elements, len); | ||
133 | |||
134 | (*dest) = GNUNET_malloc (elements * sizeof (struct GNUNET_NAMESTORE_RecordData)); | ||
135 | d = (*dest); | ||
136 | |||
137 | offset = 0; | ||
138 | for (c = 0; c < elements; c++) | ||
139 | { | ||
140 | nr = (struct GNUNET_NAMESTORE_NetworkRecord *) &src[offset]; | ||
141 | d[c].expiration = GNUNET_TIME_absolute_ntoh(nr->expiration); | ||
142 | d[c].record_type = ntohl (nr->record_type); | ||
143 | d[c].flags = ntohl (nr->flags); | ||
144 | d[c].data_size = ntohl (nr->data_size); | ||
145 | d[c].data = GNUNET_malloc (d[c].data_size); | ||
146 | GNUNET_assert (d[c].data != NULL); | ||
147 | |||
148 | offset += sizeof (struct GNUNET_NAMESTORE_NetworkRecord); | ||
149 | memcpy((char *) d[c].data, &src[offset], d[c].data_size); | ||
150 | |||
151 | offset += d[c].data_size; | ||
152 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deserialized record[%i] /w data_size %i\n", c, d[c].data_size); | ||
153 | } | ||
154 | GNUNET_assert(offset == len); | ||
155 | |||
156 | return elements; | ||
157 | } | ||
158 | |||
159 | /* end of namestore_api.c */ | ||
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 | |||
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 | 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 | |||
126 | static int | ||
127 | check () | ||
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 | |||
147 | int | ||
148 | main (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 */ | ||