Review Board 1.7.16


clang compiler warning: fixes for tests to be compiled using clang

Review Request #4555 - Created March 29, 2015 and submitted

Diederik de Groot
13
ASTERISK-24917
Reviewers
asterisk-dev
Asterisk
clang's static analyzer will throw quite a number warnings / errors during compilation, some of which can be very helpfull in finding corner-case bugs. 

fixes for tests to be compiled using clang
executing the tests one-by-one works fine (completes to end) (skipping /main/stdtime) -> 
test show results failed:


=================== /main/message/ ====== 
FAIL   test_message_queue_handler_nom /main/message/             31036ms
[test_message.c:int handler_wait_for_message(struct ast_test *):244]: Test timed out while waiting for handler to get message

Not sure if this is actually a fail or just a timeout. WIP


=================== /main/strings/ ====== 
FAIL   escape_semicolons              /main/strings/             1ms     
[Mar 29 20:13:43] ERROR[2521]: utils.c:493 char *ast_escape_semicolons(const char *, char *, int): FRACK!, Failed assertion string != NULL && outbuf != NULL (0)
-> explainable by the change made to the source. ast_alloca(0) is not being executed -> test2 = NULL: need to resolv the open question how to handle ast_alloca(0) before making any further changes.

(With revision 5 of this code, this test now passes without a problem, had to fix both the test and the function being tested though)


=================== /main/stdtime ====== 
"test execute all" fails, caused by the /main/stdtime/ test. 
START  /main/stdtime/ - timezone_watch 
[test_time.c:enum ast_test_result_state test_timezone_watch(struct ast_test_info *, enum ast_test_command, struct ast_test *):84]: Executing deletion test...
j62747*CLI> 
CLI becomes unresponsive / no further command completion for example.
Guess this will need a little further investigation. Maybe the source changes made to main/stdtime/ where not completely correct.

Seems to be caused by inotify_daemon, at least there is where the segfault happens. WIP
<?xml version="1.0" encoding="UTF-8"?>
<testsuite errors="0" time="181.742" tests="444" n
<properties>
<property name="version" value="SV

Changes between revision 8 and 9

1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

  1. /branches/13/tests/test_strings.c: Loading...
/branches/13/tests/test_strings.c
Diff Revision 8 Diff Revision 9
This file contains only whitespace changes.
1
/*
1
/*
2
 * Asterisk -- An open source telephony toolkit.
2
 * Asterisk -- An open source telephony toolkit.
3
 *
3
 *
4
 * Copyright (C) 2010, Digium, Inc.
4
 * Copyright (C) 2010, Digium, Inc.
5
 *
5
 *
6
 * Mark Michelson <mmichelson@digium.com>
6
 * Mark Michelson <mmichelson@digium.com>
7
 *
7
 *
8
 * See http://www.asterisk.org for more information about
8
 * See http://www.asterisk.org for more information about
9
 * the Asterisk project. Please do not directly contact
9
 * the Asterisk project. Please do not directly contact
10
 * any of the maintainers of this project for assistance;
10
 * any of the maintainers of this project for assistance;
11
 * the project provides a web site, mailing lists and IRC
11
 * the project provides a web site, mailing lists and IRC
12
 * channels for your use.
12
 * channels for your use.
13
 *
13
 *
14
 * This program is free software, distributed under the terms of
14
 * This program is free software, distributed under the terms of
15
 * the GNU General Public License Version 2. See the LICENSE file
15
 * the GNU General Public License Version 2. See the LICENSE file
16
 * at the top of the source tree.
16
 * at the top of the source tree.
17
 */
17
 */
18

    
   
18

   
19
/*!
19
/*!
20
 * \file
20
 * \file
21
 * \brief Dynamic string tests
21
 * \brief Dynamic string tests
22
 *
22
 *
23
 * \author Mark Michelson <mmichelson@digium.com>
23
 * \author Mark Michelson <mmichelson@digium.com>
24
 *
24
 *
25
 * This module will run some dyanmic string tests.
25
 * This module will run some dyanmic string tests.
26
 *
26
 *
27
 * \ingroup tests
27
 * \ingroup tests
28
 */
28
 */
29

    
   
29

   
30
/*** MODULEINFO
30
/*** MODULEINFO
31
	<depend>TEST_FRAMEWORK</depend>
31
	<depend>TEST_FRAMEWORK</depend>
32
	<support_level>core</support_level>
32
	<support_level>core</support_level>
33
 ***/
33
 ***/
34

    
   
34

   
35
#include "asterisk.h"
35
#include "asterisk.h"
36

    
   
36

   
37
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38

    
   
38

   
39
#include "asterisk/test.h"
39
#include "asterisk/test.h"
40
#include "asterisk/utils.h"
40
#include "asterisk/utils.h"
41
#include "asterisk/strings.h"
41
#include "asterisk/strings.h"
42
#include "asterisk/module.h"
42
#include "asterisk/module.h"
43

    
   
