test-apt-key 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #!/bin/sh
  2. set -e
  3. # apt-key is a shell script, so relatively prune to be effected by 'crazy' things:
  4. # confuses config parser as there exists no way of escaping " currently.
  5. #TMPDIR="$(mktemp -d)/This is \"fü\$\$ing cràzy\", \$(man man | head -n1 | cut -d' ' -f 1)\$!"
  6. # gpg doesn't like | in path names – documented e.g. in the man gpg2 --agent-program
  7. #TMPDIR="$(mktemp -d)/This is fü\$\$ing cràzy, \$(man man | head -n1 | cut -d' ' -f 1)\$!"
  8. TMPDIR_ADD="This is fü\$\$ing cràzy, \$(apt -v)\$!"
  9. TESTDIR="$(readlink -f "$(dirname "$0")")"
  10. . "$TESTDIR/framework"
  11. setupenvironment
  12. configarchitecture 'amd64'
  13. # start from a clean plate again
  14. cleanplate() {
  15. rm -rf rootdir/etc/apt/trusted.gpg.d/ rootdir/etc/apt/trusted.gpg
  16. mkdir rootdir/etc/apt/trusted.gpg.d/
  17. }
  18. createlistofkeys() {
  19. while [ -n "$1" ]; do
  20. # gpg 2.1 has a slightly different output format
  21. if grep -q ' rsa2048/' aptkey.list; then
  22. case "$1" in
  23. *Joe*|*Sixpack*) echo 'pub rsa2048/DBAC8DAE 2010-08-18';;
  24. *Rex*|*Expired*) echo 'pub rsa2048/27CE74F9 2013-07-12 [expired: 2013-07-13]';;
  25. *Marvin*|*Paranoid*) echo 'pub rsa2048/528144E2 2011-01-16';;
  26. *) echo 'UNKNOWN KEY';;
  27. esac
  28. else
  29. case "$1" in
  30. *Joe*|*Sixpack*) echo 'pub 2048R/DBAC8DAE 2010-08-18';;
  31. *Rex*|*Expired*) echo 'pub 2048R/27CE74F9 2013-07-12 [expired: 2013-07-13]';;
  32. *Marvin*|*Paranoid*) echo 'pub 2048R/528144E2 2011-01-16';;
  33. *) echo 'UNKNOWN KEY';;
  34. esac
  35. fi
  36. shift
  37. done
  38. }
  39. testaptkeys() {
  40. if ! aptkey list | grep '^pub' > aptkey.list; then
  41. echo -n > aptkey.list
  42. fi
  43. testfileequal './aptkey.list' "$(createlistofkeys "$@")"
  44. }
  45. echo 'APT::Key::ArchiveKeyring "./keys/joesixpack.pub";
  46. APT::Key::RemovedKeys "./keys/rexexpired.pub";' > rootdir/etc/apt/apt.conf.d/aptkey.conf
  47. testrun() {
  48. cleanplate
  49. ln -sf "${TMPWORKINGDIRECTORY}/keys/joesixpack.pub" rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  50. msgtest 'Check that paths in list output are not' 'double-slashed'
  51. aptkey list 2>&1 | grep -q '//' && msgfail || msgpass
  52. msgtest 'Check that paths in finger output are not' 'double-slashed'
  53. aptkey finger 2>&1 | grep -q '//' && msgfail || msgpass
  54. testaptkeys 'Joe Sixpack'
  55. testsuccessequal 'gpg: key DBAC8DAE: "Joe Sixpack (APT Testcases Dummy) <joe@example.org>" not changed
  56. gpg: Total number processed: 1
  57. gpg: unchanged: 1' aptkey --fakeroot update
  58. testaptkeys 'Joe Sixpack'
  59. testfailure test -e rootdir/etc/apt/trusted.gpg
  60. testsuccess aptkey --fakeroot add ./keys/rexexpired.pub
  61. msgtest 'Check if trusted.gpg is created with permissions set to' '0644'
  62. if [ "$(stat -c '%a' rootdir/etc/apt/trusted.gpg )" = '644' ]; then
  63. msgpass
  64. else
  65. msgfail
  66. fi
  67. testaptkeys 'Rex Expired' 'Joe Sixpack'
  68. msgtest 'Check that Sixpack key can be' 'exported'
  69. aptkey export 'Sixpack' > aptkey.export
  70. aptkey --keyring rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg exportall > aptkey.exportall
  71. testsuccess --nomsg cmp aptkey.export aptkey.exportall
  72. testsuccess test -s aptkey.export
  73. testsuccess test -s aptkey.exportall
  74. msgtest 'Execute update again to trigger removal of' 'Rex Expired key'
  75. testsuccess --nomsg aptkey --fakeroot update
  76. testaptkeys 'Joe Sixpack'
  77. msgtest "Try to remove a key which exists, but isn't in the" 'forced keyring'
  78. testsuccess --nomsg aptkey --fakeroot --keyring rootdir/etc/apt/trusted.gpg del DBAC8DAE
  79. testaptkeys 'Joe Sixpack'
  80. testsuccess aptkey --fakeroot del DBAC8DAE
  81. testempty aptkey list
  82. msgtest 'Test key removal with' 'lowercase key ID' #keylength somewhere between 8byte and short
  83. cleanplate
  84. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  85. testsuccess --nomsg aptkey --fakeroot del d141dbac8dae
  86. testempty aptkey list
  87. msgtest 'Test key removal with' 'single key in real file'
  88. cleanplate
  89. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  90. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  91. testempty aptkey list
  92. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  93. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  94. msgtest 'Test key removal with' 'long key ID'
  95. cleanplate
  96. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  97. testsuccess --nomsg aptkey --fakeroot del 5A90D141DBAC8DAE
  98. testempty aptkey list
  99. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  100. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  101. msgtest 'Test key removal with' 'fingerprint'
  102. cleanplate
  103. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  104. testsuccess --nomsg aptkey --fakeroot del 34A8E9D18DB320F367E8EAA05A90D141DBAC8DAE
  105. testempty aptkey list
  106. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  107. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  108. msgtest 'Test key removal with' 'single key in softlink'
  109. cleanplate
  110. ln -s "$(readlink -f ./keys/joesixpack.pub)" rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  111. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  112. testempty aptkey list
  113. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  114. testsuccess test -L rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  115. cleanplate
  116. testsuccess aptkey --fakeroot add ./keys/joesixpack.pub
  117. ln -sf "$(readlink -f ./keys/marvinparanoid.pub)" "./keys/marvin paránöid.pub"
  118. testsuccess aptkey --fakeroot add "./keys/marvin paránöid.pub"
  119. testaptkeys 'Joe Sixpack' 'Marvin Paranoid'
  120. cp -a rootdir/etc/apt/trusted.gpg keys/testcase-multikey.pub # store for reuse
  121. msgtest 'Test key removal with' 'multi key in real file'
  122. cleanplate
  123. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  124. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  125. testaptkeys 'Marvin Paranoid'
  126. testsuccess cmp keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  127. msgtest 'Test key removal with' 'multi key in softlink'
  128. cleanplate
  129. ln -s "$(readlink -f ./keys/testcase-multikey.pub)" rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  130. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  131. testaptkeys 'Marvin Paranoid'
  132. testsuccess cmp keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  133. testfailure test -L rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  134. testsuccess test -L rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  135. msgtest 'Test key removal with' 'multiple files including key'
  136. cleanplate
  137. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  138. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  139. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  140. testaptkeys 'Marvin Paranoid'
  141. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  142. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  143. testsuccess cmp keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  144. cleanplate
  145. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  146. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  147. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  148. msgtest 'Test merge-back of' 'added keys'
  149. testsuccess --nomsg aptkey adv --batch --yes --import keys/rexexpired.pub
  150. testaptkeys 'Rex Expired' 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  151. msgtest 'Test merge-back of' 'removed keys'
  152. testsuccess --nomsg aptkey adv --batch --yes --delete-keys 27CE74F9
  153. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  154. msgtest 'Test merge-back of' 'removed duplicate keys'
  155. testsuccess --nomsg aptkey adv --batch --yes --delete-keys DBAC8DAE
  156. testaptkeys 'Marvin Paranoid'
  157. cleanplate
  158. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  159. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  160. msgtest 'Test signing a file' 'with a key'
  161. echo 'Verify me. This is my signature.' > signature
  162. testsuccess --nomsg aptkey --quiet --keyring keys/marvinparanoid.pub --secret-keyring keys/marvinparanoid.sec --readonly \
  163. adv --batch --yes --default-key 'Marvin' --armor --detach-sign --sign --output signature.gpg signature
  164. testsuccess test -s signature.gpg -a -s signature
  165. for GPGV in '' 'gpgv' 'gpgv2'; do
  166. echo "APT::Key::GPGVCommand \"$GPGV\";" > rootdir/etc/apt/apt.conf.d/00gpgvcmd
  167. msgtest 'Test verify a file' 'with all keys'
  168. testsuccess --nomsg aptkey --quiet --readonly verify signature.gpg signature
  169. msgtest 'Test verify a file' 'with good keyring'
  170. testsuccess --nomsg aptkey --quiet --readonly --keyring keys/testcase-multikey.pub verify signature.gpg signature
  171. msgtest 'Test fail verify a file' 'with bad keyring'
  172. testfailure --nomsg aptkey --quiet --readonly --keyring keys/joesixpack.pub verify signature.gpg signature
  173. msgtest 'Test fail verify a file' 'with non-existing keyring'
  174. testfailure --nomsg aptkey --quiet --readonly --keyring keys/does-not-exist.pub verify signature.gpg signature
  175. testfailure test -e keys/does-not-exist.pub
  176. # note: this isn't how apts gpgv method implements keyid for verify
  177. msgtest 'Test verify a file' 'with good keyid'
  178. testsuccess --nomsg aptkey --quiet --readonly --keyid 'Paranoid' verify signature.gpg signature
  179. msgtest 'Test fail verify a file' 'with bad keyid'
  180. testfailure --nomsg aptkey --quiet --readonly --keyid 'Sixpack' verify signature.gpg signature
  181. msgtest 'Test fail verify a file' 'with non-existing keyid'
  182. testfailure --nomsg aptkey --quiet --readonly --keyid 'Kalnischkies' verify signature.gpg signature
  183. msgtest 'Test verify fails on' 'bad file'
  184. echo 'lalalalala' > signature2
  185. testfailure --nomsg aptkey --quiet --readonly verify signature.gpg signature2
  186. done
  187. }
  188. setupgpgcommand() {
  189. echo "APT::Key::GPGCommand \"$1\";" > rootdir/etc/apt/apt.conf.d/00gpgcmd
  190. msgmsg 'Force tests to be run with' "$1"
  191. testsuccess aptkey --readonly adv --version
  192. cp rootdir/tmp/testsuccess.output aptkey.version
  193. testsuccess grep "^gpg (GnuPG) $2\." aptkey.version
  194. }
  195. # run with default (whatever this is)
  196. testrun
  197. # run with …
  198. setupgpgcommand 'gpg' '1'
  199. testrun
  200. setupgpgcommand 'gpg2' '2'
  201. testrun