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