File: test-rollmgr

package info (click to toggle)
dnssec-tools 1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 16,064 kB
  • sloc: perl: 44,399; ansic: 31,547; cpp: 21,306; sh: 15,813; xml: 2,113; makefile: 1,390; pascal: 836; python: 290; csh: 11
file content (441 lines) | stat: -rwxr-xr-x 9,041 bytes parent folder | download
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
#!/usr/bin/perl
#
# Copyright 2005-2012 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details
#
#
# This script performs several tests of the DNSSEC-Tools rollmgr module.
#
# The following interfaces are tested:
#
#	 rollmgr_dir
#	 rollmgr_pidfile
#	 rollmgr_getpid
#	 rollmgr_droppid
#	 rollmgr_rmpid
#	 rollmgr_qproc
#	 rollmgr_halt
#

use strict;

use Net::DNS::SEC::Tools::rollmgr;

# test_rollmgr_dir();

# test_rollmgr_pidfile();

# test_rollmgr_getpid();

# test_rollmgr_droppid();

# test_rollmgr_rmpid();

# test_rollmgr_qproc();

test_rollmgr_halt();

exit 0;

#############################################################
#
sub test_rollmgr_dir
{
	my $dirname = rollmgr_dir();

	print "\n-----------------------------------------------------\n\n";
	print "testing rollmgr_dir()\t\t(requires manual verification)\n\n";

	print "\trollmgr directory - $dirname\n";
}

#############################################################
#
sub test_rollmgr_pidfile
{
	my $pidfile = rollmgr_pidfile();

	print "\n-----------------------------------------------------\n\n";
	print "testing rollmgr_pidfile()\t\t(requires manual verification)\n\n";

	print "\trollmgr pidfile - $pidfile\n";
}

#############################################################
#
sub test_rollmgr_getpid
{
	my $errs = 0;			# Failed tests.
	my $newpid;			# Manufactured process id.
	my $pid;			# Process id.
	my $pidfile;			# Name of roll-over manager's pidfile.

	print "\n-----------------------------------------------------\n\n";
	print "testing rollmgr_getpid()\n";

	#####################
	#
	# Make sure we have a clean slate.
	#
	$pidfile = rollmgr_pidfile();
	system("/bin/rm -f $pidfile");

	#####################
	#
	# Check that rollmgr_getpid() actually does need a pidfile.
	#
	print "\ttesting for non-existent pidfile\n";
	$pid = rollmgr_getpid();
	if($pid == -1)
	{
		print "\t\tSUCCESS:  couldn't read from a non-existent pidfile\n\n";
	}
	else
	{
		print "\t\tFAILURE:  returned pid $pid when pidfile doesn't exist\n\n";
		$errs++;
	}

	#####################
	#
	# Check that rollmgr_getpid() will succeed if the pidfile exists.
	#
	print "\ttesting for valid pid in pidfile\n";

	$newpid = 88 . $$ . 88;
	system(`/bin/echo $newpid >> $pidfile`);
	$pid = rollmgr_getpid();
	if($pid == $newpid)
	{
		print "\t\tSUCCESS:  pidfile contains correct pid\n\n";
	}
	else
	{
		print "\t\tFAILURE:  pidfile does not contain correct pid\n";
		print "\t\t          contains <$pid>, should have <$newpid>\n\n";
		$errs++;
	}

	#####################
	#
	# Check to see how many tests failed.
	#
	if($errs)
	{
		print "\t$errs test(s) of rollmgr_getpid() failed\n";
	}
	else
	{
		print "\tall rollmgr_getpid() tests succeeded\n";
	}

}

#############################################################
#
sub test_rollmgr_droppid
{
	my $errs = 0;			# Failed tests.
	my $pid;			# Process id.
	my $pidfile;			# Name of roll-over manager's pidfile.

	print "\n-----------------------------------------------------\n\n";
	print "testing rollmgr_droppid()\n";

	#####################
	#
	# Make sure we have a clean slate.
	#
	$pidfile = rollmgr_pidfile();
	system("/bin/rm -f $pidfile");

	#####################
	#
	# Check that rollmgr_droppid() will create the proper pidfile if
	# one doesn't exist.
	#
	print "\ttesting for non-existent file\n";
	rollmgr_droppid();
	$pid = `/bin/cat $pidfile`;
	if($pid == $$)
	{
		print "\t\tSUCCESS:  successfully created a new pidfile\n\n";
	}
	else
	{
		print "\t\tFAILURE:  did not properly create a new pidfile\n\n";
		$errs++;
	}

	#####################
	#
	# Check that rollmgr_droppid() will appear to succeed if the
	# pidfile contains the process' pid.
	#
	# This test assumes the previous test succeeded.
	#
	print "\ttesting for matching pid in pidfile\n";

	rollmgr_droppid();
	$pid = `/bin/cat $pidfile`;
	if($pid == $$)
	{
		print "\t\tSUCCESS:  pidfile contains correct pid\n\n";
	}
	else
	{
		print "\t\tFAILURE:  pidfile does not contain correct pid\n\n";
		$errs++;
	}

	#####################
	#
	# Check that rollmgr_droppid() won't over-write a pidfile created
	# by another process.
	#
	print "\ttesting for existent file\n";
	$pid = $$ - 10;
	system(`/bin/echo $pid >> $pidfile`);

	rollmgr_droppid();
	$pid = `/bin/cat $pidfile`;
	if($pid != $$)
	{
		print "\t\tSUCCESS:  did not over-write another process' pidfile\n\n";
	}
	else
	{
		print "\t\tFAILURE:  overwrote another process' pidfile\n\n";
		$errs++;
	}

	#####################
	#
	# Check to see how many tests failed.
	#
	if($errs)
	{
		print "\t$errs test(s) of rollmgr_droppid() failed\n";
	}
	else
	{
		print "\tall rollmgr_droppid() tests succeeded\n";
	}

}

