1 # Copyright (c) 2004-2018 Jeremy Stanley <fungi@yuggoth.org>. Permission
2 # to use, copy, modify, and distribute this software is granted under
3 # terms provided in the LICENSE file distributed with this software.
14 pidfile = "var/mudpy.pid"
16 test_account0_setup = (
17 (0, "Identify yourself:", "luser0"),
18 (0, "Enter your choice:", "n"),
19 (0, 'Enter a new password for "luser0":', "Test123"),
20 (0, "Enter the same new password again:", "Test123"),
21 (0, r"What would you like to do\?", "c"),
22 (0, "Pick a birth gender for your new avatar:", "f"),
23 (0, "Choose a name for her:", "1"),
24 (0, "What would you like to do?", "a"),
25 (0, "Whom would you like to awaken?", ""),
28 test_account1_setup = (
29 (1, "Identify yourself:", "luser1"),
30 (1, "Enter your choice:", "n"),
31 (1, 'Enter a new password for "luser1":', "Test456"),
32 (1, "Enter the same new password again:", "Test456"),
33 (1, r"What would you like to do\?", "c"),
34 (1, "Pick a birth gender for your new avatar:", "m"),
35 (1, "Choose a name for him:", "1"),
36 (1, "What would you like to do?", "a"),
37 (1, "Whom would you like to awaken?", ""),
40 test_actor_appears = (
41 (0, r"You suddenly realize that .* is here\.", ""),
44 test_explicit_punctuation = (
45 (0, "> ", "say Hello there!"),
46 (0, r'You exclaim, "Hello there\!"', ""),
47 (1, r'exclaims, "Hello there\!"', "say And you are?"),
48 (1, r'You ask, "And you are\?"', ""),
49 (0, r'asks, "And you are\?"', "say I'm me, of course."),
50 (0, r'''You say, "I'm me, of course\."''', ""),
51 (1, r'''says, "I'm me, of course\."''', "say I wouldn't be so sure..."),
52 (1, r'''You muse, "I wouldn't be so sure\.\.\."''', ""),
53 (0, r'''muses, "I wouldn't be so sure\.\.\."''', "say You mean,"),
54 (0, 'You begin, "You mean,"', ""),
55 (1, 'begins, "You mean,"', "say I know-"),
56 (1, 'You begin, "I know-"', ""),
57 (0, 'begins, "I know-"', "say Don't interrupt:"),
58 (0, r'''You begin, "Don't interrupt:"''', ""),
59 (1, r'''begins, "Don't interrupt:"''', "say I wasn't interrupting;"),
60 (1, r'''You begin, "I wasn't interrupting;"''', ""),
61 (0, r'''begins, "I wasn't interrupting;"''', ""),
64 test_implicit_punctuation = (
65 (0, '> ', "say Whatever"),
66 (0, r'You say, "Whatever\."', ""),
67 (1, r'says, "Whatever\."', ""),
70 test_typo_replacement = (
71 (1, '> ', "say That's what i think."),
72 (1, r'''You say, "That's what I think\."''', ""),
73 (0, r'''says, "That's what I think\."''', "say You know what i'd like."),
74 (0, r'''You say, "You know what I'd like\."''', ""),
75 (1, r'''says, "You know what I'd like\."''', "say Then i'll tell you."),
76 (1, r'''You say, "Then I'll tell you\."''', ""),
77 (0, r'''says, "Then I'll tell you\."''', "say Now i'm ready."),
78 (0, r'''You say, "Now I'm ready\."''', ""),
79 (1, r'''says, "Now I'm ready\."''', "say That's teh idea."),
80 (1, r'''You say, "That's the idea\."''', ""),
81 (0, r'''says, "That's the idea\."''', "say It's what theyre saying."),
82 (0, r'''You say, "It's what they're saying\."''', ""),
83 (1, r'''says, "It's what they're saying\."''', "say Well, youre right."),
84 (1, r'''You say, "Well, you're right\."''', ""),
85 (0, r'''says, "Well, you're right\."''', ""),
88 test_sentence_capitalization = (
89 (0, "> ", "say this sentence should start with a capital T."),
90 (0, 'You say, "This sentence', ""),
91 (1, 'says, "This sentence', ""),
96 (1, r'(?s)Entering chat mode .*> \(chat\) ', "Feeling chatty."),
97 (1, r'You say, "Feeling chatty\."', "!chat"),
98 (0, r'says, "Feeling chatty\."', ""),
99 (1, '> ', "say Now less chatty."),
100 (1, r'You say, "Now less chatty\."', ""),
101 (0, r'says, "Now less chatty\."', ""),
105 (0, '> ', "say " + 100 * "o"),
106 (1, r'says,\r\n"O[o]+\."', ""),
109 test_forbid_ansi_input = (
110 (0, '> ', "say \x1b[35mfoo\x1b[0m"),
111 (1, r'says, "\[35mfoo\[0m\."', ""),
115 (0, "> ", "move north"),
116 (0, r"You exit to the north\.", ""),
117 (1, r"exits to the north\.", "move north"),
118 (0, r"arrives from the south\.", "move south"),
119 (0, r"You exit to the south\.", ""),
120 (1, r"exits to the south\.", "move south"),
121 (0, r"arrives from the north\.", "move east"),
122 (0, r"You exit to the east\.", ""),
123 (1, r"exits to the east\.", "move east"),
124 (0, r"arrives from the west\.", "move west"),
125 (0, r"You exit to the west\.", ""),
126 (1, r"exits to the west\.", "move west"),
127 (0, r"arrives from the east\.", "move up"),
128 (0, r"You exit upward\.", ""),
129 (1, r"exits upward\.", "move up"),
130 (0, r"arrives from below\.", "move down"),
131 (0, r"You exit downward\.", ""),
132 (1, r"exits downward\.", "move down"),
133 (0, r"arrives from above\.", ""),
136 test_actor_disappears = (
138 (0, r"You suddenly wonder where .* went\.", ""),
141 test_account1_teardown = (
142 (1, "What would you like to do?", "d"),
143 (1, "Whom would you like to delete?", ""),
144 (1, "What would you like to do?", "p"),
145 (1, "permanently delete your account?", "y"),
146 (1, "Disconnecting...", ""),
150 (2, "Identify yourself:", "admin"),
151 (2, "Enter your choice:", "n"),
152 (2, 'Enter a new password for "admin":', "Test789"),
153 (2, "Enter the same new password again:", "Test789"),
154 (2, r"What would you like to do\?", "c"),
155 (2, "Pick a birth gender for your new avatar:", "m"),
156 (2, "Choose a name for him:", "1"),
157 (2, "What would you like to do?", "a"),
158 (2, "Whom would you like to awaken?", ""),
162 # Send a double (escaped) IAC byte within other text, which should get
163 # unescaped and deduplicated to a single \xff in the buffer and then
164 # the line of input discarded as a non-ASCII sequence
165 (2, "> ", b"say argle\xff\xffbargle\r\n"),
166 (2, r"Non-ASCII characters from admin: b'say argle\\xffbargle'.*> ", ""),
169 test_telnet_unknown = (
170 # Send an unsupported negotiation command #127 which should get filtered
171 # from the line of input
172 (2, "> ", b"say glop\xff\x7fglyf\r\n"),
173 (2, r'Unknown Telnet IAC command 127 ignored\..*"Glopglyf\.".*> ', ""),
176 test_admin_restriction = (
177 (0, "> ", "help halt"),
178 (0, r"That is not an available command\.", "halt"),
179 (0, '(not sure what "halt" means|Arglebargle, glop-glyf)', ""),
184 (2, r"halt.*Shut down the world\.", "help halt"),
185 (2, "This will save all active accounts", ""),
190 (2, r"Reloading all code modules, configs and data\."
191 r".* User admin reloaded the world\.",
192 "show element account.admin"),
193 (2, 'These are the properties of the "account.admin" element.*'
194 r' \x1b\[32mpasshash:\r\n\x1b\[31m\$.*> ', ""),
198 (2, "> ", "set actor.avatar_admin_0 gender female"),
199 (2, r'You have successfully \(re\)set the "gender" facet of element', ""),
203 (2, "> ", "set mudpy.limit password_tries 10"),
204 (2, r'The "mudpy\.limit" element is kept in read-only file', ""),
207 test_show_version = (
208 (2, "> ", "show version"),
209 (2, r"Running mudpy .* on .* Python 3.*with.*pyyaml.*> ", ""),
213 (2, "> ", "show files"),
214 (2, r'These are the current files containing the universe:.*'
215 r' \x1b\[31m\(rw\) \x1b\[32m/.*/account.yaml\x1b\[0m'
216 r' \x1b\[33m\[private\]\x1b\[0m.*> ', ""),
220 (2, "> ", "show file %s" %
221 os.path.join(os.getcwd(), "data/internal.yaml")),
222 (2, r'These are the nodes in the.*file:.*internal\.counters.*> ', ""),
226 (2, "> ", "show groups"),
227 (2, r'These are the element groups:.*'
228 r' \x1b\[32maccount\x1b\[0m.*> ', ""),
232 (2, "> ", "show group account"),
233 (2, r'These are the elements in the "account" group:.*'
234 r' \x1b\[32maccount\.admin\x1b\[0m.*> ', ""),
237 test_show_element = (
238 (2, "> ", "show element mudpy.limit"),
239 (2, r'These are the properties of the "mudpy\.limit" element.*'
240 r' \x1b\[32mpassword_tries: \x1b\[31m3.*> ',
241 "show element actor.avatar_admin_0"),
242 (2, r'These are the properties of the "actor.avatar_admin_0" element.*'
243 r' \x1b\[32mgender: \x1b\[31mfemale.*> ', ""),
247 (2, "> ", "show log"),
248 (2, r"There are [0-9]+ log lines in memory and [0-9]+ at or above level "
249 r"[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
252 test_custom_loglevel = (
253 (2, "> ", "set account.admin loglevel 2"),
254 (2, "You have successfully .*> ", "show log"),
255 (2, r"There are [0-9]+ log lines in memory and [0-9]+ at or above level "
256 r"[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
259 test_invalid_loglevel = (
260 (2, "> ", "set account.admin loglevel two"),
261 (2, r'''Value "two" of type "<class 'str'>" cannot be coerced .*> ''', ""),
264 test_log_no_errors = (
265 (2, "> ", "show log 7"),
266 (2, r"None of the [0-9]+ lines in memory matches your request\.", ""),
271 (0, "What would you like to do?", "d"),
272 (0, "Whom would you like to delete?", ""),
273 (0, "What would you like to do?", "p"),
274 (0, "permanently delete your account?", "y"),
275 (0, "Disconnecting...", ""),
277 (2, "What would you like to do?", "d"),
278 (2, "Whom would you like to delete?", ""),
279 (2, "What would you like to do?", "p"),
280 (2, "permanently delete your account?", "y"),
281 (2, "Disconnecting...", ""),
285 (test_account0_setup, "first account setup"),
286 (test_account1_setup, "second account setup"),
287 (test_actor_appears, "actor spontaneous appearance"),
288 (test_explicit_punctuation, " explicit punctuation"),
289 (test_implicit_punctuation, "implicit punctuation"),
290 (test_typo_replacement, "typo replacement"),
291 (test_sentence_capitalization, "sentence capitalization"),
292 (test_chat_mode, "chat mode"),
293 (test_wrapping, "wrapping"),
294 (test_forbid_ansi_input, "raw escape input is filtered"),
295 (test_movement, "movement"),
296 (test_actor_disappears, "actor spontaneous disappearance"),
297 (test_account1_teardown, "second account teardown"),
298 (test_admin_setup, "admin account setup"),
299 (test_telnet_iac, "escape stray telnet iac bytes"),
300 (test_telnet_unknown, "strip unknown telnet command"),
301 (test_admin_restriction, "restricted admin commands"),
302 (test_admin_help, "admin help"),
303 (test_reload, "reload"),
304 (test_set_facet, "set facet"),
305 (test_set_refused, "refuse altering read-only element"),
306 (test_show_version, "show version and diagnostic info"),
307 (test_show_files, "show a list of loaded files"),
308 (test_show_file, "show nodes from a specific file"),
309 (test_show_groups, "show groups"),
310 (test_show_group, "show group"),
311 (test_show_element, "show element"),
312 (test_show_log, "show log"),
313 (test_custom_loglevel, "custom loglevel"),
314 (test_invalid_loglevel, "invalid loglevel"),
315 (test_log_no_errors, "no errors logged"),
316 (final_cleanup, "delete remaining accounts"),
320 def start_service(config):
321 # Clean up any previously run daemon which didn't terminate
322 if os.path.exists(pidfile):
323 pidfd = open(pidfile)
324 pid = int(pidfd.read())
326 # Stop the running service
329 except ProcessLookupError:
330 # If there was no process, just remove the stale PID file
332 # If there's a preexisting hung service, we can't proceed
333 assert not os.path.exists(pidfile)
335 # Clean up any previous test output
336 for f in pathlib.Path(".").glob("capture_*.log"):
337 # have to use .name here since remove() doesn't support passing a
338 # PosixPath argument until Python3.6
340 for d in ("data", "var"):
341 shutil.rmtree(d, ignore_errors=True)
344 # Start the service and wait for it to be ready for connections
345 service = subprocess.Popen(("mudpy", config),
346 stdout=subprocess.PIPE,
347 stderr=subprocess.PIPE)
352 def stop_service(service):
355 # The no-op case when no service was started
359 # This handles when the service is running as a direct child process
361 returncode = service.wait(10)
363 print("ERROR: Service exited with code %s." % returncode)
366 # This cleans up a daemonized and disassociated service
367 if os.path.exists(pidfile):
368 pidfd = open(pidfile)
369 pid = int(pidfd.read())
371 # Stop the running service
374 except ProcessLookupError:
375 # If there was no process, just remove the stale PID file
377 # The PID file didn't disappear, so we have a hung service
378 if os.path.exists(pidfile):
379 print("ERROR: Hung daemon with PID %s." % pid)
382 # Log the contents of stdout and stderr, if any
383 stdout, stderr = service.communicate()
384 print("Recording stdout as capture_stdout.log.")
385 serviceout = open("capture_stdout.log", "w")
386 serviceout.write(stdout.decode("utf-8"))
387 print("Recording stderr as capture_stderr.log.")
388 serviceerr = open("capture_stderr.log", "w")
389 serviceerr.write(stderr.decode("utf-8"))
395 captures = ["", "", ""]
396 lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
400 if len(sys.argv) > 1:
401 # Start the service if a config file was provided on the command line
402 service = start_service(sys.argv[1])
404 luser.open("::1", 4000)
405 for test, description in dialogue:
406 print("\nTesting %s..." % description)
407 test_start = time.time()
408 for conversant, question, answer in test:
409 print("luser%s waiting for: %s" % (conversant, question))
411 index, match, received = lusers[conversant].expect(
412 [re.compile(question.encode("utf-8"), flags=re.DOTALL)], 5)
413 captures[conversant] += received.decode("utf-8")
414 except ConnectionResetError:
415 print("ERROR: Unable to connect to server.")
419 print("ERROR: luser%s premature disconnection expecting:\n\n"
421 "Check the end of capture_%s.log for received data."
422 % (conversant, question, conversant))
426 captures[conversant] += lusers[
427 conversant].read_very_eager().decode("utf-8")
431 print("ERROR: luser%s did not receive expected string:\n\n"
433 "Check the end of capture_%s.log for received data."
434 % (conversant, question, conversant))
437 if type(answer) is str:
438 print("luser%s sending: %s" % (conversant, answer))
439 lusers[conversant].write(("%s\r\n" % answer).encode("utf-8"))
440 captures[conversant] += "%s\r\n" % answer
441 elif type(answer) is bytes:
442 print("luser%s sending raw bytes: %s" % (conversant, answer))
443 lusers[conversant].get_socket().send(answer)
444 captures[conversant] += "!!!RAW BYTES: %s" % answer
446 print("ERROR: answer provided with unsupported type %s"
452 print("Completed in %.3f seconds." % (time.time() - test_start))
453 duration = time.time() - start
455 for conversant in range(len(captures)):
457 captures[conversant] += lusers[
458 conversant].read_very_eager().decode("utf-8")
461 lusers[conversant].close()
462 logfile = "capture_%s.log" % conversant
463 print("Recording session %s as %s." % (conversant, logfile))
464 log = open(logfile, "w")
465 log.write(captures[conversant])
467 if not stop_service(service):
469 print("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
477 if __name__ == '__main__':