Browse code

misctest: added option to perform tests for sip message

Daniel-Constantin Mierla authored on 07/09/2021 08:50:03
Showing 2 changed files
... ...
@@ -13,7 +13,7 @@
13 13
 	<title>Parameters</title>
14 14
 
15 15
 	<section id="misctest.p.memory">
16
-		<title><varname>memory</varname></title>
16
+		<title><varname>memory</varname> (int)</title>
17 17
 		<para>
18 18
 			Initialize for memory testing. Set it to 1 to prepare the memory
19 19
 			testing.
... ...
@@ -26,13 +26,15 @@
26 26
 				Set <varname>memory</varname> in the config file
27 27
 			</title>
28 28
 			<programlisting>
29
+...
29 30
 modparam("misctest", "memory", 1)
31
+...
30 32
 			</programlisting>
31 33
 		</example>
32 34
 	</section>
33 35
 
34 36
 	<section id="misctest.p.mem_check_content">
35
-		<title><varname>mem_check_content</varname></title>
37
+		<title><varname>mem_check_content</varname> (int)</title>
36 38
 		<para>
37 39
 			When doing the tests, check also for the possibility of the
38 40
 			memory being overwritten. When activated, the allocated memory
... ...
@@ -50,7 +52,9 @@ modparam("misctest", "memory", 1)
50 52
 				Set <varname>mem_check_content</varname> in the config file
51 53
 			</title>
52 54
 			<programlisting>
55
+...
53 56
 modparam("misctest", "mem_check_content", 1)
57
+...
54 58
 			</programlisting>
55 59
 		</example>
56 60
 		<example>
... ...
@@ -58,7 +62,69 @@ modparam("misctest", "mem_check_content", 1)
58 62
 				Set <varname>mem_check_content</varname> at runtime via kamcmd
59 63
 			</title>
60 64
 			<programlisting>
65
+...
61 66
 $ &kamcmd; cfg.seti misctest mem_check_content 1
67
+...
68
+			</programlisting>
69
+		</example>
70
+	</section>
71
+
72
+	<section id="misctest.p.message">
73
+		<title><varname>message</varname> (int)</title>
74
+		<para>
75
+			Initialize and perform message testing.
76
+		</para>
77
+		<para>
78
+			Default: 0 (off).
79
+		</para>
80
+		<example>
81
+			<title>
82
+				Set <varname>message</varname> in the config file
83
+			</title>
84
+			<programlisting>
85
+...
86
+modparam("misctest", "message", 1)
87
+...
88
+			</programlisting>
89
+		</example>
90
+	</section>
91
+
92
+	<section id="misctest.p.message_data">
93
+		<title><varname>message_data</varname> (str)</title>
94
+		<para>
95
+			Set SIP message data for testing.
96
+		</para>
97
+		<para>
98
+			Default: NULL (not set).
99
+		</para>
100
+		<example>
101
+			<title>
102
+				Set <varname>message_data</varname> in the config file
103
+			</title>
104
+			<programlisting>
105
+...
106
+modparam("misctest", "message_data", "...")
107
+...
108
+			</programlisting>
109
+		</example>
110
+	</section>
111
+
112
+	<section id="misctest.p.message_file">
113
+		<title><varname>message_file</varname> (str)</title>
114
+		<para>
115
+			Set path to file from where to read SIP message data for testing.
116
+		</para>
117
+		<para>
118
+			Default: NULL (not set).
119
+		</para>
120
+		<example>
121
+			<title>
122
+				Set <varname>message_file</varname> in the config file
123
+			</title>
124
+			<programlisting>
125
+...
126
+modparam("misctest", "message_file", "/tmp/sip-data.txt")
127
+...
62 128
 			</programlisting>
63 129
 		</example>
64 130
 	</section>
... ...
@@ -18,6 +18,11 @@
18 18
  */
19 19
 
20 20
 
21
+#include <stdio.h>
22
+#include <unistd.h>
23
+#include <stdlib.h>
24
+#include <string.h>
25
+
21 26
 #include "../../core/sr_module.h"
22 27
 #include "../../core/mem/mem.h"
23 28
 #include "../../core/str.h"
... ...
@@ -30,6 +35,15 @@
30 35
 #include "../../core/timer.h"
31 36
 #include "../../core/mod_fix.h"
32 37
 