43

   
44
AST_TEST_DEFINE(str_test)
44
AST_TEST_DEFINE(str_test)
45
{
45
{
46
	struct ast_str *stack_str;
46
	struct ast_str *stack_str;
47
	struct ast_str *heap_str;
47
	struct ast_str *heap_str;
48
	const char short_string1[] = "apple";
48
	const char short_string1[] = "apple";
49
	const char short_string2[] = "banana";
49
	const char short_string2[] = "banana";
50
	char short_string_cat[30];
50
	char short_string_cat[30];
51
	const char long_string1[] = "applebananapeachmangocherrypeargrapeplumlimetangerinepomegranategravel";
51
	const char long_string1[] = "applebananapeachmangocherrypeargrapeplumlimetangerinepomegranategravel";
52
	const char long_string2[] = "passionuglinectarinepineapplekiwilemonpaintthinner";
52
	const char long_string2[] = "passionuglinectarinepineapplekiwilemonpaintthinner";
53
	char long_string_cat[200];
53
	char long_string_cat[200];
54
	char string_limit_cat[11];
54
	char string_limit_cat[11];
55
	const int string_limit = 5;
55
	const int string_limit = 5;
56
	int current_size;
56
	int current_size;
57
	enum ast_test_result_state res = AST_TEST_PASS;
57
	enum ast_test_result_state res = AST_TEST_PASS;
58

    
   
58

   
59
	switch (cmd) {
59
	switch (cmd) {
60
	case TEST_INIT:
60
	case TEST_INIT:
61
		info->name = "str_test";
61
		info->name = "str_test";
62
		info->category = "/main/strings/";
62
		info->category = "/main/strings/";
63
		info->summary = "Test dynamic string operations";
63
		info->summary = "Test dynamic string operations";
64
		info->description = "Test setting and appending stack and heap-allocated strings";
64
		info->description = "Test setting and appending stack and heap-allocated strings";
65
		return AST_TEST_NOT_RUN;
65
		return AST_TEST_NOT_RUN;
66
	case TEST_EXECUTE:
66
	case TEST_EXECUTE:
67
		break;
67
		break;
68
	}
68
	}
69
	snprintf(short_string_cat, sizeof(short_string_cat), "%s%s", short_string1, short_string2);
69
	snprintf(short_string_cat, sizeof(short_string_cat), "%s%s", short_string1, short_string2);
70
	snprintf(long_string_cat, sizeof(long_string_cat), "%s%s", long_string1, long_string2);
70
	snprintf(long_string_cat, sizeof(long_string_cat), "%s%s", long_string1, long_string2);
71
	snprintf(string_limit_cat, string_limit, "%s", long_string1);
71
	snprintf(string_limit_cat, string_limit, "%s", long_string1);
72
	strncat(string_limit_cat, long_string2, string_limit);
72
	strncat(string_limit_cat, long_string2, string_limit);
73

    
   
73

   
74
	if (!(stack_str = ast_str_alloca(15))) {
74
	if (!(stack_str = ast_str_alloca(15))) {
75
		ast_test_status_update(test, "Failed to allocate an ast_str on the stack\n");
75
		ast_test_status_update(test, "Failed to allocate an ast_str on the stack\n");
76
		return AST_TEST_FAIL;
76
		return AST_TEST_FAIL;
77
	}
77
	}
78

    
   
78

   
79
	if (!(heap_str = ast_str_create(15))) {
79
	if (!(heap_str = ast_str_create(15))) {
80
		ast_test_status_update(test, "Failed to allocate an ast_str on the heap\n");
80
		ast_test_status_update(test, "Failed to allocate an ast_str on the heap\n");
81
	}
81
	}
82

    
   
82

   
83
	/* Stack string tests:
83
	/* Stack string tests:
84
	 * Part 1: Basic tests
84
	 * Part 1: Basic tests
85
	 * a. set a small string
85
	 * a. set a small string
86
	 * b. append a small string
86
	 * b. append a small string
87
	 * c. clear a string
87
	 * c. clear a string
88
	 * Part 2: Advanced tests
88
	 * Part 2: Advanced tests
89
	 * a. Set a string that is larger than our allocation
89
	 * a. Set a string that is larger than our allocation
90
	 * b. Append a string that is larger than our allocation
90
	 * b. Append a string that is larger than our allocation
91
	 */
91
	 */
92

    
   
92

   
93
	/* Part 1a */
93
	/* Part 1a */
94
	if (ast_str_set(&stack_str, 0, "%s", short_string1) < 0) {
94
	if (ast_str_set(&stack_str, 0, "%s", short_string1) < 0) {
95
		ast_test_status_update(test, "Error setting stack string\n");
95
		ast_test_status_update(test, "Error setting stack string\n");
96
		res = AST_TEST_FAIL;
96
		res = AST_TEST_FAIL;
97
		goto cleanup;
97
		goto cleanup;
98
	}
98
	}
99
	if (strcmp(ast_str_buffer(stack_str), short_string1)) {
99
	if (strcmp(ast_str_buffer(stack_str), short_string1)) {
100
		ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s' but"
100
		ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s' but"
101
				"instead got %s\n", short_string1, ast_str_buffer(stack_str));
101
				"instead got %s\n", short_string1, ast_str_buffer(stack_str));
102
		res = AST_TEST_FAIL;
102
		res = AST_TEST_FAIL;
103
		goto cleanup;
103
		goto cleanup;
104
	}
104
	}
105
	/* Part 1b */
105
	/* Part 1b */
106
	if (ast_str_append(&stack_str, 0, "%s", short_string2) < 0) {
106
	if (ast_str_append(&stack_str, 0, "%s", short_string2) < 0) {
107
		ast_test_status_update(test, "Error appending to stack string\n");
107
		ast_test_status_update(test, "Error appending to stack string\n");
108
		res = AST_TEST_FAIL;
108
		res = AST_TEST_FAIL;
109
		goto cleanup;
109
		goto cleanup;
110
	}
110
	}
111
	if (strcmp(ast_str_buffer(stack_str), short_string_cat)) {
111
	if (strcmp(ast_str_buffer(stack_str), short_string_cat)) {
112
		ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s'"
112
		ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s'"
113
				"but instead got %s\n", short_string_cat, ast_str_buffer(stack_str));
113
				"but instead got %s\n", short_string_cat, ast_str_buffer(stack_str));
114
		res = AST_TEST_FAIL;
