1 # Copyright (c) 2004-2020 mudpy authors. Permission to use, copy,
2 # modify, and distribute this software is granted under terms
3 # provided in the LICENSE file distributed with this software.
14 # TODO(fungi) Clean this up once Python 3.5 is no longer supported
15 if sys.version < "3.6":
17 odict = collections.OrderedDict
21 pidfile = "var/mudpy.pid"
23 test_account0_setup = (
24 (0, "Identify yourself:", "luser0"),
25 (0, "Enter your choice:", "n"),
26 (0, 'Enter a new password for "luser0":', "Test123"),
27 (0, "Enter the same new password again:", "Test123"),
28 (0, r"What would you like to do\?", "c"),
29 (0, "Pick a birth gender for your new avatar:", "f"),
30 (0, "Choose a name for her:", "1"),
31 (0, r"What would you like to do\?", "a"),
32 (0, r"Whom would you like to awaken\?", ""),
35 test_account1_setup = (
36 (1, "Identify yourself:", "luser1"),
37 (1, "Enter your choice:", "n"),
38 (1, 'Enter a new password for "luser1":', "Test456"),
39 (1, "Enter the same new password again:", "Test456"),
40 (1, r"What would you like to do\?", "c"),
41 (1, "Pick a birth gender for your new avatar:", "m"),
42 (1, "Choose a name for him:", "1"),
43 (1, r"What would you like to do\?", "a"),
44 (1, r"Whom would you like to awaken\?", ""),
47 test_actor_appears = (
48 (0, r"You suddenly realize that .* is here\.", ""),
51 test_explicit_punctuation = (
52 (0, "> ", "say Hello there!"),
53 (0, r'You exclaim, "Hello there\!"', ""),
54 (1, r'exclaims, "Hello there\!"', "say And you are?"),
55 (1, r'You ask, "And you are\?"', ""),
56 (0, r'asks, "And you are\?"', "say I'm me, of course."),
57 (0, r'''You say, "I'm me, of course\."''', ""),
58 (1, r'''says, "I'm me, of course\."''', "say I wouldn't be so sure..."),
59 (1, r'''You muse, "I wouldn't be so sure\.\.\."''', ""),
60 (0, r'''muses, "I wouldn't be so sure\.\.\."''', "say You mean,"),
61 (0, 'You begin, "You mean,"', ""),
62 (1, 'begins, "You mean,"', "say I know-"),
63 (1, 'You begin, "I know-"', ""),
64 (0, 'begins, "I know-"', "say Don't interrupt:"),
65 (0, r'''You begin, "Don't interrupt:"''', ""),
66 (1, r'''begins, "Don't interrupt:"''', "say I wasn't interrupting;"),
67 (1, r'''You begin, "I wasn't interrupting;"''', ""),
68 (0, r'''begins, "I wasn't interrupting;"''', ""),
71 test_implicit_punctuation = (
72 (0, '> ', "say Whatever"),
73 (0, r'You say, "Whatever\."', ""),
74 (1, r'says, "Whatever\."', ""),
77 test_typo_replacement = (
78 (1, '> ', "say That's what i think."),
79 (1, r'''You say, "That's what I think\."''', ""),
80 (0, r'''says, "That's what I think\."''', "say You know what i'd like."),
81 (0, r'''You say, "You know what I'd like\."''', ""),
82 (1, r'''says, "You know what I'd like\."''', "say Then i'll tell you."),
83 (1, r'''You say, "Then I'll tell you\."''', ""),
84 (0, r'''says, "Then I'll tell you\."''', "say Now i'm ready."),
85 (0, r'''You say, "Now I'm ready\."''', ""),
86 (1, r'''says, "Now I'm ready\."''', "say That's teh idea."),
87 (1, r'''You say, "That's the idea\."''', ""),
88 (0, r'''says, "That's the idea\."''', "say It's what theyre saying."),
89 (0, r'''You say, "It's what they're saying\."''', ""),
90 (1, r'''says, "It's what they're saying\."''', "say Well, youre right."),
91 (1, r'''You say, "Well, you're right\."''', ""),
92 (0, r'''says, "Well, you're right\."''', ""),
95 test_sentence_capitalization = (
96 (0, "> ", "say this sentence should start with a capital T."),
97 (0, 'You say, "This sentence', ""),
98 (1, 'says, "This sentence', ""),
103 (1, r'(?s)Entering chat mode .*> \(chat\) ', "Feeling chatty."),
104 (1, r'You say, "Feeling chatty\."', "!chat"),
105 (0, r'says, "Feeling chatty\."', ""),
106 (1, '> ', "say Now less chatty."),
107 (1, r'You say, "Now less chatty\."', ""),
108 (0, r'says, "Now less chatty\."', ""),
112 (0, '> ', "say " + 100 * "o"),
113 (1, r'says,\r\n"O[o]+\."', ""),
116 test_forbid_ansi_input = (
117 (0, '> ', "say \x1b[35mfoo\x1b[0m"),
118 (1, r'says, "\[35mfoo\[0m\."', ""),
121 test_escape_macros = (
122 (0, '> ', "say $(red)bar$(nrm)"),
123 (1, r'says, "\$\(red\)bar\$\(nrm\)\."', ""),
127 (0, "> ", "move north"),
128 (0, r"You exit to the north\.", ""),
129 (1, r"exits to the north\.", "move north"),
130 (0, r"arrives from the south\.", "move south"),
131 (0, r"You exit to the south\.", ""),
132 (1, r"exits to the south\.", "move south"),
133 (0, r"arrives from the north\.", "move east"),
134 (0, r"You exit to the east\.", ""),
135 (1, r"exits to the east\.", "move east"),
136 (0, r"arrives from the west\.", "move west"),
137 (0, r"You exit to the west\.", ""),
138 (1, r"exits to the west\.", "move west"),
139 (0, r"arrives from the east\.", "move up"),
140 (0, r"You exit upward\.", ""),
141 (1, r"exits upward\.", "move up"),
142 (0, r"arrives from below\.", "move down"),
143 (0, r"You exit downward\.", ""),
144 (1, r"exits downward\.", "move down"),
145 (0, r"arrives from above\.", ""),
148 test_actor_disappears = (
150 (0, r"You suddenly wonder where .* went\.", ""),
153 test_abort_avatar_deletion = (
154 (1, r"What would you like to do\?", "d"),
155 (1, r"Whom would you like to delete\?", ""),
156 (1, r"delete an unwanted avatar.*What would you like to do\?", ""),
159 test_avatar_creation_limit = (
160 (1, r"What would you like to do\?", "c"),
161 (1, "Pick a birth gender for your new avatar:", "m"),
162 (1, "Choose a name for him:", "3"),
163 (1, r"What would you like to do\?", "c"),
164 (1, "Pick a birth gender for your new avatar:", "m"),
165 (1, "Choose a name for him:", "4"),
166 (1, r"What would you like to do\?", "c"),
167 (1, "Pick a birth gender for your new avatar:", "m"),
168 (1, "Choose a name for him:", "7"),
169 (1, r"What would you like to do\?", "c"),
170 (1, "Pick a birth gender for your new avatar:", "m"),
171 (1, "Choose a name for him:", "5"),
172 (1, r"What would you like to do\?", "c"),
173 (1, "Pick a birth gender for your new avatar:", "m"),
174 (1, "Choose a name for him:", "2"),
175 (1, r"What would you like to do\?", "c"),
176 (1, "Pick a birth gender for your new avatar:", "m"),
177 (1, "Choose a name for him:", "6"),
178 (1, r"What would you like to do\?", "c"),
179 (1, r"That is not a valid choice\.\.\.", ""),
182 test_avatar_deletion = (
183 (1, r"What would you like to do\?", "d"),
184 (1, r"Whom would you like to delete\?", "1"),
185 (1, r"create a new avatar.*What would you like to do\?", ""),
188 test_abort_account_deletion = (
189 (1, r"What would you like to do\?", "p"),
190 (1, r"permanently delete your account\?", ""),
191 (1, r"What would you like to do\?", ""),
194 test_account_deletion = (
195 (1, r"What would you like to do\?", "p"),
196 (1, r"permanently delete your account\?", "y"),
197 (1, r"Disconnecting\.\.\.", ""),
201 (2, "Identify yourself:", "admin"),
202 (2, "Enter your choice:", "n"),
203 (2, 'Enter a new password for "admin":', "Test789"),
204 (2, "Enter the same new password again:", "Test789"),
205 (2, r"What would you like to do\?", "c"),
206 (2, "Pick a birth gender for your new avatar:", "m"),
207 (2, "Choose a name for him:", "1"),
208 (2, r"What would you like to do\?", "a"),
209 (2, r"Whom would you like to awaken\?", ""),
213 (0, "> ", "preferences"),
214 (0, r"\[32mprompt\x1b\[0m - <not set>.*> ", "preferences prompt $(foo)"),
215 (0, r"\$\(foo\) ", "preferences prompt"),
216 (0, r"\$\(foo\).*\$\(foo\) ", "preferences prompt $(time)>"),
217 (0, "[0-9]> ", "preferences loglevel 0"),
218 (0, "does not exist.*> ", "preferences prompt >"),
219 (2, "> ", "preferences loglevel 0"),
220 (2, "> ", "preferences"),
221 (2, r"\[31mloglevel\x1b\[0m - 0.*> ", "preferences loglevel zero"),
222 (2, r'''cannot be set to type "<class 'str'>"\..*> ''', ""),
226 # Send a CR+LF at the end of the line instead of the default CR+NUL,
227 # to make sure they're treated the same
228 (2, "> ", b"say I use CR+LF as my EOL, not CR+NUL.\r\n"),
229 (2, r'You say, "I use CR\+LF as my EOL, not CR\+NUL\.".*> ', ""),
233 # Send a double (escaped) IAC byte within other text, which should get
234 # unescaped and deduplicated to a single \xff in the buffer and then
235 # the line of input discarded as a non-ASCII sequence
236 (2, "> ", b"say argle\xff\xffbargle\r\0"),
237 (2, r"Non-ASCII characters from admin: b'say.*argle\\xffbargle'.*> ", ""),
240 test_telnet_unknown_command = (
241 # Send an unsupported negotiation command #127 which should get filtered
242 # from the line of input
243 (2, "> ", b"say glop\xff\x7fglyf\r\0"),
244 (2, r'Ignored unknown command 127 from admin\..*"Glopglyf\.".*> ', ""),
247 test_telnet_unknown_option = (
248 # Send an unassigned negotiation option #127 which should get logged
249 (2, "> ", b"\xff\xfe\x7f\r\0"),
250 (2, r'''Received "don't 127" from admin\..*> ''', ""),
253 test_admin_restriction = (
254 (0, "> ", "help halt"),
255 (0, r"That is not an available command\.", "halt"),
256 (0, '(not sure what "halt" means|Arglebargle, glop-glyf)', ""),
261 (2, r"halt.*Shut down the world\.", "help halt"),
262 (2, "This will save all active accounts", ""),
266 (0, "> ", "help say"),
267 (0, r"See also: .*chat.*> ", ""),
272 (0, r"h\[elp\].*m\[ove\].*> ", "he mo"),
273 (0, r"Move in a specific direction\..*> ", "mov nor"),
274 (0, r"You exit to the north\..*> ", "m s"),
275 (0, r"You exit to the south\..*> ", ""),
280 (2, r"Reloading all code modules, configs and data\."
281 r".* User admin reloaded the world\.",
282 "show element account.admin"),
283 (2, r'These are the properties of the "account\.admin" element.*'
284 r' \x1b\[32mpasshash:\r\n\x1b\[31m\$.*> ', ""),
288 (2, "> ", "set actor.avatar_admin_0 gender female"),
289 (2, r'You have successfully \(re\)set the "gender" facet of element', ""),
293 (2, "> ", "set mudpy.limit password_tries 10"),
294 (2, r'The "mudpy\.limit" element is kept in read-only file', ""),
297 test_show_version = (
298 (2, "> ", "show version"),
299 (2, r"Running mudpy .* on .* Python 3.*with.*pyyaml.*> ", ""),
303 (2, "> ", "show time"),
304 (2, r"\r\n[0-9]+ increments elapsed.*> ", ""),
308 (2, "> ", "show files"),
309 (2, r'These are the current files containing the universe:.*'
310 r' \x1b\[31m\(rw\) \x1b\[32m/.*/account\.yaml\x1b\[0m'
311 r' \x1b\[33m\[private\]\x1b\[0m.*> ', ""),
315 (2, "> ", "show file %s" %
316 os.path.join(os.getcwd(), "data/internal.yaml")),
317 (2, r'These are the nodes in the.*file:.*internal\.counters.*> ', ""),
321 (2, "> ", "show groups"),
322 (2, r'These are the element groups:.*'
323 r' \x1b\[32maccount\x1b\[0m.*> ', ""),
327 (2, "> ", "show group account"),
328 (2, r'These are the elements in the "account" group:.*'
329 r' \x1b\[32maccount\.admin\x1b\[0m.*> ', ""),
332 test_show_element = (
333 (2, "> ", "show element mudpy.limit"),
334 (2, r'These are the properties of the "mudpy\.limit" element.*'
335 r' \x1b\[32mpassword_tries: \x1b\[31m3.*> ',
336 "show element actor.avatar_admin_0"),
337 (2, r'These are the properties of the "actor\.avatar_admin_0" element.*'
338 r' \x1b\[32mgender: \x1b\[31mfemale.*> ', ""),
342 (2, "> ", "show result 12345*67890"),
343 (2, r"\r\n838102050\r\n.*> ", "show result 1/0"),
344 (2, r"Your expression raised an exception.*division by zero.*> ",
345 "show result mudpy"),
346 (2, r"<module 'mudpy' from.*> ", "show result re"),
347 (2, r"Your expression raised an exception.*name 're' is not defined.*> ",
348 "show result universe"),
349 (2, r"<mudpy\.misc\.Universe object at 0x.*> ", "show result actor"),
350 (2, r"Your expression raised an exception.*name 'actor' is not "
355 (2, "> ", "show log"),
356 (2, r"There are [0-9]+ log lines in memory and [0-9]+ at or above level "
357 r"[0-9]+\. The matching.*from [0-9]+ to [0-9]+ are:", ""),
360 test_custom_loglevel = (
361 (2, "> ", "set account.admin loglevel 2"),
362 (2, "You have successfully .*> ", "show log"),
363 (2, r"There are [0-9]+ log lines in memory and [0-9]+ at or above level "
364 r"[0-9]+\. The matching.*from [0-9]+ to [0-9]+ are:", ""),
367 test_invalid_loglevel = (
368 (2, "> ", "set account.admin loglevel two"),
369 (2, r'''Value "two" of type "<class 'str'>" cannot be coerced .*> ''', ""),
372 test_log_no_errors = (
373 (2, "> ", "show log 7"),
374 (2, r"None of the [0-9]+ lines in memory matches your request\.", ""),
379 (0, r"What would you like to do\?", "p"),
380 (0, r"permanently delete your account\?", "y"),
381 (0, r"Disconnecting\.\.\.", ""),
383 (2, r"What would you like to do\?", "p"),
384 (2, r"permanently delete your account\?", "y"),
385 (2, r"Disconnecting\.\.\.", ""),
389 (test_account0_setup, "first account setup"),
390 (test_account1_setup, "second account setup"),
391 (test_actor_appears, "actor spontaneous appearance"),
392 (test_explicit_punctuation, "explicit punctuation"),
393 (test_implicit_punctuation, "implicit punctuation"),
394 (test_typo_replacement, "typo replacement"),
395 (test_sentence_capitalization, "sentence capitalization"),
396 (test_chat_mode, "chat mode"),
397 (test_wrapping, "wrapping"),
398 (test_forbid_ansi_input, "raw escape input is filtered"),
399 (test_escape_macros, "replacement macros are escaped"),
400 (test_movement, "movement"),
401 (test_actor_disappears, "actor spontaneous disappearance"),
402 (test_abort_avatar_deletion, "abort avatar deletion"),
403 (test_avatar_creation_limit, "avatar creation limit"),
404 (test_avatar_deletion, "avatar deletion"),
405 (test_abort_account_deletion, "abort account deletion"),
406 (test_account_deletion, "account deletion"),
407 (test_admin_setup, "admin account setup"),
408 (test_preferences, "set and show preferences"),
409 (test_crlf_eol, "send crlf from the client as eol"),
410 (test_telnet_iac, "escape stray telnet iac bytes"),
411 (test_telnet_unknown_command, "strip unknown telnet command"),
412 (test_telnet_unknown_option, "log unknown telnet option"),
413 (test_admin_restriction, "restricted admin commands"),
414 (test_admin_help, "admin help"),
415 (test_help, "help command"),
416 (test_abbrev, "command abbreviation"),
417 (test_reload, "reload"),
418 (test_set_facet, "set facet"),
419 (test_set_refused, "refuse altering read-only element"),
420 (test_show_version, "show version and diagnostic info"),
421 (test_show_time, "show elapsed world clock increments"),
422 (test_show_files, "show a list of loaded files"),
423 (test_show_file, "show nodes from a specific file"),
424 (test_show_groups, "show groups"),
425 (test_show_group, "show group"),
426 (test_show_element, "show element"),
427 (test_show_result, "show result of a python expression"),
428 (test_show_log, "show log"),
429 (test_custom_loglevel, "custom loglevel"),
430 (test_invalid_loglevel, "invalid loglevel"),
431 (test_log_no_errors, "no errors logged"),
432 (final_cleanup, "delete remaining accounts"),
436 def start_service(config):
437 # Clean up any previously run daemon which didn't terminate
438 if os.path.exists(pidfile):
439 with open(pidfile) as pidfd:
440 pid = int(pidfd.read())
442 # Stop the running service
445 except ProcessLookupError:
446 # If there was no process, just remove the stale PID file
448 # If there's a preexisting hung service, we can't proceed
449 assert not os.path.exists(pidfile)
451 # Clean up any previous test output
452 for f in pathlib.Path(".").glob("capture_*.log"):
453 # have to use .name here since remove() doesn't support passing a
454 # PosixPath argument until Python3.6
456 for d in ("data", "var"):
457 shutil.rmtree(d, ignore_errors=True)
459 # Start the service and wait for it to be ready for connections
460 service = subprocess.Popen(("mudpy", config),
461 stdout=subprocess.PIPE,
462 stderr=subprocess.PIPE)
467 def stop_service(service):
470 # The no-op case when no service was started
474 # This handles when the service is running as a direct child process
476 returncode = service.wait(10)
478 tlog("\nERROR: Service exited with code %s." % returncode)
481 # This cleans up a daemonized and disassociated service
482 if os.path.exists(pidfile):
483 with open(pidfile) as pidfd:
484 pid = int(pidfd.read())
486 # Stop the running service
489 except ProcessLookupError:
490 # If there was no process, just remove the stale PID file
492 # The PID file didn't disappear, so we have a hung service
493 if os.path.exists(pidfile):
494 tlog("\nERROR: Hung daemon with PID %s." % pid)
497 # Log the contents of stdout and stderr, if any
498 stdout, stderr = service.communicate()
499 tlog("\nRecording stdout as capture_stdout.log.")
500 with open("capture_stdout.log", "w") as serviceout:
501 serviceout.write(stdout.decode("utf-8"))
502 tlog("\nRecording stderr as capture_stderr.log.")
503 with open("capture_stderr.log", "w") as serviceerr:
504 serviceerr.write(stderr.decode("utf-8"))
509 def tlog(message, quiet=False):
510 logfile = "capture_tests.log"
511 with open(logfile, "a") as logfd:
512 logfd.write(message + "\n")
514 sys.stdout.write(message)
518 def option_callback(telnet_socket, command, option):
519 if option == b'\x7f':
520 # We use this unassigned option value as a canary, so short-circuit
521 # any response to avoid endlessly looping
523 elif command in (telnetlib.DO, telnetlib.DONT):
524 telnet_socket.send(telnetlib.IAC + telnetlib.WONT + option)
525 elif command in (telnetlib.WILL, telnetlib.WONT):
526 telnet_socket.send(telnetlib.IAC + telnetlib.DONT + option)
530 captures = ["", "", ""]
531 lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
535 if len(sys.argv) > 1:
536 # Start the service if a config file was provided on the command line
537 service = start_service(sys.argv[1])
539 tlog("\nERROR: Service did not start.\n")
542 luser.open("::1", 4000)
543 luser.set_option_negotiation_callback(option_callback)
544 selected_dialogue = odict(dialogue)
545 for test, description in selected_dialogue.items():
546 tlog("\nTesting %s..." % description)
547 test_start = time.time()
548 for conversant, question, answer in test:
549 tlog("luser%s waiting for: %s" % (conversant, question),
552 index, match, received = lusers[conversant].expect(
553 [re.compile(question.encode("utf-8"), flags=re.DOTALL)], 5)
554 captures[conversant] += received.decode("utf-8")
555 except ConnectionResetError:
556 tlog("\nERROR: Unable to connect to server.")
560 tlog("\nERROR: luser%s premature disconnection expecting:\n\n"
562 "Check the end of capture_%s.log for received data."
563 % (conversant, question, conversant))
567 captures[conversant] += lusers[
568 conversant].read_very_eager().decode("utf-8")
572 tlog("\nERROR: luser%s did not receive expected string:\n\n"
574 "Check the end of capture_%s.log for received data."
575 % (conversant, question, conversant))
578 if type(answer) is str:
579 tlog("luser%s sending: %s" % (conversant, answer), quiet=True)
580 lusers[conversant].write(("%s\r\0" % answer).encode("utf-8"))
581 captures[conversant] += "%s\r\n" % answer
582 elif type(answer) is bytes:
583 tlog("luser%s sending raw bytes: %s" % (conversant, answer),
585 lusers[conversant].get_socket().send(answer)
586 captures[conversant] += "!!!RAW BYTES: %s" % answer
588 tlog("\nERROR: answer provided with unsupported type %s"
594 tlog("Completed in %.3f seconds." % (time.time() - test_start))
595 duration = time.time() - start
596 for conversant in range(len(captures)):
598 captures[conversant] += lusers[
599 conversant].read_very_eager().decode("utf-8")
602 lusers[conversant].close()
603 logfile = "capture_%s.log" % conversant
604 tlog("\nRecording session %s as %s." % (conversant, logfile))
605 log = open(logfile, "w")
606 log.write(captures[conversant])
608 if not stop_service(service):
610 tlog("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
618 if __name__ == '__main__':