tcover-unsafep.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ;;;; testcover-unsafep.el -- Use testcover to test unsafep's code coverage
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Jonathan Yavner <jyavner@engineer.com>
  4. ;; Maintainer: Jonathan Yavner <jyavner@engineer.com>
  5. ;; Keywords: safety lisp utility
  6. ;; Package: testcover
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. (require 'testcover)
  19. (defvar safe-functions)
  20. ;;;These forms are all considered safe
  21. (defconst testcover-unsafep-safe
  22. '(((lambda (x) (* x 2)) 14)
  23. (apply 'cdr (mapcar (lambda (x) (car x)) y))
  24. (cond ((= x 4) 5) (t 27))
  25. (condition-case x (car y) (error (car x)))
  26. (dolist (x y) (message "here: %s" x))
  27. (dotimes (x 14 (* x 2)) (message "here: %d" x))
  28. (let (x) (dolist (y '(1 2 3) (1+ y)) (push y x)))
  29. (let (x) (apply (lambda (x) (* x 2)) 14))
  30. (let ((x '(2))) (push 1 x) (pop x) (add-to-list 'x 2))
  31. (let ((x 1) (y 2)) (setq x (+ x y)))
  32. (let ((x 1)) (let ((y (+ x 3))) (* x y)))
  33. (let* nil (current-time))
  34. (let* ((x 1) (y (+ x 3))) (* x y))
  35. (mapcar (lambda (x &optional y &rest z) (setq y (+ x 2)) (* y 3)) '(1 2 3))
  36. (mapconcat #'(lambda (var) (propertize var 'face 'bold)) '("1" "2") ", ")
  37. (setq buffer-display-count 14 mark-active t)
  38. ;;This is not safe if you insert it into a buffer!
  39. (propertize "x" 'display '(height (progn (delete-file "x") 1))))
  40. "List of forms that `unsafep' should decide are safe.")
  41. ;;;These forms are considered unsafe
  42. (defconst testcover-unsafep-unsafe
  43. '(( (add-to-list x y)
  44. . (unquoted x))
  45. ( (add-to-list y x)
  46. . (unquoted y))
  47. ( (add-to-list 'y x)
  48. . (global-variable y))
  49. ( (not (delete-file "unsafep.el"))
  50. . (function delete-file))
  51. ( (cond (t (aset local-abbrev-table 0 0)))
  52. . (function aset))
  53. ( (cond (t (setq unsafep-vars "")))
  54. . (risky-local-variable unsafep-vars))
  55. ( (condition-case format-alist 1)
  56. . (risky-local-variable format-alist))
  57. ( (condition-case x 1 (error (setq format-alist "")))
  58. . (risky-local-variable format-alist))
  59. ( (dolist (x (sort globalvar 'car)) (princ x))
  60. . (function sort))
  61. ( (dotimes (x 14) (delete-file "x"))
  62. . (function delete-file))
  63. ( (let ((post-command-hook "/tmp/")) 1)
  64. . (risky-local-variable post-command-hook))
  65. ( (let ((x (delete-file "x"))) 2)
  66. . (function delete-file))
  67. ( (let (x) (add-to-list 'x (delete-file "x")))
  68. . (function delete-file))
  69. ( (let (x) (condition-case y (setq x 1 z 2)))
  70. . (global-variable z))
  71. ( (let (x) (condition-case z 1 (error (delete-file "x"))))
  72. . (function delete-file))
  73. ( (let (x) (mapc (lambda (x) (setcar x 1)) '((1 . 2) (3 . 4))))
  74. . (function setcar))
  75. ( (let (y) (push (delete-file "x") y))
  76. . (function delete-file))
  77. ( (let* ((x 1)) (setq y 14))
  78. . (global-variable y))
  79. ( (mapc 'car (list '(1 . 2) (cons 3 4) (kill-buffer "unsafep.el")))
  80. . (function kill-buffer))
  81. ( (mapcar x y)
  82. . (unquoted x))
  83. ( (mapcar (lambda (x) (rename-file x "x")) '("unsafep.el"))
  84. . (function rename-file))
  85. ( (mapconcat x1 x2 " ")
  86. . (unquoted x1))
  87. ( (pop format-alist)
  88. . (risky-local-variable format-alist))
  89. ( (push 1 format-alist)
  90. . (risky-local-variable format-alist))
  91. ( (setq buffer-display-count (delete-file "x"))
  92. . (function delete-file))
  93. ;;These are actually safe (they signal errors)
  94. ( (apply '(x) '(1 2 3))
  95. . (function (x)))
  96. ( (let (((x))) 1)
  97. . (variable (x)))
  98. ( (let (1) 2)
  99. . (variable 1))
  100. )
  101. "A-list of (FORM . REASON)... that`unsafep' should decide are unsafe.")
  102. (declare-function unsafep-function "unsafep" (fun))
  103. ;;;#########################################################################
  104. (defun testcover-unsafep ()
  105. "Executes all unsafep tests and displays the coverage results."
  106. (interactive)
  107. (testcover-unmark-all "unsafep.el")
  108. (testcover-start "unsafep.el")
  109. (let (save-functions)
  110. (dolist (x testcover-unsafep-safe)
  111. (if (unsafep x)
  112. (error "%S should be safe" x)))
  113. (dolist (x testcover-unsafep-unsafe)
  114. (if (not (equal (unsafep (car x)) (cdr x)))
  115. (error "%S should be unsafe: %s" (car x) (cdr x))))
  116. (setq safe-functions t)
  117. (if (or (unsafep '(delete-file "x"))
  118. (unsafep-function 'delete-file))
  119. (error "safe-functions=t should allow delete-file"))
  120. (setq safe-functions '(setcar))
  121. (if (unsafep '(setcar x 1))
  122. (error "safe-functions=(setcar) should allow setcar"))
  123. (if (not (unsafep '(setcdr x 1)))
  124. (error "safe-functions=(setcar) should not allow setcdr")))
  125. (testcover-mark-all "unsafep.el")
  126. (testcover-end "unsafep.el")
  127. (message "Done"))
  128. ;; testcover-unsafep.el ends here.