1 # Copyright (c) 2004-2019 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 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, r"What would you like to do\?", "a"),
25 (0, r"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, r"What would you like to do\?", "a"),
37 (1, r"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\."', ""),
114 test_escape_macros = (
115 (0, '> ', "say $(red)bar$(nrm)"),
116 (1, r'says, "\$\(red\)bar\$\(nrm\)\."', ""),
120 (0, "> ", "move north"),
121 (0, r"You exit to the north\.", ""),
122 (1, r"exits to the north\.", "move north"),
123 (0, r"arrives from the south\.", "move south"),
124 (0, r"You exit to the south\.", ""),
125 (1, r"exits to the south\.", "move south"),
126 (0, r"arrives from the north\.", "move east"),
127 (0, r"You exit to the east\.", ""),
128 (1, r"exits to the east\.", "move east"),
129 (0, r"arrives from the west\.", "move west"),
130 (0, r"You exit to the west\.", ""),
131 (1, r"exits to the west\.", "move west"),
132 (0, r"arrives from the east\.", "move up"),
133 (0, r"You exit upward\.", ""),
134 (1, r"exits upward\.", "move up"),
135 (0, r"arrives from below\.", "move down"),
136 (0, r"You exit downward\.", ""),
137 (1, r"exits downward\.", "move down"),
138 (0, r"arrives from above\.", ""),
141 test_actor_disappears = (
143 (0, r"You suddenly wonder where .* went\.", ""),
146 test_abort_avatar_deletion = (
147 (1, r"What would you like to do\?", "d"),
148 (1, r"Whom would you like to delete\?", ""),
149 (1, r"delete an unwanted avatar.*What would you like to do\?", ""),
152 test_avatar_creation_limit = (
153 (1, r"What would you like to do\?", "c"),
154 (1, "Pick a birth gender for your new avatar:", "m"),
155 (1, "Choose a name for him:", "3"),
156 (1, r"What would you like to do\?", "c"),
157 (1, "Pick a birth gender for your new avatar:", "m"),
158 (1, "Choose a name for him:", "4"),
159 (1, r"What would you like to do\?", "c"),
160 (1, "Pick a birth gender for your new avatar:", "m"),
161 (1, "Choose a name for him:", "7"),
162 (1, r"What would you like to do\?", "c"),
163 (1, "Pick a birth gender for your new avatar:", "m"),
164 (1, "Choose a name for him:", "5"),
165 (1, r"What would you like to do\?", "c"),
166 (1, "Pick a birth gender for your new avatar:", "m"),
167 (1, "Choose a name for him:", "2"),
168 (1, r"What would you like to do\?", "c"),
169 (1, "Pick a birth gender for your new avatar:", "m"),
170 (1, "Choose a name for him:", "6"),
171 (1, r"What would you like to do\?", "c"),
172 (1, r"That is not a valid choice\.\.\.", ""),
175 test_avatar_deletion = (
176 (1, r"What would you like to do\?", "d"),
177 (1, r"Whom would you like to delete\?", "1"),
178 (1, r"create a new avatar.*What would you like to do\?", ""),
181 test_abort_account_deletion = (
182 (1, r"What would you like to do\?", "p"),
183 (1, r"permanently delete your account\?", ""),
184 (1, r"What would you like to do\?", ""),
187 test_account_deletion = (
188 (1, r"What would you like to do\?", "p"),
189 (1, r"permanently delete your account\?", "y"),
190 (1, r"Disconnecting\.\.\.", ""),
194 (2, "Identify yourself:", "admin"),
195 (2, "Enter your choice:", "n"),
196 (2, 'Enter a new password for "admin":', "Test789"),
197 (2, "Enter the same new password again:", "Test789"),
198 (2, r"What would you like to do\?", "c"),
199 (2, "Pick a birth gender for your new avatar:", "m"),
200 (2, "Choose a name for him:", "1"),
201 (2, r"What would you like to do\?", "a"),
202 (2, r"Whom would you like to awaken\?", ""),
206 (0, "> ", "preferences"),
207 (0, r"\[32mprompt\x1b\[0m - <not set>.*> ", "preferences prompt $(foo)"),
208 (0, r"\$\(foo\) ", "preferences prompt"),
209 (0, r"\$\(foo\).*\$\(foo\) ", "preferences prompt $(time)>"),
210 (0, "[0-9]> ", "preferences loglevel 0"),
211 (0, "does not exist.*> ", "preferences prompt >"),
212 (2, "> ", "preferences loglevel 0"),
213 (2, "> ", "preferences"),
214 (2, r"\[31mloglevel\x1b\[0m - 0.*> ", "preferences loglevel zero"),
215 (2, r'''cannot be set to type "<class 'str'>"\..*> ''', ""),
219 # Send a CR+LF at the end of the line instead of the default CR+NUL,
220 # to make sure they're treated the same
221 (2, "> ", b"say I use CR+LF as my EOL, not CR+NUL.\r\n"),
222 (2, r'You say, "I use CR\+LF as my EOL, not CR\+NUL\.".*> ', ""),
226 # Send a double (escaped) IAC byte within other text, which should get
227 # unescaped and deduplicated to a single \xff in the buffer and then
228 # the line of input discarded as a non-ASCII sequence
229 (2, "> ", b"say argle\xff\xffbargle\r\0"),
230 (2, r"Non-ASCII characters from admin: b'say.*argle\\xffbargle'.*> ", ""),
233 test_telnet_unknown_command = (
234 # Send an unsupported negotiation command #127 which should get filtered
235 # from the line of input
236 (2, "> ", b"say glop\xff\x7fglyf\r\0"),
237 (2, r'Ignored unknown command 127 from admin\..*"Glopglyf\.".*> ', ""),
240 test_telnet_unknown_option = (
241 # Send an unassigned negotiation option #127 which should get logged
242 (2, "> ", b"\xff\xfe\x7f\r\0"),
243 (2, r'''Received "don't 127" from admin\..*> ''', ""),
246 test_admin_restriction = (
247 (0, "> ", "help halt"),
248 (0, r"That is not an available command\.", "halt"),
249 (0, '(not sure what "halt" means|Arglebargle, glop-glyf)', ""),
254 (2, r"halt.*Shut down the world\.", "help halt"),
255 (2, "This will save all active accounts", ""),
259 (0, "> ", "help say"),
260 (0, r"See also: .*chat.*> ", ""),
265 (0, r"h\[elp\].*m\[ove\].*> ", "he mo"),
266 (0, r"Move in a specific direction\..*> ", "mov nor"),
267 (0, r"You exit to the north\..*> ", "m s"),
268 (0, r"You exit to the south\..*> ", ""),
273 (2, r"Reloading all code modules, configs and data\."
274 r".* User admin reloaded the world\.",
275 "show element account.admin"),
276 (2, r'These are the properties of the "account\.admin" element.*'
277 r' \x1b\[32mpasshash:\r\n\x1b\[31m\$.*> ', ""),
281 (2, "> ", "set actor.avatar_admin_0 gender female"),
282 (2, r'You have successfully \(re\)set the "gender" facet of element', ""),
286 (2, "> ", "set mudpy.limit password_tries 10"),
287 (2, r'The "mudpy\.limit" element is kept in read-only file', ""),
290 test_show_version = (
291 (2, "> ", "show version"),
292 (2, r"Running mudpy .* on .* Python 3.*with.*pyyaml.*> ", ""),
296 (2, "> ", "show time"),
297 (2, r"\r\n[0-9]+ increments elapsed.*> ", ""),
301 (2, "> ", "show files"),
302 (2, r'These are the current files containing the universe:.*'
303 r' \x1b\[31m\(rw\) \x1b\[32m/.*/account\.yaml\x1b\[0m'
304 r' \x1b\[33m\[private\]\x1b\[0m.*> ', ""),
308 (2, "> ", "show file %s" %
309 os.path.join(os.getcwd(), "data/internal.yaml")),
310 (2, r'These are the nodes in the.*file:.*internal\.counters.*> ', ""),
314 (2, "> ", "show groups"),
315 (2, r'These are the element groups:.*'
316 r' \x1b\[32maccount\x1b\[0m.*> ', ""),
320 (2, "> ", "show group account"),
321 (2, r'These are the elements in the "account" group:.*'
322 r' \x1b\[32maccount\.admin\x1b\[0m.*> ', ""),
325 test_show_element = (
326 (2, "> ", "show element mudpy.limit"),
327 (2, r'These are the properties of the "mudpy\.limit" element.*'
328 r' \x1b\[32mpassword_tries: \x1b\[31m3.*> ',
329 "show element actor.avatar_admin_0"),
330 (2, r'These are the properties of the "actor\.avatar_admin_0" element.*'
331 r' \x1b\[32mgender: \x1b\[31mfemale.*> ', ""),
335 (2, "> ", "show result 12345*67890"),
336 (2, r"\r\n838102050\r\n.*> ", "show result 1/0"),
337 (2, r"Your expression raised an exception.*division by zero.*> ",
338 "show result mudpy"),
339 (2, r"<module 'mudpy' from .*> ", "show result re"),
340 (2, r"Your expression raised an exception.*name 're' is not defined.*> ",
341 "show result universe"),
342 (2, r"<mudpy\.misc\.Universe object at 0x.*> ", "show result actor"),
343 (2, r"Your expression raised an exception.*name 'actor' is not "
348 (2, "> ", "show log"),
349 (2, r"There are [0-9]+ log lines in memory and [0-9]+ at or above level "
350 r"[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
353 test_custom_loglevel = (
354 (2, "> ", "set account.admin loglevel 2"),
355 (2, "You have successfully .*> ", "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 lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
360 test_invalid_loglevel = (
361 (2, "> ", "set account.admin loglevel two"),
362 (2, r'''Value "two" of type "<class 'str'>" cannot be coerced .*> ''', ""),
365 test_log_no_errors = (
366 (2, "> ", "show log 7"),
367 (2, r"None of the [0-9]+ lines in memory matches your request\.", ""),
372 (0, r"What would you like to do\?", "p"),
373 (0, r"permanently delete your account\?", "y"),
374 (0, r"Disconnecting\.\.\.", ""),
376 (2, r"What would you like to do\?", "p"),
377 (2, r"permanently delete your account\?", "y"),
378 (2, r"Disconnecting\.\.\.", ""),
382 (test_account0_setup, "first account setup"),
383 (test_account1_setup, "second account setup"),
384 (test_actor_appears, "actor spontaneous appearance"),
385 (test_explicit_punctuation, "explicit punctuation"),
386 (test_implicit_punctuation, "implicit punctuation"),
387 (test_typo_replacement, "typo replacement"),
388 (test_sentence_capitalization, "sentence capitalization"),
389 (test_chat_mode, "chat mode"),
390 (test_wrapping, "wrapping"),
391 (test_forbid_ansi_input, "raw escape input is filtered"),
392 (test_escape_macros, "replacement macros are escaped"),
393 (test_movement, "movement"),
394 (test_actor_disappears, "actor spontaneous disappearance"),
395 (test_abort_avatar_deletion, "abort avatar deletion"),
396 (test_avatar_creation_limit, "avatar creation limit"),
397 (test_avatar_deletion, "avatar deletion"),
398 (test_abort_account_deletion, "abort account deletion"),
399 (test_account_deletion, "account deletion"),
400 (test_admin_setup, "admin account setup"),
401 (test_preferences, "set and show preferences"),
402 (test_crlf_eol, "send crlf from the client as eol"),
403 (test_telnet_iac, "escape stray telnet iac bytes"),
404 (test_telnet_unknown_command, "strip unknown telnet command"),
405 (test_telnet_unknown_option, "log unknown telnet option"),
406 (test_admin_restriction, "restricted admin commands"),
407 (test_admin_help, "admin help"),
408 (test_help, "help command"),
409 (test_abbrev, "command abbreviation"),
410 (test_reload, "reload"),
411 (test_set_facet, "set facet"),
412 (test_set_refused, "refuse altering read-only element"),
413 (test_show_version, "show version and diagnostic info"),
414 (test_show_time, "show elapsed world clock increments"),
415 (test_show_files, "show a list of loaded files"),
416 (test_show_file, "show nodes from a specific file"),
417 (test_show_groups, "show groups"),
418 (test_show_group, "show group"),
419 (test_show_element, "show element"),
420 (test_show_result, "show result of a python expression"),
421 (test_show_log, "show log"),
422 (test_custom_loglevel, "custom loglevel"),
423 (test_invalid_loglevel, "invalid loglevel"),
424 (test_log_no_errors, "no errors logged"),
425 (final_cleanup, "delete remaining accounts"),
429 def start_service(config):
430 # Clean up any previously run daemon which didn't terminate
431 if os.path.exists(pidfile):
432 pidfd = open(pidfile)
433 pid = int(pidfd.read())
435 # Stop the running service
438 except ProcessLookupError:
439 # If there was no process, just remove the stale PID file
441 # If there's a preexisting hung service, we can't proceed
442 assert not os.path.exists(pidfile)
444 # Clean up any previous test output
445 for f in pathlib.Path(".").glob("capture_*.log"):
446 # have to use .name here since remove() doesn't support passing a
447 # PosixPath argument until Python3.6
449 for d in ("data", "var"):
450 shutil.rmtree(d, ignore_errors=True)
452 # Start the service and wait for it to be ready for connections
453 service = subprocess.Popen(("mudpy", config),
454 stdout=subprocess.PIPE,
455 stderr=subprocess.PIPE)
460 def stop_service(service):
463 # The no-op case when no service was started
467 # This handles when the service is running as a direct child process
469 returncode = service.wait(10)
471 tlog("\nERROR: Service exited with code %s." % returncode)
474 # This cleans up a daemonized and disassociated service
475 if os.path.exists(pidfile):
476 pidfd = open(pidfile)
477 pid = int(pidfd.read())
479 # Stop the running service
482 except ProcessLookupError:
483 # If there was no process, just remove the stale PID file
485 # The PID file didn't disappear, so we have a hung service
486 if os.path.exists(pidfile):
487 tlog("\nERROR: Hung daemon with PID %s." % pid)
490 # Log the contents of stdout and stderr, if any
491 stdout, stderr = service.communicate()
492 tlog("\nRecording stdout as capture_stdout.log.")
493 serviceout = open("capture_stdout.log", "w")
494 serviceout.write(stdout.decode("utf-8"))
495 tlog("\nRecording stderr as capture_stderr.log.")
496 serviceerr = open("capture_stderr.log", "w")
497 serviceerr.write(stderr.decode("utf-8"))
502 def tlog(message, quiet=False):
503 logfile = "capture_tests.log"
504 with open(logfile, "a") as logfd:
505 logfd.write(message + "\n")
507 sys.stdout.write(message)
511 def option_callback(telnet_socket, command, option):
512 if option == b'\x7f':
513 # We use this unassigned option value as a canary, so short-circuit
514 # any response to avoid endlessly looping
516 elif command in (telnetlib.DO, telnetlib.DONT):
517 telnet_socket.send(telnetlib.IAC + telnetlib.WONT + option)
518 elif command in (telnetlib.WILL, telnetlib.WONT):
519 telnet_socket.send(telnetlib.IAC + telnetlib.DONT + option)
523 captures = ["", "", ""]
524 lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
528 if len(sys.argv) > 1:
529 # Start the service if a config file was provided on the command line
530 service = start_service(sys.argv[1])
532 luser.open("::1", 4000)
533 luser.set_option_negotiation_callback(option_callback)
534 for test, description in dialogue:
535 tlog("\nTesting %s..." % description)
536 test_start = time.time()
537 for conversant, question, answer in test:
538 tlog("luser%s waiting for: %s" % (conversant, question),
541 index, match, received = lusers[conversant].expect(
542 [re.compile(question.encode("utf-8"), flags=re.DOTALL)], 5)
543 captures[conversant] += received.decode("utf-8")
544 except ConnectionResetError:
545 tlog("\nERROR: Unable to connect to server.")
549 tlog("\nERROR: luser%s premature disconnection expecting:\n\n"
551 "Check the end of capture_%s.log for received data."
552 % (conversant, question, conversant))
556 captures[conversant] += lusers[
557 conversant].read_very_eager().decode("utf-8")
561 tlog("\nERROR: luser%s did not receive expected string:\n\n"
563 "Check the end of capture_%s.log for received data."
564 % (conversant, question, conversant))
567 if type(answer) is str:
568 tlog("luser%s sending: %s" % (conversant, answer), quiet=True)
569 lusers[conversant].write(("%s\r\0" % answer).encode("utf-8"))
570 captures[conversant] += "%s\r\n" % answer
571 elif type(answer) is bytes:
572 tlog("luser%s sending raw bytes: %s" % (conversant, answer),
574 lusers[conversant].get_socket().send(answer)
575 captures[conversant] += "!!!RAW BYTES: %s" % answer
577 tlog("\nERROR: answer provided with unsupported type %s"
583 tlog("Completed in %.3f seconds." % (time.time() - test_start))
584 duration = time.time() - start
585 for conversant in range(len(captures)):
587 captures[conversant] += lusers[
588 conversant].read_very_eager().decode("utf-8")
591 lusers[conversant].close()
592 logfile = "capture_%s.log" % conversant
593 tlog("\nRecording session %s as %s." % (conversant, logfile))
594 log = open(logfile, "w")
595 log.write(captures[conversant])
597 if not stop_service(service):
599 tlog("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
607 if __name__ == '__main__':