File: cwtest.pl

package info (click to toggle)
cwdaemon 0.10.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,192 kB
  • sloc: sh: 3,872; ansic: 2,425; perl: 1,181; makefile: 66
file content (505 lines) | stat: -rwxr-xr-x 11,640 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/perl


# cwtest.pl - test script for cwdaemon
# Copyright (C) 2012 Jenö Vágó  HA5SE
# Copyright (C) 2012 - 2015 Kamil Ignacak
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


use warnings;
use strict;

use IO::Socket::INET;
use IO::Handle;
use Time::HiRes qw(usleep);
use Getopt::Long;

use cwdaemon::client;





# How many times to run a basic set of tests.
my $cycles = 50000;
my $cycle = 0;

# Number of test strings (number of request/reply pairs) to be used in
# each test.
my $n_test_strings = 20;

# Milliseconds multiplier.
# How long to sleep after every basic set of tests (the set is run in loop) [milliseconds].
my $in_loop_sleep_milliseconds = 1;

# Some test strings are randomly generated, and have random
# length. This is maximal random length.
my $string_len_max = 10;


my $result = GetOptions("cycles=i"       => \$cycles,
			"nstrings=i"     => \$n_test_strings,
			"stringlenmax=i" => \$string_len_max)

    or die "Problems with getting options: $@\n";





my $seed = time;
print "Using seed $seed.\n\n";
srand($seed);





# Number of tokens (tX) and errors (eX) in cwtest_send_X().
my $global_t1 = 0;
my $global_e1 = 0;
my $global_t2 = 0;
my $global_e2 = 0;
my $global_t3 = 0;
my $global_e3 = 0;
my $global_t4 = 0;
my $global_e4 = 0;
my $global_t5 = 0;
my $global_e5 = 0;





my $server_port = 6789;
my $cwsocket = IO::Socket::INET->new(PeerAddr => "localhost",
				     PeerPort => $server_port,
				     Proto    => "udp",
				     Type     => SOCK_DGRAM)
    or die "Couldn't setup udp server on port $server_port : $@\n";





sub INT_handler
{
    print "\n";
    cwtest_print_stats($cycle, $cycles);
    $cwsocket->close();

    exit(0);
}

$SIG{'INT'} = 'INT_handler';





for ($cycle = 0; $cycle < $cycles; $cycle++) {

    &cwdaemon_test0;
    &cwdaemon_test1;
    &cwdaemon_test2;
    &cwdaemon_test3;
    &cwdaemon_test4;
    &cwdaemon_test5;
    
    cwtest_print_stats($cycle, $cycles);
}


$cwsocket->close();








# ==========================================================================
#                                    subs
# ==========================================================================





sub cwdaemon_test0
{
    print("\n\n\n--- Test 0:\n");
    print("\n");

    print "PTT ON\n\n";
    print $cwsocket chr(27).'a1';
 
    cwtest_send_hang($cwsocket, "paris ");
}





sub cwdaemon_test1
{
    print("\n\n\n--- Test 1:\n");

    print("escaped request: \"<ESC>h<constant, non-empty reply text>\"\n");
    print("request: \"<single-character request text>\"\n");
    print("expected reply: \"h<non-empty reply text>\\r\\n\"\n");
    print("\n");

    print "PTT ON\n\n";
    print $cwsocket chr(27).'a1';


    # Single-character request texts
    my @request_texts = cwtest_get_random_strings($n_test_strings, 1);
    
    # Constant, non-empty reply texts
    my @reply_expected_texts = ("ack") x $n_test_strings;
    
    cwtest_request(1, $cwsocket, \@request_texts, \@reply_expected_texts);
}





sub cwdaemon_test2
{
    print("\n\n\n--- Test 2:\n");
    
    print("escaped request: \"<ESC>h<empty reply text>\"\n");
    print("request: \"<single-character request text>\"\n");
    print("expected reply: \"h\\r\\n\"\n");
    print("\n");

    print "PTT ON\n\n";
    print $cwsocket chr(27).'a1';

    # Single-character request texts
    my @request_texts = cwtest_get_random_strings($n_test_strings, 1);
    
    # Empty reply texts.
    my @reply_expected_texts = ("") x $n_test_strings;
    
    cwtest_request(1, $cwsocket, \@request_texts, \@reply_expected_texts);
}





