Fix deprecated regex pattern syntax in selftests
[mudpy.git] / mudpy / tests / selftest.py
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.
4
5 import os
6 import re
7 import sys
8 import telnetlib
9 import time
10
11 test_account0_setup = (
12     (0, "Identify yourself:", "luser0"),
13     (0, "Enter your choice:", "n"),
14     (0, 'Enter a new password for "luser0":', "Test123"),
15     (0, "Enter the same new password again:", "Test123"),
16     (0, r"What would you like to do\?", "c"),
17     (0, "Pick a birth gender for your new avatar:", "f"),
18     (0, "Choose a name for her:", "1"),
19     (0, "What would you like to do?", "a"),
20     (0, "Whom would you like to awaken?", ""),
21 )
22
23 test_account1_setup = (
24     (1, "Identify yourself:", "luser1"),
25     (1, "Enter your choice:", "n"),
26     (1, 'Enter a new password for "luser1":', "Test456"),
27     (1, "Enter the same new password again:", "Test456"),
28     (1, r"What would you like to do\?", "c"),
29     (1, "Pick a birth gender for your new avatar:", "m"),
30     (1, "Choose a name for him:", "1"),
31     (1, "What would you like to do?", "a"),
32     (1, "Whom would you like to awaken?", ""),
33 )
34
35 test_actor_appears = (
36     (0, r"You suddenly realize that .* is here\.", ""),
37 )
38
39 test_explicit_punctuation = (
40     (0, "> ", "say Hello there!"),
41     (0, r'You exclaim, "Hello there\!"', ""),
42     (1, r'exclaims, "Hello there\!"', "say And you are?"),
43     (1, r'You ask, "And you are\?"', ""),
44     (0, r'asks, "And you are\?"', "say I'm me, of course."),
45     (0, r'''You say, "I'm me, of course\."''', ""),
46     (1, r'''says, "I'm me, of course\."''', "say I wouldn't be so sure..."),
47     (1, r'''You muse, "I wouldn't be so sure\.\.\."''', ""),
48     (0, r'''muses, "I wouldn't be so sure\.\.\."''', "say You mean,"),
49     (0, 'You begin, "You mean,"', ""),
50     (1, 'begins, "You mean,"', "say I know-"),
51     (1, 'You begin, "I know-"', ""),
52     (0, 'begins, "I know-"', "say Don't interrupt:"),
53     (0, r'''You begin, "Don't interrupt:"''', ""),
54     (1, r'''begins, "Don't interrupt:"''', "say I wasn't interrupting;"),
55     (1, r'''You begin, "I wasn't interrupting;"''', ""),
56     (0, r'''begins, "I wasn't interrupting;"''', ""),
57 )
58
59 test_implicit_punctuation = (
60     (0, '> ', "say Whatever"),
61     (0, r'You say, "Whatever\."', ""),
62     (1, r'says, "Whatever\."', ""),
63 )
64
65 test_typo_replacement = (
66     (1, '> ', "say That's what i think."),
67     (1, r'''You say, "That's what I think\."''', ""),
68     (0, r'''says, "That's what I think\."''', "say You know what i'd like."),
69     (0, r'''You say, "You know what I'd like\."''', ""),
70     (1, r'''says, "You know what I'd like\."''', "say Then i'll tell you."),
71     (1, r'''You say, "Then I'll tell you\."''', ""),
72     (0, r'''says, "Then I'll tell you\."''', "say Now i'm ready."),
73     (0, r'''You say, "Now I'm ready\."''', ""),
74     (1, r'''says, "Now I'm ready\."''', "say That's teh idea."),
75     (1, r'''You say, "That's the idea\."''', ""),
76     (0, r'''says, "That's the idea\."''', "say It's what theyre saying."),
77     (0, r'''You say, "It's what they're saying\."''', ""),
78     (1, r'''says, "It's what they're saying\."''', "say Well, youre right."),
79     (1, r'''You say, "Well, you're right\."''', ""),
80     (0, r'''says, "Well, you're right\."''', ""),
81 )
82
83 test_sentence_capitalization = (
84     (0, "> ", "say this sentence should start with a capital T."),
85     (0, 'You say, "This sentence', ""),
86     (1, 'says, "This sentence', ""),
87 )
88
89 test_chat_mode = (
90     (1, '> ', "chat"),
91     (1, r'(?s)Entering chat mode .*> \(chat\) ', "Feeling chatty."),
92     (1, r'You say, "Feeling chatty\."', "!chat"),
93     (0, r'says, "Feeling chatty\."', ""),
94     (1, '> ', "say Now less chatty."),
95     (1, r'You say, "Now less chatty\."', ""),
96     (0, r'says, "Now less chatty\."', ""),
97 )
98
99 test_wrapping = (
100     (0, '> ', "say " + 100 * "o"),
101     (1, r'says,\r\n"O[o]+\."', ""),
102 )
103
104 test_forbid_ansi_input = (
105     (0, '> ', "say \x1b[35mfoo\x1b[0m"),
106     (1, r'says, "\[35mfoo\[0m\."', ""),
107 )
108
109 test_movement = (
110     (0, "> ", "move north"),
111     (0, r"You exit to the north\.", ""),
112     (1, r"exits to the north\.", "move north"),
113     (0, r"arrives from the south\.", "move south"),
114     (0, r"You exit to the south\.", ""),
115     (1, r"exits to the south\.", "move south"),
116     (0, r"arrives from the north\.", "move east"),
117     (0, r"You exit to the east\.", ""),
118     (1, r"exits to the east\.", "move east"),
119     (0, r"arrives from the west\.", "move west"),
120     (0, r"You exit to the west\.", ""),
121     (1, r"exits to the west\.", "move west"),
122     (0, r"arrives from the east\.", "move up"),
123     (0, r"You exit upward\.", ""),
124     (1, r"exits upward\.", "move up"),
125     (0, r"arrives from below\.", "move down"),
126     (0, r"You exit downward\.", ""),
127     (1, r"exits downward\.", "move down"),
128     (0, r"arrives from above\.", ""),
129 )
130
131 test_actor_disappears = (
132     (1, "> ", "quit"),
133     (0, r"You suddenly wonder where .* went\.", ""),
134 )
135
136 test_account1_teardown = (
137     (1, "What would you like to do?", "d"),
138     (1, "Whom would you like to delete?", ""),
139     (1, "What would you like to do?", "p"),
140     (1, "permanently delete your account?", "y"),
141     (1, "Disconnecting...", ""),
142 )
143
144 test_admin_setup = (
145     (2, "Identify yourself:", "admin"),
146     (2, "Enter your choice:", "n"),
147     (2, 'Enter a new password for "admin":', "Test789"),
148     (2, "Enter the same new password again:", "Test789"),
149     (2, r"What would you like to do\?", "c"),
150     (2, "Pick a birth gender for your new avatar:", "m"),
151     (2, "Choose a name for him:", "1"),
152     (2, "What would you like to do?", "a"),
153     (2, "Whom would you like to awaken?", ""),
154 )
155
156 test_telnet_iac = (
157     # Send a double (escaped) IAC byte within other text, which should get
158     # unescaped and deduplicated to a single \xff in the buffer and then
159     # the line of input discarded as a non-ASCII sequence
160     (2, "> ", b"say argle\xff\xffbargle\r\n"),
161     (2, r"Non-ASCII characters from admin: b'say argle\\xffbargle'.*> ", ""),
162 )
163
164 test_telnet_unknown = (
165     # Send an unsupported negotiation command #127 which should get filtered
166     # from the line of input
167     (2, "> ", b"say glop\xff\x7fglyf\r\n"),
168     (2, r'Unknown Telnet IAC command 127 ignored\..*"Glopglyf\.".*> ', ""),
169 )
170
171 test_admin_restriction = (
172     (0, "> ", "help halt"),
173     (0, r"That is not an available command\.", "halt"),
174     (0, '(not sure what "halt" means|Arglebargle, glop-glyf)', ""),
175 )
176
177 test_admin_help = (
178     (2, "> ", "help"),
179     (2, r"halt.*Shut down the world\.", "help halt"),
180     (2, "This will save all active accounts", ""),
181 )
182
183 test_reload = (
184     (2, "> ", "reload"),
185     (2, r"Reloading all code modules, configs and data\."
186         r".* User admin reloaded the world\.",
187      "show element account.admin"),
188     (2, 'These are the properties of the "account.admin" element.*'
189         r'  \x1b\[32mpasshash:\r\n\x1b\[31m\$.*> ', ""),
190 )
191
192 test_set_facet = (
193     (2, "> ", "set actor.avatar_admin_0 gender female"),
194     (2, r'You have successfully \(re\)set the "gender" facet of element', ""),
195 )
196
197 test_set_refused = (
198     (2, "> ", "set mudpy.limit password_tries 10"),
199     (2, r'The "mudpy\.limit" element is kept in read-only file', ""),
200 )
201
202 test_show_version = (
203     (2, "> ", "show version"),
204     (2, r"Running mudpy .* on .* Python 3.*with.*pyyaml.*> ", ""),
205 )
206
207 test_show_files = (
208     (2, "> ", "show files"),
209     (2, r'These are the current files containing the universe:.*'
210         r'  \x1b\[31m\(rw\) \x1b\[32m/.*/account.yaml\x1b\[0m'
211         r' \x1b\[33m\[private\]\x1b\[0m.*> ', ""),
212 )
213
214 test_show_file = (
215     (2, "> ", "show file %s" %
216         os.path.join(os.getcwd(), "data/internal.yaml")),
217     (2, r'These are the nodes in the.*file:.*internal\.counters.*> ', ""),
218 )
219
220 test_show_groups = (
221     (2, "> ", "show groups"),
222     (2, r'These are the element groups:.*'
223         r'  \x1b\[32maccount\x1b\[0m.*> ', ""),
224 )
225
226 test_show_group = (
227     (2, "> ", "show group account"),
228     (2, r'These are the elements in the "account" group:.*'
229         r'  \x1b\[32maccount\.admin\x1b\[0m.*> ', ""),
230 )
231
232 test_show_element = (
233     (2, "> ", "show element mudpy.limit"),
234     (2, r'These are the properties of the "mudpy\.limit" element.*'
235         r'  \x1b\[32mpassword_tries: \x1b\[31m3.*> ',
236      "show element actor.avatar_admin_0"),
237     (2, r'These are the properties of the "actor.avatar_admin_0" element.*'
238         r'  \x1b\[32mgender: \x1b\[31mfemale.*> ', ""),
239 )
240
241 test_show_log = (
242     (2, "> ", "show log"),
243     (2, r"There are [0-9]+ log lines in memory and [0-9]+ at or above level "
244         r"[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
245 )
246
247 test_custom_loglevel = (
248     (2, "> ", "set account.admin loglevel 2"),
249     (2, "You have successfully .*> ", "show log"),
250     (2, r"There are [0-9]+ log lines in memory and [0-9]+ at or above level "
251         r"[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
252 )
253
254 test_invalid_loglevel = (
255     (2, "> ", "set account.admin loglevel two"),
256     (2, r'''Value "two" of type "<class 'str'>" cannot be coerced .*> ''', ""),
257 )
258
259 test_log_no_errors = (
260     (2, "> ", "show log 7"),
261     (2, r"None of the [0-9]+ lines in memory matches your request\.", ""),
262 )
263
264 final_cleanup = (
265     (0, "> ", "quit"),
266     (0, "What would you like to do?", "d"),
267     (0, "Whom would you like to delete?", ""),
268     (0, "What would you like to do?", "p"),
269     (0, "permanently delete your account?", "y"),
270     (0, "Disconnecting...", ""),
271     (2, "> ", "quit"),
272     (2, "What would you like to do?", "d"),
273     (2, "Whom would you like to delete?", ""),
274     (2, "What would you like to do?", "p"),
275     (2, "permanently delete your account?", "y"),
276     (2, "Disconnecting...", ""),
277 )
278
279 dialogue = (
280     (test_account0_setup, "first account setup"),
281     (test_account1_setup, "second account setup"),
282     (test_actor_appears, "actor spontaneous appearance"),
283     (test_explicit_punctuation, " explicit punctuation"),
284     (test_implicit_punctuation, "implicit punctuation"),
285     (test_typo_replacement, "typo replacement"),
286     (test_sentence_capitalization, "sentence capitalization"),
287     (test_chat_mode, "chat mode"),
288     (test_wrapping, "wrapping"),
289     (test_forbid_ansi_input, "raw escape input is filtered"),
290     (test_movement, "movement"),
291     (test_actor_disappears, "actor spontaneous disappearance"),
292     (test_account1_teardown, "second account teardown"),
293     (test_admin_setup, "admin account setup"),
294     (test_telnet_iac, "escape stray telnet iac bytes"),
295     (test_telnet_unknown, "strip unknown telnet command"),
296     (test_admin_restriction, "restricted admin commands"),
297     (test_admin_help, "admin help"),
298     (test_reload, "reload"),
299     (test_set_facet, "set facet"),
300     (test_set_refused, "refuse altering read-only element"),
301     (test_show_version, "show version and diagnostic info"),
302     (test_show_files, "show a list of loaded files"),
303     (test_show_file, "show nodes from a specific file"),
304     (test_show_groups, "show groups"),
305     (test_show_group, "show group"),
306     (test_show_element, "show element"),
307     (test_show_log, "show log"),
308     (test_custom_loglevel, "custom loglevel"),
309     (test_invalid_loglevel, "invalid loglevel"),
310     (test_log_no_errors, "no errors logged"),
311     (final_cleanup, "delete remaining accounts"),
312 )
313
314
315 def main():
316     captures = ["", "", ""]
317     lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
318     success = True
319     start = time.time()
320     for luser in lusers:
321         luser.open("::1", 4000)
322     for test, description in dialogue:
323         print("\nTesting %s..." % description)
324         test_start = time.time()
325         for conversant, question, answer in test:
326             print("luser%s waiting for: %s" % (conversant, question))
327             try:
328                 index, match, received = lusers[conversant].expect(
329                     [re.compile(question.encode("utf-8"), flags=re.DOTALL)], 5)
330                 captures[conversant] += received.decode("utf-8")
331             except ConnectionResetError:
332                 print("ERROR: Unable to connect to server.")
333                 success = False
334                 break
335             except EOFError:
336                 print("ERROR: luser%s premature disconnection expecting:\n\n"
337                       "%s\n\n"
338                       "Check the end of capture_%s.log for received data."
339                       % (conversant, question, conversant))
340                 success = False
341                 break
342             try:
343                 captures[conversant] += lusers[
344                     conversant].read_very_eager().decode("utf-8")
345             except Exception:
346                 pass
347             if index is not 0:
348                 print("ERROR: luser%s did not receive expected string:\n\n"
349                       "%s\n\n"
350                       "Check the end of capture_%s.log for received data."
351                       % (conversant, question, conversant))
352                 success = False
353                 break
354             if type(answer) is str:
355                 print("luser%s sending: %s" % (conversant, answer))
356                 lusers[conversant].write(("%s\r\n" % answer).encode("utf-8"))
357                 captures[conversant] += "%s\r\n" % answer
358             elif type(answer) is bytes:
359                 print("luser%s sending raw bytes: %s" % (conversant, answer))
360                 lusers[conversant].get_socket().send(answer)
361                 captures[conversant] += "!!!RAW BYTES: %s" % answer
362             else:
363                 print("ERROR: answer provided with unsupported type %s"
364                       % type(answer))
365                 success = False
366                 break
367         if not success:
368             break
369         print("Completed in %.3f seconds." % (time.time() - test_start))
370     duration = time.time() - start
371     print("")
372     for conversant in range(len(captures)):
373         try:
374             captures[conversant] += lusers[
375                 conversant].read_very_eager().decode("utf-8")
376         except Exception:
377             pass
378         lusers[conversant].close()
379         logfile = "capture_%s.log" % conversant
380         print("Recording session %s as %s." % (conversant, logfile))
381         log = open(logfile, "w")
382         log.write(captures[conversant])
383         log.close()
384     print("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
385     if success:
386         print("SUCCESS")
387     else:
388         print("FAILURE")
389         sys.exit(1)
390
391
392 if __name__ == '__main__':
393     sys.exit(main())