Guest User

Untitled

a guest
May 25th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. """ What do I have to test?
  2.  
  3. ## TESTS FOR SERVER
  4.  
  5. -> test that server is running on IP and PORT
  6. -> test that server responds -ERR when faced with a GET cmd of more than 255.
  7. -> test that server responds -ERR when command is not GET
  8. -> test that server does handle timeouts
  9. -> test that server only accepts normal files, not directories
  10. -> test that server handles client abruptly closing connection
  11. -> test that server correctly closes connection with QUIT cmd.
  12.  
  13. """
  14. import socket
  15. import time
  16. import sys
  17. import os
  18.  
  19. tests_suite = []
  20. BUF = 4096
  21.  
  22. def tests(func):
  23. tests_suite.append(func)
  24. return func
  25.  
  26. def connect_socket():
  27. try:
  28. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29. s.connect((sys.argv[1], int(sys.argv[2])))
  30. except Exception as e:
  31. raise e
  32. return s
  33.  
  34. @tests
  35. def testRunning():
  36. try:
  37. s = connect_socket()
  38. except Exception as e:
  39. print(e)
  40. return False
  41. s.close()
  42. return True
  43.  
  44. @tests
  45. def testTooLong():
  46. s = connect_socket()
  47. payload = 'GET ' + 'A'*400 + '\r\n'
  48. s.send(payload.encode())
  49. data = s.recv(BUF).decode()
  50. s.close()
  51. return True if data == '-ERR\r\n' else False
  52.  
  53. @tests
  54. def testTimeout():
  55. s = connect_socket()
  56. time.sleep(15)
  57. data = s.recv(BUF).decode()
  58. s.close()
  59. return True if len(data) is 0 else False
  60.  
  61. @tests
  62. def testDirectories():
  63. s = connect_socket()
  64. payload = b'GET files/file.txt\r\n'
  65. s.send(payload)
  66. data = s.recv(BUF).decode()
  67. s.close()
  68. return True if data == '-ERR\r\n' else False
  69.  
  70. @tests
  71. def testAbruptClose():
  72. s = connect_socket()
  73. payload = b'GET rockyou.txt\r\n'
  74. s.send(payload)
  75. time.sleep(2)
  76. s.close()
  77. return True
  78.  
  79. @tests
  80. def testQUIT():
  81. s = connect_socket()
  82. payload = b'QUIT\r\n'
  83. s.send(payload)
  84. data = s.recv(BUF)
  85. s.close()
  86. return True if len(data) is 0 else False
  87.  
  88. @tests
  89. def testNotGET():
  90. s = connect_socket()
  91. payload = b'GOT filename.txt\r\n'
  92. s.send(payload)
  93. data = s.recv(BUF).decode()
  94. s.close()
  95. return True if data == '-ERR\r\n' else False
  96.  
  97. @tests
  98. def testMultipleFilenames():
  99. s = connect_socket()
  100. payload = b'GET file.txt file2.txt\r\n'
  101. s.send(payload)
  102. data = s.recv(BUF).decode()
  103. s.close()
  104. return True if data == '-ERR\r\n' else False
  105.  
  106. @tests
  107.  
  108. def main():
  109. if len(sys.argv) < 3:
  110. print(f'Usage: {sys.argv[0]} <server_IP> <server_PORT')
  111. exit(0)
  112. for test in tests_suite:
  113. print(f'{test.__name__} ==> ')
  114. if(test()):
  115. print(f'TRUE')
  116. else:
  117. print(f'FALSE -> EXITING')
  118. exit(0)
  119.  
  120. if __name__ == '__main__':
  121. main()
Add Comment
Please, Sign In to add comment