3 #=======================================================================
5 # File ID: 05b995a4-f988-11dd-aac9-000475e441b9
7 # Test suite for fromhex(1).
10 # ©opyleft 2008– Ø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 $CMDB = "fromhex";
28 our $CMD = "../$CMDB";
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 testcmd
("echo 76 72 c3 b8 76 6c 65 62 c3 b8 74 74 65 | $CMD", # {{{
125 'Read standard two-digit lowercase hex',
129 testcmd
("echo 466ac3b873756c66206865722c2076656c6b6f6d6d656e2e | $CMD", # {{{
130 'Fjøsulf her, velkommen.',
133 'No spaces between hex',
137 testcmd
("echo '4j4r6%%574.20 677w26_1756c6+ 573206§9206cg69:612/1.' | $CMD", # {{{
138 'Det graules i lia!',
141 'Ignore non-hex digits',
145 testcmd
("echo 4B4A48426a6b62686a68626a6b6861732F262F36353435252629282f0A | $CMD", # {{{
146 "KJHBjkbhjhbjkhas/&/6545%&)(/\n",
149 'Include upper case',
153 testcmd
("$CMD -d fromhex-files/decimal.txt", # {{{
155 __________________________
156 < Have to test decimal too >
157 --------------------------
166 'Read decimal numbers with -d, ignore other chars',
170 testcmd
("echo 53 74 65 69 6b 6a 65 20 66 69 6e 65 20 67 61 72 64 69 6e 65 2 | $CMD", # {{{
171 'Steikje fine gardine',
174 'Don\'t output the byte that\'s missing a nibble',
178 testcmd
("$CMD fromhex-files/single.txt", # {{{
187 'Read single digits on their own line',
191 testcmd
("$CMD fromhex-files/standardpudding.bin", # {{{
193 Standardpudding til Folket. Det er vel ikke for mye forlangt. Eller skal
194 vi sitte her og råtne i luksusen?
197 Øyvind _~ + ) + ' |/ _ \
198 ,_______________| |______ , , . (~) + +
199 /________________________/\ . * + U *
201 +-------------------------------------------------------------_ (o_.'
202 | All you touch and all you see is all your life will ever be. -_ //\
203 | ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ -_ V_/_
204 +-------------------------------------------------------------------------
208 'Read digits from file with lots of binary noise',
212 diag
('Test -u/--unicode option...');
213 testcmd
("echo 59 65 70_2E 20.2192/263a 2190 20 1f6BD | $CMD --unicode", # {{{
217 'Read hex values in Unicode mode (--unicode)',
221 testcmd
("echo 89 101 112_46 32%8594 9786 8592+32 128701 | $CMD -du", # {{{
225 'Read decimal values in Unicode mode (-u)',
229 testcmd
("echo 89 101 112-46 32%8594?9786 8592b32 128701 | $CMD -d", # {{{
231 "fromhex: Cannot print byte value 8594 in bytewise mode, use -u\n",
233 'Read high decimal values when not in Unicode mode',
237 diag
('Suppress Perl warnings about invalid UTF-8...');
238 testcmd
("echo ffff | $CMD -u", # {{{
242 "Perl doesn't complain about U+FFFF",
246 testcmd
("echo d800 | $CMD -u", # {{{
250 "Perl doesn't complain about U+D800",
254 testcmd
("echo 65535 | $CMD -ud", # {{{
258 "Perl doesn't complain about U+FFFF (decimal)",
262 testcmd
("echo 55296 | $CMD -ud", # {{{
266 "Perl doesn't complain about U+D800 (decimal)",
270 diag
('Enable Perl UTF-8 warnings with -w/--warnings');
271 likecmd
("echo ffff | $CMD -uw", # {{{
275 "Perl complains about U+FFFF",
279 likecmd
("echo d800 | $CMD -u --warning", # {{{
281 '/surrogate .*D800/i',
283 "Perl complains about U+D800",
287 likecmd
("echo 65535 | $CMD -u --decimal -w", # {{{
291 "Perl complains about U+FFFF (decimal)",
295 likecmd
("echo 55296 | $CMD -udw", # {{{
297 '/surrogate .*D800/i',
299 "Perl complains about U+D800 (decimal)",
307 if ($Opt{'all'} || $Opt{'todo'}) {
308 diag
('Running TODO tests...'); # {{{
313 # Insert TODO tests here.
319 diag
('Testing finished.');
326 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
327 defined($descriptions{$Desc}) &&
328 BAIL_OUT
("testcmd(): '$Desc' description is used twice");
329 $descriptions{$Desc} = 1;
331 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
332 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
333 my $TMP_STDERR = "$CMDB-stderr.tmp";
336 if (defined($Exp_stderr)) {
337 $stderr_cmd = " 2>$TMP_STDERR";
339 $retval &= is
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
341 if (defined($Exp_stderr)) {
342 $retval &= is
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
345 diag
("Warning: stderr not defined for '$Txt'");
347 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
355 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
356 defined($descriptions{$Desc}) &&
357 BAIL_OUT
("likecmd(): '$Desc' description is used twice");
358 $descriptions{$Desc} = 1;
360 my $cmd_outp_str = $Opt{'verbose'} >= 1 ?
"\"$Cmd\" - " : '';
361 my $Txt = join('', $cmd_outp_str, defined($Desc) ?
$Desc : '');
362 my $TMP_STDERR = "$CMDB-stderr.tmp";
365 if (defined($Exp_stderr)) {
366 $stderr_cmd = " 2>$TMP_STDERR";
368 $retval &= like
(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
370 if (defined($Exp_stderr)) {
371 $retval &= like
(file_data
($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
374 diag
("Warning: stderr not defined for '$Txt'");
376 $retval &= is
($ret_val >> 8, $Exp_retval, "$Txt (retval)");
383 # Return file content as a string {{{
387 open(my $fp, '<', $File) or return undef;
396 # Print program version {{{
397 print("$progname $VERSION\n");
403 # Send the help message to stdout {{{
406 if ($Opt{'verbose'}) {
412 Usage: $progname [options]
414 Contains tests for the $CMDB(1) program.
419 Run all tests, also TODOs.
423 Be more quiet. Can be repeated to increase silence.
425 Run only the TODO tests.
427 Increase level of verbosity. Can be repeated.
429 Print version information.
437 # Print a status message to stderr based on verbosity level {{{
438 my ($verbose_level, $Txt) = @_;
440 $verbose_level > $Opt{'verbose'} && return;
441 print(STDERR
"$progname: $Txt\n");
448 # This program is free software; you can redistribute it and/or modify
449 # it under the terms of the GNU General Public License as published by
450 # the Free Software Foundation; either version 2 of the License, or (at
451 # your option) any later version.
453 # This program is distributed in the hope that it will be useful, but
454 # WITHOUT ANY WARRANTY; without even the implied warranty of
455 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
456 # See the GNU General Public License for more details.
458 # You should have received a copy of the GNU General Public License
459 # along with this program.
460 # If not, see L<http://www.gnu.org/licenses/>.
462 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :