test-apt-key 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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' 'different key specs'
  68. cleanplate
  69. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  70. cp -a keys/marvinparanoid.pub rootdir/etc/apt/trusted.gpg.d/marvinparanoid.gpg
  71. testsuccess --nomsg aptkey --fakeroot del 0xDBAC8DAE 528144E2
  72. testempty aptkey list
  73. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  74. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  75. testfailure test -e rootdir/etc/apt/trusted.gpg.d/marvinparanoid.gpg
  76. testsuccess cmp keys/marvinparanoid.pub rootdir/etc/apt/trusted.gpg.d/marvinparanoid.gpg~
  77. msgtest 'Test key removal with' 'long key ID'
  78. cleanplate
  79. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  80. testsuccess --nomsg aptkey --fakeroot del 5A90D141DBAC8DAE
  81. testempty aptkey list
  82. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  83. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  84. msgtest 'Test key removal with' 'fingerprint'
  85. cleanplate
  86. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  87. testsuccess --nomsg aptkey --fakeroot del 34A8E9D18DB320F367E8EAA05A90D141DBAC8DAE
  88. testempty aptkey list
  89. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  90. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  91. msgtest 'Test key removal with' 'single key in softlink'
  92. cleanplate
  93. ln -s "$(readlink -f ./keys/joesixpack.pub)" rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  94. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  95. testempty aptkey list
  96. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  97. testsuccess test -L rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  98. cleanplate
  99. testsuccess aptkey --fakeroot add ./keys/joesixpack.pub
  100. ln -sf "$(readlink -f ./keys/marvinparanoid.pub)" "./keys/marvin paránöid.pub"
  101. testsuccess aptkey --fakeroot add "./keys/marvin paránöid.pub"
  102. testaptkeys 'Joe Sixpack' 'Marvin Paranoid'
  103. cp -a rootdir/etc/apt/trusted.gpg keys/testcase-multikey.pub # store for reuse
  104. msgtest 'Test key removal with' 'multi key in real file'
  105. cleanplate
  106. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  107. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  108. testaptkeys 'Marvin Paranoid'
  109. testsuccess cmp keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  110. msgtest 'Test key removal with' 'multi key in softlink'
  111. cleanplate
  112. ln -s "$(readlink -f ./keys/testcase-multikey.pub)" rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  113. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  114. testaptkeys 'Marvin Paranoid'
  115. testsuccess cmp keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  116. testfailure test -L rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  117. testsuccess test -L rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  118. msgtest 'Test key removal with' 'multiple files including key'
  119. cleanplate
  120. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  121. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  122. testsuccess --nomsg aptkey --fakeroot del DBAC8DAE
  123. testaptkeys 'Marvin Paranoid'
  124. testfailure test -e rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  125. testsuccess cmp keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg~
  126. testsuccess cmp keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg~
  127. cleanplate
  128. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  129. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  130. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  131. msgtest 'Test merge-back of' 'added keys'
  132. testsuccess --nomsg aptkey adv --batch --yes --import keys/rexexpired.pub
  133. testaptkeys 'Rex Expired' 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  134. msgtest 'Test merge-back of' 'removed keys'
  135. testsuccess --nomsg aptkey adv --batch --yes --delete-keys 27CE74F9
  136. testaptkeys 'Joe Sixpack' 'Joe Sixpack' 'Marvin Paranoid'
  137. msgtest 'Test merge-back of' 'removed duplicate keys'
  138. testsuccess --nomsg aptkey adv --batch --yes --delete-keys DBAC8DAE
  139. testaptkeys 'Marvin Paranoid'
  140. cleanplate
  141. cp -a keys/joesixpack.pub rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
  142. cp -a keys/testcase-multikey.pub rootdir/etc/apt/trusted.gpg.d/multikey.gpg
  143. msgtest 'Test signing a file' 'with a key'
  144. echo 'Verify me. This is my signature.' > signature
  145. testsuccess --nomsg aptkey --quiet --keyring keys/marvinparanoid.pub --secret-keyring keys/marvinparanoid.sec --readonly \
  146. adv --batch --yes --default-key 'Marvin' --armor --detach-sign --sign --output signature.gpg signature
  147. testsuccess test -s signature.gpg -a -s signature
  148. for GPGV in '' 'gpgv' 'gpgv2'; do
  149. echo "APT::Key::GPGVCommand \"$GPGV\";" > rootdir/etc/apt/apt.conf.d/00gpgvcmd
  150. msgtest 'Test verify a file' 'with all keys'
  151. testsuccess --nomsg aptkey --quiet --readonly verify signature.gpg signature
  152. msgtest 'Test verify a file' 'with good keyring'
  153. testsuccess --nomsg aptkey --quiet --readonly --keyring keys/testcase-multikey.pub verify signature.gpg signature
  154. msgtest 'Test fail verify a file' 'with bad keyring'
  155. testfailure --nomsg aptkey --quiet --readonly --keyring keys/joesixpack.pub verify signature.gpg signature
  156. msgtest 'Test fail verify a file' 'with non-existing keyring'
  157. testfailure --nomsg aptkey --quiet --readonly --keyring keys/does-not-exist.pub verify signature.gpg signature
  158. testfailure test -e keys/does-not-exist.pub
  159. # note: this isn't how apts gpgv method implements keyid for verify
  160. msgtest 'Test verify a file' 'with good keyid'
  161. testsuccess --nomsg aptkey --quiet --readonly --keyid 'Paranoid' verify signature.gpg signature
  162. msgtest 'Test fail verify a file' 'with bad keyid'
  163. testfailure --nomsg aptkey --quiet --readonly --keyid 'Sixpack' verify signature.gpg signature
  164. msgtest 'Test fail verify a file' 'with non-existing keyid'
  165. testfailure --nomsg aptkey --quiet --readonly --keyid 'Kalnischkies' verify signature.gpg signature
  166. msgtest 'Test verify fails on' 'bad file'
  167. echo 'lalalalala' > signature2
  168. testfailure --nomsg aptkey --quiet --readonly verify signature.gpg signature2
  169. done
  170. }
  171. setupgpgcommand() {
  172. echo "APT::Key::GPGCommand \"$1\";" > rootdir/etc/apt/apt.conf.d/00gpgcmd
  173. msgmsg 'Force tests to be run with' "$1"
  174. testsuccess aptkey --readonly adv --version
  175. cp rootdir/tmp/testsuccess.output aptkey.version
  176. testsuccess grep "^gpg (GnuPG) $2\." aptkey.version
  177. }
  178. # run with default (whatever this is)
  179. testrun
  180. # run with …
  181. setupgpgcommand 'gpg' '1'
  182. testrun
  183. setupgpgcommand 'gpg2' '2'
  184. testrun