aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorF. Jason Park2022-12-29 06:43:19 -0800
committerF. Jason Park2023-04-08 14:23:51 -0700
commit0f7fc5cfdf97a8280ea8f012e50af9e615e8c6ef (patch)
tree27b9b6f57a917207c20a7051a71baf856d611362 /test
parent8dd209eea47f3b8e1fce6dc12c13d33da1154d89 (diff)
downloademacs-0f7fc5cfdf97a8280ea8f012e50af9e615e8c6ef.tar.gz
emacs-0f7fc5cfdf97a8280ea8f012e50af9e615e8c6ef.zip
Be smarter about switching to TLS from M-x erc
* lisp/erc/erc.el (erc--warn-unencrypted): Remove unused internal function. (erc-select-read-args): Offer to use TLS when user runs M-x erc and opts for default server and port or provides the well-known IANA TLS port or enters an ircs:// URL at the server prompt. For the last two, do this immediately instead of calling `erc-tls' interactively and imposing a review of just-chosen values. Also remove error warnings and ensure `erc-tls' still works by setting `erc-server-connect-function' to `erc-open-tls-stream' when appropriate. Include the word "URL" in server prompt. (erc--with-entrypoint-environment): Add new macro for empowering an entry point's interactive form to bind special variables in their command's body without shadowing them in the lambda list. (erc, erc-tls): Add internal keyword argument for interactive use, but don't make it `keywordp' or advertise its presence. Also use new helper macro, `erc--with-entrypoint-environment', to temporarily bind special vars given by interactive helper `erc-select-read-args'. * test/lisp/erc/erc-tests.el (erc--with-entrypoint-environment): Add new test. (erc-select-read-args): Modify return values to expect additional internal keyword argument where appropriate. (erc-tls): Make assertions about environment. (erc--interactive): New test. (Bug#60428.)
Diffstat (limited to 'test')
-rw-r--r--test/lisp/erc/erc-tests.el159
1 files changed, 136 insertions, 23 deletions
diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el
index ae19b7d0aad..c5905ab4f67 100644
--- a/test/lisp/erc/erc-tests.el
+++ b/test/lisp/erc/erc-tests.el
@@ -1064,32 +1064,62 @@
1064 (should (string-match erc--server-connect-dumb-ipv6-regexp 1064 (should (string-match erc--server-connect-dumb-ipv6-regexp
1065 (concat "[" a "]"))))) 1065 (concat "[" a "]")))))
1066 1066
1067(ert-deftest erc--with-entrypoint-environment ()
1068 (let ((env '((erc-join-buffer . foo)
1069 (erc-server-connect-function . bar))))
1070 (erc--with-entrypoint-environment env
1071 (should (eq erc-join-buffer 'foo))
1072 (should (eq erc-server-connect-function 'bar)))))
1073
1067(ert-deftest erc-select-read-args () 1074(ert-deftest erc-select-read-args ()
1068 1075
1069 (ert-info ("Does not default to TLS") 1076 (ert-info ("Prompts for switch to TLS by default")
1070 (should (equal (ert-simulate-keys "\r\r\r\r" 1077 (should (equal (ert-simulate-keys "\r\r\r\ry\r"
1071 (erc-select-read-args)) 1078 (erc-select-read-args))
1072 (list :server "irc.libera.chat" 1079 (list :server "irc.libera.chat"
1073 :port 6667 1080 :port 6697
1081 :nick (user-login-name)
1082 '&interactive-env '((erc-server-connect-function
1083 . erc-open-tls-stream))))))
1084
1085 (ert-info ("Switches to TLS when port matches default TLS port")
1086 (should (equal (ert-simulate-keys "irc.gnu.org\r6697\r\r\r"
1087 (erc-select-read-args))
1088 (list :server "irc.gnu.org"
1089 :port 6697
1090 :nick (user-login-name)
1091 '&interactive-env '((erc-server-connect-function
1092 . erc-open-tls-stream))))))
1093
1094 (ert-info ("Switches to TLS when URL is ircs://")
1095 (should (equal (ert-simulate-keys "ircs://irc.gnu.org\r\r\r\r"
1096 (erc-select-read-args))
1097 (list :server "irc.gnu.org"
1098 :port 6697
1074 :nick (user-login-name) 1099 :nick (user-login-name)
1075 :password nil)))) 1100 '&interactive-env '((erc-server-connect-function
1101 . erc-open-tls-stream))))))
1102
1103 (ert-info ("Opt out of non-TLS warning manually")
1104 (should (equal (ert-simulate-keys "\r\r\r\rn\r"
1105 (erc-select-read-args))
1106 (list :server "irc.libera.chat"
1107 :port 6667
1108 :nick (user-login-name)))))
1076 1109
1077 (ert-info ("Override default TLS") 1110 (ert-info ("Override default TLS")
1078 (should (equal (ert-simulate-keys "irc://irc.libera.chat\r\r\r\r" 1111 (should (equal (ert-simulate-keys "irc://irc.libera.chat\r\r\r\r"
1079 (erc-select-read-args)) 1112 (erc-select-read-args))
1080 (list :server "irc.libera.chat" 1113 (list :server "irc.libera.chat"
1081 :port 6667 1114 :port 6667
1082 :nick (user-login-name) 1115 :nick (user-login-name)))))
1083 :password nil))))
1084 1116
1085 (ert-info ("Address includes port") 1117 (ert-info ("Address includes port")
1086 (should (equal (ert-simulate-keys 1118 (should (equal (ert-simulate-keys "localhost:6667\rnick\r\r"
1087 "localhost:6667\rnick\r\r"
1088 (erc-select-read-args)) 1119 (erc-select-read-args))
1089 (list :server "localhost" 1120 (list :server "localhost"
1090 :port 6667 1121 :port 6667
1091 :nick "nick" 1122 :nick "nick"))))
1092 :password nil))))
1093 1123
1094 (ert-info ("Address includes nick, password skipped via option") 1124 (ert-info ("Address includes nick, password skipped via option")
1095 (should (equal (ert-simulate-keys "nick@localhost:6667\r" 1125 (should (equal (ert-simulate-keys "nick@localhost:6667\r"
@@ -1097,8 +1127,7 @@
1097 (erc-select-read-args))) 1127 (erc-select-read-args)))
1098 (list :server "localhost" 1128 (list :server "localhost"
1099 :port 6667 1129 :port 6667
1100 :nick "nick" 1130 :nick "nick"))))
1101 :password nil))))
1102 1131
1103 (ert-info ("Address includes nick and password") 1132 (ert-info ("Address includes nick and password")
1104 (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r\r" 1133 (should (equal (ert-simulate-keys "nick:sesame@localhost:6667\r\r"
@@ -1113,37 +1142,40 @@
1113 (erc-select-read-args)) 1142 (erc-select-read-args))
1114 (list :server "[::1]" 1143 (list :server "[::1]"
1115 :port 6667 1144 :port 6667
1116 :nick (user-login-name) 1145 :nick (user-login-name)))))
1117 :password nil))))
1118 1146
1119 (ert-info ("IPv6 address with port") 1147 (ert-info ("IPv6 address with port")
1120 (should (equal (ert-simulate-keys "[::1]:6667\r\r\r" 1148 (should (equal (ert-simulate-keys "[::1]:6667\r\r\r"
1121 (erc-select-read-args)) 1149 (erc-select-read-args))
1122 (list :server "[::1]" 1150 (list :server "[::1]"
1123 :port 6667 1151 :port 6667
1124 :nick (user-login-name) 1152 :nick (user-login-name)))))
1125 :password nil))))
1126 1153
1127 (ert-info ("IPv6 address includes nick") 1154 (ert-info ("IPv6 address includes nick")
1128 (should (equal (ert-simulate-keys "nick@[::1]:6667\r\r" 1155 (should (equal (ert-simulate-keys "nick@[::1]:6667\r\r"
1129 (erc-select-read-args)) 1156 (erc-select-read-args))
1130 (list :server "[::1]" 1157 (list :server "[::1]"
1131 :port 6667 1158 :port 6667
1132 :nick "nick" 1159 :nick "nick")))))
1133 :password nil)))))
1134 1160
1135(ert-deftest erc-tls () 1161(ert-deftest erc-tls ()
1136 (let (calls) 1162 (let (calls env)
1137 (cl-letf (((symbol-function 'user-login-name) 1163 (cl-letf (((symbol-function 'user-login-name)
1138 (lambda (&optional _) "tester")) 1164 (lambda (&optional _) "tester"))
1139 ((symbol-function 'erc-open) 1165 ((symbol-function 'erc-open)
1140 (lambda (&rest r) (push r calls)))) 1166 (lambda (&rest r)
1167 (push `((erc-server-connect-function
1168 ,erc-server-connect-function))
1169 env)
1170 (push r calls))))
1141 1171
1142 (ert-info ("Defaults") 1172 (ert-info ("Defaults")
1143 (erc-tls) 1173 (erc-tls)
1144 (should (equal (pop calls) 1174 (should (equal (pop calls)
1145 '("irc.libera.chat" 6697 "tester" "unknown" t 1175 '("irc.libera.chat" 6697 "tester" "unknown" t
1146 nil nil nil nil nil "user" nil)))) 1176 nil nil nil nil nil "user" nil)))
1177 (should (equal (pop env)
1178 '((erc-server-connect-function erc-open-tls-stream)))))
1147 1179
1148 (ert-info ("Full") 1180 (ert-info ("Full")
1149 (erc-tls :server "irc.gnu.org" 1181 (erc-tls :server "irc.gnu.org"
@@ -1156,7 +1188,9 @@
1156 :id 'GNU.org) 1188 :id 'GNU.org)
1157 (should (equal (pop calls) 1189 (should (equal (pop calls)
1158 '("irc.gnu.org" 7000 "bob" "Bob's Name" t 1190 '("irc.gnu.org" 7000 "bob" "Bob's Name" t
1159 "bob:changeme" nil nil nil t "bobo" GNU.org)))) 1191 "bob:changeme" nil nil nil t "bobo" GNU.org)))
1192 (should (equal (pop env)
1193 '((erc-server-connect-function erc-open-tls-stream)))))
1160 1194
1161 ;; Values are often nil when called by lisp code, which leads to 1195 ;; Values are often nil when called by lisp code, which leads to
1162 ;; null params. This is why `erc-open' recomputes almost 1196 ;; null params. This is why `erc-open' recomputes almost
@@ -1172,7 +1206,86 @@
1172 :password "bob:changeme")) 1206 :password "bob:changeme"))
1173 (should (equal (pop calls) 1207 (should (equal (pop calls)
1174 '(nil 7000 nil "Bob's Name" t 1208 '(nil 7000 nil "Bob's Name" t
1175 "bob:changeme" nil nil nil nil "bobo" nil))))))) 1209 "bob:changeme" nil nil nil nil "bobo" nil)))
1210 (should (equal (pop env)
1211 '((erc-server-connect-function erc-open-tls-stream)))))
1212
1213 (ert-info ("Interactive")
1214 (ert-simulate-keys "nick:sesame@localhost:6667\r\r"
1215 (call-interactively #'erc-tls))
1216 (should (equal (pop calls)
1217 '("localhost" 6667 "nick" "unknown" t "sesame"
1218 nil nil nil nil "user" nil)))
1219 (should (equal (pop env)
1220 '((erc-server-connect-function
1221 erc-open-tls-stream)))))
1222
1223 (ert-info ("Custom connect function")
1224 (let ((erc-server-connect-function 'my-connect-func))
1225 (erc-tls)
1226 (should (equal (pop calls)
1227 '("irc.libera.chat" 6697 "tester" "unknown" t
1228 nil nil nil nil nil "user" nil)))
1229 (should (equal (pop env)
1230 '((erc-server-connect-function my-connect-func))))))
1231
1232 (ert-info ("Advised default function overlooked") ; intentional
1233 (advice-add 'erc-server-connect-function :around #'ignore
1234 '((name . erc-tests--erc-tls)))
1235 (erc-tls)
1236 (should (equal (pop calls)
1237 '("irc.libera.chat" 6697 "tester" "unknown" t
1238 nil nil nil nil nil "user" nil)))
1239 (should (equal (pop env)
1240 '((erc-server-connect-function erc-open-tls-stream))))
1241 (advice-remove 'erc-server-connect-function 'erc-tests--erc-tls))
1242
1243 (ert-info ("Advised non-default function honored")
1244 (let ((f (lambda (&rest r) (ignore r))))
1245 (cl-letf (((symbol-value 'erc-server-connect-function) f))
1246 (advice-add 'erc-server-connect-function :around #'ignore
1247 '((name . erc-tests--erc-tls)))
1248 (erc-tls)
1249 (should (equal (pop calls)
1250 '("irc.libera.chat" 6697 "tester" "unknown" t
1251 nil nil nil nil nil "user" nil)))
1252 (should (equal (pop env) `((erc-server-connect-function ,f))))
1253 (advice-remove 'erc-server-connect-function
1254 'erc-tests--erc-tls)))))))
1255
1256;; See `erc-select-read-args' above for argument parsing.
1257;; This only tests the "hidden" arguments.
1258
1259(ert-deftest erc--interactive ()
1260 (let (calls env)
1261 (cl-letf (((symbol-function 'user-login-name)
1262 (lambda (&optional _) "tester"))
1263 ((symbol-function 'erc-open)
1264 (lambda (&rest r)
1265 (push `((erc-server-connect-function
1266 ,erc-server-connect-function))
1267 env)
1268 (push r calls))))
1269
1270 (ert-info ("Default click-through accept TLS upgrade")
1271 (ert-simulate-keys "\r\r\r\ry\r"
1272 (call-interactively #'erc))
1273 (should (equal (pop calls)
1274 '("irc.libera.chat" 6697 "tester" "unknown" t nil
1275 nil nil nil nil "user" nil)))
1276 (should (equal (pop env)
1277 '((erc-server-connect-function erc-open-tls-stream)))))
1278
1279 (ert-info ("Nick supplied, decline TLS upgrade")
1280 (ert-simulate-keys "\r\rdummy\r\rn\r"
1281 (call-interactively #'erc))
1282 (should (equal (pop calls)
1283 '("irc.libera.chat" 6667 "dummy" "unknown" t nil
1284 nil nil nil nil "user" nil)))
1285 (should (equal (pop env)
1286 '(
1287 (erc-server-connect-function
1288 erc-open-network-stream))))))))
1176 1289
1177(defun erc-tests--make-server-buf (name) 1290(defun erc-tests--make-server-buf (name)
1178 (with-current-buffer (get-buffer-create name) 1291 (with-current-buffer (get-buffer-create name)