114
		res = AST_TEST_FAIL;
115
		goto cleanup;
115
		goto cleanup;
116
	}
116
	}
117
	/* Part 1c */
117
	/* Part 1c */
118
	ast_str_reset(stack_str);
118
	ast_str_reset(stack_str);
119
	if (ast_str_strlen(stack_str) != 0) {
119
	if (ast_str_strlen(stack_str) != 0) {
120
		ast_test_status_update(test, "ast_str_reset resulted in non-zero length for stack_str\n");
120
		ast_test_status_update(test, "ast_str_reset resulted in non-zero length for stack_str\n");
121
		res = AST_TEST_FAIL;
121
		res = AST_TEST_FAIL;
122
		goto cleanup;
122
		goto cleanup;
123
	}
123
	}
124

    
   
124

   
125
	/* Part 2a */
125
	/* Part 2a */
126
	if (ast_str_set(&stack_str, -1, "%s", long_string1) < 0) {
126
	if (ast_str_set(&stack_str, -1, "%s", long_string1) < 0) {
127
		ast_test_status_update(test, "Error setting stack string with long input\n");
127
		ast_test_status_update(test, "Error setting stack string with long input\n");
128
		res = AST_TEST_FAIL;
128
		res = AST_TEST_FAIL;
129
		goto cleanup;
129
		goto cleanup;
130
	}
130
	}
131
	if (strncmp(ast_str_buffer(stack_str), long_string1, ast_str_strlen(stack_str))) {
131
	if (strncmp(ast_str_buffer(stack_str), long_string1, ast_str_strlen(stack_str))) {
132
		ast_test_status_update(test, "Stack string not set to what is expected.\n");
132
		ast_test_status_update(test, "Stack string not set to what is expected.\n");
133
		res = AST_TEST_FAIL;
133
		res = AST_TEST_FAIL;
134
		goto cleanup;
134
		goto cleanup;
135
	}
135
	}
136
	/* Part 2b */
136
	/* Part 2b */
137
	if (ast_str_append(&stack_str, -1, "%s", long_string2) < 0) {
137
	if (ast_str_append(&stack_str, -1, "%s", long_string2) < 0) {
138
		ast_test_status_update(test, "Error appending long string to full stack string buffer\n");
138
		ast_test_status_update(test, "Error appending long string to full stack string buffer\n");
139
		res = AST_TEST_FAIL;
139
		res = AST_TEST_FAIL;
140
		goto cleanup;
140
		goto cleanup;
141
	}
141
	}
142
	if (strncmp(ast_str_buffer(stack_str), long_string_cat, ast_str_strlen(stack_str))) {
142
	if (strncmp(ast_str_buffer(stack_str), long_string_cat, ast_str_strlen(stack_str))) {
143
		ast_test_status_update(test, "Stack string not set to what is expected.\n");
143
		ast_test_status_update(test, "Stack string not set to what is expected.\n");
144
		res = AST_TEST_FAIL;
144
		res = AST_TEST_FAIL;
145
		goto cleanup;
145
		goto cleanup;
146
	}
146
	}
147

    
   
147

   
148
	/* Heap string tests
148
	/* Heap string tests
149
	 *
149
	 *
150
	 * All stack string tests from part 1.
150
	 * All stack string tests from part 1.
151
	 * All stack string tests 2a and 2b.
151
	 * All stack string tests 2a and 2b.
152
	 * Tests 2a and 2b from stack string tests, passing 0 as max_len
152
	 * Tests 2a and 2b from stack string tests, passing 0 as max_len
153
	 * instead of -1. This allows for the buffer to grow.
153
	 * instead of -1. This allows for the buffer to grow.
154
	 */
154
	 */
155
	/* Part 1a */
155
	/* Part 1a */
156
	if (ast_str_set(&heap_str, 0, "%s", short_string1) < 0) {
156
	if (ast_str_set(&heap_str, 0, "%s", short_string1) < 0) {
157
		ast_test_status_update(test, "Error setting heap string\n");
157
		ast_test_status_update(test, "Error setting heap string\n");
158
		res = AST_TEST_FAIL;
158
		res = AST_TEST_FAIL;
159
		goto cleanup;
159
		goto cleanup;
160
	}
160
	}
161
	if (strcmp(ast_str_buffer(heap_str), short_string1)) {
161
	if (strcmp(ast_str_buffer(heap_str), short_string1)) {
162
		ast_test_status_update(test, "ast_str_set failed for heap string. Expected '%s' but"
162
		ast_test_status_update(test, "ast_str_set failed for heap string. Expected '%s' but"
163
				"instead got %s\n", short_string1, ast_str_buffer(heap_str));
163
				"instead got %s\n", short_string1, ast_str_buffer(heap_str));
164
		res = AST_TEST_FAIL;
164
		res = AST_TEST_FAIL;
165
		goto cleanup;
165
		goto cleanup;
166
	}
166
	}
167
	/* Part 1b */
167
	/* Part 1b */
168
	if (ast_str_append(&heap_str, 0, "%s", short_string2) < 0) {
168
	if (ast_str_append(&heap_str, 0, "%s", short_string2) < 0) {
169
		ast_test_status_update(test, "Error appending to heap string\n");
169
		ast_test_status_update(test, "Error appending to heap string\n");
170
		res = AST_TEST_FAIL;
170
		res = AST_TEST_FAIL;
171
		goto cleanup;
171
		goto cleanup;
172
	}
172
	}
