| 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Daniel Kahn Gillmor <[email protected]> |
| 4 | |
| 5 | # put pinentry-tty through its paces on an otherwise clean |
| 6 | # pseudoterminal. |
| 7 | |
| 8 | import posix |
| 9 | import subprocess |
| 10 | import codecs |
| 11 | import time |
| 12 | |
| 13 | # block on pinentry verification: |
| 14 | def isok(pinentry): |
| 15 | l = pinentry.stdout.readline() |
| 16 | if not l.startswith('OK'): |
| 17 | raise Exception("notok: " + l if l else '<None>') |
| 18 | |
| 19 | |
| 20 | (master, pty) = posix.openpty() |
| 21 | # depends on /proc! |
| 22 | ptyname = posix.readlink("/proc/self/fd/%d"%(pty)) |
| 23 | |
| 24 | p = subprocess.Popen(['pinentry-tty'], |
| 25 | shell=False, |
| 26 | stdin=subprocess.PIPE, |
| 27 | stdout=subprocess.PIPE, |
| 28 | universal_newlines=True, |
| 29 | bufsize=1) |
| 30 | isok(p) |
| 31 | |
| 32 | p.stdin.write("option ttyname %s\n"%(ptyname)) |
| 33 | isok(p) |
| 34 | |
| 35 | p.stdin.write("setprompt foo bar\n") |
| 36 | isok(p) |
| 37 | |
| 38 | p.stdin.write("getpin\n") |
| 39 | p.stdin.flush() |
| 40 | |
| 41 | # avoid failures on laggy build daemons |
| 42 | time.sleep(2.0) |
| 43 | |
| 44 | prompt = codecs.decode(posix.read(master, 1000)) |
| 45 | print("---begin pinentry prompt---") |
| 46 | print(prompt) |
| 47 | print("---end pinentry prompt---") |
| 48 | |
| 49 | posix.write(master, codecs.encode("abc123\n")) |
| 50 | |
| 51 | answer = p.stdout.readline() |
| 52 | if answer.strip() != "D abc123": |
| 53 | raise Exception("wrongpass" + answer) |
| 54 | isok(p) |
| 55 | |
| 56 | p.stdin.write("bye\n") |
| 57 | isok(p) |
| 58 | |
| 59 | ret = p.wait() |
| 60 | if ret: |
| 61 | raise Exception("pinentry terminated wrong: %d!"%(ret)) |
| 62 | |
| 63 | posix.close(pty) |
| 64 | posix.close(master) |