sub cwdaemon_test3
{
    print("\n\n\n--- Test 3:\n");

    print("escaped request: \"<ESC>h<random non-empty reply text>\"\n");
    print("request: \"<random multi-character request text>\"\n");
    print("expected reply: \"h<reply text specified in escaped request>\\r\\n\"\n");
    print("\n");

    print "PTT ON\n\n";
    print $cwsocket chr(27).'a1';

    # Multi-character request texts
    my @request_texts = cwtest_get_random_strings($n_test_strings, $string_len_max);
    
    # Non-constant, non-empty reply texts.
    my @reply_expected_texts = cwtest_get_random_strings($n_test_strings, $string_len_max);
    
    cwtest_request(1, $cwsocket, \@request_texts, \@reply_expected_texts);
}





sub cwdaemon_test4
{
    print("\n\n\n--- Test 4:\n");
    
    print("request: \"<single-character request text>^\"\n");
    print("expected reply: \"<single-character reply==request text>\\r\\n\"\n");
    print("\n");

    print "PTT OFF\n\n";
    print $cwsocket chr(27).'a0';


    # Single-character request texts.
    my @request_texts = cwtest_get_random_strings($n_test_strings, 1);
    
    # Single-character reply texts equal to request texts.
    my @reply_expected_texts = @request_texts;
    
    cwtest_request(0, $cwsocket, \@request_texts, \@reply_expected_texts);
}





sub cwdaemon_test5
{
    print("\n\n\n--- Test 5:\n");
    
    print("request: \"<multi-character request text>^\"\n");
    print("expected reply: \"<multi-character reply==request text>\\r\\n\"\n");
    print("\n");

    print "PTT OFF\n\n";
    print $cwsocket chr(27).'a0';

    # Multi-character request texts
    my @request_texts = cwtest_get_random_strings($n_test_strings, $string_len_max);

    # Multi-character reply texts equal to request texts.
    my @reply_expected_texts = @request_texts;

    cwtest_request(0, $cwsocket, \@request_texts, \@reply_expected_texts);
}





# This function can perform two modes of communication with cwdaemon server.
#
# First mode of communication (Escape mode) first sends two requests:
# One to define reply from server, and one to define text to be played:
# "<ESC>h<empty or non-empty reply text>"
# "<single- or multi-character text to be played>"
#
# Then function waits for reply from server. In the first type of
# communication the reply has following form:
#
# "h<empty or non-empty reply text>\r\n"
#
#
#
# Second mode of communication sends only one request - a request that
# defines text to be played, which is followed by caret character
# ('^'). The caret character means that the text to be played is also
# a text to be sent in reply from server to client.
#
#
#
# Mode of communication is selected with first argument: $esc_mode,
# being 1 for Escape mode, and 0 for caret mode.
sub cwtest_request
{
    my $esc_mode = shift; 
    my $cwsocket = shift;
    my ($request_texts, $reply_expected_texts) = @_;

    my $n_requests = @$request_texts;
    my $n_replies = @$reply_expected_texts;

    if ($n_requests != $n_replies) {
	die "Lengths of args don't match";
    }


    # Constant expected reply text.
    #
    # cwdaemon allows 'expected_reply' to be empty string, this will
    # be tested elsewhere.

    for (my $i = 0; $i < $n_requests; $i++) {

	my $request_prefix = "";
	my $request_text = @$request_texts[$i];
	my $reply_expected_text = @$reply_expected_texts[$i];


	print("\n");
	print("sending request text           '$request_text'\n");
	print("    expecting reply text:      '$reply_expected_text'\n");


	if ($esc_mode == 1) {
	    $request_prefix = "h";
	    cwdaemon::client::send_request_esc_h($cwsocket, $request_text, $reply_expected_text);
	} else {
	    $request_prefix = "";
	    cwdaemon::client::send_request_caret($cwsocket, $request_text);
	}


	my ($reply_prefix, $reply_text, $reply_postfix) = cwdaemon::client::receive($cwsocket, $request_prefix);
	$global_t1++;



	if (!defined($reply_prefix) || !defined($reply_text) || !defined($reply_postfix)) {
	    $global_e1++;
	    print("undefined\n");

	} elsif ($reply_prefix ne $request_prefix || $reply_text ne $reply_expected_text || $reply_postfix ne "\r\n") {
	    $global_e1++;
	    print("unequal\n");

	} else {
	    print("    received full reply: ");
	    if (length($reply_prefix) != 0) {
		print("'$reply_prefix' + ");
	    } else {
		print("      ");
	    }
	    print("'$reply_text' + '\\r\\n'");
	}

	print "\n";

	my $r = rand($in_loop_sleep_milliseconds * 1000);
	usleep(int($r));
    }
}