173
	if (strcmp(ast_str_buffer(heap_str), short_string_cat)) {
173
	if (strcmp(ast_str_buffer(heap_str), short_string_cat)) {
174
		ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s'"
174
		ast_test_status_update(test, "ast_str_set failed for stack string. Expected '%s'"
175
				"but instead got %s\n", short_string_cat, ast_str_buffer(stack_str));
175
				"but instead got %s\n", short_string_cat, ast_str_buffer(stack_str));
176
		res = AST_TEST_FAIL;
176
		res = AST_TEST_FAIL;
177
		goto cleanup;
177
		goto cleanup;
178
	}
178
	}
179
	/* Part 1c */
179
	/* Part 1c */
180
	ast_str_reset(heap_str);
180
	ast_str_reset(heap_str);
181
	if (ast_str_strlen(heap_str) != 0) {
181
	if (ast_str_strlen(heap_str) != 0) {
182
		ast_test_status_update(test, "ast_str_reset resulted in non-zero length for stack_str\n");
182
		ast_test_status_update(test, "ast_str_reset resulted in non-zero length for stack_str\n");
183
		res = AST_TEST_FAIL;
183
		res = AST_TEST_FAIL;
184
		goto cleanup;
184
		goto cleanup;
185
	}
185
	}
186
	/* Part 2a with -1 arg */
186
	/* Part 2a with -1 arg */
187
	current_size = ast_str_size(heap_str);
187
	current_size = ast_str_size(heap_str);
188
	if (ast_str_set(&heap_str, -1, "%s", long_string1) < 0) {
188
	if (ast_str_set(&heap_str, -1, "%s", long_string1) < 0) {
189
		ast_test_status_update(test, "Error setting heap string with long input\n");
189
		ast_test_status_update(test, "Error setting heap string with long input\n");
190
		res = AST_TEST_FAIL;
190
		res = AST_TEST_FAIL;
191
		goto cleanup;
191
		goto cleanup;
192
	}
192
	}
193
	if (current_size != ast_str_size(heap_str)) {
193
	if (current_size != ast_str_size(heap_str)) {
194
		ast_test_status_update(test, "Heap string changed size during ast_str_set when it was"
194
		ast_test_status_update(test, "Heap string changed size during ast_str_set when it was"
195
				"instructed not to. Was %d and now is %d\n", current_size, (int) ast_str_size(heap_str));
195
				"instructed not to. Was %d and now is %d\n", current_size, (int) ast_str_size(heap_str));
196
		res = AST_TEST_FAIL;
196
		res = AST_TEST_FAIL;
197
		goto cleanup;
197
		goto cleanup;
198
	}
198
	}
199
	if (strncmp(ast_str_buffer(heap_str), long_string1, ast_str_strlen(heap_str))) {
199
	if (strncmp(ast_str_buffer(heap_str), long_string1, ast_str_strlen(heap_str))) {
200
		ast_test_status_update(test, "Heap string not set to what is expected.\n");
200
		ast_test_status_update(test, "Heap string not set to what is expected.\n");
201
		res = AST_TEST_FAIL;
201
		res = AST_TEST_FAIL;
202
		goto cleanup;
202
		goto cleanup;
203
	}
203
	}
204
	/* Part 2b with -1 arg */
204
	/* Part 2b with -1 arg */
205
	current_size = ast_str_size(heap_str);
205
	current_size = ast_str_size(heap_str);
206
	if (ast_str_append(&heap_str, -1, "%s", long_string2) < 0) {
206
	if (ast_str_append(&heap_str, -1, "%s", long_string2) < 0) {
207
		ast_test_status_update(test, "Error appending long string to full heap string buffer\n");
207
		ast_test_status_update(test, "Error appending long string to full heap string buffer\n");
208
		res = AST_TEST_FAIL;
208
		res = AST_TEST_FAIL;
209
		goto cleanup;
209
		goto cleanup;
210
	}
210
	}
211
	if (current_size != ast_str_size(heap_str)) {
211
	if (current_size != ast_str_size(heap_str)) {
212
		ast_test_status_update(test, "Heap string changed size during ast_str_append when it was"
212
		ast_test_status_update(test, "Heap string changed size during ast_str_append when it was"
213
				"instructed not to. Was %d and now is %d\n", current_size, (int) ast_str_size(heap_str));
213
				"instructed not to. Was %d and now is %d\n", current_size, (int) ast_str_size(heap_str));
214
		res = AST_TEST_FAIL;
214
		res = AST_TEST_FAIL;
215
		goto cleanup;
215
		goto cleanup;
216
	}
216
	}
217
	if (strncmp(ast_str_buffer(heap_str), long_string_cat, ast_str_strlen(heap_str))) {
217
	if (strncmp(ast_str_buffer(heap_str), long_string_cat, ast_str_strlen(heap_str))) {
218
		ast_test_status_update(test, "Heap string not set to what is expected.\n");
218
		ast_test_status_update(test, "Heap string not set to what is expected.\n");
219
		res = AST_TEST_FAIL;
219
		res = AST_TEST_FAIL;
220
		goto cleanup;
220
		goto cleanup;
221
	}
221
	}
222
	/* reset string before continuing */
222
	/* reset string before continuing */
223
	ast_str_reset(heap_str);
223
	ast_str_reset(heap_str);
224
	/* Part 2a with 0 arg */
224
	/* Part 2a with 0 arg */
225
	if (ast_str_set(&heap_str, 0, "%s", long_string1) < 0) {
225
	if (ast_str_set(&heap_str, 0, "%s", long_string1) < 0) {
226
		ast_test_status_update(test, "Error setting heap string with long input\n");
226
		ast_test_status_update(test, "Error setting heap string with long input\n");
227
		res = AST_TEST_FAIL;
227
		res = AST_TEST_FAIL;
228
		goto cleanup;
228
		goto cleanup;
229
	}
229
	}
