test-apt-key 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. echo 'APT::Key::ArchiveKeyring "./keys/joesixpack.pub";
  19. APT::Key::RemovedKeys "./keys/rexexpired.pub";' > rootdir/etc/apt/apt.conf.d/aptkey.conf
  20. testrun() {
  21. cleanplate
  22. ln -sf "${TMPWORKINGDIRECTORY}/keys/joesixpack.pub" rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  23. msgtest 'Check that paths in list output are not' 'double-slashed'
  24. aptkey list 2>&1 | grep -q '//' && msgfail || msgpass
  25. msgtest 'Check that paths in finger output are not' 'double-slashed'
  26. aptkey finger 2>&1 | grep -q '//' && msgfail || msgpass
  27. testaptkeys 'Joe Sixpack'
  28. testsuccessequal 'gpg: key DBAC8DAE: "Joe Sixpack (APT Testcases Dummy) <joe@example.org>" not changed
  29. gpg: Total number processed: 1
  30. gpg: unchanged: 1' aptkey --fakeroot update
  31. testaptkeys 'Joe Sixpack'
  32. testfailure test -e rootdir/etc/apt/trusted.gpg
  33. testsuccess aptkey --fakeroot add ./keys/rexexpired.pub
  34. msgtest 'Check if trusted.gpg is created with permissions set to' '0644'
  35. if [ "$(stat -c '%a' rootdir/etc/apt/trusted.gpg )" = '644' ]; then
  36. msgpass
  37. else
  38. msgfail
  39. fi
  40. testaptkeys 'Rex Expired' 'Joe Sixpack'
  41. msgtest 'Check that Sixpack key can be' 'exported'
  42. aptkey export 'Sixpack' > aptkey.export
  43. aptkey --keyring rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg exportall > aptkey.exportall
  44. testsuccess --nomsg cmp aptkey.export aptkey.exportall
  45. testsuccess test -s aptkey.export
  46. testsuccess test -s aptkey.exportall
  47. msgtest 'Execute update again to trigger removal of' 'Rex Expired key'
  48. testsuccess --nomsg aptkey --fakeroot update
  49. testaptkeys 'Joe Sixpack'
  50. msgtest "Try to remove a key which exists, but isn't in the" 'forced keyring'
  51. testsuccess --nomsg aptkey --fakeroot --keyring rootdir/etc/apt/trusted.gpg del DBAC8DAE
  52. testaptkeys 'Joe Sixpack'
  53. testsuccess aptkey --fakeroot del DBAC8DAE
  54. testempty aptkey list
  55. msgtest 'Test key removal with' 'lowercase key ID' #keylength somewhere between 8byte and short
  56. cleanplate
  57. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  58. testsuccess --nomsg aptkey --fakeroot del d141dbac8dae
  59. testempty aptkey list
  60. msgtest 'Test key removal with' 'single key in real file'
  61. cleanplate
  62. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  63. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  64. testempty aptkey list
  65. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  66. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  67. msgtest 'Test key removal with' 'long key ID'
  68. cleanplate
  69. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  70. testsuccess --nomsg aptkey --fakeroot del 5A90D141DBAC8DAE
  71. testempty aptkey list
  72. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  73. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  74. msgtest 'Test key removal with' 'fingerprint'
  75. cleanplate
  76. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  77. testsuccess --nomsg aptkey --fakeroot del 34A8E9D18DB320F367E8EAA05A90D141DBAC8DAE
  78. testempty aptkey list
  79. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  80. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  81. msgtest 'Test key removal with' 'single key in softlink'
  82. cleanplate
  83. ln -s "$(readlink -f ./keys/joesixpack.pub)" rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  84. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  85. testempty aptkey list
  86. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  87. testsuccess test -L rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  88. cleanplate
  89. testsuccess aptkey --fakeroot add ./keys/joesixpack.pub
  90. ln -sf "$(readlink -f ./keys/marvinparanoid.pub)" "./keys/marvin paránöid.pub"
  91. testsuccess aptkey --fakeroot add "./keys/marvin paránöid.pub"
  92. testaptkeys 'Joe Sixpack' 'Marvin Paranoid'
  93. cp -a rootdir/etc/apt/trusted.gpg keys/testcase-multikey.pub # store for reuse
  94. msgtest 'Test key removal with' 'multi key in real file'
  95. cleanplate
  96. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  97. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  98. testaptkeys 'Marvin Paranoid'
  99. testsuccess cmp keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  100. msgtest 'Test key removal with' 'multi key in softlink'
  101. cleanplate
  102. ln -s "$(readlink -f ./keys/testcase-multikey.pub)" rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  103. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  104. testaptkeys 'Marvin Paranoid'
  105. testsuccess cmp keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  106. testfailure test -L rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  107. testsuccess test -L rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  108. msgtest 'Test key removal with' 'multiple files including key'
  109. cleanplate
  110. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  111. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  112. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  113. testaptkeys 'Marvin Paranoid'
  114. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  115. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  116. testsuccess cmp keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  117. cleanplate
  118. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  119. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  120. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  121. msgtest 'Test merge-back of' 'added keys'
  122. testsuccess --nomsg aptkey adv --batch --yes --import keys/rexexpired.pub
  123. testaptkeys 'Rex Expired' 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  124. msgtest 'Test merge-back of' 'removed keys'
  125. testsuccess --nomsg aptkey adv --batch --yes --delete-keys 27CE74F9
  126. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  127. msgtest 'Test merge-back of' 'removed duplicate keys'
  128. testsuccess --nomsg aptkey adv --batch --yes --delete-keys DBAC8DAE
  129. testaptkeys 'Marvin Paranoid'
  130. cleanplate
  131. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  132. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  133. msgtest 'Test signing a file' 'with a key'
  134. echo 'Verify me. This is my signature.' > signature
  135. testsuccess --nomsg aptkey --quiet --keyring keys/marvinparanoid.pub --secret-keyring keys/marvinparanoid.sec --readonly \
  136. adv --batch --yes --default-key 'Marvin' --armor --detach-sign --sign --output signature.gpg signature
  137. testsuccess test -s signature.gpg -a -s signature
  138. for GPGV in '' 'gpgv' 'gpgv2'; do
  139. echo "APT::Key::GPGVCommand \"$GPGV\";" > rootdir/etc/apt/apt.conf.d/00gpgvcmd
  140. msgtest 'Test verify a file' 'with all keys'
  141. testsuccess --nomsg aptkey --quiet --readonly verify signature.gpg signature
  142. msgtest 'Test verify a file' 'with good keyring'
  143. testsuccess --nomsg aptkey --quiet --readonly --keyring keys/testcase-multikey.pub verify signature.gpg signature
  144. msgtest 'Test fail verify a file' 'with bad keyring'
  145. testfailure --nomsg aptkey --quiet --readonly --keyring keys/joesixpack.pub verify signature.gpg signature
  146. msgtest 'Test fail verify a file' 'with non-existing keyring'
  147. testfailure --nomsg aptkey --quiet --readonly --keyring keys/does-not-exist.pub verify signature.gpg signature
  148. testfailure test -e keys/does-not-exist.pub
  149. # note: this isn't how apts gpgv method implements keyid for verify
  150. msgtest 'Test verify a file' 'with good keyid'
  151. testsuccess --nomsg aptkey --quiet --readonly --keyid 'Paranoid' verify signature.gpg signature
  152. msgtest 'Test fail verify a file' 'with bad keyid'
  153. testfailure --nomsg aptkey --quiet --readonly --keyid 'Sixpack' verify signature.gpg signature
  154. msgtest 'Test fail verify a file' 'with non-existing keyid'
  155. testfailure --nomsg aptkey --quiet --readonly --keyid 'Kalnischkies' verify signature.gpg signature
  156. msgtest 'Test verify fails on' 'bad file'
  157. echo 'lalalalala' > signature2
  158. testfailure --nomsg aptkey --quiet --readonly verify signature.gpg signature2
  159. done
  160. }
  161. setupgpgcommand() {
  162. echo "APT::Key::GPGCommand \"$1\";" > rootdir/etc/apt/apt.conf.d/00gpgcmd
  163. msgmsg 'Force tests to be run with' "$1"
  164. testsuccess aptkey --readonly adv --version
  165. cp rootdir/tmp/testsuccess.output aptkey.version
  166. testsuccess grep "^gpg (GnuPG) $2\." aptkey.version
  167. }
  168. # run with default (whatever this is)
  169. testrun
  170. # run with …
  171. setupgpgcommand 'gpg' '1'
  172. testrun
  173. setupgpgcommand 'gpg2' '2'
  174. testrun