;;; erc-dcc-tests.el --- Tests for erc-dcc -*- lexical-binding:t -*- ;; Copyright (C) 2020-2022 Free Software Foundation, Inc. ;; 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 . ;;; Commentary: ;;; Code: (require 'ert) (require 'erc-dcc) (ert-deftest erc-dcc-ctcp-query-send-regexp () (let ((s "DCC SEND \"file name\" 2130706433 9899 1405135128")) (should (string-match erc-dcc-ctcp-query-send-regexp s)) (should-not (match-string 2 s)) (should (string= "file name" (match-string 1 s))) (should (string= "SEND" (match-string 6 s)))) (let ((s "DCC SEND \"file \\\" name\" 2130706433 9899 1405135128")) (should (string-match erc-dcc-ctcp-query-send-regexp s)) (should-not (match-string 2 s)) (should (string= "SEND" (match-string 6 s))) (should (string= "file \" name" (erc-dcc-unquote-filename (match-string 1 s))))) (let ((s "DCC SEND filename 2130706433 9899 1405135128")) (should (string-match erc-dcc-ctcp-query-send-regexp s)) (should (string= "filename" (match-string 2 s))) (should (string= "2130706433" (match-string 3 s))) (should (string= "9899" (match-string 4 s))) (should (string= "1405135128" (match-string 5 s)))) (let ((s "DCC TSEND filename 2130706433 9899 1405135128")) (should (string-match erc-dcc-ctcp-query-send-regexp s)) (should (string= "TSEND" (match-string 6 s))))) ;; This also indirectly tests base functionality for ;; `erc-dcc-do-LIST-command' (defun erc-dcc-tests--dcc-handle-ctcp-send (turbo) (with-current-buffer (get-buffer-create "fake-server") (erc-mode) (setq erc-server-process (start-process "fake" (current-buffer) "sleep" "1") erc-input-marker (make-marker) erc-insert-marker (make-marker) erc-server-current-nick "dummy") (set-process-query-on-exit-flag erc-server-process nil) (should-not erc-dcc-list) (erc-ctcp-query-DCC erc-server-process "tester" "~tester" "fake.irc" "dummy" (concat "DCC " (if turbo "TSEND" "SEND") " foo 2130706433 9899 1405135128")) (should-not (cdr erc-dcc-list)) (should (equal (plist-put (car erc-dcc-list) :parent 'fake) `(:nick "tester!~tester@fake.irc" :type GET :peer nil :parent fake :ip "127.0.0.1" :port "9899" :file "foo" :size 1405135128 :turbo ,(and turbo t) :secure nil))) (goto-char (point-min)) (should (search-forward "file foo offered by tester" nil t)) (erc-dcc-do-LIST-command erc-server-process) (should (search-forward-regexp (concat "GET +no +1405135128 +foo" (and turbo " +(T)") "$") nil t)) (when noninteractive (let (erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook) (kill-buffer)))) ;; `erc-dcc-list' is global; must leave it empty (should erc-dcc-list) (setq erc-dcc-list nil)) (ert-deftest erc-dcc-handle-ctcp-send--base () (erc-dcc-tests--dcc-handle-ctcp-send nil)) (ert-deftest erc-dcc-handle-ctcp-send--turbo () (erc-dcc-tests--dcc-handle-ctcp-send t)) (ert-deftest erc-dcc-do-GET-command () (with-temp-buffer (let* ((proc (start-process "fake" (current-buffer) "sleep" "1")) erc-accidental-paste-threshold-seconds erc-send-completed-hook erc-kill-channel-hook erc-kill-server-hook erc-kill-buffer-hook (elt `(:nick "tester!~tester@fake.irc" :type GET :peer nil :parent ,proc :ip "127.0.0.1" :port "9899" :file "foo.bin" :size 1405135128)) (erc-dcc-list (list elt)) ;; calls) (erc-mode) (setq erc-server-process proc erc-input-marker (make-marker) erc-insert-marker (make-marker) erc-server-current-nick "dummy") (set-process-query-on-exit-flag proc nil) (cl-letf (((symbol-function 'read-file-name) (lambda (&rest _) "foo.bin")) ((symbol-function 'erc-dcc-get-file) (lambda (&rest r) (push r calls)))) (goto-char (point-max)) (set-marker erc-insert-marker (point-max)) (erc-display-prompt) (ert-info ("No turbo") (should-not (plist-member elt :turbo)) (goto-char erc-input-marker) (insert "/dcc GET tester foo.bin") (erc-send-current-line) (should-not (plist-member (car erc-dcc-list) :turbo)) (should (equal (pop calls) (list elt "foo.bin" proc)))) (ert-info ("Arg turbo in pos 2") (should-not (plist-member elt :turbo)) (goto-char erc-input-marker) (insert "/dcc GET -t tester foo.bin") (erc-send-current-line) (should (eq t (plist-get (car erc-dcc-list) :turbo))) (should (equal (pop calls) (list elt "foo.bin" proc)))) (ert-info ("Arg turbo in pos 4") (setq elt (plist-put elt :turbo nil) erc-dcc-list (list elt)) (goto-char erc-input-marker) (insert "/dcc GET tester -t foo.bin") (erc-send-current-line) (should (eq t (plist-get (car erc-dcc-list) :turbo))) (should (equal (pop calls) (list elt "foo.bin" proc)))) (ert-info ("Arg turbo in pos 6") (setq elt (plist-put elt :turbo nil) erc-dcc-list (list elt)) (goto-char erc-input-marker) (insert "/dcc GET tester foo.bin -t") (erc-send-current-line) (should (eq t (plist-get (car erc-dcc-list) :turbo))) (should (equal (pop calls) (list elt "foo.bin" proc)))))))) ;;; erc-dcc-tests.el ends here