230
	if (strcmp(ast_str_buffer(heap_str), long_string1)) {
230
	if (strcmp(ast_str_buffer(heap_str), long_string1)) {
231
		ast_test_status_update(test, "Heap string does not contain what was expected. Expected %s"
231
		ast_test_status_update(test, "Heap string does not contain what was expected. Expected %s"
232
				"but have %s instead\n", long_string1, ast_str_buffer(heap_str));
232
				"but have %s instead\n", long_string1, ast_str_buffer(heap_str));
233
		res = AST_TEST_FAIL;
233
		res = AST_TEST_FAIL;
234
		goto cleanup;
234
		goto cleanup;
235
	}
235
	}
236
	/* Part 2b with 0 arg */
236
	/* Part 2b with 0 arg */
237
	if (ast_str_append(&heap_str, 0, "%s", long_string2) < 0) {
237
	if (ast_str_append(&heap_str, 0, "%s", long_string2) < 0) {
238
		ast_test_status_update(test, "Error setting heap string with long input\n");
238
		ast_test_status_update(test, "Error setting heap string with long input\n");
239
		res = AST_TEST_FAIL;
239
		res = AST_TEST_FAIL;
240
		goto cleanup;
240
		goto cleanup;
241
	}
241
	}
242
	if (strcmp(ast_str_buffer(heap_str), long_string_cat)) {
242
	if (strcmp(ast_str_buffer(heap_str), long_string_cat)) {
243
		ast_test_status_update(test, "Heap string does not contain what was expected. Expected %s"
243
		ast_test_status_update(test, "Heap string does not contain what was expected. Expected %s"
244
				"but have %s instead\n", long_string_cat, ast_str_buffer(heap_str));
244
				"but have %s instead\n", long_string_cat, ast_str_buffer(heap_str));
245
		res = AST_TEST_FAIL;
245
		res = AST_TEST_FAIL;
246
		goto cleanup;
246
		goto cleanup;
247
	}
247
	}
248

    
   
248

   
249
cleanup:
249
cleanup:
250
	ast_free(heap_str);
250
	ast_free(heap_str);
251
	return res;
251
	return res;
252
}
252
}
253

    
   
253

   
254
AST_TEST_DEFINE(begins_with_test)
254
AST_TEST_DEFINE(begins_with_test)
255
{
255
{
256
	switch (cmd) {
256
	switch (cmd) {
257
	case TEST_INIT:
257
	case TEST_INIT:
258
		info->name = "begins_with";
258
		info->name = "begins_with";
259
		info->category = "/main/strings/";
259
		info->category = "/main/strings/";
260
		info->summary = "Test ast_begins_with";
260
		info->summary = "Test ast_begins_with";
261
		info->description = "Test ast_begins_with";
261
		info->description = "Test ast_begins_with";
262
		return AST_TEST_NOT_RUN;
262
		return AST_TEST_NOT_RUN;
263
	case TEST_EXECUTE:
263
	case TEST_EXECUTE:
264
		break;
264
		break;
265
	}
265
	}
266

    
   
266

   
267
	// prefixes
267
	// prefixes
268
	ast_test_validate(test, 1 == ast_begins_with("foobar", "foobar"));
268
	ast_test_validate(test, 1 == ast_begins_with("foobar", "foobar"));
269
	ast_test_validate(test, 1 == ast_begins_with("foobar", "foo"));
269
	ast_test_validate(test, 1 == ast_begins_with("foobar", "foo"));
270
	ast_test_validate(test, 1 == ast_begins_with("foobar", ""));
270
	ast_test_validate(test, 1 == ast_begins_with("foobar", ""));
271
	ast_test_validate(test, 1 == ast_begins_with("", ""));
271
	ast_test_validate(test, 1 == ast_begins_with("", ""));
272

    
   
272

   
273
	// not prefixes
273
	// not prefixes
274
	ast_test_validate(test, 0 == ast_begins_with("foobar", "bang"));
274
	ast_test_validate(test, 0 == ast_begins_with("foobar", "bang"));
275
	ast_test_validate(test, 0 == ast_begins_with("foobar", "foobat"));
275
	ast_test_validate(test, 0 == ast_begins_with("foobar", "foobat"));
276
	ast_test_validate(test, 0 == ast_begins_with("boo", "boom"));
276
	ast_test_validate(test, 0 == ast_begins_with("boo", "boom"));
277
	ast_test_validate(test, 0 == ast_begins_with("", "blitz"));
277
	ast_test_validate(test, 0 == ast_begins_with("", "blitz"));
278

    
   
278

   
279
	// nothing failed; we're all good!
279
	// nothing failed; we're all good!
280
	return AST_TEST_PASS;
280
	return AST_TEST_PASS;
281
}
281
}
282

    
   