# This function sends following request to the server:
# "<ESC>h<non-empty reply text>"
# "<single-character text to be played>"
#
# This function expects the following reply from the server:
# "h<non-empty reply text>\r\n"
sub cwtest_send_hang
{
    my $cwsocket = shift;
    my $txt = shift;

    for (my $i = 0; $i < length($txt); $i++) {
	# cwdaemon allows 'expected' to be empty string,
	# this will be tested in cwtest_send_2.
	my $expected = "reply";
	my $request_prefix = "h";

	my $text = substr($txt, $i, 1);

	print "sending  '" . $text . "'\n";

        # Use "<ESC>h" request to define reply expected from the server.
	print $cwsocket chr(27) . $request_prefix . $expected;
	# Send text to be played by server.
	print $cwsocket $text;

	my ($reply_prefix, $reply_text, $reply_postfix) = cwdaemon::client::receive($cwsocket, $request_prefix);

	$global_t1++;
	if ($reply_text ne $expected) {
	    $global_e1++;
	    die "die 1, incorrect reply: '$reply_text' != '$expected'\n";
	}

	print "\n"; # To clearly separate pairs of send/reply from each other.


	usleep(10000);
    }
}




# Get an array of random strings
# The strings can be used as request texts or expected reply texts.
sub cwtest_get_random_strings
{
    my $n_strings = shift;
    my $len_max = shift;

    my @chars = ("A".."Z", "a".."z", "0".."9");
    my @spaces = (" ") x 20;
    @chars = (@chars, @spaces);

    my @strings = ("") x $n_strings;
    for (my $i = 0; $i < $n_strings; $i++) {

	# http://www.perlmonks.org/?node_id=233023
	my $string;
	my $n = int(rand($len_max));
	$string .= $chars[rand @chars] for 0..$n;

	$strings[$i] = $string;
    }

    return @strings;
}





sub cwtest_print_stats
{
    my $cycle = shift;
    my $cycles = shift;

    printf("\n=================================================\n");
    printf("Finished cycle " . ($cycle + 1) . " / $cycles\n");

    printf("Stats 1: $global_e1 errors / $global_t1 tokens (");
    if ($global_t1) {
	printf("%.4f%%", 1.0 * $global_e1 / $global_t1);
    } else {
	printf("%.4f%%", 0.0);
    }
    printf(")\n");

    printf("Stats 2: $global_e2 errors / $global_t2 tokens (");
    if ($global_t2) {
	printf("%.4f%%", 1.0 * $global_e2 / $global_t2);
    } else {
	printf("%.4f%%", 0.0);
    }
    printf(")\n");

    printf("Stats 3: $global_e3 errors / $global_t3 tokens (");
    if ($global_t3) {
	printf("%.4f%%", 1.0 * $global_e3 / $global_t3);
    } else {
	printf("%.4f%%", 0.0);
    }
    printf(")\n");

    printf("Stats 4: $global_e4 errors / $global_t4 tokens (");
    if ($global_t4) {
	printf("%.4f%%", 1.0 * $global_e4 / $global_t4);
    } else {
	printf("%.4f%%", 0.0);
    }
    printf(")\n");

    printf("Stats 5: $global_e5 errors / $global_t5 tokens (");
    if ($global_t5) {
	printf("%.4f%%", 1.0 * $global_e5 / $global_t5);
    } else {
	printf("%.4f%%", 0.0);
    }
    printf(")\n");

    printf("=================================================\n\n");

}