3b62f4beb359 — Oben Sonne 10 years ago
server: test with bad client input
3 files changed, 79 insertions(+), 4 deletions(-)

M server/src/remuco/net.py
M server/src/tests/__init__.py
A => server/src/tests/test_badclient.py
M server/src/remuco/net.py +11 -2
@@ 71,11 71,20 @@ class Client(object):
         for msg in messages:
             self.rem.debug("message from %s:\n%s" % (self, framedump(msg)))
             try:
-                cmd, params = json.loads(msg)
+                obj = json.loads(msg)
+                if not isinstance(obj, list) or len(obj) != 2:
+                    raise ValueError('message must be a 2-element list')
+                cmd, params = obj
+                if not isinstance(cmd, basestring):
+                    raise ValueError('command must be string')
+                if not isinstance(params, dict):
+                    raise ValueError('parameters must be a dictionary')
             except ValueError as e:
                 self.rem.warning("invalid message from %s (%s)" % (self, e))
-                self.send('bounce', xcmd=cmd, reason=str(e))
+                self.send('bounce', xcmd='?', reason='message malformed')
                 continue
+
+            self.rem.info('cmd, params = %s, %s' % (cmd, params))
             params['client'] = self
             try:
                 self._handler.clientmessage(self, cmd, params)

          
M server/src/tests/__init__.py +5 -2
@@ 102,8 102,11 @@ class Client(object):
         for m in messages:
             log("s %s" % m)
             s = "%s\n\n" % json.dumps(m)
-            self.fp.write(s)
-            self.fp.flush()
+            self.sndraw(s)
+
+    def sndraw(self, data):
+        self.fp.write(data)
+        self.fp.flush()
 
     def receive(self):
         """

          
A => server/src/tests/test_badclient.py +63 -0
@@ 0,0 1,63 @@ 
+"""
+Test session with Bong player.
+
+"""
+
+import os
+import time
+import tempfile
+import textwrap
+
+import json
+
+from . import Client, Server, Process, log, BINDIR
+
+WAIT_FOR_PLAYER_UPDATE = 2
+
+
+def test_1(runningplayers=(), **helloparams):
+
+    s = Server()
+    time.sleep(1)
+
+    c = Client()
+    c.exp("""
+        - hello
+        - version: "1.0"
+    """)
+
+    invalidmsg = """
+        - bounce
+        - reason: message malformed
+          xcmd: "?"
+    """
+
+    # syntax okay, unknwon message
+    c.sndraw(json.dumps(['m', {}]) + "\n\n")
+    c.exp("""
+        - bounce
+        - reason: command unknown
+          xcmd: m
+    """)
+    # too many elems
+    c.sndraw(json.dumps(['m', {}, 2]) + "\n\n")
+    c.exp(invalidmsg)
+    # params is not a dict
+    c.sndraw(json.dumps(['m', []]) + "\n\n")
+    c.exp(invalidmsg)
+    # only one elem
+    c.sndraw(json.dumps(['m']) + "\n\n")
+    c.exp(invalidmsg)
+    # no elem
+    c.sndraw(json.dumps([]) + "\n\n")
+    c.exp(invalidmsg)
+    # not a list
+    c.sndraw('1234' + "\n\n")
+    c.exp(invalidmsg)
+    # malformed json
+    c.sndraw('afdsaf\nsdf32132' + "\n\n")
+    c.exp(invalidmsg)
+
+    c.disconnect()
+    s.stop()
+