summaryrefslogtreecommitdiff
path: root/test.py
diff options
context:
space:
mode:
Diffstat (limited to 'test.py')
-rwxr-xr-xtest.py157
1 files changed, 157 insertions, 0 deletions
diff --git a/test.py b/test.py
new file mode 100755
index 0000000..d6d06ae
--- /dev/null
+++ b/test.py
@@ -0,0 +1,157 @@
+#!/usr/bin/env python3
+
+import sys, socket, inspect
+
+opensockets=[]
+
+encoding="latin-1"
+
+def connect():
+ sock=socket.socket()
+ sock.connect(("localhost",1423))
+ opensockets.append(sock)
+ return sock
+
+def close(sock):
+ sock.close()
+ opensockets.remove(sock)
+
+def sendraw(sock,text):
+ sock.sendall(text.encode(encoding))
+
+def sendline(sock,line):
+ sendraw(sock,line+"\n")
+
+def recvraw(sock,length):
+ text=""
+ while len(text)<length:
+ text+=sock.recv(length-len(text)).decode(encoding)
+ return text
+
+def recvline(sock):
+ line=""
+ while True:
+ c=sock.recv(1).decode(encoding)
+ if c=="\n":
+ break
+ line+=c
+ return line
+
+def recvint(sock):
+ line=recvline(sock)
+ spl=line.split(" ")
+ assert len(spl)==3 and spl[0]=="int"
+ return int(spl[2])
+
+def sendlineok(sock,line):
+ sendline(sock,line)
+ cmd=line.split(" ")[0]
+ expect(sock,"ok "+cmd)
+
+def sendlineerror(sock,line,msg):
+ sendline(sock,line)
+ cmd=line.split(" ")[0]
+ expect(sock,"error "+cmd+" "+msg)
+
+def sendlinelist(sock,line,lst):
+ sendline(sock,line)
+ cmd=line.split(" ")[0]
+ expect(sock,"list "+cmd+" "+str(len(lst))+("" if len(lst)==0 else " "+" ".join(lst)))
+
+def expectraw(sock,text):
+ got=recvraw(sock,len(text))
+ if got!=text:
+ print("\nEXPECTRAW error: expected '"+text+"', got '"+got+"'")
+ sys.exit(1)
+
+def expect(sock,line):
+ got=recvline(sock)
+ if got!=line:
+ print("\nEXPECT error: expected '"+line+"', got '"+got+"'")
+ sys.exit(1)
+
+
+testfunctions=[]
+
+def testfunction(tag):
+ def dec(func):
+ def newfunc():
+ global opensockets
+ socks=[]
+ for i in range(len(inspect.signature(func).parameters)):
+ socks.append(connect())
+ func(*socks)
+ for sock in opensockets: sock.close()
+ opensockets=[]
+ testfunctions.append((tag,newfunc))
+ return dec
+
+def runtests():
+ for (tag,f) in testfunctions:
+ print("- "+tag+": ",end="")
+ sys.stdout.flush()
+ f()
+ print("\x1B[32mOK\x1B[0m")
+ print("\x1B[32m>> All OK!\x1B[0m")
+
+
+
+@testfunction("Pingpong")
+def testping(s):
+ sendline(s,"ping")
+ expect(s,"pong")
+
+@testfunction("Create room, list, query")
+def testroom(s,t):
+ sendlinelist(s,"room_query",[])
+ sendlinelist(t,"room_query",[])
+ sendlinelist(s,"room_list game",[])
+ sendlinelist(t,"room_list game",[])
+ sendlineok(s,"room_create game room 1 2")
+ sendlinelist(s,"room_query",["room"])
+ sendlinelist(t,"room_query",[])
+ sendlinelist(s,"room_list game",["room"])
+ sendlinelist(t,"room_list game",["room"])
+
+@testfunction("Create private room, list, query")
+def testprivateroom(s,t):
+ sendlineok(s,"room_create game room 0 2")
+ sendlinelist(s,"room_query",["room"])
+ sendlinelist(t,"room_query",[])
+ sendlinelist(s,"room_list game",[])
+ sendlinelist(t,"room_list game",[])
+
+@testfunction("Join room")
+def testjoin(s,t):
+ sendlineok(s,"room_create game room 1 2")
+ sendlineok(t,"room_join game room")
+ sendlinelist(t,"room_query",["room"])
+ sendlineok(t,"room_create game room2 1 1")
+ sendlinelist(t,"room_query",["room","room2"])
+ sendlineerror(s,"room_join game room2","Room full")
+
+@testfunction("Player list, messaging")
+def testid(s,t):
+ sendline(s,"id"); sid=recvint(s)
+ sendline(t,"id"); tid=recvint(t)
+ sendlineok(s,"room_create game room 0 2")
+ sendlineok(t,"room_join game room")
+ sendlinelist(s,"room_player_list game room",[str(sid),str(tid)])
+ sendlinelist(t,"room_player_list game room",[str(sid),str(tid)])
+
+ sendline(s,"room_message game room "+str(tid)+" 4")
+ sendraw(s,"kaas")
+ expect(s,"ok room_message")
+ expect(t,"room_message "+str(sid)+" 4")
+ expectraw(t,"kaas")
+
+ sendline(t,"room_message game room "+str(sid)+" 256")
+ sendraw(t,"".join(chr(i) for i in range(256)))
+ expect(t,"ok room_message")
+ expect(s,"room_message "+str(tid)+" 256")
+ expectraw(s,"".join(chr(i) for i in range(256)))
+
+
+
+if __name__=="__main__":
+ runtests()