| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | import os, subprocess
|
| 7 |
|
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 |
|
| 13 | |
| 14 | SIZE=256
|
| 15 |
|
| 16 | NAME_MAX=int(subprocess.check_output(["getconf", "NAME_MAX", "."]))
|
| 17 |
|
| 18 | test_num=0
|
| 19 | pass_num=0
|
| 20 | fail_num=0
|
| 21 |
|
| 22 | code='''#!/usr/bin/perl
|
| 23 | print "Executed interpreter! Args:\n";
|
| 24 | print "0 : '$0'\n";
|
| 25 | $counter = 1;
|
| 26 | foreach my $a (@ARGV) {
|
| 27 | print "$counter : '$a'\n";
|
| 28 | $counter++;
|
| 29 | }
|
| 30 | '''
|
| 31 |
|
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | def test(name, size, good=True, leading="", root="./", target="/perl",
|
| 46 | fill="A", arg="", newline="\n", hashbang="#!"):
|
| 47 | global test_num, pass_num, fail_num, tests, NAME_MAX
|
| 48 | test_num += 1
|
| 49 | if test_num > tests:
|
| 50 | raise ValueError("more binfmt_script tests than expected! (want %d, expected %d)"
|
| 51 | % (test_num, tests))
|
| 52 |
|
| 53 | middle = ""
|
| 54 | remaining = size - len(hashbang) - len(leading) - len(root) - len(target) - len(arg)
|
| 55 | |
| 56 | while remaining >= NAME_MAX:
|
| 57 | middle += fill * (NAME_MAX - 1)
|
| 58 | middle += '/'
|
| 59 | remaining -= NAME_MAX
|
| 60 | middle += fill * remaining
|
| 61 |
|
| 62 | dirpath = root + middle
|
| 63 | binary = dirpath + target
|
| 64 | if len(target):
|
| 65 | os.makedirs(dirpath, mode=0o755, exist_ok=True)
|
| 66 | open(binary, "w").write(code)
|
| 67 | os.chmod(binary, 0o755)
|
| 68 |
|
| 69 | buf=hashbang + leading + root + middle + target + arg + newline
|
| 70 | if len(newline) > 0:
|
| 71 | buf += 'echo this is not really perl\n'
|
| 72 |
|
| 73 | script = "binfmt_script-%s" % (name)
|
| 74 | open(script, "w").write(buf)
|
| 75 | os.chmod(script, 0o755)
|
| 76 |
|
| 77 | proc = subprocess.Popen(["./%s" % (script)], shell=True,
|
| 78 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
| 79 | stdout = proc.communicate()[0]
|
| 80 |
|
| 81 | if proc.returncode == 0 and b'Executed interpreter' in stdout:
|
| 82 | if good:
|
| 83 | print("ok %d - binfmt_script %s (successful good exec)"
|
| 84 | % (test_num, name))
|
| 85 | pass_num += 1
|
| 86 | else:
|
| 87 | print("not ok %d - binfmt_script %s succeeded when it should have failed"
|
| 88 | % (test_num, name))
|
| 89 | fail_num = 1
|
| 90 | else:
|
| 91 | if good:
|
| 92 | print("not ok %d - binfmt_script %s failed when it should have succeeded (rc:%d)"
|
| 93 | % (test_num, name, proc.returncode))
|
| 94 | fail_num = 1
|
| 95 | else:
|
| 96 | print("ok %d - binfmt_script %s (correctly failed bad exec)"
|
| 97 | % (test_num, name))
|
| 98 | pass_num += 1
|
| 99 |
|
| 100 | |
| 101 | os.unlink(script)
|
| 102 | if len(target):
|
| 103 | elements = binary.split('/')
|
| 104 | os.unlink(binary)
|
| 105 | elements.pop()
|
| 106 | while len(elements) > 1:
|
| 107 | os.rmdir("/".join(elements))
|
| 108 | elements.pop()
|
| 109 |
|
| 110 | tests=27
|
| 111 | print("TAP version 1.3")
|
| 112 | print("1..%d" % (tests))
|
| 113 |
|
| 114 | |
| 115 |
|
| 116 | |
| 117 | test(name="too-big", size=SIZE+80, good=False)
|
| 118 | |
| 119 | test(name="exact", size=SIZE, good=False)
|
| 120 | |
| 121 | test(name="exact-space", size=SIZE, good=False, leading=" ")
|
| 122 | |
| 123 | test(name="whitespace-too-big", size=SIZE+71, good=False, root="",
|
| 124 | fill=" ", target="")
|
| 125 | |
| 126 | test(name="truncated", size=SIZE+17, good=False, leading=" " * 19)
|
| 127 | |
| 128 | test(name="empty", size=2, good=False, root="",
|
| 129 | fill="", target="", newline="")
|
| 130 | |
| 131 | test(name="spaces", size=SIZE-1, good=False, root="", fill=" ",
|
| 132 | target="", newline="")
|
| 133 | |
| 134 | test(name="newline-prefix", size=SIZE-1, good=False, leading="\n",
|
| 135 | root="", fill=" ", target="")
|
| 136 |
|
| 137 | |
| 138 |
|
| 139 | |
| 140 | |
| 141 | test(name="test.pl", size=439, leading=" ",
|
| 142 | root="./nix/store/bwav8kz8b3y471wjsybgzw84mrh4js9-perl-5.28.1/bin",
|
| 143 | arg=" -I/nix/store/x6yyav38jgr924nkna62q3pkp0dgmzlx-perl5.28.1-File-Slurp-9999.25/lib/perl5/site_perl -I/nix/store/ha8v67sl8dac92r9z07vzr4gv1y9nwqz-perl5.28.1-Net-DBus-1.1.0/lib/perl5/site_perl -I/nix/store/dcrkvnjmwh69ljsvpbdjjdnqgwx90a9d-perl5.28.1-XML-Parser-2.44/lib/perl5/site_perl -I/nix/store/rmji88k2zz7h4zg97385bygcydrf2q8h-perl5.28.1-XML-Twig-3.52/lib/perl5/site_perl")
|
| 144 | |
| 145 | test(name="one-under", size=SIZE-1)
|
| 146 | |
| 147 | test(name="two-under", size=SIZE-2)
|
| 148 | |
| 149 | test(name="exact-trunc-whitespace", size=SIZE, arg=" ")
|
| 150 | |
| 151 | test(name="exact-trunc-arg", size=SIZE, arg=" f")
|
| 152 | |
| 153 | test(name="one-under-full-arg", size=SIZE-1, arg=" f")
|
| 154 | |
| 155 | test(name="one-under-no-nl", size=SIZE-1, newline="")
|
| 156 | |
| 157 | test(name="half-under-no-nl", size=int(SIZE/2), newline="")
|
| 158 | |
| 159 | test(name="one-under-trunc-arg", size=SIZE-1, arg=" ")
|
| 160 | |
| 161 | test(name="one-under-leading", size=SIZE-1, leading=" ")
|
| 162 | |
| 163 | test(name="one-under-leading-trunc-arg", size=SIZE-1, leading=" ", arg=" ")
|
| 164 | |
| 165 | test(name="two-under-no-nl", size=SIZE-2, newline="")
|
| 166 | test(name="two-under-trunc-arg", size=SIZE-2, arg=" ")
|
| 167 | test(name="two-under-leading", size=SIZE-2, leading=" ")
|
| 168 | test(name="two-under-leading-trunc-arg", size=SIZE-2, leading=" ", arg=" ")
|
| 169 | |
| 170 | test(name="two-under-no-nl", size=int(SIZE/2), newline="")
|
| 171 | test(name="two-under-trunc-arg", size=int(SIZE/2), arg=" ")
|
| 172 | test(name="two-under-leading", size=int(SIZE/2), leading=" ")
|
| 173 | test(name="two-under-lead-trunc-arg", size=int(SIZE/2), leading=" ", arg=" ")
|
| 174 |
|
| 175 | print("# Totals: pass:%d fail:%d xfail:0 xpass:0 skip:0 error:0" % (pass_num, fail_num))
|
| 176 |
|
| 177 | if test_num != tests:
|
| 178 | raise ValueError("fewer binfmt_script tests than expected! (ran %d, expected %d"
|
| 179 | % (test_num, tests))
|