Asterisk - The Open Source Telephony Project  21.4.1
test_conversions.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2017, Digium, Inc.
5  *
6  * Kevin Harwell <kharwell@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*!
20  * \file
21  * \brief Conversions Unit Tests
22  *
23  * \author Kevin Harwell <kharwell@digium.com>
24  *
25  */
26 
27 /*** MODULEINFO
28  <depend>TEST_FRAMEWORK</depend>
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 #include "asterisk/test.h"
35 #include "asterisk/module.h"
36 #include "asterisk/conversions.h"
37 
38 #define CATEGORY "/main/conversions/"
39 
40 AST_TEST_DEFINE(str_to_int)
41 {
42  const char *invalid = "abc";
43  const char *invalid_partial = "7abc";
44  const char *negative = "-7";
45  const char *negative_spaces = " -7";
46  const char *negative_out_of_range = "-9999999999";
47  const char *out_of_range = "9999999999";
48  const char *spaces = " ";
49  const char *valid = "7";
50  const char *valid_spaces = " 7";
51  const char *valid_decimal = "08";
52  int val;
53  char str[64];
54 
55  switch (cmd) {
56  case TEST_INIT:
57  info->name = __func__;
58  info->category = CATEGORY;
59  info->summary = "convert a string to a signed integer";
60  info->description = info->summary;
61  return AST_TEST_NOT_RUN;
62  case TEST_EXECUTE:
63  break;
64  }
65 
66  ast_test_validate(test, ast_str_to_int(NULL, &val));
67  ast_test_validate(test, ast_str_to_int("\0", &val));
68  ast_test_validate(test, ast_str_to_int(invalid, &val));
69  ast_test_validate(test, ast_str_to_int(invalid_partial, &val));
70  ast_test_validate(test, !ast_str_to_int(negative, &val));
71  ast_test_validate(test, !ast_str_to_int(negative_spaces, &val));
72  ast_test_validate(test, ast_str_to_int(negative_out_of_range, &val));
73  ast_test_validate(test, ast_str_to_int(out_of_range, &val));
74  ast_test_validate(test, ast_str_to_int(spaces, &val));
75  ast_test_validate(test, !ast_str_to_int(valid, &val));
76  ast_test_validate(test, !ast_str_to_int(valid_spaces, &val));
77  ast_test_validate(test, !ast_str_to_int(valid_decimal, &val));
78 
79  ast_test_validate(test, snprintf(str, sizeof(str), "%d", INT_MAX) > 0);
80  ast_test_validate(test, !ast_str_to_int(str, &val));
81  ast_test_validate(test, val == INT_MAX);
82 
83  ast_test_validate(test, snprintf(str, sizeof(str), "%d", INT_MIN) > 0);
84  ast_test_validate(test, !ast_str_to_int(str, &val));
85  ast_test_validate(test, val == INT_MIN);
86 
87  return AST_TEST_PASS;
88 }
89 
90 AST_TEST_DEFINE(str_to_uint)
91 {
92  const char *invalid = "abc";
93  const char *invalid_partial = "7abc";
94  const char *negative = "-7";
95  const char *negative_spaces = " -7";
96  const char *out_of_range = "9999999999";
97  const char *spaces = " ";
98  const char *valid = "7";
99  const char *valid_spaces = " 7";
100  const char *valid_decimal = "08";
101  unsigned int val;
102  char str[64];
103 
104  switch (cmd) {
105  case TEST_INIT:
106  info->name = __func__;
107  info->category = CATEGORY;
108  info->summary = "convert a string to an unsigned integer";
109  info->description = info->summary;
110  return AST_TEST_NOT_RUN;
111  case TEST_EXECUTE:
112  break;
113  }
114 
115  ast_test_validate(test, ast_str_to_uint(NULL, &val));
116  ast_test_validate(test, ast_str_to_uint("\0", &val));
117  ast_test_validate(test, ast_str_to_uint(invalid, &val));
118  ast_test_validate(test, ast_str_to_uint(invalid_partial, &val));
119  ast_test_validate(test, ast_str_to_uint(negative, &val));
120  ast_test_validate(test, ast_str_to_uint(negative_spaces, &val));
121  ast_test_validate(test, ast_str_to_uint(out_of_range, &val));
122  ast_test_validate(test, ast_str_to_uint(spaces, &val));
123  ast_test_validate(test, !ast_str_to_uint(valid, &val));
124  ast_test_validate(test, !ast_str_to_uint(valid_spaces, &val));
125  ast_test_validate(test, !ast_str_to_uint(valid_decimal, &val));
126 
127  ast_test_validate(test, snprintf(str, sizeof(str), "%u", UINT_MAX) > 0);
128  ast_test_validate(test, !ast_str_to_uint(str, &val));
129  ast_test_validate(test, val == UINT_MAX);
130 
131  return AST_TEST_PASS;
132 }
133 
134 AST_TEST_DEFINE(str_to_long)
135 {
136  const char *invalid = "abc";
137  const char *invalid_partial = "7abc";
138  const char *negative = "-7";
139  const char *negative_spaces = " -7";
140  const char *negative_out_of_range = "-99999999999999999999";
141  const char *out_of_range = "99999999999999999999";
142  const char *spaces = " ";
143  const char *valid = "7";
144  const char *valid_spaces = " 7";
145  const char *valid_decimal = "08";
146  long val;
147  char str[64];
148 
149  switch (cmd) {
150  case TEST_INIT:
151  info->name = __func__;
152  info->category = CATEGORY;
153  info->summary = "convert a string to a signed long";
154  info->description = info->summary;
155  return AST_TEST_NOT_RUN;
156  case TEST_EXECUTE:
157  break;
158  }
159 
160  ast_test_validate(test, ast_str_to_long(NULL, &val));
161  ast_test_validate(test, ast_str_to_long("\0", &val));
162  ast_test_validate(test, ast_str_to_long(invalid, &val));
163  ast_test_validate(test, ast_str_to_long(invalid_partial, &val));
164  ast_test_validate(test, !ast_str_to_long(negative, &val));
165  ast_test_validate(test, !ast_str_to_long(negative_spaces, &val));
166  ast_test_validate(test, ast_str_to_long(negative_out_of_range, &val));
167  ast_test_validate(test, ast_str_to_long(out_of_range, &val));
168  ast_test_validate(test, ast_str_to_long(spaces, &val));
169  ast_test_validate(test, !ast_str_to_long(valid, &val));
170  ast_test_validate(test, !ast_str_to_long(valid_spaces, &val));
171  ast_test_validate(test, !ast_str_to_long(valid_decimal, &val));
172 
173  ast_test_validate(test, snprintf(str, sizeof(str), "%ld", LONG_MAX) > 0);
174  ast_test_validate(test, !ast_str_to_long(str, &val));
175  ast_test_validate(test, val == LONG_MAX);
176 
177  ast_test_validate(test, snprintf(str, sizeof(str), "%ld", LONG_MIN) > 0);
178  ast_test_validate(test, !ast_str_to_long(str, &val));
179  ast_test_validate(test, val == LONG_MIN);
180 
181  return AST_TEST_PASS;
182 }
183 
184 AST_TEST_DEFINE(str_to_ulong)
185 {
186  const char *invalid = "abc";
187  const char *invalid_partial = "7abc";
188  const char *negative = "-7";
189  const char *negative_spaces = " -7";
190  const char *out_of_range = "99999999999999999999";
191  const char *spaces = " ";
192  const char *valid = "7";
193  const char *valid_spaces = " 7";
194  const char *valid_decimal = "08";
195  unsigned long val;
196  char str[64];
197 
198  switch (cmd) {
199  case TEST_INIT:
200  info->name = __func__;
201  info->category = CATEGORY;
202  info->summary = "convert a string to an unsigned long";
203  info->description = info->summary;
204  return AST_TEST_NOT_RUN;
205  case TEST_EXECUTE:
206  break;
207  }
208 
209  ast_test_validate(test, ast_str_to_ulong(NULL, &val));
210  ast_test_validate(test, ast_str_to_ulong("\0", &val));
211  ast_test_validate(test, ast_str_to_ulong(invalid, &val));
212  ast_test_validate(test, ast_str_to_ulong(invalid_partial, &val));
213  ast_test_validate(test, ast_str_to_ulong(negative, &val));
214  ast_test_validate(test, ast_str_to_ulong(negative_spaces, &val));
215  ast_test_validate(test, ast_str_to_ulong(out_of_range, &val));
216  ast_test_validate(test, ast_str_to_ulong(spaces, &val));
217  ast_test_validate(test, !ast_str_to_ulong(valid, &val));
218  ast_test_validate(test, !ast_str_to_ulong(valid_spaces, &val));
219  ast_test_validate(test, !ast_str_to_ulong(valid_decimal, &val));
220 
221  ast_test_validate(test, snprintf(str, sizeof(str), "%lu", ULONG_MAX) > 0);
222  ast_test_validate(test, !ast_str_to_ulong(str, &val));
223  ast_test_validate(test, val == ULONG_MAX);
224 
225  return AST_TEST_PASS;
226 }
227 
228 AST_TEST_DEFINE(str_to_imax)
229 {
230  const char *invalid = "abc";
231  const char *invalid_partial = "7abc";
232  const char *negative = "-7";
233  const char *negative_spaces = " -7";
234  const char *negative_out_of_range = "-99999999999999999999999999999999999999999999999999";
235  const char *out_of_range = "99999999999999999999999999999999999999999999999999";
236  const char *spaces = " ";
237  const char *valid = "7";
238  const char *valid_spaces = " 7";
239  const char *valid_decimal = "08";
240  intmax_t val;
241  char str[64];
242 
243  switch (cmd) {
244  case TEST_INIT:
245  info->name = __func__;
246  info->category = CATEGORY;
247  info->summary = "convert a string to a signed max size integer";
248  info->description = info->summary;
249  return AST_TEST_NOT_RUN;
250  case TEST_EXECUTE:
251  break;
252  }
253 
254  ast_test_validate(test, ast_str_to_imax(NULL, &val));
255  ast_test_validate(test, ast_str_to_imax("\0", &val));
256  ast_test_validate(test, ast_str_to_imax(invalid, &val));
257  ast_test_validate(test, ast_str_to_imax(invalid_partial, &val));
258  ast_test_validate(test, !ast_str_to_imax(negative, &val));
259  ast_test_validate(test, !ast_str_to_imax(negative_spaces, &val));
260  ast_test_validate(test, ast_str_to_imax(negative_out_of_range, &val));
261  ast_test_validate(test, ast_str_to_imax(out_of_range, &val));
262  ast_test_validate(test, ast_str_to_imax(spaces, &val));
263  ast_test_validate(test, !ast_str_to_imax(valid, &val));
264  ast_test_validate(test, !ast_str_to_imax(valid_spaces, &val));
265  ast_test_validate(test, !ast_str_to_imax(valid_decimal, &val));
266 
267  ast_test_validate(test, snprintf(str, sizeof(str), "%jd", INTMAX_MAX) > 0);
268  ast_test_validate(test, !ast_str_to_imax(str, &val));
269  ast_test_validate(test, val == INTMAX_MAX);
270 
271  ast_test_validate(test, snprintf(str, sizeof(str), "%jd", INTMAX_MIN) > 0);
272  ast_test_validate(test, !ast_str_to_imax(str, &val));
273  ast_test_validate(test, val == INTMAX_MIN);
274 
275  return AST_TEST_PASS;
276 }
277 
278 
279 AST_TEST_DEFINE(str_to_umax)
280 {
281  const char *invalid = "abc";
282  const char *invalid_partial = "7abc";
283  const char *negative = "-7";
284  const char *negative_spaces = " -7";
285  const char *out_of_range = "99999999999999999999999999999999999999999999999999";
286  const char *spaces = " ";
287  const char *valid = "7";
288  const char *valid_spaces = " 7";
289  const char *valid_decimal = "08";
290  uintmax_t val;
291  char str[64];
292 
293  switch (cmd) {
294  case TEST_INIT:
295  info->name = __func__;
296  info->category = CATEGORY;
297  info->summary = "convert a string to an unsigned max size integer";
298  info->description = info->summary;
299  return AST_TEST_NOT_RUN;
300  case TEST_EXECUTE:
301  break;
302  }
303 
304  ast_test_validate(test, ast_str_to_umax(NULL, &val));
305  ast_test_validate(test, ast_str_to_umax("\0", &val));
306  ast_test_validate(test, ast_str_to_umax(invalid, &val));
307  ast_test_validate(test, ast_str_to_umax(invalid_partial, &val));
308  ast_test_validate(test, ast_str_to_umax(negative, &val));
309  ast_test_validate(test, ast_str_to_umax(negative_spaces, &val));
310  ast_test_validate(test, ast_str_to_umax(out_of_range, &val));
311  ast_test_validate(test, ast_str_to_umax(spaces, &val));
312  ast_test_validate(test, !ast_str_to_umax(valid, &val));
313  ast_test_validate(test, !ast_str_to_umax(valid_spaces, &val));
314  ast_test_validate(test, !ast_str_to_umax(valid_decimal, &val));
315 
316  ast_test_validate(test, snprintf(str, sizeof(str), "%ju", UINTMAX_MAX) > 0);
317  ast_test_validate(test, !ast_str_to_umax(str, &val));
318  ast_test_validate(test, val == UINTMAX_MAX);
319 
320  return AST_TEST_PASS;
321 }
322 
323 static int load_module(void)
324 {
325  AST_TEST_REGISTER(str_to_int);
326  AST_TEST_REGISTER(str_to_uint);
327  AST_TEST_REGISTER(str_to_long);
328  AST_TEST_REGISTER(str_to_ulong);
329  AST_TEST_REGISTER(str_to_imax);
330  AST_TEST_REGISTER(str_to_umax);
332 }
333 
334 static int unload_module(void)
335 {
336  AST_TEST_UNREGISTER(str_to_int);
337  AST_TEST_UNREGISTER(str_to_uint);
338  AST_TEST_UNREGISTER(str_to_long);
339  AST_TEST_UNREGISTER(str_to_ulong);
340  AST_TEST_UNREGISTER(str_to_imax);
341  AST_TEST_UNREGISTER(str_to_umax);
342  return 0;
343 }
344 
345 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Conversions test module");
Asterisk main include file. File version handling, generic pbx functions.
int ast_str_to_umax(const char *str, uintmax_t *res)
Convert the given string to an unsigned max size integer.
Definition: conversions.c:119
Test Framework API.
int ast_str_to_imax(const char *str, intmax_t *res)
Convert the given string to a signed max size integer.
Definition: conversions.c:92
#define CATEGORY
Conversion utility functions.
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition: conversions.c:44
int ast_str_to_ulong(const char *str, unsigned long *res)
Convert the given string to an unsigned long.
Definition: conversions.c:80
int ast_str_to_uint(const char *str, unsigned int *res)
Convert the given string to an unsigned integer.
Definition: conversions.c:56
#define AST_TEST_DEFINE(hdr)
Definition: test.h:126
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
int ast_str_to_long(const char *str, long *res)
Convert the given string to a signed long.
Definition: conversions.c:68