282

   
283
AST_TEST_DEFINE(ends_with_test)
283
AST_TEST_DEFINE(ends_with_test)
284
{
284
{
285
	switch (cmd) {
285
	switch (cmd) {
286
	case TEST_INIT:
286
	case TEST_INIT:
287
		info->name = "ends_with";
287
		info->name = "ends_with";
288
		info->category = "/main/strings/";
288
		info->category = "/main/strings/";
289
		info->summary = "Test ast_ends_with";
289
		info->summary = "Test ast_ends_with";
290
		info->description = "Test ast_ends_with";
290
		info->description = "Test ast_ends_with";
291
		return AST_TEST_NOT_RUN;
291
		return AST_TEST_NOT_RUN;
292
	case TEST_EXECUTE:
292
	case TEST_EXECUTE:
293
		break;
293
		break;
294
	}
294
	}
295

    
   
295

   
296
	// prefixes
296
	// prefixes
297
	ast_test_validate(test, 1 == ast_ends_with("foobar", "foobar"));
297
	ast_test_validate(test, 1 == ast_ends_with("foobar", "foobar"));
298
	ast_test_validate(test, 1 == ast_ends_with("foobar", "bar"));
298
	ast_test_validate(test, 1 == ast_ends_with("foobar", "bar"));
299
	ast_test_validate(test, 1 == ast_ends_with("foobar", ""));
299
	ast_test_validate(test, 1 == ast_ends_with("foobar", ""));
300
	ast_test_validate(test, 1 == ast_ends_with("", ""));
300
	ast_test_validate(test, 1 == ast_ends_with("", ""));
301

    
   
301

   
302
	// not suffixes
302
	// not suffixes
303
	ast_test_validate(test, 0 == ast_ends_with("bar", "bbar"));
303
	ast_test_validate(test, 0 == ast_ends_with("bar", "bbar"));
304
	ast_test_validate(test, 0 == ast_ends_with("foobar", "bang"));
304
	ast_test_validate(test, 0 == ast_ends_with("foobar", "bang"));
305
	ast_test_validate(test, 0 == ast_ends_with("foobar", "foobat"));
305
	ast_test_validate(test, 0 == ast_ends_with("foobar", "foobat"));
306
	ast_test_validate(test, 0 == ast_ends_with("boo", "boom"));
306
	ast_test_validate(test, 0 == ast_ends_with("boo", "boom"));
307
	ast_test_validate(test, 0 == ast_ends_with("", "blitz"));
307
	ast_test_validate(test, 0 == ast_ends_with("", "blitz"));
308

    
   
308

   
309
	// nothing failed; we're all good!
309
	// nothing failed; we're all good!
310
	return AST_TEST_PASS;
310
	return AST_TEST_PASS;
311
}
311
}
312

    
   
312

   
313
AST_TEST_DEFINE(strsep_test)
313
AST_TEST_DEFINE(strsep_test)
314
{
314
{
315
	char *test1, *test2, *test3;
315
	char *test1, *test2, *test3;
316

    
   
316

   
317
	switch (cmd) {
317
	switch (cmd) {
318
	case TEST_INIT:
318
	case TEST_INIT:
319
		info->name = "strsep";
319
		info->name = "strsep";
320
		info->category = "/main/strings/";
320
		info->category = "/main/strings/";
321
		info->summary = "Test ast_strsep";
321
		info->summary = "Test ast_strsep";
322
		info->description = "Test ast_strsep";
322
		info->description = "Test ast_strsep";
323
		return AST_TEST_NOT_RUN;
323
		return AST_TEST_NOT_RUN;
324
	case TEST_EXECUTE:
324
	case TEST_EXECUTE:
325
		break;
325
		break;
326
	}
326
	}
327

    
   
327

   
328
	test1 = ast_strdupa("ghi=jkl,mno='pqr,stu',abc=def, vwx = yz1 ,  vwx = yz1 ,  '"
328
	test1 = ast_strdupa("ghi=jkl,mno='pqr,stu',abc=def, vwx = yz1 ,  vwx = yz1 ,  '"
329
		" vwx = yz1 ' ,  ' vwx , yz1 ',v\"w\"x, '\"x,v\",\"x\"' , \" i\\'m a test\""
329
		" vwx = yz1 ' ,  ' vwx , yz1 ',v\"w\"x, '\"x,v\",\"x\"' , \" i\\'m a test\""
330
		", \" i\\'m a, test\", \" i\\'m a, test\", e\\,nd, end\\");
330
		", \" i\\'m a, test\", \" i\\'m a, test\", e\\,nd, end\\");
331

    
   
331

   
332
	test2 = ast_strsep(&test1, ',', 0);
332
	test2 = ast_strsep(&test1, ',', 0);
333
	ast_test_validate(test, 0 == strcmp("ghi=jkl", test2));
333
	ast_test_validate(test, 0 == strcmp("ghi=jkl", test2));
334

    
   
334

   
335
	test3 = ast_strsep(&test2, '=', 0);
335
	test3 = ast_strsep(&test2, '=', 0);
336
	ast_test_validate(test, 0 == strcmp("ghi", test3));
336
	ast_test_validate(test, 0 == strcmp("ghi", test3));
337

    
   
337

   
338
	test3 = ast_strsep(&test2, '=', 0);
338
	test3 = ast_strsep(&test2, '=', 0);
339
	ast_test_validate(test, 0 == strcmp("jkl", test3));
339
	ast_test_validate(test, 0 == strcmp("jkl", test3));
340

    
   
340

   
341
	test2 = ast_strsep(&test1, ',', 0);
341
	test2 = ast_strsep(&test1, ',', 0);
342
	ast_test_validate(test, 0 == strcmp("mno='pqr,stu'", test2));
342
	ast_test_validate(test, 0 == strcmp("mno='pqr,stu'", test2));
343

    
   
343

   
344
	test3 = ast_strsep(&test2, '=', 0);
344
	test3 = ast_strsep(&test2, '=', 0);
345
	ast_test_validate(test, 0 == strcmp("mno", test3));
345
	ast_test_validate(test, 0 == strcmp("mno", test3));
346

    
   
346

   
347
	test3 = ast_strsep(&test2, '=', 0);
347
	test3 = ast_strsep(&test2, '=', 0);
348
	ast_test_validate(test, 0 == strcmp("'pqr,stu'", test3));
348
	ast_test_validate(test, 0 == strcmp("'pqr,stu'", test3));
349

    
   
349

   
350
	test2 = ast_strsep(&test1, ',', 0);
350
	test2 = ast_strsep(&test1, ',', 0);
351
	ast_test_validate(test, 0 == strcmp("abc=def", test2));
351
	ast_test_validate(test, 0 == strcmp("abc=def", test2));
352

    
   
352

   
353
	test2 = ast_strsep(&test1, ',', 0);
353
	test2 = ast_strsep(&test1, ',', 0);
354
	ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2));
