perlmodules.t: Remove Module::Starter and Module::Starter::Plugin::CGIApp
[sunny256-utils.git] / tests / tohex.t
blob8676eba72f5e01287dcbc576e4d7be09587c623c
1 #!/usr/bin/env perl
3 #=======================================================================
4 # tohex.t
5 # File ID: 93717826-f988-11dd-8870-000475e441b9
7 # Test suite for tohex(1).
9 # Character set: UTF-8
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 #=======================================================================
15 use strict;
16 use warnings;
18 BEGIN {
19 use Test::More qw{no_plan};
20 # use_ok() goes here
23 use Getopt::Long;
25 local $| = 1;
27 our $CMDB = "tohex";
28 our $CMD = "../$CMDB";
30 our %Opt = (
32 'all' => 0,
33 'help' => 0,
34 'quiet' => 0,
35 'todo' => 0,
36 'verbose' => 0,
37 'version' => 0,
41 our $progname = $0;
42 $progname =~ s/^.*\/(.*?)$/$1/;
43 our $VERSION = '0.0.0';
45 my %descriptions = ();
47 Getopt::Long::Configure('bundling');
48 GetOptions(
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'}) {
62 print_version();
63 exit(0);
66 exit(main());
68 sub main {
69 # {{{
70 my $Retval = 0;
72 diag(sprintf('========== Executing %s v%s ==========',
73 $progname, $VERSION));
75 if ($Opt{'todo'} && !$Opt{'all'}) {
76 goto todo_section;
79 =pod
81 testcmd("$CMD command", # {{{
82 <<'END',
83 [expected stdout]
84 END
85 '',
87 'description',
90 # }}}
92 =cut
94 diag('Testing -h (--help) option...');
95 likecmd("$CMD -h", # {{{
96 '/ Show this help/i',
97 '/^$/',
99 'Option -h prints help screen',
102 # }}}
103 diag('Testing -v (--verbose) option...');
104 likecmd("$CMD -hv", # {{{
105 '/^\n\S+ \d+\.\d+\.\d+/s',
106 '/^$/',
108 'Option -v with -h returns version number and help screen',
111 # }}}
112 diag('Testing --version option...');
113 likecmd("$CMD --version", # {{{
114 '/^\S+ \d+\.\d+\.\d+/',
115 '/^$/',
117 'Option --version returns version number',
120 # }}}
121 testcmd("$CMD file.bin", # {{{
123 "tohex: file.bin: Unknown command line argument\n",
125 'No command line arguments allowed',
128 # }}}
129 testcmd("echo This is it. | $CMD", # {{{
130 "54 68 69 73 20 69 73 20 69 74 2e 0a\n",
133 'ASCII to hex',
136 # }}}
137 testcmd("echo This is it. | $CMD -d", # {{{
138 "84 104 105 115 32 105 115 32 105 116 46 10\n",
141 'ASCII to decimal output (-d)',
144 # }}}
145 testcmd("echo This is a somewhat longer test. With ☮, ❤ and Linux. Oh, and €. | $CMD", # {{{
146 <<END,
147 54 68 69 73 20 69 73 20 61 20 73 6f 6d 65 77 68
148 61 74 20 6c 6f 6e 67 65 72 20 74 65 73 74 2e 20
149 57 69 74 68 20 e2 98 ae 2c 20 e2 9d a4 20 61 6e
150 64 20 4c 69 6e 75 78 2e 20 4f 68 2c 20 61 6e 64
151 20 e2 82 ac 2e 0a
155 "Hex output, wrap every 16th byte",
158 # }}}
159 testcmd("echo This is a somewhat longer test. With ☮, ❤ and Linux. Oh, and €. | $CMD -d", # {{{
160 <<END,
161 84 104 105 115 32 105 115 32 97 32 115 111 109 101 119 104
162 97 116 32 108 111 110 103 101 114 32 116 101 115 116 46 32
163 87 105 116 104 32 226 152 174 44 32 226 157 164 32 97 110
164 100 32 76 105 110 117 120 46 32 79 104 44 32 97 110 100
165 32 226 130 172 46 10
169 "Decimal output, with wrap every 16th byte",
172 # }}}
173 testcmd("echo -n This is 16 chars | $CMD", # {{{
174 "54 68 69 73 20 69 73 20 31 36 20 63 68 61 72 73\n",
177 'No extra LF at the end',
180 # }}}
181 testcmd("echo We have ☮, ❤ and Linux. Oh, and €. | $CMD -u", # {{{
182 <<END,
183 57 65 20 68 61 76 65 20 262e 2c 20 2764 20 61 6e 64
184 20 4c 69 6e 75 78 2e 20 4f 68 2c 20 61 6e 64 20
185 20ac 2e 0a
189 'Hex and Unicode mode (-u)',
192 # }}}
193 testcmd("echo We have ☮, ❤ and Linux. Oh, and €. | $CMD -d --unicode", # {{{
194 <<END,
195 87 101 32 104 97 118 101 32 9774 44 32 10084 32 97 110 100
196 32 76 105 110 117 120 46 32 79 104 44 32 97 110 100 32
197 8364 46 10
201 'Decimal and Unicode mode (--unicode)',
204 # }}}
205 testcmd("$CMD -u <tohex-files/utf8.txt", # {{{
206 <<END,
207 2554 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550
208 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2557 0a 2551 20 20 4a
209 61 77 6f 68 6c 2c 20 6d 65 69 6e 20 4d 61 64 6f
210 6e 6e 61 20 20 20 2551 0a 2551 20 20 20 20 20 20 20
211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
212 20 20 2551 0a 2551 20 20 4c 65 69 64 69 20 4d 61 64
213 e5 6e 6e 61 20 20 20 20 20 20 20 20 20 20 2551 0a
214 2551 20 20 6b 6a 69 6c 64 72 65 6e 20 e6 74 74 20
215 6a e5 72 20 66 69 69 74 20 20 2551 0a 2551 20 20 77
216 e5 6e 64 65 72 20 68 e5 75 20 6a 75 20 6d e6 6e
217 65 64 73 6a 20 20 2551 0a 2551 20 20 74 6f 20 6d 65
218 69 6b 20 65 6e 64 73 20 6d 69 69 74 20 2764 20 20
219 20 20 2551 0a 255a 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550
220 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 2550 255d 0a
224 'Open file, read UTF-8 and output hex (-u)',
227 # }}}
228 testcmd("$CMD --unicode --decimal <tohex-files/utf8.txt", # {{{
229 <<END,
230 9556 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552
231 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9559 10 9553 32 32 74
232 97 119 111 104 108 44 32 109 101 105 110 32 77 97 100 111
233 110 110 97 32 32 32 9553 10 9553 32 32 32 32 32 32 32
234 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
235 32 32 9553 10 9553 32 32 76 101 105 100 105 32 77 97 100
236 229 110 110 97 32 32 32 32 32 32 32 32 32 32 9553 10
237 9553 32 32 107 106 105 108 100 114 101 110 32 230 116 116 32
238 106 229 114 32 102 105 105 116 32 32 9553 10 9553 32 32 119
239 229 110 100 101 114 32 104 229 117 32 106 117 32 109 230 110
240 101 100 115 106 32 32 9553 10 9553 32 32 116 111 32 109 101
241 105 107 32 101 110 100 115 32 109 105 105 116 32 10084 32 32
242 32 32 9553 10 9562 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552
243 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9552 9565 10
247 'Read from file, use --decimal and --unicode',
250 # }}}
251 testcmd("$CMD <tohex-files/random256.bin", # {{{
252 <<END,
253 4a a7 39 a3 8b 3e 9b 1f 6c 6b b1 12 fb 17 c4 ba
254 ad 68 9a ee de ce 4f ac df af fd b3 eb ff a5 b0
255 dc c1 31 48 b6 9c ec ea 25 fa 7f 43 83 a0 84 70
256 3a 97 26 13 8c 0c 81 c2 d4 f5 93 5e 22 e1 8f 03
257 f1 1b 55 cf 89 f6 4d e9 7c cd 7b f7 c8 51 56 4e
258 6e f8 a8 80 77 a1 0a dd b8 bf 65 3d 2d cb 2f be
259 62 fe db bc d9 d2 99 e3 2a 37 33 53 6a 5b 0e 4b
260 0f 82 79 6f d0 aa 7e f0 2b 61 5d 40 66 b5 34 d3
261 45 24 88 c5 e7 72 a2 f2 8e ae 42 bd 1a f4 10 b9
262 49 7a 91 41 67 ef a9 09 15 a6 da 6d ed 2c e5 d6
263 18 e4 fc 98 0d ab d7 0b 1e 90 c3 c0 5f 86 16 c6
264 2e a4 1d 14 71 e0 3c 87 96 e6 f9 b7 32 94 20 ca
265 3b 4c 8d 63 19 bb cc 02 11 5a e2 60 d1 73 e8 00
266 3f 52 b2 d8 92 07 74 8a 01 04 c7 85 d5 05 78 36
267 9f c9 5c b4 21 f3 06 46 95 30 9e 27 28 58 35 75
268 76 7d 47 44 50 1c 57 64 29 9d 38 69 54 23 08 59
272 'Read from binary file, output hex',
275 # }}}
276 testcmd("$CMD -d <tohex-files/random256.bin", # {{{
277 <<END,
278 74 167 57 163 139 62 155 31 108 107 177 18 251 23 196 186
279 173 104 154 238 222 206 79 172 223 175 253 179 235 255 165 176
280 220 193 49 72 182 156 236 234 37 250 127 67 131 160 132 112
281 58 151 38 19 140 12 129 194 212 245 147 94 34 225 143 3
282 241 27 85 207 137 246 77 233 124 205 123 247 200 81 86 78
283 110 248 168 128 119 161 10 221 184 191 101 61 45 203 47 190
284 98 254 219 188 217 210 153 227 42 55 51 83 106 91 14 75
285 15 130 121 111 208 170 126 240 43 97 93 64 102 181 52 211
286 69 36 136 197 231 114 162 242 142 174 66 189 26 244 16 185
287 73 122 145 65 103 239 169 9 21 166 218 109 237 44 229 214
288 24 228 252 152 13 171 215 11 30 144 195 192 95 134 22 198
289 46 164 29 20 113 224 60 135 150 230 249 183 50 148 32 202
290 59 76 141 99 25 187 204 2 17 90 226 96 209 115 232 0
291 63 82 178 216 146 7 116 138 1 4 199 133 213 5 120 54
292 159 201 92 180 33 243 6 70 149 48 158 39 40 88 53 117
293 118 125 71 68 80 28 87 100 41 157 56 105 84 35 8 89
297 'Read from binary file, output decimal',
300 # }}}
302 todo_section:
305 if ($Opt{'all'} || $Opt{'todo'}) {
306 diag('Running TODO tests...'); # {{{
308 TODO: {
310 local $TODO = '';
311 # Insert TODO tests here.
314 # TODO tests }}}
317 diag('Testing finished.');
318 return $Retval;
319 # }}}
320 } # main()
322 sub testcmd {
323 # {{{
324 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
325 defined($descriptions{$Desc}) &&
326 BAIL_OUT("testcmd(): '$Desc' description is used twice");
327 $descriptions{$Desc} = 1;
328 my $stderr_cmd = '';
329 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
330 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
331 my $TMP_STDERR = "$CMDB-stderr.tmp";
332 my $retval = 1;
334 if (defined($Exp_stderr)) {
335 $stderr_cmd = " 2>$TMP_STDERR";
337 $retval &= is(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
338 my $ret_val = $?;
339 if (defined($Exp_stderr)) {
340 $retval &= is(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
341 unlink($TMP_STDERR);
342 } else {
343 diag("Warning: stderr not defined for '$Txt'");
345 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
347 return $retval;
348 # }}}
349 } # testcmd()
351 sub likecmd {
352 # {{{
353 my ($Cmd, $Exp_stdout, $Exp_stderr, $Exp_retval, $Desc) = @_;
354 defined($descriptions{$Desc}) &&
355 BAIL_OUT("likecmd(): '$Desc' description is used twice");
356 $descriptions{$Desc} = 1;
357 my $stderr_cmd = '';
358 my $cmd_outp_str = $Opt{'verbose'} >= 1 ? "\"$Cmd\" - " : '';
359 my $Txt = join('', $cmd_outp_str, defined($Desc) ? $Desc : '');
360 my $TMP_STDERR = "$CMDB-stderr.tmp";
361 my $retval = 1;
363 if (defined($Exp_stderr)) {
364 $stderr_cmd = " 2>$TMP_STDERR";
366 $retval &= like(`$Cmd$stderr_cmd`, $Exp_stdout, "$Txt (stdout)");
367 my $ret_val = $?;
368 if (defined($Exp_stderr)) {
369 $retval &= like(file_data($TMP_STDERR), $Exp_stderr, "$Txt (stderr)");
370 unlink($TMP_STDERR);
371 } else {
372 diag("Warning: stderr not defined for '$Txt'");
374 $retval &= is($ret_val >> 8, $Exp_retval, "$Txt (retval)");
376 return $retval;
377 # }}}
378 } # likecmd()
380 sub file_data {
381 # Return file content as a string {{{
382 my $File = shift;
383 my $Txt;
385 open(my $fp, '<', $File) or return undef;
386 local $/ = undef;
387 $Txt = <$fp>;
388 close($fp);
389 return $Txt;
390 # }}}
391 } # file_data()
393 sub print_version {
394 # Print program version {{{
395 print("$progname $VERSION\n");
396 return;
397 # }}}
398 } # print_version()
400 sub usage {
401 # Send the help message to stdout {{{
402 my $Retval = shift;
404 if ($Opt{'verbose'}) {
405 print("\n");
406 print_version();
408 print(<<"END");
410 Usage: $progname [options]
412 Contains tests for the $CMDB(1) program.
414 Options:
416 -a, --all
417 Run all tests, also TODOs.
418 -h, --help
419 Show this help.
420 -q, --quiet
421 Be more quiet. Can be repeated to increase silence.
422 -t, --todo
423 Run only the TODO tests.
424 -v, --verbose
425 Increase level of verbosity. Can be repeated.
426 --version
427 Print version information.
430 exit($Retval);
431 # }}}
432 } # usage()
434 sub msg {
435 # Print a status message to stderr based on verbosity level {{{
436 my ($verbose_level, $Txt) = @_;
438 $verbose_level > $Opt{'verbose'} && return;
439 print(STDERR "$progname: $Txt\n");
440 return;
441 # }}}
442 } # msg()
444 __END__
446 # This program is free software; you can redistribute it and/or modify
447 # it under the terms of the GNU General Public License as published by
448 # the Free Software Foundation; either version 2 of the License, or (at
449 # your option) any later version.
451 # This program is distributed in the hope that it will be useful, but
452 # WITHOUT ANY WARRANTY; without even the implied warranty of
453 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
454 # See the GNU General Public License for more details.
456 # You should have received a copy of the GNU General Public License
457 # along with this program.
458 # If not, see L<http://www.gnu.org/licenses/>.
460 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :