Debian Codegraph @

tests/datamash-sha.pl

raw
1 #!/usr/bin/env perl
2 =pod
3 Unit Tests for GNU Datamash - tests sha1/256/5125 operations
4
5 Copyright (C) 2013-2021 Assaf Gordon <[email protected]>
6 Copyright (C) 2022-2025 Timothy Rice <[email protected]>
7
8 This file is part of GNU Datamash.
9
10 GNU Datamash is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 GNU Datamash is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU Datamash. If not, see <https://www.gnu.org/licenses/>.
22
23 Written by Assaf Gordon.
24 =cut
25 ## NOTE: Perl<5.10 don't have Digest::SHA core module -
26 ## So skip only those tests if needed.
27 ## Other line operations (e.g. md5/base64) are tested in the main
28 ## unit test module 'datamash-tests.pl'.
29 use strict;
30 use warnings;
31
32 # Until a better way comes along to auto-use Coreutils Perl modules
33 # as in the coreutils' autotools system.
34 use Coreutils;
35 use CuSkip;
36 use CuTmpdir qw(datamash);
37
38 ## Perl 5.8 and earlier do not have Digest::SHA as core module.
39 ## Skip the test if it is not found.
40 my $have_sha =
41 eval qq{use Digest::SHA qw(sha1_hex sha224_hex sha256_hex
42 sha384_hex sha512_hex);1;};
43
44 CuSkip::skip "requires Perl>5.8 with Digest::SHA module\nload error:\n$@"
45 unless $have_sha;
46
47 (my $program_name = $0) =~ s|.*/||;
48 my $prog_bin = 'datamash';
49
50 ## Cross-Compiling portability hack:
51 ## under qemu/binfmt, argv[0] (which is used to report errors) will contain
52 ## the full path of the binary, if the binary is on the $PATH.
53 ## So we try to detect what is the actual returned value of the program
54 ## in case of an error.
55 my $prog = `$prog_bin --foobar 2>&1 | head -n 1 | cut -f1 -d:`;
56 chomp $prog if $prog;
57 $prog = $prog_bin unless $prog;
58
59 # Turn off localization of executable's output.
60 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
61
62 my $in_g1=<<'EOF';
63 A 100
64 A 10
65 A 50
66 A 35
67 EOF
68
69 # Header line, with custom field separator
70 my $in_hdr2=<<'EOF';
71 x:y:z
72 A:3:W
73 A:5:W
74 A:7:W
75 A:11:X
76 A:13:X
77 B:17:Y
78 B:19:Z
79 C:23:Z
80 EOF
81
82 =pod
83 Example:
84 my $data = "a 1\nb 2\n";
85 my $out = transform_column($data, 2, \&md5_hex);
86 # out => md5_hex("1") . "\n" . md5_hex("2") . "\n" ;
87 =cut
88 sub transform_column($$$)
89 {
90 my $input_text = shift;
91 my $input_column = shift;
92 my $function = shift;
93
94 return join "",
95 map { "$_\n" }
96 map { &$function($_->[ $input_column - 1 ]) }
97 map { [ split / / ] }
98 split("\n", $input_text);
99 }
100
101 my $out_g1_sha1 = transform_column ($in_g1, 2, \&sha1_hex);
102 my $out_g1_sha224 = transform_column ($in_g1, 2, \&sha224_hex);
103 my $out_g1_sha256 = transform_column ($in_g1, 2, \&sha256_hex);
104 my $out_g1_sha384 = transform_column ($in_g1, 2, \&sha384_hex);
105 my $out_g1_sha512 = transform_column ($in_g1, 2, \&sha512_hex);
106
107 my @Tests =
108 (
109 ['sha1-1', '-W sha1 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha1}],
110 ['sha224-1','-W sha224 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha224}],
111 ['sha256-1','-W sha256 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha256}],
112 ['sha384-1','-W sha384 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha384}],
113 ['sha512-1','-W sha512 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_sha512}],
114 );
115
116 my $save_temps = $ENV{SAVE_TEMPS};
117 my $verbose = $ENV{VERBOSE};
118
119 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
120 exit $fail;