all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 60f47518f674244360d122cb5f049d964a141692 9091 bytes (raw)
name: test/lisp/net/sieve-manage-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
 
;;; sieve-manage-tests.el --- tests for sieve-manage.el       -*- lexical-binding: t; -*-

;; Copyright (C) 2022 Free Software Foundation, Inc.

;; Author: Kai Tetzlaff <emacs@tetzco.de>

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'ert)
(require 'sieve-manage)

;;; test data

(defvar smt/script-multibyte-unix
  "if body :matches \"ä\" { stop; }\n"
  "Simple multibyte sieve script with unix EOL.")

(defvar smt/script-multibyte-dos
  "if body :matches \"ä\" { stop; }\r\n"
  "Simple multibyte sieve script with dos EOL.")

(defvar smt/script-multibyte-mac
  "if body :matches \"ä\" { stop; }\r"
  "Simple multibyte sieve script with mac EOL.")

(defvar smt/string-empty ""
  "Empty sieve string.")

(defvar smt/string-unibyte "Hello world!"
  "Unibyte sieve string.")

(defvar smt/string-multibyte "äöüßÄÖÜ"
  "Multibyte sieve string.")

(defvar smt/script-name-unibyte "abcdefg.sieve"
  "Unibyte sieve script name.")

(defvar smt/script-name-multibyte "äöüßÄÖÜ.sieve"
  "Multibyte sieve script name.")

;;; helper functions

(defun smt/mk-literal-s2c (string)
  "Encode multibyte STRING to managesieve 'literal-s2c'."
  (sieve-manage-encode (cons 'mb-literal-s2c string)))
;; (smt/mk-literal-s2c smt/string-multibyte)

(defun smt/mk-rsp-oknobye (type &optional resp-code string)
  "Encode TYPE, RESP-CODE and STRING to managesieve 'response-oknobye'."
  (when (memq type '(OK NO BYE))
    (concat
     (string-join
      (delete nil
              (list (symbol-name type)
                    (when resp-code
                      (format "(%s)" resp-code))
                    (when string
                      (format "\"%s\""
                              (sieve-manage--encode-multibyte string)))))
      " ")
     "\r\n")))
;; (smt/mk-rsp-oknobye 'OK nil "Getscript completed.")
;; (smt/mk-rsp-oknobye 'NO "TRYLATER" "Server is busy.")

(defun smt/mk-rsp-getscript-ok (script)
  "Encode SCRIPT to rfc5804 \\='response-getscript\\='."
    (concat (smt/mk-literal-s2c script)
            "\r\n"
            (smt/mk-rsp-oknobye 'OK "Getscript completed.")))
;; (smt/mk-rsp-getscript-ok smt/script-multibyte-unix)

(defun smt/managesieve-getscript (script &optional nocleanup)
  "Simulate rfc5804 response for GETSCRIPT command.
The value of SCRIPT is used as the actual sieve script.
Use for testing `sieve-manage-getscript' function.
If NOCLEANUP is non-nil, keep all created buffers."
  (let* ((script-name "test.sieve")
         ;; `sieve-manage-server' and `sieve-manage-port' are used in
         ;; `sieve-manage-make-process-buffer'
         (sieve-manage-server)
         (sieve-manage-port "sieve")
         ;; sieve-manage process buffer
         (process-buffer (sieve-manage-make-process-buffer))
         ;; sieve-manage buffer to receive downloaded sieve script
         (output-buffer (generate-new-buffer script-name)))
    ;; use cl-letf to mock some functions in call chain of
    ;; sieve-manage-getscript
    (cl-letf (((symbol-function 'sieve-manage-send)
               ;; simulate sieve server getscript response
               ;; containing 'script'
               (lambda (&rest _)
                 (with-current-buffer process-buffer
                   (goto-char (point-min))
                   (insert (smt/mk-rsp-getscript-ok script)))))
              ((symbol-function 'accept-process-output)
               (lambda (&optional _ _ _ _) nil))
              ((symbol-function 'get-buffer-process) (lambda (_) nil)))
      ;; extract sieve script from process-buffer and put it into
      ;; output-buffer
      (sieve-manage-getscript script-name output-buffer process-buffer)
      ;; extract script from output-buffer and return it as a string
      (let ((script (with-current-buffer output-buffer
                      (set-buffer-modified-p nil)
                      (buffer-string)))
              (coding-system
               (buffer-local-value 'buffer-file-coding-system
                                   output-buffer)))
        ;; cleanup
        (unless nocleanup
          (kill-buffer process-buffer)
          (kill-buffer output-buffer)
          (when (get-buffer sieve-manage-log)
            (kill-buffer sieve-manage-log)))
        ;; return (script . coding-system)
        (cons script coding-system)))))

;;; tests

(ert-deftest ert/manage-sieve-mk-quoted ()
  (should-error (sieve-manage-mk-quoted 42)
                :type 'sieve-manage-encode-error)
  (should-error (sieve-manage-mk-quoted nil)
                :type 'sieve-manage-encode-error)
  (should (equal (format "\"%s\"" smt/script-name-unibyte)
                 (sieve-manage-mk-quoted smt/script-name-unibyte)))
  (should (equal "\"\""
                 (sieve-manage-mk-quoted ""))))

(ert-deftest ert/manage-sieve-mk-literal-c2s ()
  (should-error (sieve-manage-mk-literal-c2s 42)
                :type 'sieve-manage-encode-error)
  (should-error (sieve-manage-mk-literal-c2s nil)
                :type 'sieve-manage-encode-error)
  (should (equal (format "{%d+}\r\n%s"
                         (length smt/script-name-unibyte)
                         smt/script-name-unibyte)
                 (sieve-manage-mk-literal-c2s smt/script-name-unibyte)))
  (should (equal "{0+}\r\n"
                 (sieve-manage-mk-literal-c2s ""))))

(ert-deftest ert/manage-sieve-mk-literal-s2c ()
  (should-error (sieve-manage-mk-literal-s2c 42)
                :type 'sieve-manage-encode-error)
  (should-error (sieve-manage-mk-literal-s2c nil)
                :type 'sieve-manage-encode-error)
  (should (equal (format "{%d}\r\n%s"
                         (length smt/script-name-unibyte)
                         smt/script-name-unibyte)
                 (sieve-manage-mk-literal-s2c smt/script-name-unibyte)))
  (should (equal "{0}\r\n"
                 (sieve-manage-mk-literal-s2c ""))))

(ert-deftest ert/sieve-manage-encode ()
  ;; unsupported data types
  (should-error (sieve-manage-encode nil)
                :type 'sieve-manage-encode-error)
  (should-error
   ;; RFC5804 doesn't support negative numbers
   (sieve-manage-encode -1) :type 'sieve-manage-encode-error)

  ;; number [0..4294967296]
  (should (equal (format "%d" 0) (sieve-manage-encode 0)))
  (should (equal (format "%d" 255) (sieve-manage-encode 255)))
  (should (equal (format "%d" 4294967296)
                 (sieve-manage-encode 4294967296)))

  ;; simple string
  (should (equal (format "\"%s\"" smt/script-name-unibyte)
                 (sieve-manage-encode smt/script-name-unibyte)))
  (should (equal (format "\"%s\"" (encode-coding-string
                                   smt/script-name-multibyte
                                   'utf-8-unix))
                 (sieve-manage-encode smt/script-name-multibyte)))

  ;; request explicit quoted encoding
  (should (equal (format "{%d+}\r\n%s"
                         (length smt/script-name-unibyte)
                         smt/script-name-unibyte)
                 (sieve-manage-encode (cons 'literal-c2s smt/script-name-unibyte))))
  (should (equal "{0+}\r\n"
                 (sieve-manage-encode (cons 'literal-c2s ""))))

  ;; request explicit literal-c2s encoding
  (should (equal (format "{%d+}\r\n%s"
                         (length smt/script-name-unibyte)
                         smt/script-name-unibyte)
                 (sieve-manage-encode (cons 'literal-c2s smt/script-name-unibyte))))
  (should (equal "{0+}\r\n"
                 (sieve-manage-encode (cons 'literal-c2s ""))))

  ;; request explicit literal-s2c encoding
  (should (equal (format "{%d}\r\n%s"
                         (length smt/script-name-unibyte)
                         smt/script-name-unibyte)
                 (sieve-manage-encode (cons 'literal-s2c smt/script-name-unibyte))))
  (should (equal "{0}\r\n"
                 (sieve-manage-encode (cons 'literal-s2c ""))))
  )


(ert-deftest ert/managesieve-getscript-multibyte ()
  (let ((ret (smt/managesieve-getscript smt/script-multibyte-unix)))
    (should (string= smt/script-multibyte-unix (car ret)))
    (should (eq 'utf-8-unix (cdr ret))))
  (let ((ret (smt/managesieve-getscript smt/script-multibyte-dos)))
    (should (string= smt/script-multibyte-unix (car ret)))
    (should (eq 'utf-8-dos (cdr ret))))
  (let ((ret (smt/managesieve-getscript smt/script-multibyte-mac)))
    (should (string= smt/script-multibyte-unix (car ret)))
    (should (eq 'utf-8-mac (cdr ret))))
  )

;;; sieve-manage-tests.el ends here

debug log:

solving 60f47518f67 ...
found 60f47518f67 in https://yhetil.org/emacs/878rhut6aq.fsf@tetzco.de/
found 010c9071608 in https://yhetil.org/emacs/87sfg65hwl.fsf@tetzco.de/ ||
	https://yhetil.org/emacs/87mt6buxlb.fsf@tetzco.de/ ||
	https://yhetil.org/emacs/878rhut6aq.fsf@tetzco.de/ ||
	https://yhetil.org/emacs/87pmb966i0.fsf@tetzco.de/ ||
	https://yhetil.org/emacs/871qnlrzka.fsf@tetzco.de/

applying [1/2] https://yhetil.org/emacs/87sfg65hwl.fsf@tetzco.de/
diff --git a/test/lisp/net/sieve-manage-tests.el b/test/lisp/net/sieve-manage-tests.el
new file mode 100644
index 00000000000..010c9071608

Checking patch test/lisp/net/sieve-manage-tests.el...
Applied patch test/lisp/net/sieve-manage-tests.el cleanly.

skipping https://yhetil.org/emacs/87mt6buxlb.fsf@tetzco.de/ for 010c9071608
skipping https://yhetil.org/emacs/878rhut6aq.fsf@tetzco.de/ for 010c9071608
skipping https://yhetil.org/emacs/87pmb966i0.fsf@tetzco.de/ for 010c9071608
skipping https://yhetil.org/emacs/871qnlrzka.fsf@tetzco.de/ for 010c9071608
index at:
100644 010c907160844f7780ac8e57b3ca613f5e1cab12	test/lisp/net/sieve-manage-tests.el

applying [2/2] https://yhetil.org/emacs/878rhut6aq.fsf@tetzco.de/
diff --git a/test/lisp/net/sieve-manage-tests.el b/test/lisp/net/sieve-manage-tests.el
index 010c9071608..60f47518f67 100644

Checking patch test/lisp/net/sieve-manage-tests.el...
Applied patch test/lisp/net/sieve-manage-tests.el cleanly.

index at:
100644 60f47518f674244360d122cb5f049d964a141692	test/lisp/net/sieve-manage-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.