354
	ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2));
355

    
   
355

   
356
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
356
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
357
	ast_test_validate(test, 0 == strcmp("vwx = yz1", test2));
357
	ast_test_validate(test, 0 == strcmp("vwx = yz1", test2));
358

    
   
358

   
359
	test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP);
359
	test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP);
360
	ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2));
360
	ast_test_validate(test, 0 == strcmp(" vwx = yz1 ", test2));
361

    
   
361

   
362
	test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM);
362
	test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM);
363
	ast_test_validate(test, 0 == strcmp("vwx , yz1", test2));
363
	ast_test_validate(test, 0 == strcmp("vwx , yz1", test2));
364

    
   
364

   
365
	test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM);
365
	test2 = ast_strsep(&test1, ',', AST_STRSEP_STRIP | AST_STRSEP_TRIM);
366
	ast_test_validate(test, 0 == strcmp("v\"w\"x", test2));
366
	ast_test_validate(test, 0 == strcmp("v\"w\"x", test2));
367

    
   
367

   
368
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
368
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
369
	ast_test_validate(test, 0 == strcmp("'\"x,v\",\"x\"'", test2));
369
	ast_test_validate(test, 0 == strcmp("'\"x,v\",\"x\"'", test2));
370

    
   
370

   
371
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
371
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM);
372
	ast_test_validate(test, 0 == strcmp("\" i\\'m a test\"", test2));
372
	ast_test_validate(test, 0 == strcmp("\" i\\'m a test\"", test2));
373

    
   
373

   
374
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
374
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
375
	ast_test_validate(test, 0 == strcmp("\" i'm a, test\"", test2));
375
	ast_test_validate(test, 0 == strcmp("\" i'm a, test\"", test2));
376

    
   
376

   
377
	test2 = ast_strsep(&test1, ',', AST_STRSEP_ALL);
377
	test2 = ast_strsep(&test1, ',', AST_STRSEP_ALL);
378
	ast_test_validate(test, 0 == strcmp("i'm a, test", test2));
378
	ast_test_validate(test, 0 == strcmp("i'm a, test", test2));
379

    
   
379

   
380
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
380
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
381
	ast_test_validate(test, 0 == strcmp("e,nd", test2));
381
	ast_test_validate(test, 0 == strcmp("e,nd", test2));
382

    
   
382

   
383
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
383
	test2 = ast_strsep(&test1, ',', AST_STRSEP_TRIM | AST_STRSEP_UNESCAPE);
384
	ast_test_validate(test, 0 == strcmp("end", test2));
384
	ast_test_validate(test, 0 == strcmp("end", test2));
385

    
   
385

   
386
	// nothing failed; we're all good!
386
	// nothing failed; we're all good!
387
	return AST_TEST_PASS;
387
	return AST_TEST_PASS;
388
}
388
}
389

    
   
389

   
390
static int test_semi(char *string1, char *string2, int test_len)
390
static int test_semi(char *string1, char *string2, int test_len)
391
{
391
{
392
	char *test2 = NULL;
392
	char *test2 = NULL;
393
	
393

   
394
	if (test_len > 0) {
394
	if (test_len > 0) {
395
		test2 = ast_alloca(test_len);
395
		test2 = ast_alloca(test_len);
396
		*test2 = '\0';
396
		*test2 = '\0';
397
	} else if (test_len == 0) {
397
	} else if (test_len == 0) {
398
		test2 = "";
398
		test2 = "";
399
	}
399
	}
400
	ast_escape_semicolons(string1, test2, test_len);
400
	ast_escape_semicolons(string1, test2, test_len);
401
	if (test2 != NULL && strcmp(string2, test2) == 0) {
401
	if (test2 != NULL && strcmp(string2, test2) == 0) {
402
		return 1;
402
		return 1;
403
	} else {
403
	} else {
404
		return 0;
404
		return 0;
405
	}
405
	}
406
}
406
}
407

    
   