#############################################################
#
sub test_rollmgr_rmpid
{
	my $errs = 0;			# Failed tests.
	my $pid;			# Process id.
	my $pidfile;			# Name of roll-over manager's pidfile.
	my $ret;			# rollmgr-rmpid() return code.

	print "\n-----------------------------------------------------\n\n";
	print "testing rollmgr_rmpid()\n";

	#####################
	#
	# Make sure we have a clean slate.
	#
	$pidfile = rollmgr_pidfile();
	system("/bin/rm -f $pidfile");

	#####################
	#
	# Check that rollmgr_rmpid() will give the proper errcode if the
	# pidfile if doesn't exist.
	#
	print "\ttesting for non-existent file\n";
	if(($ret=rollmgr_rmpid()) == 0)
	{
		print "\t\tSUCCESS:  couldn't delete non-existent pidfile\n\n";
	}
	else
	{
		if($ret == 1)
		{
			print "\t\tFAILURE:  improperly deleted pidfile\n\n";
		}
		else
		{
			print "\t\tFAILURE:  failed for reason $ret\n\n";
		}
		$errs++;
	}

	#####################
	#
	# Check that rollmgr_rmpid() will delete our own pidfile.
	#
	print "\ttesting for existent file with our pid\n";
	system(`/bin/echo $$ >> $pidfile`);

	if(($ret=rollmgr_rmpid()) == 1)
	{
		print "\t\tSUCCESS:  deleted our own pidfile\n\n";
	}
	else
	{
		print "\t\tFAILURE:  failed for reason $ret\n\n";
		$errs++;
	}

	#####################
	#
	# Check that rollmgr_rmpid() won't delete a pidfile created
	# by another process.
	#
	print "\ttesting for existent file with our pid\n";
	$pid = $$ - 10;
	system(`/bin/echo $pid >> $pidfile`);

	if(($ret=rollmgr_rmpid()) == -1)
	{
		print "\t\tSUCCESS:  unable to delete someone else's pidfile\n\n";
	}
	else
	{
		if($ret == 1)
		{
			print "\t\tFAILURE:  deleted someone else's pidfile\n\n";
		}
		else
		{
			print "\t\tFAILURE:  failed for reason $ret\n\n";
		}
		$errs++;
	}

	#####################
	#
	# Check to see how many tests failed.
	#
	if($errs)
	{
		print "\t$errs test(s) of rollmgr_rmpid() failed\n";
	}
	else
	{
		print "\tall rollmgr_rmpid() tests succeeded\n";
	}

}

#############################################################


sub test_rollmgr_qproc
{
	my $caught = 0;				# Signal-caught flag.
	my $child;				# Child's pid.
	my $parent = $$;			# Parent's pid.
	my $pidfile;				# Roll-over manager's pidfile.
	my $naptime = 30;			# Time to sleep().
	my %procs;				# Process names.

	print "\n-----------------------------------------------------\n\n";
	print "testing rollmgr_qproc()\n";
	print "\t(may pause for up to $naptime seconds)\n\n";

	$pidfile = rollmgr_pidfile();
	system(`/bin/echo $parent > $pidfile`);

	$child = fork();

	#
	# The child process will let the parent fake-up a roll-over manager,
	# and then tell it to process its queue.
	#
	if($child == 0)
	{
		sleep(2);
		rollmgr_qproc();

		exit(0);
	}

	#
	# Fake-up a roll-over manager for this test.
	#
	$SIG{HUP} = sub
	{
		print "\tSUCCESS:  fake roll-over manager received the \"process queue\" command \n";
		$caught = 1;
	};

	#
	# Wait a bit...
	#
	sleep($naptime);

	#
	# If we didn't receive the "process queue" command, then give an
	# error message.
	#
	if(!$caught)
	{
		print "\tFAILURE:  \"process queue\" command not sent to the fake roll-over manager\n";
	}
}

#############################################################

sub test_rollmgr_halt
{
	my $caught = 0;				# Signal-caught flag.
	my $child;				# Child's pid.
	my $parent = $$;			# Parent's pid.
	my $pidfile;				# Roll-over manager's pidfile.
	my $naptime = 30;			# Time to sleep().
	my %procs;				# Process names.

	print "\n-----------------------------------------------------\n\n";
	print "testing rollmgr_halt()\n";
	print "\t(may pause for up to $naptime seconds)\n\n";

	$pidfile = rollmgr_pidfile();
	system(`/bin/echo $parent > $pidfile`);

	$child = fork();

	#
	# The child process will let the parent fake-up a roll-over manager,
	# and then tell it to process its queue.
	#
	if($child == 0)
	{
		sleep(2);
		rollmgr_halt();

		exit(0);
	}

	#
	# Fake-up a roll-over manager for this test.
	#
	$SIG{INT} = sub
	{
		$caught = 1;
	};

	#
	# Wait a bit...
	#
	sleep($naptime);

	#
	# If we didn't receive the "halt" command, then give an
	# error message.
	#
	if($caught)
	{
		print "\tSUCCESS:  fake roll-over manager received the \"halt\" command \n";
	}
	else
	{
		print "\tFAILURE:  \"halt\" command not sent to the fake roll-over manager\n";
	}
}