Add version command and diagnostic logging
[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_movement = (
100     (0, "> ", "move north"),
101     (0, r"You exit to the north\.", ""),
102     (1, r"exits to the north\.", "move north"),
103     (0, r"arrives from the south\.", "move south"),
104     (0, r"You exit to the south\.", ""),
105     (1, r"exits to the south\.", "move south"),
106     (0, r"arrives from the north\.", "move east"),
107     (0, r"You exit to the east\.", ""),
108     (1, r"exits to the east\.", "move east"),
109     (0, r"arrives from the west\.", "move west"),
110     (0, r"You exit to the west\.", ""),
111     (1, r"exits to the west\.", "move west"),
112     (0, r"arrives from the east\.", "move up"),
113     (0, r"You exit upward\.", ""),
114     (1, r"exits upward\.", "move up"),
115     (0, r"arrives from below\.", "move down"),
116     (0, r"You exit downward\.", ""),
117     (1, r"exits downward\.", "move down"),
118     (0, r"arrives from above\.", ""),
119 )
120
121 test_actor_disappears = (
122     (1, "> ", "quit"),
123     (0, r"You suddenly wonder where .* went\.", ""),
124 )
125
126 test_account1_teardown = (
127     (1, "What would you like to do?", "d"),
128     (1, "Whom would you like to delete?", ""),
129     (1, "What would you like to do?", "p"),
130     (1, "permanently delete your account?", "y"),
131     (1, "Disconnecting...", ""),
132 )
133
134 test_admin_setup = (
135     (2, "Identify yourself:", "admin"),
136     (2, "Enter your choice:", "n"),
137     (2, 'Enter a new password for "admin":', "Test789"),
138     (2, "Enter the same new password again:", "Test789"),
139     (2, r"What would you like to do\?", "c"),
140     (2, "Pick a birth gender for your new avatar:", "m"),
141     (2, "Choose a name for him:", "1"),
142     (2, "What would you like to do?", "a"),
143     (2, "Whom would you like to awaken?", ""),
144 )
145
146 test_telnet_iac = (
147     # Send a double (escaped) IAC byte within other text, which should get
148     # unescaped and deduplicated to a single \xff in the buffer and then
149     # the line of input discarded as a non-UTF-8 sequence
150     (2, "> ", b"say argle\xff\xffbargle\r\n"),
151     (2, r"Non-UTF-8 sequence from admin: b'say argle\\xffbargle'.*> ", ""),
152 )
153
154 test_telnet_unknown = (
155     # Send an unsupported negotiation command #127 which should get filtered
156     # from the line of input
157     (2, "> ", b"say glop\xff\x7fglyf\r\n"),
158     (2, r'Unknown Telnet IAC command 127 ignored\..*"Glopglyf\.".*> ', ""),
159 )
160
161 test_admin_restriction = (
162     (0, "> ", "help halt"),
163     (0, r"That is not an available command\.", "halt"),
164     (0, '(not sure what "halt" means|Arglebargle, glop-glyf)', ""),
165 )
166
167 test_admin_help = (
168     (2, "> ", "help"),
169     (2, r"halt.*Shut down the world\.", "help halt"),
170     (2, "This will save all active accounts", ""),
171 )
172
173 test_reload = (
174     (2, "> ", "reload"),
175     (2, r"Reloading all code modules, configs and data\."
176         r".* User admin reloaded the world\.", ""),
177 )
178
179 test_set_facet = (
180     (2, "> ", "set actor.avatar_admin_0 gender female"),
181     (2, r'You have successfully \(re\)set the "gender" facet of element', ""),
182 )
183
184 test_set_refused = (
185     (2, "> ", "set mudpy.limit password_tries 10"),
186     (2, r'The "mudpy\.limit" element is kept in read-only file', ""),
187 )
188
189 test_show_version = (
190     (2, "> ", "show version"),
191     (2, r"Running mudpy .* on .* Python 3.*with.*pyyaml.*> ", ""),
192 )
193
194 test_show_files = (
195     (2, "> ", "show files"),
196     (2, r'These are the current files containing the universe:.*'
197         r'  \x1b\[31m\(rw\) \x1b\[32m/.*/account.yaml\x1b\[0m'
198         r' \x1b\[33m\[private\]\x1b\[0m.*> ', ""),
199 )
200
201 test_show_file = (
202     (2, "> ", "show file %s" %
203         os.path.join(os.getcwd(), "data/internal.yaml")),
204     (2, r'These are the nodes in the.*file:.*internal\.counters.*> ', ""),
205 )
206
207 test_show_groups = (
208     (2, "> ", "show groups"),
209     (2, r'These are the element groups:.*'
210         r'  \x1b\[32maccount\x1b\[0m.*> ', ""),
211 )
212
213 test_show_group = (
214     (2, "> ", "show group account"),
215     (2, r'These are the elements in the "account" group:.*'
216         r'  \x1b\[32maccount\.admin\x1b\[0m.*> ', ""),
217 )
218
219 test_show_element = (
220     (2, "> ", "show element mudpy.limit"),
221     (2, r'These are the properties of the "mudpy\.limit" element.*'
222         r'  \x1b\[32mpassword_tries: \x1b\[31m3.*> ',
223      "show element actor.avatar_admin_0"),
224     (2, r'These are the properties of the "actor.avatar_admin_0" element.*'
225         r'  \x1b\[32mgender: \x1b\[31mfemale.*> ', ""),
226 )
227
228 test_show_log = (
229     (2, "> ", "show log"),
230     (2, r"There are [0-9]+ log lines in memory and [0-9]+ at or above level "
231         r"[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
232 )
233
234 test_custom_loglevel = (
235     (2, "> ", "set account.admin loglevel 2"),
236     (2, "You have successfully .*> ", "show log"),
237     (2, r"There are [0-9]+ log lines in memory and [0-9]+ at or above level "
238         r"[0-9]+\. The matching lines\r\nfrom [0-9]+ to [0-9]+ are:", ""),
239 )
240
241 test_invalid_loglevel = (
242     (2, "> ", "set account.admin loglevel two"),
243     (2, r'''Value "two" of type "<class 'str'>" cannot be coerced .*> ''', ""),
244 )
245
246 test_log_no_errors = (
247     (2, "> ", "show log 7"),
248     (2, r"None of the [0-9]+ lines in memory matches your request\.", ""),
249 )
250
251 dialogue = (
252     (test_account0_setup, "first account setup"),
253     (test_account1_setup, "second account setup"),
254     (test_actor_appears, "actor spontaneous appearance"),
255     (test_explicit_punctuation, " explicit punctuation"),
256     (test_implicit_punctuation, "implicit punctuation"),
257     (test_typo_replacement, "typo replacement"),
258     (test_sentence_capitalization, "sentence capitalization"),
259     (test_chat_mode, "chat mode"),
260     (test_movement, "movement"),
261     (test_actor_disappears, "actor spontaneous disappearance"),
262     (test_account1_teardown, "second account teardown"),
263     (test_admin_setup, "admin account setup"),
264     (test_telnet_iac, "escape stray telnet iac bytes"),
265     (test_telnet_unknown, "strip unknown telnet command"),
266     (test_admin_restriction, "restricted admin commands"),
267     (test_admin_help, "admin help"),
268     (test_reload, "reload"),
269     (test_set_facet, "set facet"),
270     (test_set_refused, "refuse altering read-only element"),
271     (test_show_version, "show version and diagnostic info"),
272     (test_show_files, "show a list of loaded files"),
273     (test_show_file, "show nodes from a specific file"),
274     (test_show_groups, "show groups"),
275     (test_show_group, "show group"),
276     (test_show_element, "show element"),
277     (test_show_log, "show log"),
278     (test_custom_loglevel, "custom loglevel"),
279     (test_invalid_loglevel, "invalid loglevel"),
280     (test_log_no_errors, "no errors logged"),
281 )
282
283
284 def main():
285     captures = ["", "", ""]
286     lusers = [telnetlib.Telnet(), telnetlib.Telnet(), telnetlib.Telnet()]
287     success = True
288     start = time.time()
289     for luser in lusers:
290         luser.open("::1", 4000)
291     for test, description in dialogue:
292         print("\nTesting %s..." % description)
293         test_start = time.time()
294         for conversant, question, answer in test:
295             print("luser%s waiting for: %s" % (conversant, question))
296             try:
297                 index, match, received = lusers[conversant].expect(
298                     [re.compile(question.encode("utf-8"), flags=re.DOTALL)], 5)
299                 captures[conversant] += received.decode("utf-8")
300             except ConnectionResetError:
301                 print("ERROR: Unable to connect to server.")
302                 success = False
303                 break
304             except EOFError:
305                 print("ERROR: luser%s premature disconnection expecting:\n\n"
306                       "%s\n\n"
307                       "Check the end of capture_%s.log for received data."
308                       % (conversant, question, conversant))
309                 success = False
310                 break
311             try:
312                 captures[conversant] += lusers[
313                     conversant].read_very_eager().decode("utf-8")
314             except Exception:
315                 pass
316             if index is not 0:
317                 print("ERROR: luser%s did not receive expected string:\n\n"
318                       "%s\n\n"
319                       "Check the end of capture_%s.log for received data."
320                       % (conversant, question, conversant))
321                 success = False
322                 break
323             if type(answer) is str:
324                 print("luser%s sending: %s" % (conversant, answer))
325                 lusers[conversant].write(("%s\r\n" % answer).encode("utf-8"))
326                 captures[conversant] += "%s\r\n" % answer
327             elif type(answer) is bytes:
328                 print("luser%s sending raw bytes: %s" % (conversant, answer))
329                 lusers[conversant].get_socket().send(answer)
330                 captures[conversant] += "!!!RAW BYTES: %s" % answer
331             else:
332                 print("ERROR: answer provided with unsupported type %s"
333                       % type(answer))
334                 success = False
335                 break
336         if not success:
337             break
338         print("Completed in %.3f seconds." % (time.time() - test_start))
339     duration = time.time() - start
340     print("")
341     for conversant in range(len(captures)):
342         try:
343             captures[conversant] += lusers[
344                 conversant].read_very_eager().decode("utf-8")
345         except Exception:
346             pass
347         lusers[conversant].close()
348         logfile = "capture_%s.log" % conversant
349         print("Recording session %s as %s." % (conversant, logfile))
350         log = open(logfile, "w")
351         log.write(captures[conversant])
352         log.close()
353     print("\nRan %s tests in %.3f seconds." % (len(dialogue), duration))
354     if success:
355         print("SUCCESS")
356     else:
357         print("FAILURE")
358         sys.exit(1)
359
360
361 if __name__ == '__main__':
362     sys.exit(main())