407

   
408
AST_TEST_DEFINE(escape_semicolons_test)
408
AST_TEST_DEFINE(escape_semicolons_test)
409
{
409
{
410
	switch (cmd) {
410
	switch (cmd) {
411
	case TEST_INIT:
411
	case TEST_INIT:
412
		info->name = "escape_semicolons";
412
		info->name = "escape_semicolons";
413
		info->category = "/main/strings/";
413
		info->category = "/main/strings/";
414
		info->summary = "Test ast_escape_semicolons";
414
		info->summary = "Test ast_escape_semicolons";
415
		info->description = "Test ast_escape_semicolons";
415
		info->description = "Test ast_escape_semicolons";
416
		return AST_TEST_NOT_RUN;
416
		return AST_TEST_NOT_RUN;
417
	case TEST_EXECUTE:
417
	case TEST_EXECUTE:
418
		break;
418
		break;
419
	}
419
	}
420

    
   
420

   
421

    
   
421

   
422
	ast_test_validate(test, test_semi("this is a ;test", "this is a \\;test", 18));
422
	ast_test_validate(test, test_semi("this is a ;test", "this is a \\;test", 18));
423
	ast_test_validate(test, test_semi(";", "\\;", 3));
423
	ast_test_validate(test, test_semi(";", "\\;", 3));
424

    
   
424

   
425
	/* The following tests should return empty because there's not enough room to output
425
	/* The following tests should return empty because there's not enough room to output
426
	 * an escaped ; or even a single character.
426
	 * an escaped ; or even a single character.
427
	 */
427
	 */
428
	ast_test_validate(test, test_semi(";", "", 0));
428
	ast_test_validate(test, test_semi(";", "", 0));
429
	ast_test_validate(test, test_semi(";", "", 1));
429
	ast_test_validate(test, test_semi(";", "", 1));
430
	ast_test_validate(test, test_semi(";", "", 2));
430
	ast_test_validate(test, test_semi(";", "", 2));
431
	ast_test_validate(test, test_semi("x", "", 0));
431
	ast_test_validate(test, test_semi("x", "", 0));
432
	ast_test_validate(test, test_semi("x", "", 1));
432
	ast_test_validate(test, test_semi("x", "", 1));
433

    
   
433

   
434
	/* At least some output should be produced now. */
434
	/* At least some output should be produced now. */
435
	ast_test_validate(test, test_semi("xx;xx", "x", 2));
435
	ast_test_validate(test, test_semi("xx;xx", "x", 2));
436
	ast_test_validate(test, test_semi("xx;xx", "xx", 3));
436
	ast_test_validate(test, test_semi("xx;xx", "xx", 3));
437

    
   
437

   
438
	/* There's still not enough room to output \; so
438
	/* There's still not enough room to output \; so
439
	 * don't even print the \
439
	 * don't even print the \
440
	 */
440
	 */
441
	ast_test_validate(test, test_semi("xx;xx", "xx", 4));
441
	ast_test_validate(test, test_semi("xx;xx", "xx", 4));
442

    
   
442

   
443
	ast_test_validate(test, test_semi("xx;xx", "xx\\;", 5));
443
	ast_test_validate(test, test_semi("xx;xx", "xx\\;", 5));
444
	ast_test_validate(test, test_semi("xx;xx", "xx\\;x", 6));
444
	ast_test_validate(test, test_semi("xx;xx", "xx\\;x", 6));
445
	ast_test_validate(test, test_semi("xx;xx", "xx\\;xx", 7));
445
	ast_test_validate(test, test_semi("xx;xx", "xx\\;xx", 7));
446
	ast_test_validate(test, test_semi("xx;xx", "xx\\;xx", 8));
446
	ast_test_validate(test, test_semi("xx;xx", "xx\\;xx", 8));
447

    
   
447

   
448
	/* Random stuff */
448
	/* Random stuff */
449
	ast_test_validate(test, test_semi("xx;xx;this is a test", "xx\\;xx\\;this is a test", 32));
449
	ast_test_validate(test, test_semi("xx;xx;this is a test", "xx\\;xx\\;this is a test", 32));
450
	ast_test_validate(test, test_semi(";;;;;", "\\;\\;\\;\\;\\;", 32));
450
	ast_test_validate(test, test_semi(";;;;;", "\\;\\;\\;\\;\\;", 32));
451
	ast_test_validate(test, test_semi(";;;;;", "\\;\\;\\;\\;", 10));
451
	ast_test_validate(test, test_semi(";;;;;", "\\;\\;\\;\\;", 10));
452
	ast_test_validate(test, test_semi(";;;;;", "\\;\\;\\;\\;\\;", 11));
452
	ast_test_validate(test, test_semi(";;;;;", "\\;\\;\\;\\;\\;", 11));
453
	ast_test_validate(test, test_semi(";;\\;;;", "\\;\\;\\\\;\\;\\;", 32));
453
	ast_test_validate(test, test_semi(";;\\;;;", "\\;\\;\\\\;\\;\\;", 32));
454

    
   
454

   
455
	return AST_TEST_PASS;
455
	return AST_TEST_PASS;
456
}
456
}
457

    
   
457

   
458
static int unload_module(void)
458
static int unload_module(void)
459
{
459
{
460
	AST_TEST_UNREGISTER(str_test);
460
	AST_TEST_UNREGISTER(str_test);
461
	AST_TEST_UNREGISTER(begins_with_test);
461
	AST_TEST_UNREGISTER(begins_with_test);
462
	AST_TEST_UNREGISTER(ends_with_test);
462
	AST_TEST_UNREGISTER(ends_with_test);
463
	AST_TEST_UNREGISTER(strsep_test);
463
	AST_TEST_UNREGISTER(strsep_test);
464
	AST_TEST_UNREGISTER(escape_semicolons_test);
464
	AST_TEST_UNREGISTER(escape_semicolons_test);
465
	return 0;
465
	return 0;
466
}
466
}
467

    
   
467

   
468
static int load_module(void)
468
static int load_module(void)
469
{
469
{
470
	AST_TEST_REGISTER(str_test);
470
	AST_TEST_REGISTER(str_test);
471
	AST_TEST_REGISTER(begins_with_test);
471
	AST_TEST_REGISTER(begins_with_test);
472
	AST_TEST_REGISTER(ends_with_test);
472
	AST_TEST_REGISTER(ends_with_test);
473
	AST_TEST_REGISTER(strsep_test);
473
	AST_TEST_REGISTER(strsep_test);
474
	AST_TEST_REGISTER(escape_semicolons_test);
474
	AST_TEST_REGISTER(escape_semicolons_test);
475
	return AST_MODULE_LOAD_SUCCESS;
475
	return AST_MODULE_LOAD_SUCCESS;
476
}
476
}
477

    
   
477

   
478
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dynamic string test module");
478
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dynamic string test module");
  1. /branches/13/tests/test_strings.c: Loading...

https://reviewboard.asterisk.org/ runs on a server provided by Digium, Inc. and uses bandwidth donated to the open source Asterisk community by API Digital Communications in Huntsville, AL USA.
Please report problems with this site to asteriskteam@digium.com.