aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_strings.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_strings.c')
-rw-r--r--src/lib/util/test_strings.c174
1 files changed, 174 insertions, 0 deletions
diff --git a/src/lib/util/test_strings.c b/src/lib/util/test_strings.c
new file mode 100644
index 000000000..806324be3
--- /dev/null
+++ b/src/lib/util/test_strings.c
@@ -0,0 +1,174 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009 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 util/test_strings.c
22 * @brief testcase for strings.c
23 */
24
25#include "platform.h"
26#include "gnunet_util_lib.h"
27
28
29#define WANT(a, b) if (0 != strcmp (a, b)) { fprintf (stderr, \
30 "Got `%s', wanted `%s'\n", \
31 b, a); GNUNET_free (b); \
32 GNUNET_break (0); \
33 return 1; } else { GNUNET_free (b); \
34}
35#define WANTNF(a, b) do { if (0 != strcmp (a, b)) { fprintf (stderr, \
36 "Got `%s', wanted `%s'\n", \
37 b, a); \
38 GNUNET_break (0); return 1; \
39 } } while (0)
40#define WANTB(a, b, l) if (0 != memcmp (a, b, l)) { GNUNET_break (0); return 1; \
41} else { }
42
43#define URLENCODE_TEST_VECTOR_PLAIN "Asbjlaw=ljsdlasjd?人aslkdsa"
44
45#define URLENCODE_TEST_VECTOR_ENCODED \
46 "Asbjlaw\%3Dljsdlasjd\%3F\%E4\%BA\%BAaslkdsa"
47
48int
49main (int argc, char *argv[])
50{
51 char buf[128];
52 char *r;
53 char *b;
54 const char *bc;
55 struct GNUNET_TIME_Absolute at;
56 struct GNUNET_TIME_Absolute atx;
57 struct GNUNET_TIME_Relative rt;
58 struct GNUNET_TIME_Relative rtx;
59 const char *hdir;
60 struct GNUNET_STRINGS_IPv6NetworkPolicy *pol;
61
62 pol = GNUNET_STRINGS_parse_ipv6_policy ("::1;");
63 GNUNET_assert (NULL != pol);
64 GNUNET_free (pol);
65
66 GNUNET_log_setup ("test_strings", "ERROR", NULL);
67 sprintf (buf, "4 %s", _ (/* size unit */ "b"));
68 b = GNUNET_STRINGS_byte_size_fancy (4);
69 WANT (buf, b);
70 sprintf (buf, "10 %s", _ (/* size unit */ "KiB"));
71 b = GNUNET_STRINGS_byte_size_fancy (10240);
72 WANT (buf, b);
73 sprintf (buf, "10 %s", _ (/* size unit */ "TiB"));
74 b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL);
75 WANT (buf, b);
76 sprintf (buf, "4 %s", _ (/* time unit */ "ms"));
77 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
78 (GNUNET_TIME_UNIT_MILLISECONDS,
79 4), GNUNET_YES);
80 WANTNF (buf, bc);
81 sprintf (buf, "7 %s", _ (/* time unit */ "s"));
82 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
83 (GNUNET_TIME_UNIT_MILLISECONDS,
84 7 * 1000), GNUNET_YES);
85 WANTNF (buf, bc);
86 sprintf (buf, "7 %s", _ (/* time unit */ "h"));
87 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
88 (GNUNET_TIME_UNIT_MILLISECONDS,
89 7 * 60 * 60 * 1000),
90 GNUNET_YES);
91 WANTNF (buf, bc);
92
93 hdir = getenv ("HOME");
94
95 GNUNET_snprintf (buf, sizeof(buf), "%s%s", hdir, DIR_SEPARATOR_STR);
96 b = GNUNET_STRINGS_filename_expand ("~");
97 GNUNET_assert (b != NULL);
98 WANT (buf, b);
99 GNUNET_STRINGS_buffer_fill (buf, sizeof(buf), 3, "a", "btx", "c");
100 WANTB ("a\0btx\0c", buf, 8);
101 if (6 != GNUNET_STRINGS_buffer_tokenize (buf, sizeof(buf), 2, &r, &b))
102 return 1;
103 r = GNUNET_strdup (r);
104 WANT ("a", r);
105 b = GNUNET_strdup (b);
106 WANT ("btx", b);
107 if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b))
108 return 1;
109 at.abs_value_us = 5000000;
110 bc = GNUNET_STRINGS_absolute_time_to_string (at);
111 /* bc should be something like "Wed Dec 31 17:00:05 1969"
112 * where the details of the day and hour depend on the timezone;
113 * however, the "0:05 19" should always be there; hence: */
114 if (NULL == strstr (bc, "0:05 19"))
115 {
116 fprintf (stderr, "Got %s\n", bc);
117 GNUNET_break (0);
118 return 1;
119 }
120 /* Normalization */
121 r = "q\u0307\u0323"; /* Non-canonical order */
122
123 b = GNUNET_STRINGS_utf8_normalize (r);
124 GNUNET_assert (0 == strcmp ("q\u0323\u0307", b));
125 GNUNET_free (b);
126 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
127 WANT ("TEST", b);
128
129 at = GNUNET_TIME_UNIT_FOREVER_ABS;
130 bc = GNUNET_STRINGS_absolute_time_to_string (at);
131 GNUNET_assert (GNUNET_OK ==
132 GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
133 GNUNET_assert (atx.abs_value_us == at.abs_value_us);
134
135 at.abs_value_us = 50000000000;
136 bc = GNUNET_STRINGS_absolute_time_to_string (at);
137
138 GNUNET_assert (GNUNET_OK ==
139 GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
140
141 if (atx.abs_value_us != at.abs_value_us)
142 {
143 GNUNET_assert (0);
144 }
145
146 GNUNET_log_skip (2, GNUNET_NO);
147 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
148 GNUNET_log_skip (0, GNUNET_YES);
149 WANT ("TEST", b);
150
151 GNUNET_assert (GNUNET_OK ==
152 GNUNET_STRINGS_fancy_time_to_relative ("15m", &rt));
153 GNUNET_assert (GNUNET_OK ==
154 GNUNET_STRINGS_fancy_time_to_relative ("15 m", &rtx));
155 GNUNET_assert (rt.rel_value_us == rtx.rel_value_us);
156
157 GNUNET_assert (0 != GNUNET_STRINGS_urlencode (strlen (
158 URLENCODE_TEST_VECTOR_PLAIN),
159 URLENCODE_TEST_VECTOR_PLAIN,
160 &b));
161 WANT (URLENCODE_TEST_VECTOR_ENCODED, b);
162 GNUNET_free (b);
163 GNUNET_assert (0 !=
164 GNUNET_STRINGS_urldecode (
165 URLENCODE_TEST_VECTOR_ENCODED,
166 strlen (URLENCODE_TEST_VECTOR_ENCODED),
167 &b));
168 WANT (URLENCODE_TEST_VECTOR_PLAIN, b);
169 GNUNET_free (b);
170 return 0;
171}
172
173
174/* end of test_strings.c */