38
+#include "../../core/parser/sdp/sdp.h"
39
+#include "../../core/parser/parse_uri.c"
40
+#include "../../core/parser/parse_hname2.h"
41
+#include "../../core/parser/contact/parse_contact.h"
42
+#include "../../core/parser/parse_refer_to.h"
43
+#include "../../core/parser/parse_ppi_pai.h"
44
+#include "../../core/parser/parse_privacy.h"
45
+#include "../../core/parser/parse_diversion.h"
46
+
33 47
 MODULE_VERSION
34 48
 
35 49
 static int mt_mem_alloc_f(struct sip_msg *, char *, char *);
... ...
@@ -37,7 +51,13 @@ static int mt_mem_free_f(struct sip_msg *, char *, char *);
37 51
 static int mod_init(void);
38 52
 static void mod_destroy(void);
39 53
 
54
+static int misctest_memory_init(void);
55
+static int misctest_message_init(void);
56
+
40 57
 static int misctest_memory = 0;
58
+static int misctest_message = 0;
59
+static str misctest_message_data = STR_NULL;
60
+static str misctest_message_file = STR_NULL;
41 61
 
42 62
 /* clang-format off */
43 63
 static cmd_export_t cmds[]={
... ...
@@ -89,6 +109,9 @@ static rpc_export_t mt_rpc[];
89 109
 /* clang-format off */
90 110
 static param_export_t params[]={
91 111
 	{"memory", PARAM_INT, &misctest_memory},
112
+	{"message", PARAM_INT, &misctest_message},
113
+	{"message_data", PARAM_STR, &misctest_message_data},
114
+	{"message_file", PARAM_STR, &misctest_message_file},
92 115
 	{"mem_check_content", PARAM_INT, &default_mt_cfg.mem_check_content},
93 116
 	{0,0,0}
94 117
 };
... ...
@@ -129,7 +152,7 @@ struct allocated_list
129 152
 	volatile int no;
130 153
 };
131 154
 
132
-struct allocated_list *alloc_lst;
155
+struct allocated_list *alloc_lst = NULL;
133 156
 
134 157
 
135 158
 struct rnd_time_test
... ...
@@ -159,7 +182,7 @@ struct rnd_time_test_lst
159 182
 };
160 183
 
161 184
 
162
-struct rnd_time_test_lst *rndt_lst;
185
+struct rnd_time_test_lst *rndt_lst = NULL;
163 186
 
164 187
 static unsigned long mem_unleak(unsigned long size);
165 188
 static void mem_destroy_all_tests();
... ...
@@ -175,22 +198,18 @@ static int mod_init(void)
175 198
 	}
176 199
 
177 200
 	if(misctest_memory!=0) {
178
-		alloc_lst = shm_malloc(sizeof(*alloc_lst));
179
-		if(alloc_lst == 0)
201
+		if(misctest_memory_init()<0) {
180 202
 			goto error;
181
-		alloc_lst->chunks = 0;
182
-		atomic_set_long(&alloc_lst->size, 0);
183
-		atomic_set_int(&alloc_lst->no, 0);
184
-		if(lock_init(&alloc_lst->lock) == 0)
185
-			goto error;
186
-		rndt_lst = shm_malloc(sizeof(*rndt_lst));
187
-		if(rndt_lst == 0)
188
-			goto error;
189
-		rndt_lst->tests = 0;
190
-		atomic_set_int(&rndt_lst->last_id, 0);
191
-		if(lock_init(&rndt_lst->lock) == 0)
203
+		}
204
+	}
205
+
206
+	if(misctest_message!=0) {
207
+		if(misctest_message_init()<0) {
192 208
 			goto error;
209
+		}
210
+		return -1;
193 211
 	}
212
+
194 213
 	return 0;
195 214
 error:
196 215
 	return -1;
... ...
@@ -215,6 +234,113 @@ static void mod_destroy()
215 234
 	}
216 235
 }
217 236
 
