sandbox.test 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ;;;; sandbox.test --- tests guile's evaluator -*- scheme -*-
  2. ;;;; Copyright (C) 2017 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (define-module (test-suite sandbox)
  18. #:use-module (test-suite lib)
  19. #:use-module (ice-9 sandbox))
  20. (define exception:bad-expression
  21. (cons 'syntax-error "Bad expression"))
  22. (define exception:failed-match
  23. (cons 'syntax-error "failed to match any pattern"))
  24. (define exception:not-a-list
  25. (cons 'wrong-type-arg "Not a list"))
  26. (define exception:wrong-length
  27. (cons 'wrong-type-arg "wrong length"))
  28. (define (usleep-loop usecs)
  29. (unless (zero? usecs)
  30. (usleep-loop (usleep usecs))))
  31. (define (busy-loop)
  32. (busy-loop))
  33. (with-test-prefix "time limit"
  34. (pass-if "0 busy loop"
  35. (call-with-time-limit 0 busy-loop (lambda () #t)))
  36. (pass-if "0.001 busy loop"
  37. (call-with-time-limit 0.001 busy-loop (lambda () #t)))
  38. (pass-if "0 sleep"
  39. (call-with-time-limit 0 (lambda () (usleep-loop #e1e6) #f)
  40. (lambda () #t)))
  41. (pass-if "0.001 sleep"
  42. (call-with-time-limit 0.001 (lambda () (usleep-loop #e1e6) #f)
  43. (lambda () #t))))
  44. (define (alloc-loop)
  45. (let lp ((ret #t))
  46. (and ret
  47. (lp (cons #t #t)))))
  48. (define (recur-loop)
  49. (1+ (recur-loop)))
  50. (with-test-prefix "allocation limit"
  51. (pass-if "0 alloc loop"
  52. (call-with-allocation-limit 0 alloc-loop (lambda () #t)))
  53. (pass-if "1e6 alloc loop"
  54. (call-with-allocation-limit #e1e6 alloc-loop (lambda () #t)))
  55. (pass-if "0 recurse"
  56. (call-with-allocation-limit 0 recur-loop (lambda () #t)))
  57. (pass-if "1e6 recurse"
  58. (call-with-allocation-limit #e1e6 recur-loop (lambda () #t))))
  59. (define-syntax-rule (pass-if-unbound foo)
  60. (pass-if-exception (format #f "~a unavailable" 'foo)
  61. exception:unbound-var (eval-in-sandbox 'foo))
  62. )
  63. (with-test-prefix "eval-in-sandbox"
  64. (pass-if-equal 42
  65. (eval-in-sandbox 42))
  66. (pass-if-equal 'foo
  67. (eval-in-sandbox ''foo))
  68. (pass-if-equal '(1 . 2)
  69. (eval-in-sandbox '(cons 1 2)))
  70. (pass-if-unbound @@)
  71. (pass-if-unbound foo)
  72. (pass-if-unbound set!)
  73. (pass-if-unbound open-file)
  74. (pass-if-unbound current-input-port)
  75. (pass-if-unbound call-with-output-file)
  76. (pass-if-unbound vector-set!)
  77. (pass-if-equal vector-set!
  78. (eval-in-sandbox 'vector-set!
  79. #:bindings all-pure-and-impure-bindings))
  80. (pass-if-exception "limit exceeded"
  81. '(limit-exceeded . "")
  82. (eval-in-sandbox '(let lp () (lp)))))