1#!/usr/bin/perl 2# Exercise basenc. 3 4# Copyright (C) 2006-2023 Free Software Foundation, Inc. 5 6# This program is free software: you can redistribute it and/or modify 7# it under the terms of the GNU General Public License as published by 8# the Free Software Foundation, either version 3 of the License, or 9# (at your option) any later version. 10 11# This program is distributed in the hope that it will be useful, 12# but WITHOUT ANY WARRANTY; without even the implied warranty of 13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14# GNU General Public License for more details. 15 16# You should have received a copy of the GNU General Public License 17# along with this program. If not, see <https://www.gnu.org/licenses/>. 18 19# This test exercises the various encoding (other than base64/32). 20# It also does not test the general options (e.g. --wrap), as that code is 21# shared and tested in base64. 22 23use strict; 24 25(my $program_name = $0) =~ s|.*/||; 26my $prog = 'basenc'; 27 28# Turn off localization of executable's output. 29@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; 30 31 32my $base64_in = "\x54\x0f\xdc\xf0\x0f\xaf\x4a"; 33my $base64_out = "VA/c8A+vSg=="; 34my $base64url_out = $base64_out; 35$base64url_out =~ y|+/|-_|; 36my $base64url_out_nl = $base64url_out; 37$base64url_out_nl =~ s/(..)/\1\n/g; # add newline every two characters 38 39 40# Bug 49741: 41# The input is 'abc' in base64, in an 8K buffer (larger than 1024*5, 42# the buffer size which caused the bug). 43my $base64_bug49741_in = "YWJj" x 2000 ; 44my $base64_bug49741_out = "abc" x 2000 ; 45 46 47my $base32_in = "\xfd\xd8\x07\xd1\xa5"; 48my $base32_out = "7XMAPUNF"; 49my $x = $base32_out; 50$x =~ y|ABCDEFGHIJKLMNOPQRSTUVWXYZ234567|0123456789ABCDEFGHIJKLMNOPQRSTUV|; 51my $base32hex_out = $x; 52 53# base32 with padding and newline 54my $base32_in2 = "\xFF\x00"; 55my $base32_out2 = "74AA===="; 56$x = $base32_out2; 57$x =~ y|ABCDEFGHIJKLMNOPQRSTUVWXYZ234567|0123456789ABCDEFGHIJKLMNOPQRSTUV|; 58my $base32hex_out2 = $x; 59my $base32hex_out2_nl = $x; 60$base32hex_out2_nl =~ s/(...)/\1\n/g; # Add newline every 3 characters 61 62my $base16_in = "\xfd\xd8\x07\xd1\xa5"; 63my $base16_out = "FDD807D1A5"; 64 65my $z85_in = "\x86\x4F\xD2\x6F\xB5\x59\xF7\x5B"; 66my $z85_out = 'HelloWorld'; 67 68my $base2lsbf_ab = "1000011001000110"; 69my $base2lsbf_ab_nl = $base2lsbf_ab; 70$base2lsbf_ab_nl =~ s/(...)/\1\n/g; # Add newline every 3 characters 71my $base2msbf_ab = "0110000101100010"; 72my $base2msbf_ab_nl = $base2msbf_ab; 73$base2msbf_ab_nl =~ s/(...)/\1\n/g; # Add newline every 3 characters 74 75my $try_help = "Try '$prog --help' for more information.\n"; 76 77my @Tests = 78( 79 # These are mainly for higher coverage 80 ['help', '--help', {IN=>''}, {OUT=>""}, {OUT_SUBST=>'s/.*//sm'}], 81 82 # Typical message is " unrecognized option '--foobar'", but on 83 # Open/NetBSD it is " unknown option -- foobar". 84 ['error', '--foobar', {IN=>''}, {OUT=>""}, {EXIT=>1}, 85 {ERR=>"$prog: foobar\n" . $try_help }, 86 {ERR_SUBST=>"s/(unrecognized|unknown) option [-' ]*foobar[' ]*/foobar/"}], 87 88 ['noenc', '', {IN=>''}, {EXIT=>1}, 89 {ERR=>"$prog: missing encoding type\n" . $try_help }], 90 91 ['extra', '--base64 A B', {IN=>''}, {EXIT=>1}, 92 {ERR=>"$prog: extra operand 'B'\n" . $try_help}], 93 94 95 ['empty1', '--base64', {IN=>''}, {OUT=>""}], 96 ['empty2', '--base64url', {IN=>''}, {OUT=>""}], 97 ['empty3', '--base32', {IN=>''}, {OUT=>""}], 98 ['empty4', '--base32hex', {IN=>''}, {OUT=>""}], 99 ['empty5', '--base16', {IN=>''}, {OUT=>""}], 100 ['empty6', '--base2msbf', {IN=>''}, {OUT=>""}], 101 ['empty7', '--base2lsbf', {IN=>''}, {OUT=>""}], 102 ['empty8', '--z85', {IN=>''}, {OUT=>""}], 103 104 105 106 107 ['b64_1', '--base64', {IN=>$base64_in}, {OUT=>$base64_out}], 108 ['b64_2', '--base64 -d', {IN=>$base64_out}, {OUT=>$base64_in}], 109 ['b64_3', '--base64 -d -i', {IN=>'&'.$base64_out},{OUT=>$base64_in}], 110 111 ['b64u_1', '--base64url', {IN=>$base64_in}, {OUT=>$base64url_out}], 112 ['b64u_2', '--base64url -d', {IN=>$base64url_out}, {OUT=>$base64_in}], 113 ['b64u_3', '--base64url -di', {IN=>'&'.$base64url_out} , {OUT=>$base64_in}], 114 ['b64u_4', '--base64url -di', {IN=>'/'.$base64url_out.'+'},{OUT=>$base64_in}], 115 ['b64u_5', '--base64url -d', {IN=>$base64url_out_nl}, {OUT=>$base64_in}], 116 ['b64u_6', '--base64url -di', {IN=>$base64url_out_nl}, {OUT=>$base64_in}], 117 # ensure base64url fails to decode base64 input with "+" and "/" 118 ['b64u_7', '--base64url -d', {IN=>$base64_out}, 119 {EXIT=>1}, {ERR=>"$prog: invalid input\n"}], 120 121 ['b64_bug49741', '--base64 -d', {IN=>$base64_bug49741_in}, 122 {OUT=>$base64_bug49741_out}], 123 124 125 126 ['b32_1', '--base32', {IN=>$base32_in}, {OUT=>$base32_out}], 127 ['b32_2', '--base32 -d', {IN=>$base32_out}, {OUT=>$base32_in}], 128 ['b32_3', '--base32 -d -i', {IN=>'&'.$base32_out},{OUT=>$base32_in}], 129 ['b32_4', '--base32', {IN=>$base32_in2}, {OUT=>$base32_out2}], 130 ['b32_5', '--base32 -d', {IN=>$base32_out2}, {OUT=>$base32_in2}], 131 ['b32_6', '--base32 -d -i', {IN=>$base32_out2}, {OUT=>$base32_in2}], 132 133 134 135 ['b32h_1', '--base32hex', {IN=>$base32_in}, {OUT=>$base32hex_out}], 136 ['b32h_2', '--base32hex -d', {IN=>$base32hex_out}, {OUT=>$base32_in}], 137 ['b32h_3', '--base32hex -d -i', {IN=>'/'.$base32hex_out}, {OUT=>$base32_in}], 138 ['b32h_4', '--base32hex -d -i', {IN=>'W'.$base32hex_out}, {OUT=>$base32_in}], 139 ['b32h_5', '--base32hex -d', {IN=>$base32hex_out.'W'}, , {OUT=>$base32_in}, 140 {EXIT=>1}, {ERR=>"$prog: invalid input\n"}], 141 ['b32h_6', '--base32hex -d', {IN=>$base32hex_out.'/'}, {OUT=>$base32_in}, 142 {EXIT=>1}, {ERR=>"$prog: invalid input\n"}], 143 ['b32h_7', '--base32hex', {IN=>$base32_in2}, {OUT=>$base32hex_out2}], 144 ['b32h_8', '--base32hex -d', {IN=>$base32hex_out2}, {OUT=>$base32_in2}], 145 ['b32h_9', '--base32hex -di', {IN=>$base32hex_out2}, {OUT=>$base32_in2}], 146 ['b32h_10', '--base32hex -d', {IN=>$base32hex_out2_nl}, {OUT=>$base32_in2}], 147 ['b32h_11', '--base32hex -di', {IN=>$base32hex_out2_nl}, {OUT=>$base32_in2}], 148 149 150 151 ['b16_1', '--base16', {IN=>$base16_in}, {OUT=>$base16_out}], 152 ['b16_2', '--base16 -d', {IN=>$base16_out}, {OUT=>$base16_in}], 153 ['b16_3', '--base16 -d -i', {IN=>'&'. $base16_out}, {OUT=>$base16_in}], 154 ['b16_4', '--base16 -d -i', {IN=>$base16_out.'G'}, {OUT=>$base16_in}], 155 ['b16_5', '--base16 -d', {IN=>'.'}, {EXIT=>1}, 156 {ERR=>"$prog: invalid input\n"}], 157 ['b16_6', '--base16 -d', {IN=>'='}, {EXIT=>1}, 158 {ERR=>"$prog: invalid input\n"}], 159 ['b16_7', '--base16 -d', {IN=>'G'}, {EXIT=>1}, 160 {ERR=>"$prog: invalid input\n"}], 161 ['b16_8', '--base16 -d', {IN=>"AB\nCD"}, {OUT=>"\xAB\xCD"}], 162 ['b16_9', '--base16 -d', {IN=>lc ($base16_out)}, {OUT=>$base16_in}], 163 ['b16_10', '--base16 -d -i', {IN=>lc ($base16_out)}, {OUT=>$base16_in}], 164 165 166 167 ['b2m_1', '--base2m', {IN=>"\xC1"}, {OUT=>"11000001"}], 168 ['b2m_2', '--base2m -d', {IN=>'11000001'}, {OUT=>"\xC1"}], 169 ['b2m_3', '--base2m -d', {IN=>"110\n00001"}, {OUT=>"\xC1"}], 170 ['b2m_4', '--base2m -di', {IN=>"110x00001"}, {OUT=>"\xC1"}], 171 ['b2m_5', '--base2m -d', {IN=>"110x00001"}, {EXIT=>1}, 172 {ERR=>"$prog: invalid input\n"}], 173 ['b2m_6', '--base2m -d', {IN=>"11000001x"}, {OUT=>"\xC1"}, {EXIT=>1}, 174 {ERR=>"$prog: invalid input\n"}], 175 ['b2m_7', '--base2m -d', {IN=>"1"}, {EXIT=>1}, 176 {ERR=>"$prog: invalid input\n"}], 177 ['b2m_8', '--base2m -d', {IN=>"1000100"}, {EXIT=>1}, 178 {ERR=>"$prog: invalid input\n"}], 179 ['b2m_9', '--base2m -d', {IN=>"100010000000000"}, {OUT=>"\x88"}, {EXIT=>1}, 180 {ERR=>"$prog: invalid input\n"}], 181 ['b2m_10','--base2m', {IN=>"ab"}, {OUT=>$base2msbf_ab}], 182 ['b2m_11','--base2m -d', {IN=>$base2msbf_ab}, {OUT=>"ab"}], 183 ['b2m_12','--base2m -d', {IN=>$base2msbf_ab_nl}, {OUT=>"ab"}], 184 185 186 ['b2l_1', '--base2l', {IN=>"\x83"}, {OUT=>"11000001"}], 187 ['b2l_2', '--base2l -d', {IN=>'11000001'}, {OUT=>"\x83"}], 188 ['b2l_3', '--base2l -d', {IN=>"110\n00001"}, {OUT=>"\x83"}], 189 ['b2l_4', '--base2l -di', {IN=>"110x00001"}, {OUT=>"\x83"}], 190 ['b2l_5', '--base2l -d', {IN=>"110x00001"}, {EXIT=>1}, 191 {ERR=>"$prog: invalid input\n"}], 192 ['b2l_6', '--base2l -d', {IN=>"11000001x"}, {OUT=>"\x83"}, {EXIT=>1}, 193 {ERR=>"$prog: invalid input\n"}], 194 ['b2l_7', '--base2l -d', {IN=>"1"}, {EXIT=>1}, 195 {ERR=>"$prog: invalid input\n"}], 196 ['b2l_8', '--base2l -d', {IN=>"1000100"}, {EXIT=>1}, 197 {ERR=>"$prog: invalid input\n"}], 198 ['b2l_9', '--base2l -d', {IN=>"100010000000000"}, {OUT=>"\x11"}, {EXIT=>1}, 199 {ERR=>"$prog: invalid input\n"}], 200 ['b2l_10','--base2l', {IN=>"ab"}, {OUT=>$base2lsbf_ab}], 201 ['b2l_11','--base2l -d', {IN=>$base2lsbf_ab}, {OUT=>"ab"}], 202 ['b2l_12','--base2l -d', {IN=>$base2lsbf_ab_nl}, {OUT=>"ab"}], 203 204 205 206 207 208 ['z85_1', '--z85', {IN=>$z85_in}, {OUT=>$z85_out}], 209 ['z85_2', '--z85 -d', {IN=>$z85_out}, {OUT=>$z85_in}], 210 ['z85_3', '--z85 -d -i', {IN=>'~'. $z85_out}, {OUT=>$z85_in}], 211 ['z85_4', '--z85 -d -i', {IN=>' '. $z85_out}, {OUT=>$z85_in}], 212 ['z85_5', '--z85 -d', {IN=>'%j$qP'}, {OUT=>"\xFF\xDD\xBB\x99"}], 213 ['z85_6', '--z85 -d -i', {IN=>'%j~$qP'}, {OUT=>"\xFF\xDD\xBB\x99"}], 214 215 # z85 encoding require input to be multiple of 5 octets 216 ['z85_7', '--z85 -d', {IN=>'hello'}, {OUT=>"5jXu"}], 217 ['z85_8', '--z85 -d', {IN=>'helloX'}, {OUT=>"5jXu"}, {EXIT=>1}, 218 {ERR=>"$prog: invalid input\n"}], 219 ['z85_9', '--z85 -d', {IN=>"he\nl\nlo"}, {OUT=>"5jXu"}], 220 221 # Invalid input characters (space ~ ") 222 ['z85_10', '--z85 -d', {IN=>' j$qP'}, {EXIT=>1}, 223 {ERR=>"$prog: invalid input\n"}], 224 ['z85_11', '--z85 -d', {IN=>'%j$q~'}, {EXIT=>1}, 225 {ERR=>"$prog: invalid input\n"}], 226 ['z85_12', '--z85 -d', {IN=>'%j$"P'}, {EXIT=>1}, 227 {ERR=>"$prog: invalid input\n"}], 228 229 # Invalid length (binary input must be a multiple of 4 octets, 230 # z85-encoded input must be a multiple of 5 octets) 231 ['z85_20', '--z85', {IN=>'A'}, {EXIT=>1}, 232 {ERR=>"$prog: invalid input (length must be multiple of 4 characters)\n"}], 233 ['z85_21', '--z85', {IN=>'AB'}, {EXIT=>1}, 234 {ERR=>"$prog: invalid input (length must be multiple of 4 characters)\n"}], 235 ['z85_22', '--z85', {IN=>'ABC'}, {EXIT=>1}, 236 {ERR=>"$prog: invalid input (length must be multiple of 4 characters)\n"}], 237 ['z85_23', '--z85', {IN=>'ABCD'}, {OUT=>'k%^}b'}], 238 ['z85_24', '--z85', {IN=>'ABCDE'}, {EXIT=>1}, 239 {ERR=>"$prog: invalid input (length must be multiple of 4 characters)\n"}], 240 241 ['z85_30', '--z85 -d', {IN=>'A'}, {EXIT=>1}, 242 {ERR=>"$prog: invalid input\n"}], 243 ['z85_31', '--z85 -d', {IN=>'AB'}, {EXIT=>1}, 244 {ERR=>"$prog: invalid input\n"}], 245 ['z85_32', '--z85 -d', {IN=>'ABC'}, {EXIT=>1}, 246 {ERR=>"$prog: invalid input\n"}], 247 ['z85_33', '--z85 -d', {IN=>'ABCD'}, {EXIT=>1}, 248 {ERR=>"$prog: invalid input\n"}], 249 ['z85_34', '--z85 -d', {IN=>'ABCDE'}, {OUT=>"\x71\x61\x9e\xb6"}], 250 ['z85_35', '--z85 -d', {IN=>'ABCDEF'},{OUT=>"\x71\x61\x9e\xb6"}, 251 {EXIT=>1}, {ERR=>"$prog: invalid input\n"}], 252 253 # largest possible value 254 ['z85_40', '--z85', {IN=>"\xFF\xFF\xFF\xFF"},{OUT=>"%nSc0"}], 255 ['z85_41', '--z85 -d', {IN=>"%nSc0"}, {OUT=>"\xFF\xFF\xFF\xFF"}], 256 # Invalid encoded data - will decode to more than 0xFFFFFFFF 257 ['z85_42', '--z85 -d', {IN=>"%nSc1"}, {EXIT=>1}, 258 {ERR=>"$prog: invalid input\n"}], 259 ['z85_43', '--z85 -d', {IN=>"%nSd0"}, {EXIT=>1}, 260 {ERR=>"$prog: invalid input\n"}], 261 ['z85_44', '--z85 -d', {IN=>"%nTc0"}, {EXIT=>1}, 262 {ERR=>"$prog: invalid input\n"}], 263 ['z85_45', '--z85 -d', {IN=>"%oSc0"}, {EXIT=>1}, 264 {ERR=>"$prog: invalid input\n"}], 265 ['z85_46', '--z85 -d', {IN=>'$nSc0'}, {EXIT=>1}, 266 {ERR=>"$prog: invalid input\n"}], 267 ['z85_47', '--z85 -d', {IN=>'#0000'}, {EXIT=>1}, 268 {ERR=>"$prog: invalid input\n"}], 269); 270 271# Prepend the command line argument and append a newline to end 272# of each expected 'OUT' string. 273my $t; 274 275Test: 276foreach $t (@Tests) 277 { 278 foreach my $e (@$t) 279 { 280 ref $e && ref $e eq 'HASH' && defined $e->{OUT_SUBST} 281 and next Test; 282 } 283 284 push @$t, {OUT_SUBST=>'s/\n$//s'}; 285 } 286 287 288 289my $save_temps = $ENV{DEBUG}; 290my $verbose = $ENV{VERBOSE}; 291 292my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); 293 294exit $fail; 295