dbus-tests.el 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ;;; dbus-tests.el --- Tests of D-Bus integration into Emacs
  2. ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
  3. ;; Author: Michael Albinus <michael.albinus@gmx.de>
  4. ;; This program is free software: you can redistribute it and/or
  5. ;; modify it under the terms of the GNU General Public License as
  6. ;; published by the Free Software Foundation, either version 3 of the
  7. ;; License, or (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
  16. ;;; Code:
  17. (require 'ert)
  18. (require 'dbus)
  19. (setq dbus-debug nil)
  20. (defvar dbus--test-enabled-session-bus
  21. (and (featurep 'dbusbind)
  22. (dbus-ignore-errors (dbus-get-unique-name :session)))
  23. "Check, whether we are registered at the session bus.")
  24. (defvar dbus--test-enabled-system-bus
  25. (and (featurep 'dbusbind)
  26. (dbus-ignore-errors (dbus-get-unique-name :system)))
  27. "Check, whether we are registered at the system bus.")
  28. (defun dbus--test-availability (bus)
  29. "Test availability of D-Bus BUS."
  30. (should (dbus-list-names bus))
  31. (should (dbus-list-activatable-names bus))
  32. (should (dbus-list-known-names bus))
  33. (should (dbus-get-unique-name bus)))
  34. (ert-deftest dbus-test00-availability-session ()
  35. "Test availability of D-Bus `:session'."
  36. :expected-result (if dbus--test-enabled-session-bus :passed :failed)
  37. (dbus--test-availability :session))
  38. (ert-deftest dbus-test00-availability-system ()
  39. "Test availability of D-Bus `:system'."
  40. :expected-result (if dbus--test-enabled-system-bus :passed :failed)
  41. (dbus--test-availability :system))
  42. (ert-deftest dbus-test01-type-conversion ()
  43. "Check type conversion functions."
  44. (let ((ustr "0123abc_xyz\x01\xff")
  45. (mstr "Grüß Göttin"))
  46. (should
  47. (string-equal
  48. (dbus-byte-array-to-string (dbus-string-to-byte-array "")) ""))
  49. (should
  50. (string-equal
  51. (dbus-byte-array-to-string (dbus-string-to-byte-array ustr)) ustr))
  52. (should
  53. (string-equal
  54. (dbus-byte-array-to-string (dbus-string-to-byte-array mstr) 'multibyte)
  55. mstr))
  56. ;; Should not work for multibyte strings.
  57. (should-not
  58. (string-equal
  59. (dbus-byte-array-to-string (dbus-string-to-byte-array mstr)) mstr))
  60. (should
  61. (string-equal
  62. (dbus-unescape-from-identifier (dbus-escape-as-identifier "")) ""))
  63. (should
  64. (string-equal
  65. (dbus-unescape-from-identifier (dbus-escape-as-identifier ustr)) ustr))
  66. ;; Should not work for multibyte strings.
  67. (should-not
  68. (string-equal
  69. (dbus-unescape-from-identifier (dbus-escape-as-identifier mstr)) mstr))))
  70. (defun dbus--test-register-service (bus)
  71. "Check service registration at BUS."
  72. ;; Cleanup.
  73. (dbus-ignore-errors (dbus-unregister-service bus dbus-service-emacs))
  74. ;; Register an own service.
  75. (should (eq (dbus-register-service bus dbus-service-emacs) :primary-owner))
  76. (should (member dbus-service-emacs (dbus-list-known-names bus)))
  77. (should (eq (dbus-register-service bus dbus-service-emacs) :already-owner))
  78. (should (member dbus-service-emacs (dbus-list-known-names bus)))
  79. ;; Unregister the service.
  80. (should (eq (dbus-unregister-service bus dbus-service-emacs) :released))
  81. (should-not (member dbus-service-emacs (dbus-list-known-names bus)))
  82. (should (eq (dbus-unregister-service bus dbus-service-emacs) :non-existent))
  83. (should-not (member dbus-service-emacs (dbus-list-known-names bus)))
  84. ;; `dbus-service-dbus' is reserved for the BUS itself.
  85. (should-error (dbus-register-service bus dbus-service-dbus))
  86. (should-error (dbus-unregister-service bus dbus-service-dbus)))
  87. (ert-deftest dbus-test02-register-service-session ()
  88. "Check service registration at `:session' bus."
  89. (skip-unless (and dbus--test-enabled-session-bus
  90. (dbus-register-service :session dbus-service-emacs)))
  91. (dbus--test-register-service :session)
  92. (let ((service "org.freedesktop.Notifications"))
  93. (when (member service (dbus-list-known-names :session))
  94. ;; Cleanup.
  95. (dbus-ignore-errors (dbus-unregister-service :session service))
  96. (should (eq (dbus-register-service :session service) :in-queue))
  97. (should (eq (dbus-unregister-service :session service) :released))
  98. (should
  99. (eq (dbus-register-service :session service :do-not-queue) :exists))
  100. (should (eq (dbus-unregister-service :session service) :not-owner)))))
  101. (ert-deftest dbus-test02-register-service-system ()
  102. "Check service registration at `:system' bus."
  103. (skip-unless (and dbus--test-enabled-system-bus
  104. (dbus-register-service :system dbus-service-emacs)))
  105. (dbus--test-register-service :system))
  106. (ert-deftest dbus-test02-register-service-own-bus ()
  107. "Check service registration with an own bus.
  108. This includes initialization and closing the bus."
  109. ;; Start bus.
  110. (let ((output
  111. (ignore-errors
  112. (shell-command-to-string "dbus-launch --sh-syntax")))
  113. bus pid)
  114. (skip-unless (stringp output))
  115. (when (string-match "DBUS_SESSION_BUS_ADDRESS='\\(.+\\)';" output)
  116. (setq bus (match-string 1 output)))
  117. (when (string-match "DBUS_SESSION_BUS_PID=\\([[:digit:]]+\\);" output)
  118. (setq pid (match-string 1 output)))
  119. (unwind-protect
  120. (progn
  121. (skip-unless
  122. (dbus-ignore-errors
  123. (and bus pid
  124. (featurep 'dbusbind)
  125. (dbus-init-bus bus)
  126. (dbus-get-unique-name bus)
  127. (dbus-register-service bus dbus-service-emacs))))
  128. ;; Run the test.
  129. (dbus--test-register-service bus))
  130. ;; Save exit.
  131. (when pid (call-process "kill" nil nil nil pid)))))
  132. (ert-deftest dbus-test03-peer-interface ()
  133. "Check `dbus-interface-peer' methods."
  134. (skip-unless
  135. (and dbus--test-enabled-session-bus
  136. (dbus-register-service :session dbus-service-emacs)
  137. ;; "GetMachineId" is not implemented (yet). When it returns a
  138. ;; value, another D-Bus client like dbus-monitor is reacting
  139. ;; on `dbus-interface-peer'. We cannot test then.
  140. (not
  141. (dbus-ignore-errors
  142. (dbus-call-method
  143. :session dbus-service-emacs dbus-path-dbus
  144. dbus-interface-peer "GetMachineId" :timeout 100)))))
  145. (should (dbus-ping :session dbus-service-emacs 100))
  146. (dbus-unregister-service :session dbus-service-emacs)
  147. (should-not (dbus-ping :session dbus-service-emacs 100)))
  148. (defun dbus-test-all (&optional interactive)
  149. "Run all tests for \\[dbus]."
  150. (interactive "p")
  151. (funcall
  152. (if interactive 'ert-run-tests-interactively 'ert-run-tests-batch) "^dbus"))
  153. (provide 'dbus-tests)
  154. ;;; dbus-tests.el ends here