237
+static int misctest_memory_init(void)
238
+{
239
+	alloc_lst = shm_malloc(sizeof(*alloc_lst));
240
+	if(alloc_lst == 0)
241
+		goto error;
242
+	alloc_lst->chunks = 0;
243
+	atomic_set_long(&alloc_lst->size, 0);
244
+	atomic_set_int(&alloc_lst->no, 0);
245
+	if(lock_init(&alloc_lst->lock) == 0)
246
+		goto error;
247
+	rndt_lst = shm_malloc(sizeof(*rndt_lst));
248
+	if(rndt_lst == 0)
249
+		goto error;
250
+	rndt_lst->tests = 0;
251
+	atomic_set_int(&rndt_lst->last_id, 0);
252
+	if(lock_init(&rndt_lst->lock) == 0)
253
+		goto error;
254
+
255
+	return 0;
256
+error:
257
+	return -1;
258
+}
259
+
260
+static int misctest_message_init(void)
261
+{
262
+	char tbuf[BUF_SIZE];
263
+	FILE *f;
264
+	long fsize;
265
+    sip_msg_t tmsg = { };
266
+
267
+	if(misctest_message_data.s!=0 && misctest_message_data.len>0) {
268
+		if(misctest_message_data.len>=BUF_SIZE-2) {
269
+			LM_ERR("the data is too big\n");
270
+			return -1;
271
+		}
272
+		memcpy(tbuf, misctest_message_data.s, misctest_message_data.len);
273
+		tbuf[misctest_message_data.len] = '\0';
274
+		tmsg.len = misctest_message_data.len;
275
+	} else if(misctest_message_file.s!=0 && misctest_message_file.len>0) {
276
+		LM_DBG("reading data from file: %.*s\n", misctest_message_file.len,
277
+				misctest_message_file.s);
278
+		f = fopen(misctest_message_file.s, "r");
279
+		if(f==NULL) {
280
+			LM_ERR("cannot open file: %.*s\n", misctest_message_file.len,
281
+					misctest_message_file.s);
282
+			return -1;
283
+		}
284
+		fseek(f, 0, SEEK_END);
285
+		fsize = ftell(f);
286
+		if(fsize<0) {
287
+			LM_ERR("ftell failed on file: %.*s\n", misctest_message_file.len,
288
+					misctest_message_file.s);
289
+			fclose(f);
290
+			return -1;
291
+		}
292
+		fseek(f, 0, SEEK_SET);
293
+
294
+		if(fsize>=BUF_SIZE-2) {
295
+			LM_ERR("the file data is too big\n");
296
+			return -1;
297
+
298
+		}
299
+		if(fread(tbuf, fsize, 1, f) != fsize) {
300
+			if(ferror(f)) {
301
+				LM_ERR("error reading from file: %.*s\n",
302
+					misctest_message_file.len, misctest_message_file.s);
303
+			}
304
+		}
305
+		fclose(f);
306
+
307
+		tbuf[fsize] = 0;
308
+		tmsg.len = (int)fsize;
309
+	} else {
310
+		LM_ERR("no input data\n");
311
+		return -1;
312
+	}
313
+
314
+    tmsg.buf = tbuf;
315
+
316
+    LM_INFO("using data: [[%.*s]]\n", tmsg.len, tmsg.buf);
317
+
318
+    if (parse_msg(tmsg.buf, tmsg.len, &tmsg) < 0) {
319
+        goto cleanup;
320
+    }
321
+
322
+    parse_sdp(&tmsg);
323
+
324
+    parse_headers(&tmsg, HDR_TO_F, 0);
325
+
326
+    parse_contact_header(&tmsg);
327
+
328
+    parse_refer_to_header(&tmsg);
329
+
330
+    parse_to_header(&tmsg);
331
+
332
+    parse_pai_header(&tmsg);
333
+
334
+    parse_diversion_header(&tmsg);
335
+
336
+    parse_privacy(&tmsg);
337
+
338
+cleanup:
339
+    free_sip_msg(&tmsg);
340
+
341
+    return 0;
342
+
343
+}
218 344
 
219 345
 /** record a memory chunk list entry.
220 346
  * @param addr - address of the newly allocated memory
... ...
@@ -399,7 +525,7 @@ static unsigned long mem_unleak(unsigned long size)
399 525
 
400 526
 
401 527
 /** realloc randomly size bytes.
402
- * Chooses randomly a previously allocated chunk and realloc's it.
528
+ * Chooses randomly a previously allocated chunk and realloc')s it.
403 529
  * @param size - size.
404 530
  * @param diff - filled with difference, >= 0 means more bytes were alloc.,
405 531
  *               < 0 means bytes were freed.