test.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. # Utility for testing the web server. Test cases:
  17. # Assume the device supports 'n' simultaneous open sockets
  18. #
  19. # HTTP Server Tests
  20. #
  21. # 0. Firmware Settings:
  22. # - Create a dormant thread whose sole job is to call httpd_stop() when instructed
  23. # - Measure the following before httpd_start() is called:
  24. # - current free memory
  25. # - current free sockets
  26. # - Measure the same whenever httpd_stop is called
  27. # - Register maximum possible URI handlers: should be successful
  28. # - Register one more URI handler: should fail
  29. # - Deregister on URI handler: should be successful
  30. # - Register on more URI handler: should succeed
  31. # - Register separate handlers for /hello, /hello/type_html. Also
  32. # ensure that /hello/type_html is registered BEFORE /hello. (tests
  33. # that largest matching URI is picked properly)
  34. # - Create URI handler /adder. Make sure it uses a custom free_ctx
  35. # structure to free it up
  36. # 1. Using Standard Python HTTP Client
  37. # - simple GET on /hello (returns Hello World. Ensures that basic
  38. # firmware tests are complete, or returns error)
  39. # - POST on /hello (should fail)
  40. # - PUT on /hello (should fail)
  41. # - simple POST on /echo (returns whatever the POST data)
  42. # - simple PUT on /echo (returns whatever the PUT data)
  43. # - GET on /echo (should fail)
  44. # - simple GET on /hello/type_html (returns Content type as text/html)
  45. # - simple GET on /hello/status_500 (returns HTTP status 500)
  46. # - simple GET on /false_uri (returns HTTP status 404)
  47. # - largest matching URI handler is picked is already verified because
  48. # of /hello and /hello/type_html tests
  49. #
  50. #
  51. # 2. Session Tests
  52. # - Sessions + Pipelining basics:
  53. # - Create max supported sessions
  54. # - On session i,
  55. # - send 3 back-to-back POST requests with data i on /adder
  56. # - read back 3 responses. They should be i, 2i and 3i
  57. # - Tests that
  58. # - pipelining works
  59. # - per-session context is maintained for all supported
  60. # sessions
  61. # - Close all sessions
  62. #
  63. # - Cleanup leftover data: Tests that the web server properly cleans
  64. # up leftover data
  65. # - Create a session
  66. # - POST on /leftover_data with 52 bytes of data (data includes
  67. # \r\n)(the handler only
  68. # reads first 10 bytes and returns them, leaving the rest of the
  69. # bytes unread)
  70. # - GET on /hello (should return 'Hello World')
  71. # - POST on /false_uri with 52 bytes of data (data includes \r\n)
  72. # (should return HTTP 404)
  73. # - GET on /hello (should return 'Hello World')
  74. #
  75. # - Test HTTPd Asynchronous response
  76. # - Create a session
  77. # - GET on /async_data
  78. # - returns 'Hello World!' as a response
  79. # - the handler schedules an async response, which generates a second
  80. # response 'Hello Double World!'
  81. #
  82. # - Spillover test
  83. # - Create max supported sessions with the web server
  84. # - GET /hello on all the sessions (should return Hello World)
  85. # - Create one more session, this should fail
  86. # - GET /hello on all the sessions (should return Hello World)
  87. #
  88. # - Timeout test
  89. # - Create a session and only Send 'GE' on the same (simulates a
  90. # client that left the network halfway through a request)
  91. # - Wait for recv-wait-timeout
  92. # - Server should automatically close the socket
  93. ############# TODO TESTS #############
  94. # 3. Stress Tests
  95. #
  96. # - httperf
  97. # - Run the following httperf command:
  98. # httperf --server=10.31.130.126 --wsess=8,50,0.5 --rate 8 --burst-length 2
  99. #
  100. # - The above implies that the test suite will open
  101. # - 8 simultaneous connections with the server
  102. # - the rate of opening the sessions will be 8 per sec. So in our
  103. # case, a new connection will be opened every 0.2 seconds for 1 second
  104. # - The burst length 2 indicates that 2 requests will be sent
  105. # simultaneously on the same connection in a single go
  106. # - 0.5 seconds is the time between sending out 2 bursts
  107. # - 50 is the total number of requests that will be sent out
  108. #
  109. # - So in the above example, the test suite will open 8
  110. # connections, each separated by 0.2 seconds. On each connection
  111. # it will send 2 requests in a single burst. The bursts on a
  112. # single connection will be separated by 0.5 seconds. A total of
  113. # 25 bursts (25 x 2 = 50) will be sent out
  114. # 4. Leak Tests
  115. # - Simple Leak test
  116. # - Simple GET on /hello/restart (returns success, stop web server, measures leaks, restarts webserver)
  117. # - Simple GET on /hello/restart_results (returns the leak results)
  118. # - Leak test with open sockets
  119. # - Open 8 sessions
  120. # - Simple GET on /hello/restart (returns success, stop web server,
  121. # measures leaks, restarts webserver)
  122. # - All sockets should get closed
  123. # - Simple GET on /hello/restart_results (returns the leak results)
  124. import threading
  125. from multiprocessing import Process, Queue
  126. import socket
  127. import time
  128. import argparse
  129. import httplib
  130. import sys
  131. import string
  132. import random
  133. _verbose_ = False
  134. class Session:
  135. def __init__(self, addr, port):
  136. self.client = socket.create_connection((addr, int(port)))
  137. self.client.settimeout(15)
  138. self.target = addr
  139. self.status = 0
  140. self.encoding = ''
  141. self.content_type = ''
  142. self.content_len = 0
  143. def send_err_check(self, request, data=None):
  144. rval = True
  145. try:
  146. self.client.sendall(request);
  147. if data:
  148. self.client.sendall(data)
  149. except socket.error as err:
  150. self.client.close()
  151. print "Socket Error in send :", err
  152. rval = False
  153. return rval
  154. def send_get(self, path, headers=None):
  155. request = "GET " + path + " HTTP/1.1\r\nHost: " + self.target
  156. if headers:
  157. for field, value in headers.iteritems():
  158. request += "\r\n"+field+": "+value
  159. request += "\r\n\r\n"
  160. return self.send_err_check(request)
  161. def send_put(self, path, data, headers=None):
  162. request = "PUT " + path + " HTTP/1.1\r\nHost: " + self.target
  163. if headers:
  164. for field, value in headers.iteritems():
  165. request += "\r\n"+field+": "+value
  166. request += "\r\nContent-Length: " + str(len(data)) +"\r\n\r\n"
  167. return self.send_err_check(request, data)
  168. def send_post(self, path, data, headers=None):
  169. request = "POST " + path + " HTTP/1.1\r\nHost: " + self.target
  170. if headers:
  171. for field, value in headers.iteritems():
  172. request += "\r\n"+field+": "+value
  173. request += "\r\nContent-Length: " + str(len(data)) +"\r\n\r\n"
  174. return self.send_err_check(request, data)
  175. def read_resp_hdrs(self):
  176. try:
  177. state = 'nothing'
  178. resp_read = ''
  179. while True:
  180. char = self.client.recv(1)
  181. if char == '\r' and state == 'nothing':
  182. state = 'first_cr'
  183. elif char == '\n' and state == 'first_cr':
  184. state = 'first_lf'
  185. elif char == '\r' and state == 'first_lf':
  186. state = 'second_cr'
  187. elif char == '\n' and state == 'second_cr':
  188. state = 'second_lf'
  189. else:
  190. state = 'nothing'
  191. resp_read += char
  192. if state == 'second_lf':
  193. break
  194. # Handle first line
  195. line_hdrs = resp_read.splitlines()
  196. line_comp = line_hdrs[0].split()
  197. self.status = line_comp[1]
  198. del line_hdrs[0]
  199. self.encoding = ''
  200. self.content_type = ''
  201. headers = dict()
  202. # Process other headers
  203. for h in range(len(line_hdrs)):
  204. line_comp = line_hdrs[h].split(':')
  205. if line_comp[0] == 'Content-Length':
  206. self.content_len = int(line_comp[1])
  207. if line_comp[0] == 'Content-Type':
  208. self.content_type = line_comp[1].lstrip()
  209. if line_comp[0] == 'Transfer-Encoding':
  210. self.encoding = line_comp[1].lstrip()
  211. if len(line_comp) == 2:
  212. headers[line_comp[0]] = line_comp[1].lstrip()
  213. return headers
  214. except socket.error as err:
  215. self.client.close()
  216. print "Socket Error in recv :", err
  217. return None
  218. def read_resp_data(self):
  219. try:
  220. read_data = ''
  221. if self.encoding != 'chunked':
  222. while len(read_data) != self.content_len:
  223. read_data += self.client.recv(self.content_len)
  224. else:
  225. chunk_data_buf = ''
  226. while (True):
  227. # Read one character into temp buffer
  228. read_ch = self.client.recv(1)
  229. # Check CRLF
  230. if (read_ch == '\r'):
  231. read_ch = self.client.recv(1)
  232. if (read_ch == '\n'):
  233. # If CRLF decode length of chunk
  234. chunk_len = int(chunk_data_buf, 16)
  235. # Keep adding to contents
  236. self.content_len += chunk_len
  237. rem_len = chunk_len
  238. while (rem_len):
  239. new_data = self.client.recv(rem_len)
  240. read_data += new_data
  241. rem_len -= len(new_data)
  242. chunk_data_buf = ''
  243. # Fetch remaining CRLF
  244. if self.client.recv(2) != "\r\n":
  245. # Error in packet
  246. print "Error in chunked data"
  247. return None
  248. if not chunk_len:
  249. # If last chunk
  250. break
  251. continue
  252. chunk_data_buf += '\r'
  253. # If not CRLF continue appending
  254. # character to chunked data buffer
  255. chunk_data_buf += read_ch
  256. return read_data
  257. except socket.error as err:
  258. self.client.close()
  259. print "Socket Error in recv :", err
  260. return None
  261. def close(self):
  262. self.client.close()
  263. def test_val(text, expected, received):
  264. if expected != received:
  265. print " Fail!"
  266. print " [reason] " + text + ":"
  267. print " expected: " + str(expected)
  268. print " received: " + str(received)
  269. return False
  270. return True
  271. class adder_thread (threading.Thread):
  272. def __init__(self, id, dut, port):
  273. threading.Thread.__init__(self)
  274. self.id = id
  275. self.dut = dut
  276. self.depth = 3
  277. self.session = Session(dut, port)
  278. def run(self):
  279. self.response = []
  280. # Pipeline 3 requests
  281. if (_verbose_):
  282. print " Thread: Using adder start " + str(self.id)
  283. for _ in range(self.depth):
  284. self.session.send_post('/adder', str(self.id))
  285. time.sleep(2)
  286. for _ in range(self.depth):
  287. self.session.read_resp_hdrs()
  288. self.response.append(self.session.read_resp_data())
  289. def adder_result(self):
  290. if len(self.response) != self.depth:
  291. print "Error : missing response packets"
  292. return False
  293. for i in range(len(self.response)):
  294. if not test_val("Thread" + str(self.id) + " response[" + str(i) + "]",
  295. str(self.id * (i + 1)), str(self.response[i])):
  296. return False
  297. return True
  298. def close(self):
  299. self.session.close()
  300. def get_hello(dut, port):
  301. # GET /hello should return 'Hello World!'
  302. print "[test] GET /hello returns 'Hello World!' =>",
  303. conn = httplib.HTTPConnection(dut, int(port))
  304. conn.request("GET", "/hello")
  305. resp = conn.getresponse()
  306. if not test_val("status_code", 200, resp.status):
  307. conn.close()
  308. return False
  309. if not test_val("data", "Hello World!", resp.read()):
  310. conn.close()
  311. return False
  312. if not test_val("data", "application/json", resp.getheader('Content-Type')):
  313. conn.close()
  314. return False
  315. print "Success"
  316. conn.close()
  317. return True
  318. def put_hello(dut, port):
  319. # PUT /hello returns 405'
  320. print "[test] PUT /hello returns 405' =>",
  321. conn = httplib.HTTPConnection(dut, int(port))
  322. conn.request("PUT", "/hello", "Hello")
  323. resp = conn.getresponse()
  324. if not test_val("status_code", 405, resp.status):
  325. conn.close()
  326. return False
  327. print "Success"
  328. conn.close()
  329. return True
  330. def post_hello(dut, port):
  331. # POST /hello returns 405'
  332. print "[test] POST /hello returns 404' =>",
  333. conn = httplib.HTTPConnection(dut, int(port))
  334. conn.request("POST", "/hello", "Hello")
  335. resp = conn.getresponse()
  336. if not test_val("status_code", 405, resp.status):
  337. conn.close()
  338. return False
  339. print "Success"
  340. conn.close()
  341. return True
  342. def post_echo(dut, port):
  343. # POST /echo echoes data'
  344. print "[test] POST /echo echoes data' =>",
  345. conn = httplib.HTTPConnection(dut, int(port))
  346. conn.request("POST", "/echo", "Hello")
  347. resp = conn.getresponse()
  348. if not test_val("status_code", 200, resp.status):
  349. conn.close()
  350. return False
  351. if not test_val("data", "Hello", resp.read()):
  352. conn.close()
  353. return False
  354. print "Success"
  355. conn.close()
  356. return True
  357. def put_echo(dut, port):
  358. # PUT /echo echoes data'
  359. print "[test] PUT /echo echoes data' =>",
  360. conn = httplib.HTTPConnection(dut, int(port))
  361. conn.request("PUT", "/echo", "Hello")
  362. resp = conn.getresponse()
  363. if not test_val("status_code", 200, resp.status):
  364. conn.close()
  365. return False
  366. if not test_val("data", "Hello", resp.read()):
  367. conn.close()
  368. return False
  369. print "Success"
  370. conn.close()
  371. return True
  372. def get_echo(dut, port):
  373. # GET /echo returns 404'
  374. print "[test] GET /echo returns 405' =>",
  375. conn = httplib.HTTPConnection(dut, int(port))
  376. conn.request("GET", "/echo")
  377. resp = conn.getresponse()
  378. if not test_val("status_code", 405, resp.status):
  379. conn.close()
  380. return False
  381. print "Success"
  382. conn.close()
  383. return True
  384. def get_hello_type(dut, port):
  385. # GET /hello/type_html returns text/html as Content-Type'
  386. print "[test] GET /hello/type_html has Content-Type of text/html =>",
  387. conn = httplib.HTTPConnection(dut, int(port))
  388. conn.request("GET", "/hello/type_html")
  389. resp = conn.getresponse()
  390. if not test_val("status_code", 200, resp.status):
  391. conn.close()
  392. return False
  393. if not test_val("data", "Hello World!", resp.read()):
  394. conn.close()
  395. return False
  396. if not test_val("data", "text/html", resp.getheader('Content-Type')):
  397. conn.close()
  398. return False
  399. print "Success"
  400. conn.close()
  401. return True
  402. def get_hello_status(dut, port):
  403. # GET /hello/status_500 returns status 500'
  404. print "[test] GET /hello/status_500 returns status 500 =>",
  405. conn = httplib.HTTPConnection(dut, int(port))
  406. conn.request("GET", "/hello/status_500")
  407. resp = conn.getresponse()
  408. if not test_val("status_code", 500, resp.status):
  409. conn.close()
  410. return False
  411. print "Success"
  412. conn.close()
  413. return True
  414. def get_false_uri(dut, port):
  415. # GET /false_uri returns status 404'
  416. print "[test] GET /false_uri returns status 404 =>",
  417. conn = httplib.HTTPConnection(dut, int(port))
  418. conn.request("GET", "/false_uri")
  419. resp = conn.getresponse()
  420. if not test_val("status_code", 404, resp.status):
  421. conn.close()
  422. return False
  423. print "Success"
  424. conn.close()
  425. return True
  426. def parallel_sessions_adder(dut, port, max_sessions):
  427. # POSTs on /adder in parallel sessions
  428. print "[test] POST {pipelined} on /adder in " + str(max_sessions) + " sessions =>",
  429. t = []
  430. # Create all sessions
  431. for i in xrange(max_sessions):
  432. t.append(adder_thread(i, dut, port))
  433. for i in xrange(len(t)):
  434. t[i].start()
  435. for i in xrange(len(t)):
  436. t[i].join()
  437. res = True
  438. for i in xrange(len(t)):
  439. if not test_val("Thread" + str(i) + " Failed", t[i].adder_result(), True):
  440. res = False
  441. t[i].close()
  442. if (res):
  443. print "Success"
  444. return res
  445. def async_response_test(dut, port):
  446. # Test that an asynchronous work is executed in the HTTPD's context
  447. # This is tested by reading two responses over the same session
  448. print "[test] Test HTTPD Work Queue (Async response) =>",
  449. s = Session(dut, port)
  450. s.send_get('/async_data')
  451. s.read_resp_hdrs()
  452. if not test_val("First Response", "Hello World!", s.read_resp_data()):
  453. s.close()
  454. return False
  455. s.read_resp_hdrs()
  456. if not test_val("Second Response", "Hello Double World!", s.read_resp_data()):
  457. s.close()
  458. return False
  459. s.close()
  460. print "Success"
  461. return True
  462. def leftover_data_test(dut, port):
  463. # Leftover data in POST is purged (valid and invalid URIs)
  464. print "[test] Leftover data in POST is purged (valid and invalid URIs) =>",
  465. s = httplib.HTTPConnection(dut + ":" + port)
  466. s.request("POST", url='/leftover_data', body="abcdefghijklmnopqrstuvwxyz\r\nabcdefghijklmnopqrstuvwxyz")
  467. resp = s.getresponse()
  468. if not test_val("Partial data", "abcdefghij", resp.read()):
  469. s.close()
  470. return False
  471. s.request("GET", url='/hello')
  472. resp = s.getresponse()
  473. if not test_val("Hello World Data", "Hello World!", resp.read()):
  474. s.close()
  475. return False
  476. s.request("POST", url='/false_uri', body="abcdefghijklmnopqrstuvwxyz\r\nabcdefghijklmnopqrstuvwxyz")
  477. resp = s.getresponse()
  478. if not test_val("False URI Status", str(404), str(resp.status)):
  479. s.close()
  480. return False
  481. resp.read()
  482. s.request("GET", url='/hello')
  483. resp = s.getresponse()
  484. if not test_val("Hello World Data", "Hello World!", resp.read()):
  485. s.close()
  486. return False
  487. s.close()
  488. print "Success"
  489. return True
  490. def spillover_session(dut, port, max_sess):
  491. # Session max_sess_sessions + 1 is rejected
  492. print "[test] Session max_sess_sessions (" + str(max_sess) + ") + 1 is rejected =>",
  493. s = []
  494. _verbose_ = True
  495. for i in xrange(max_sess + 1):
  496. if (_verbose_):
  497. print "Executing " + str(i)
  498. try:
  499. a = httplib.HTTPConnection(dut + ":" + port)
  500. a.request("GET", url='/hello')
  501. resp = a.getresponse()
  502. if not test_val("Connection " + str(i), "Hello World!", resp.read()):
  503. a.close()
  504. break
  505. s.append(a)
  506. except:
  507. if (_verbose_):
  508. print "Connection " + str(i) + " rejected"
  509. a.close()
  510. break
  511. # Close open connections
  512. for a in s:
  513. a.close()
  514. # Check if number of connections is equal to max_sess
  515. print ["Fail","Success"][len(s) == max_sess]
  516. return (len(s) == max_sess)
  517. def recv_timeout_test(dut, port):
  518. print "[test] Timeout occurs if partial packet sent =>",
  519. s = Session(dut, port)
  520. s.client.sendall("GE")
  521. s.read_resp_hdrs()
  522. resp = s.read_resp_data()
  523. if not test_val("Request Timeout", "Server closed this connection", resp):
  524. s.close()
  525. return False
  526. s.close()
  527. print "Success"
  528. return True
  529. def packet_size_limit_test(dut, port, test_size):
  530. print "[test] send size limit test =>",
  531. retry = 5
  532. while (retry):
  533. retry -= 1
  534. print "data size = ", test_size
  535. s = httplib.HTTPConnection(dut + ":" + port)
  536. random_data = ''.join(string.printable[random.randint(0,len(string.printable))-1] for _ in range(test_size))
  537. path = "/echo"
  538. s.request("POST", url=path, body=random_data)
  539. resp = s.getresponse()
  540. if not test_val("Error", "200", str(resp.status)):
  541. if test_val("Error", "408", str(resp.status)):
  542. print "Data too large to be allocated"
  543. test_size = test_size/10
  544. else:
  545. print "Unexpected error"
  546. s.close()
  547. print "Retry..."
  548. continue
  549. resp = resp.read()
  550. result = (resp == random_data)
  551. if not result:
  552. test_val("Data size", str(len(random_data)), str(len(resp)))
  553. s.close()
  554. print "Retry..."
  555. continue
  556. s.close()
  557. print "Success"
  558. return True
  559. print "Failed"
  560. return False
  561. def code_500_server_error_test(dut, port):
  562. print "[test] 500 Server Error test =>",
  563. s = Session(dut, port)
  564. s.client.sendall("abcdefgh\0")
  565. s.read_resp_hdrs()
  566. resp = s.read_resp_data()
  567. # Presently server sends back 400 Bad Request
  568. #if not test_val("Server Error", "500", s.status):
  569. #s.close()
  570. #return False
  571. if not test_val("Server Error", "400", s.status):
  572. s.close()
  573. return False
  574. s.close()
  575. print "Success"
  576. return True
  577. def code_501_method_not_impl(dut, port):
  578. print "[test] 501 Method Not Implemented =>",
  579. s = Session(dut, port)
  580. path = "/hello"
  581. s.client.sendall("ABC " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n")
  582. s.read_resp_hdrs()
  583. resp = s.read_resp_data()
  584. # Presently server sends back 400 Bad Request
  585. #if not test_val("Server Error", "501", s.status):
  586. #s.close()
  587. #return False
  588. if not test_val("Server Error", "400", s.status):
  589. s.close()
  590. return False
  591. s.close()
  592. print "Success"
  593. return True
  594. def code_505_version_not_supported(dut, port):
  595. print "[test] 505 Version Not Supported =>",
  596. s = Session(dut, port)
  597. path = "/hello"
  598. s.client.sendall("GET " + path + " HTTP/2.0\r\nHost: " + dut + "\r\n\r\n")
  599. s.read_resp_hdrs()
  600. resp = s.read_resp_data()
  601. if not test_val("Server Error", "505", s.status):
  602. s.close()
  603. return False
  604. s.close()
  605. print "Success"
  606. return True
  607. def code_400_bad_request(dut, port):
  608. print "[test] 400 Bad Request =>",
  609. s = Session(dut, port)
  610. path = "/hello"
  611. s.client.sendall("XYZ " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n")
  612. s.read_resp_hdrs()
  613. resp = s.read_resp_data()
  614. if not test_val("Client Error", "400", s.status):
  615. s.close()
  616. return False
  617. s.close()
  618. print "Success"
  619. return True
  620. def code_404_not_found(dut, port):
  621. print "[test] 404 Not Found =>",
  622. s = Session(dut, port)
  623. path = "/dummy"
  624. s.client.sendall("GET " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n")
  625. s.read_resp_hdrs()
  626. resp = s.read_resp_data()
  627. if not test_val("Client Error", "404", s.status):
  628. s.close()
  629. return False
  630. s.close()
  631. print "Success"
  632. return True
  633. def code_405_method_not_allowed(dut, port):
  634. print "[test] 405 Method Not Allowed =>",
  635. s = Session(dut, port)
  636. path = "/hello"
  637. s.client.sendall("POST " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n")
  638. s.read_resp_hdrs()
  639. resp = s.read_resp_data()
  640. if not test_val("Client Error", "405", s.status):
  641. s.close()
  642. return False
  643. s.close()
  644. print "Success"
  645. return True
  646. def code_408_req_timeout(dut, port):
  647. print "[test] 408 Request Timeout =>",
  648. s = Session(dut, port)
  649. s.client.sendall("POST /echo HTTP/1.1\r\nHost: " + dut + "\r\nContent-Length: 10\r\n\r\nABCD")
  650. s.read_resp_hdrs()
  651. resp = s.read_resp_data()
  652. if not test_val("Client Error", "408", s.status):
  653. s.close()
  654. return False
  655. s.close()
  656. print "Success"
  657. return True
  658. def code_411_length_required(dut, port):
  659. print "[test] 411 Length Required =>",
  660. s = Session(dut, port)
  661. path = "/echo"
  662. s.client.sendall("POST " + path + " HTTP/1.1\r\nHost: " + dut + "\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n")
  663. s.read_resp_hdrs()
  664. resp = s.read_resp_data()
  665. # Presently server sends back 400 Bad Request
  666. #if not test_val("Client Error", "411", s.status):
  667. #s.close()
  668. #return False
  669. if not test_val("Client Error", "400", s.status):
  670. s.close()
  671. return False
  672. s.close()
  673. print "Success"
  674. return True
  675. def send_getx_uri_len(dut, port, length):
  676. s = Session(dut, port)
  677. method = "GET "
  678. version = " HTTP/1.1\r\n"
  679. path = "/"+"x"*(length - len(method) - len(version) - len("/"))
  680. s.client.sendall(method)
  681. time.sleep(1)
  682. s.client.sendall(path)
  683. time.sleep(1)
  684. s.client.sendall(version + "Host: " + dut + "\r\n\r\n")
  685. s.read_resp_hdrs()
  686. resp = s.read_resp_data()
  687. s.close()
  688. return s.status
  689. def code_414_uri_too_long(dut, port, max_uri_len):
  690. print "[test] 414 URI Too Long =>",
  691. status = send_getx_uri_len(dut, port, max_uri_len)
  692. if not test_val("Client Error", "404", status):
  693. return False
  694. status = send_getx_uri_len(dut, port, max_uri_len + 1)
  695. if not test_val("Client Error", "414", status):
  696. return False
  697. print "Success"
  698. return True
  699. def send_postx_hdr_len(dut, port, length):
  700. s = Session(dut, port)
  701. path = "/echo"
  702. host = "Host: " + dut
  703. custom_hdr_field = "\r\nCustom: "
  704. custom_hdr_val = "x"*(length - len(host) - len(custom_hdr_field) - len("\r\n\r\n") + len("0"))
  705. request = "POST " + path + " HTTP/1.1\r\n" + host + custom_hdr_field + custom_hdr_val + "\r\n\r\n"
  706. s.client.sendall(request[:length/2])
  707. time.sleep(1)
  708. s.client.sendall(request[length/2:])
  709. hdr = s.read_resp_hdrs()
  710. resp = s.read_resp_data()
  711. s.close()
  712. if "Custom" in hdr:
  713. return (hdr["Custom"] == custom_hdr_val), resp
  714. return False, s.status
  715. def code_431_hdr_too_long(dut, port, max_hdr_len):
  716. print "[test] 431 Header Too Long =>",
  717. res, status = send_postx_hdr_len(dut, port, max_hdr_len)
  718. if not res:
  719. return False
  720. res, status = send_postx_hdr_len(dut, port, max_hdr_len + 1)
  721. if not test_val("Client Error", "431", status):
  722. return False
  723. print "Success"
  724. return True
  725. def test_upgrade_not_supported(dut, port):
  726. print "[test] Upgrade Not Supported =>",
  727. s = Session(dut, port)
  728. path = "/hello"
  729. s.client.sendall("OPTIONS * HTTP/1.1\r\nHost:" + dut + "\r\nUpgrade: TLS/1.0\r\nConnection: Upgrade\r\n\r\n");
  730. s.read_resp_hdrs()
  731. resp = s.read_resp_data()
  732. if not test_val("Client Error", "200", s.status):
  733. s.close()
  734. return False
  735. s.close()
  736. print "Success"
  737. return True
  738. if __name__ == '__main__':
  739. ########### Execution begins here...
  740. # Configuration
  741. # Max number of threads/sessions
  742. max_sessions = 7
  743. max_uri_len = 512
  744. max_hdr_len = 512
  745. parser = argparse.ArgumentParser(description='Run HTTPD Test')
  746. parser.add_argument('-4','--ipv4', help='IPv4 address')
  747. parser.add_argument('-6','--ipv6', help='IPv6 address')
  748. parser.add_argument('-p','--port', help='Port')
  749. args = vars(parser.parse_args())
  750. dut4 = args['ipv4']
  751. dut6 = args['ipv6']
  752. port = args['port']
  753. dut = dut4
  754. _verbose_ = True
  755. print "### Basic HTTP Client Tests"
  756. get_hello(dut, port)
  757. post_hello(dut, port)
  758. put_hello(dut, port)
  759. post_echo(dut, port)
  760. get_echo(dut, port)
  761. put_echo(dut, port)
  762. get_hello_type(dut, port)
  763. get_hello_status(dut, port)
  764. get_false_uri(dut, port)
  765. print "### Error code tests"
  766. code_500_server_error_test(dut, port)
  767. code_501_method_not_impl(dut, port)
  768. code_505_version_not_supported(dut, port)
  769. code_400_bad_request(dut, port)
  770. code_404_not_found(dut, port)
  771. code_405_method_not_allowed(dut, port)
  772. code_408_req_timeout(dut, port)
  773. code_414_uri_too_long(dut, port, max_uri_len)
  774. code_431_hdr_too_long(dut, port, max_hdr_len)
  775. test_upgrade_not_supported(dut, port)
  776. # Not supported yet (Error on chunked request)
  777. ###code_411_length_required(dut, port)
  778. print "### Sessions and Context Tests"
  779. parallel_sessions_adder(dut, port, max_sessions)
  780. leftover_data_test(dut, port)
  781. async_response_test(dut, port)
  782. spillover_session(dut, port, max_sessions)
  783. recv_timeout_test(dut, port)
  784. packet_size_limit_test(dut, port, 50*1024)
  785. get_hello(dut, port)
  786. sys.exit()