3 #=======================================================================
5 # File ID: 0d6b92c8-284a-11e5-8487-000df06acc56
7 # Test suite for findhex(1).
10 # ©opyleft 2015– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
19 use Test
::More
qw{no_plan
};
27 our $CMD_BASENAME = "findhex";
28 our $CMD = "../$CMD_BASENAME";
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0';
45 my %descriptions = ();
47 Getopt
::Long
::Configure
('bundling');
50 'all|a' => \
$Opt{'all'},
51 'help|h' => \
$Opt{'help'},
52 'quiet|q+' => \
$Opt{'quiet'},
53 'todo|t' => \
$Opt{'todo'},
54 'verbose|v+' => \
$Opt{'verbose'},
55 'version' => \
$Opt{'version'},
57 ) || die("$progname: Option error. Use -h for help.\n");
59 $Opt{'verbose'} -= $Opt{'quiet'};
60 $Opt{'help'} && usage
(0);
61 if ($Opt{'version'}) {
72 diag
(sprintf('========== Executing %s v%s ==========',
73 $progname, $VERSION));
75 if ($Opt{'todo'} && !$Opt{'all'}) {
81 testcmd("$CMD command", # {{{
94 diag
('Testing -h (--help) option...');
95 likecmd
("$CMD -h", # {{{
99 'Option -h prints help screen',
103 diag
('Testing -v (--verbose) option...');
104 likecmd
("$CMD -hv", # {{{
105 '/^\n\S+ \d+\.\d+\.\d+/s',
108 'Option -v with -h returns version number and help screen',
112 diag
('Testing --version option...');
113 likecmd
("$CMD --version", # {{{
114 '/^\S+ \d+\.\d+\.\d+/',
117 'Option --version returns version number',
121 my $stdtxt = 'asBAdcjkbw abdFF 2349.kjdc3211a abd 2349jk';
122 diag
('No options (except -vvv)...');
123 testcmd
("echo $stdtxt | $CMD -vvv", # {{{
134 "findhex: minlen = '1', maxlen = '0'\n",
136 'Find all hexadecimal numbers',
140 diag
('Testing -d/--decimal option...');
141 testcmd
("echo $stdtxt | $CMD -vvv -d", # {{{
147 "findhex: minlen = '1', maxlen = '0'\n",
149 'Find all decimal numbers',
153 diag
('Testing -i/--ignore-case option...');
154 testcmd
("echo $stdtxt | $CMD -i", # {{{
167 'Find all hex numbers, regardless of case',
171 diag
('Testing -l/--length option...');
172 testcmd
("echo $stdtxt | $CMD -l 4 -vvv", # {{{
177 "findhex: minlen = '4', maxlen = '4'\n",
179 'Find all hexadecimal with length of four chars',
183 testcmd
("echo $stdtxt | $CMD -l 4- -vvv", # {{{
189 "findhex: minlen = '4', maxlen = '0'\n",
191 'Find all hex four or more in length',
195 testcmd
("echo $stdtxt | $CMD -l -3 -vvv", # {{{
203 "findhex: minlen = '1', maxlen = '3'\n",
205 'Up to three chars in length',
209 testcmd
("echo $stdtxt | $CMD -l 3-4 -vvv", # {{{
216 "findhex: minlen = '3', maxlen = '4'\n",
218 'Three or four chars',
223 diag
('Test various predefined units...');
225 unit_test
('byte', 2);
226 unit_test
('crc16', 4);
227 unit_test
('crc32', 8);
228 unit_test
('git', 40);
229 unit_test
('gzip', 8);
231 unit_test
('md2', 32);
232 unit_test
('md4', 32);
233 unit_test
('md5', 32);
234 unit_test
('sha0', 40);
235 unit_test
('sha1', 40);
236 unit_test
('sha224', 56);
237 unit_test
('sha256', 64);
238 unit_test
('sha384', 96);
239 unit_test
('sha512', 128);
240 unit_test
('skein256', 64);
241 unit_test
('skein384', 96);
242 unit_test
('skein512', 128);
245 testcmd
("$CMD -l yaman", # {{{
247 "findhex: yaman: Unknown length unit\n",
253 diag
('Testing -u/--unique option...');
254 testcmd
("echo $stdtxt | $CMD -u", # {{{
265 'Don\'t print same value twice with -u',
269 testcmd
("echo $stdtxt badc AbD | $CMD -u -i", # {{{
281 'Unique with upper/lower case',
285 diag
('Various option combinations...');
286 testcmd
("echo $stdtxt | $CMD -vvv -d -l -3", # {{{
288 "findhex: minlen = '1', maxlen = '3'\n",
290 'No decimal-only with length 3 or less',
294 testcmd
("echo $stdtxt | $CMD --decimal --unique", # {{{
305 testcmd
("echo $stdtxt | $CMD -i -l -3 --verbose -v -v", # {{{
311 "findhex: minlen = '1', maxlen = '3'\n",
313 'Upper/lower case, max length 3',
317 testcmd
("echo $stdtxt | $CMD -i --length 4-5 --verbose -q --quiet -vvvv", # {{{
324 "findhex: minlen = '4', maxlen = '5'\n",
326 'Length 4-5, upper/lower case, -v and -q',
333 if ($Opt{'all'} || $Opt{'todo'}) {
334 diag
('Running TODO tests...'); # {{{
339 # Insert TODO tests here.
345 diag
('Testing finished.');
352 my ($name, $size) = @_;
353 return(testcmd
("$CMD -vvv -l $name /dev/null",
355 "findhex: minlen = '$size', maxlen = '$size'\n",
357 "Unit: $name, size: $size",
364 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
365 defined($descriptions{$Desc}) &&
366 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
367 $descriptions{$Desc} = 1;
369 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
370 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
371 my $TMP_STDERR = "$CMD_BASENAME-stderr.tmp";
374 if (defined($Exp_stderr)) {
375 $stderr_cmd = " 2>$TMP_STDERR";
377 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
379 if (defined($Exp_stderr)) {
380 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
383 diag
("Warning: stderr not defined for '$Txt'");
385 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
393 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
394 defined($descriptions{$Desc}) &&
395 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
396 $descriptions{$Desc} = 1;
398 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
399 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
400 my $TMP_STDERR = "$CMD_BASENAME-stderr.tmp";
403 if (defined($Exp_stderr)) {
404 $stderr_cmd = " 2>$TMP_STDERR";
406 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
408 if (defined($Exp_stderr)) {
409 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
412 diag
("Warning: stderr not defined for '$Txt'");
414 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
421 # Return file content as a string {{{
425 open(my $fp, '<', $File) or return undef;
434 # Print program version {{{
435 print("$progname $VERSION\n");
441 # Send the help message to stdout {{{
444 if ($Opt{'verbose'}) {
450 Usage: $progname [options]
452 Contains tests for the $CMD_BASENAME(1) program.
457 Run all tests, also TODOs.
461 Be more quiet. Can be repeated to increase silence.
463 Run only the TODO tests.
465 Increase level of verbosity. Can be repeated.
467 Print version information.
475 # Print a status message to stderr based on verbosity level {{{
476 my ($verbose_level, $Txt) = @_;
478 $verbose_level > $Opt{'verbose'} && return;
479 print(STDERR
"$progname: $Txt\n");
486 # This program is free software; you can redistribute it and/or modify
487 # it under the terms of the GNU General Public License as published by
488 # the Free Software Foundation; either version 2 of the License, or (at
489 # your option) any later version.
491 # This program is distributed in the hope that it will be useful, but
492 # WITHOUT ANY WARRANTY; without even the implied warranty of
493 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
494 # See the GNU General Public License for more details.
496 # You should have received a copy of the GNU General Public License
497 # along with this program.
498 # If not, see L<http://www.gnu.org/licenses/>.
500 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :