test_libgpgme.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import os
  2. import infra.basetest
  3. class TestLibGpgme(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_PACKAGE_LIBGPGME=y
  7. BR2_TARGET_ROOTFS_CPIO=y
  8. # BR2_TARGET_ROOTFS_TAR is not set
  9. """
  10. def test_run(self):
  11. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  12. self.emulator.boot(arch="armv5",
  13. kernel="builtin",
  14. options=["-initrd", cpio_file])
  15. self.emulator.login()
  16. # We check the binary program can execute.
  17. self.assertRunOk("gpgme-tool --version")
  18. # Some common data for all the tests.
  19. plain_data = "Hello Buildroot!"
  20. gpg_userid = "br-test@buildroot"
  21. plain_file = "reference-plain.txt"
  22. enc_file = "encrypted.dat"
  23. dec_file = "decrypted.txt"
  24. # We did not create a gpg key yet. We should not be able to
  25. # list our key.
  26. gpgme_listkey = f"echo LISTKEYS | gpgme-tool | grep '{gpg_userid}'"
  27. _, exit_code = self.emulator.run(gpgme_listkey)
  28. self.assertNotEqual(exit_code, 0)
  29. # We now create our gpg key.
  30. cmd = "gpg --batch --passphrase ''"
  31. cmd += f" --quick-generate-key {gpg_userid} default default"
  32. self.assertRunOk(cmd, timeout=30)
  33. # We should now see our key in the list.
  34. self.assertRunOk(gpgme_listkey)
  35. # We generate a plain text data file.
  36. cmd = f"echo '{plain_data}' > {plain_file}"
  37. self.assertRunOk(cmd)
  38. # We encrypt the plain text file using gpgme-tool commands.
  39. gpgme_enc_cmds = [
  40. "RESET",
  41. f"INPUT FILE={plain_file}",
  42. f"OUTPUT FILE={enc_file}",
  43. f"RECIPIENT {gpg_userid}",
  44. "ENCRYPT",
  45. "BYE"
  46. ]
  47. cmd = "gpgme-tool <<EOF\n"
  48. cmd += "\n".join(gpgme_enc_cmds)
  49. cmd += "\nEOF"
  50. self.assertRunOk(cmd)
  51. # The output encrypted file is supposed to exist and to have a
  52. # size greater than zero.
  53. self.assertRunOk(f"test -s {enc_file}")
  54. # The output encrypted file is also expected to be different
  55. # from the input plain text file.
  56. _, exit_code = self.emulator.run(f"cmp {plain_file} {enc_file}")
  57. self.assertNotEqual(exit_code, 0)
  58. # We now decrypt the encrypted file using gpgme-tool commands.
  59. gpgme_dec_cmds = [
  60. "RESET",
  61. f"INPUT FILE={enc_file}",
  62. f"OUTPUT FILE={dec_file}",
  63. "DECRYPT",
  64. "BYE"
  65. ]
  66. cmd = "gpgme-tool <<EOF\n"
  67. cmd += "\n".join(gpgme_dec_cmds)
  68. cmd += "\nEOF"
  69. self.assertRunOk(cmd)
  70. # The decrypted file is supposed to be the same as the initial
  71. # plain text file.
  72. cmd = f"cmp {plain_file} {dec_file}"
  73. self.assertRunOk(cmd)