aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_strings.c
diff options
context:
space:
mode:
authorMartin Schanzenbach <schanzen@gnunet.org>2023-10-18 13:37:38 +0200
committerMartin Schanzenbach <schanzen@gnunet.org>2023-10-18 13:37:38 +0200
commit9ef4abad615bea12d13be542b8ae5fbeb2dfee32 (patch)
tree8875a687e004d331c9ea6a1d511a328c72b88113 /src/util/test_strings.c
parente95236b3ed78cd597c15f34b89385295702b627f (diff)
downloadgnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.tar.gz
gnunet-9ef4abad615bea12d13be542b8ae5fbeb2dfee32.zip
NEWS: Refactoring components under src/ into lib/, plugin/, cli/ and service/
This also includes a necessary API refactoring of crypto from IDENTITY to UTIL.
Diffstat (limited to 'src/util/test_strings.c')
-rw-r--r--src/util/test_strings.c169
1 files changed, 0 insertions, 169 deletions
diff --git a/src/util/test_strings.c b/src/util/test_strings.c
deleted file mode 100644
index 0e39b9958..000000000
--- a/src/util/test_strings.c
+++ /dev/null
@@ -1,169 +0,0 @@
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
61 GNUNET_log_setup ("test_strings", "ERROR", NULL);
62 sprintf (buf, "4 %s", _ (/* size unit */ "b"));
63 b = GNUNET_STRINGS_byte_size_fancy (4);
64 WANT (buf, b);
65 sprintf (buf, "10 %s", _ (/* size unit */ "KiB"));
66 b = GNUNET_STRINGS_byte_size_fancy (10240);
67 WANT (buf, b);
68 sprintf (buf, "10 %s", _ (/* size unit */ "TiB"));
69 b = GNUNET_STRINGS_byte_size_fancy (10240LL * 1024LL * 1024LL * 1024LL);
70 WANT (buf, b);
71 sprintf (buf, "4 %s", _ (/* time unit */ "ms"));
72 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
73 (GNUNET_TIME_UNIT_MILLISECONDS,
74 4), GNUNET_YES);
75 WANTNF (buf, bc);
76 sprintf (buf, "7 %s", _ (/* time unit */ "s"));
77 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
78 (GNUNET_TIME_UNIT_MILLISECONDS,
79 7 * 1000), GNUNET_YES);
80 WANTNF (buf, bc);
81 sprintf (buf, "7 %s", _ (/* time unit */ "h"));
82 bc = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_relative_multiply
83 (GNUNET_TIME_UNIT_MILLISECONDS,
84 7 * 60 * 60 * 1000),
85 GNUNET_YES);
86 WANTNF (buf, bc);
87
88 hdir = getenv ("HOME");
89
90 GNUNET_snprintf (buf, sizeof(buf), "%s%s", hdir, DIR_SEPARATOR_STR);
91 b = GNUNET_STRINGS_filename_expand ("~");
92 GNUNET_assert (b != NULL);
93 WANT (buf, b);
94 GNUNET_STRINGS_buffer_fill (buf, sizeof(buf), 3, "a", "btx", "c");
95 WANTB ("a\0btx\0c", buf, 8);
96 if (6 != GNUNET_STRINGS_buffer_tokenize (buf, sizeof(buf), 2, &r, &b))
97 return 1;
98 r = GNUNET_strdup (r);
99 WANT ("a", r);
100 b = GNUNET_strdup (b);
101 WANT ("btx", b);
102 if (0 != GNUNET_STRINGS_buffer_tokenize (buf, 2, 2, &r, &b))
103 return 1;
104 at.abs_value_us = 5000000;
105 bc = GNUNET_STRINGS_absolute_time_to_string (at);
106 /* bc should be something like "Wed Dec 31 17:00:05 1969"
107 * where the details of the day and hour depend on the timezone;
108 * however, the "0:05 19" should always be there; hence: */
109 if (NULL == strstr (bc, "0:05 19"))
110 {
111 fprintf (stderr, "Got %s\n", bc);
112 GNUNET_break (0);
113 return 1;
114 }
115 /* Normalization */
116 r = "q\u0307\u0323"; /* Non-canonical order */
117
118 b = GNUNET_STRINGS_utf8_normalize (r);
119 GNUNET_assert (0 == strcmp ("q\u0323\u0307", b));
120 GNUNET_free (b);
121 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "ASCII");
122 WANT ("TEST", b);
123
124 at = GNUNET_TIME_UNIT_FOREVER_ABS;
125 bc = GNUNET_STRINGS_absolute_time_to_string (at);
126 GNUNET_assert (GNUNET_OK ==
127 GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
128 GNUNET_assert (atx.abs_value_us == at.abs_value_us);
129
130 at.abs_value_us = 50000000000;
131 bc = GNUNET_STRINGS_absolute_time_to_string (at);
132
133 GNUNET_assert (GNUNET_OK ==
134 GNUNET_STRINGS_fancy_time_to_absolute (bc, &atx));
135
136 if (atx.abs_value_us != at.abs_value_us)
137 {
138 GNUNET_assert (0);
139 }
140
141 GNUNET_log_skip (2, GNUNET_NO);
142 b = GNUNET_STRINGS_to_utf8 ("TEST", 4, "unknown");
143 GNUNET_log_skip (0, GNUNET_YES);
144 WANT ("TEST", b);
145
146 GNUNET_assert (GNUNET_OK ==
147 GNUNET_STRINGS_fancy_time_to_relative ("15m", &rt));
148 GNUNET_assert (GNUNET_OK ==
149 GNUNET_STRINGS_fancy_time_to_relative ("15 m", &rtx));
150 GNUNET_assert (rt.rel_value_us == rtx.rel_value_us);
151
152 GNUNET_assert (0 != GNUNET_STRINGS_urlencode (URLENCODE_TEST_VECTOR_PLAIN,
153 strlen (
154 URLENCODE_TEST_VECTOR_PLAIN),
155 &b));
156 WANT (URLENCODE_TEST_VECTOR_ENCODED, b);
157 GNUNET_free (b);
158 GNUNET_assert (0 !=
159 GNUNET_STRINGS_urldecode (URLENCODE_TEST_VECTOR_ENCODED,
160 strlen (
161 URLENCODE_TEST_VECTOR_ENCODED),
162 &b));
163 WANT (URLENCODE_TEST_VECTOR_PLAIN, b);
164 GNUNET_free (b);
165 return 0;
166}
167
168
169/* end of test_strings.c */