test.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. import socket
  126. import time
  127. import argparse
  128. import requests
  129. import signal
  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(30)
  138. self.target = addr
  139. def send_get(self, path, headers=None):
  140. request = "GET " + path + " HTTP/1.1\r\nHost: " + self.target
  141. if headers:
  142. for field, value in headers.iteritems():
  143. request += "\r\n"+field+": "+value
  144. request += "\r\n\r\n"
  145. self.client.send(request);
  146. def send_put(self, path, data, headers=None):
  147. request = "PUT " + path + " HTTP/1.1\r\nHost: " + self.target
  148. if headers:
  149. for field, value in headers.iteritems():
  150. request += "\r\n"+field+": "+value
  151. request += "\r\nContent-Length: " + str(len(data)) +"\r\n\r\n"
  152. self.client.send(request)
  153. self.client.send(data)
  154. def send_post(self, path, data, headers=None):
  155. request = "POST " + 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\nContent-Length: " + str(len(data)) +"\r\n\r\n"
  160. self.client.send(request)
  161. self.client.send(data)
  162. def read_resp_hdrs(self):
  163. state = 'nothing'
  164. resp_read = ''
  165. while True:
  166. char = self.client.recv(1)
  167. if char == '\r' and state == 'nothing':
  168. state = 'first_cr'
  169. elif char == '\n' and state == 'first_cr':
  170. state = 'first_lf'
  171. elif char == '\r' and state == 'first_lf':
  172. state = 'second_cr'
  173. elif char == '\n' and state == 'second_cr':
  174. state = 'second_lf'
  175. else:
  176. state = 'nothing'
  177. resp_read += char
  178. if state == 'second_lf':
  179. break;
  180. # Handle first line
  181. line_hdrs = resp_read.splitlines()
  182. line_comp = line_hdrs[0].split()
  183. self.status = line_comp[1]
  184. del line_hdrs[0]
  185. self.encoding = ''
  186. self.content_type = ''
  187. headers = dict()
  188. # Process other headers
  189. for h in range(len(line_hdrs)):
  190. line_comp = line_hdrs[h].split(':')
  191. if line_comp[0] == 'Content-Length':
  192. self.content_len = int(line_comp[1])
  193. if line_comp[0] == 'Content-Type':
  194. self.content_type = line_comp[1].lstrip()
  195. if line_comp[0] == 'Transfer-Encoding':
  196. self.encoding = line_comp[1].lstrip()
  197. if len(line_comp) == 2:
  198. headers[line_comp[0]] = line_comp[1].lstrip()
  199. return headers
  200. def read_resp_data(self):
  201. read_data = ''
  202. if self.encoding != 'chunked':
  203. while len(read_data) != self.content_len:
  204. read_data += self.client.recv(self.content_len)
  205. self.content_len = 0
  206. else:
  207. chunk_data_buf = ''
  208. while (True):
  209. # Read one character into temp buffer
  210. read_ch = self.client.recv(1)
  211. # Check CRLF
  212. if (read_ch == '\r'):
  213. read_ch = self.client.recv(1)
  214. if (read_ch == '\n'):
  215. # If CRLF decode length of chunk
  216. chunk_len = int(chunk_data_buf, 16)
  217. # Keep adding to contents
  218. self.content_len += chunk_len
  219. read_data += self.client.recv(chunk_len)
  220. chunk_data_buf = ''
  221. # Fetch remaining CRLF
  222. if self.client.recv(2) != "\r\n":
  223. # Error in packet
  224. return None
  225. if not chunk_len:
  226. # If last chunk
  227. break
  228. continue
  229. chunk_data_buf += '\r'
  230. # If not CRLF continue appending
  231. # character to chunked data buffer
  232. chunk_data_buf += read_ch
  233. return read_data
  234. def close(self):
  235. self.client.close()
  236. def test_val(text, expected, received):
  237. if expected != received:
  238. print " Fail!"
  239. print " [reason] " + text + ":"
  240. print " expected: " + str(expected)
  241. print " received: " + str(received)
  242. return False
  243. return True
  244. class myThread (threading.Thread):
  245. def __init__(self, id, dut, port):
  246. threading.Thread.__init__(self)
  247. self.id = id
  248. self.dut = dut
  249. self.session = Session(dut, port)
  250. def run(self):
  251. self.response = []
  252. # Pipeline 3 requests
  253. if (_verbose_):
  254. print " Thread: Using adder start " + str(self.id)
  255. self.session.send_post('/adder', str(self.id));
  256. time.sleep(1)
  257. self.session.send_post('/adder', str(self.id));
  258. time.sleep(1)
  259. self.session.send_post('/adder', str(self.id));
  260. time.sleep(1)
  261. self.session.read_resp_hdrs()
  262. self.response.append(self.session.read_resp_data())
  263. self.session.read_resp_hdrs()
  264. self.response.append(self.session.read_resp_data())
  265. self.session.read_resp_hdrs()
  266. self.response.append(self.session.read_resp_data())
  267. def adder_result(self):
  268. for i in range(len(self.response)):
  269. # print self.response[i]
  270. if not test_val("thread" + str(self.id) + ": response[" + str(i) + "]",
  271. str(self.id * (i + 1)), str(self.response[i])):
  272. return False
  273. return True
  274. def close(self):
  275. self.session.close()
  276. def get_hello(dut, port):
  277. # GET /hello should return 'Hello World!'
  278. print "[test] GET /hello returns 'Hello World!' =>",
  279. r = requests.get("http://" + dut + ":" + port + "/hello")
  280. if not test_val("status_code", 200, r.status_code):
  281. return False
  282. if not test_val("data", "Hello World!", r.text):
  283. return False
  284. if not test_val("data", "application/json", r.headers['Content-Type']):
  285. return False
  286. print "Success"
  287. return True
  288. def post_hello(dut, port):
  289. # PUT /hello returns 405'
  290. print "[test] PUT /hello returns 405' =>",
  291. r = requests.put("http://" + dut + ":" + port + "/hello", data="Hello")
  292. if not test_val("status_code", 405, r.status_code):
  293. return False
  294. print "Success"
  295. return True
  296. def put_hello(dut, port):
  297. # POST /hello returns 405'
  298. print "[test] POST /hello returns 404' =>",
  299. r = requests.post("http://" + dut + ":" + port + "/hello", data="Hello")
  300. if not test_val("status_code", 405, r.status_code):
  301. return False
  302. print "Success"
  303. return True
  304. def post_echo(dut, port):
  305. # POST /echo echoes data'
  306. print "[test] POST /echo echoes data' =>",
  307. r = requests.post("http://" + dut + ":" + port + "/echo", data="Hello")
  308. if not test_val("status_code", 200, r.status_code):
  309. return False
  310. if not test_val("data", "Hello", r.text):
  311. return False
  312. print "Success"
  313. return True
  314. def put_echo(dut, port):
  315. # POST /echo echoes data'
  316. print "[test] PUT /echo echoes data' =>",
  317. r = requests.put("http://" + dut + ":" + port + "/echo", data="Hello")
  318. if not test_val("status_code", 200, r.status_code):
  319. return False
  320. if not test_val("data", "Hello", r.text):
  321. return False
  322. print "Success"
  323. return True
  324. def get_echo(dut, port):
  325. # GET /echo returns 404'
  326. print "[test] GET /echo returns 405' =>",
  327. r = requests.get("http://" + dut + ":" + port + "/echo")
  328. if not test_val("status_code", 405, r.status_code):
  329. return False
  330. print "Success"
  331. return True
  332. def get_hello_type(dut, port):
  333. # GET /hello/type_html returns text/html as Content-Type'
  334. print "[test] GET /hello/type_html has Content-Type of text/html =>",
  335. r = requests.get("http://" + dut + ":" + port + "/hello/type_html")
  336. if not test_val("status_code", 200, r.status_code):
  337. return False
  338. if not test_val("data", "Hello World!", r.text):
  339. return False
  340. if not test_val("data", "text/html", r.headers['Content-Type']):
  341. return False
  342. print "Success"
  343. return True
  344. def get_hello_status(dut, port):
  345. # GET /hello/status_500 returns status 500'
  346. print "[test] GET /hello/status_500 returns status 500 =>",
  347. r = requests.get("http://" + dut + ":" + port + "/hello/status_500")
  348. if not test_val("status_code", 500, r.status_code):
  349. return False
  350. print "Success"
  351. return True
  352. def get_false_uri(dut, port):
  353. # GET /false_uri returns status 404'
  354. print "[test] GET /false_uri returns status 404 =>",
  355. r = requests.get("http://" + dut + ":" + port + "/false_uri")
  356. if not test_val("status_code", 404, r.status_code):
  357. return False
  358. print "Success"
  359. return True
  360. def parallel_sessions_adder(dut, port, max_sessions):
  361. # POSTs on /adder in parallel sessions
  362. print "[test] POST {pipelined} on /adder in " + str(max_sessions) + " sessions =>",
  363. t = []
  364. # Create all sessions
  365. for i in xrange(max_sessions):
  366. t.append(myThread(i * 2, dut, port))
  367. for i in xrange(len(t)):
  368. t[i].start()
  369. for i in xrange(len(t)):
  370. t[i].join()
  371. res = True
  372. for i in xrange(len(t)):
  373. if not t[i].adder_result():
  374. if not test_val("Thread" + str(i) + "Failed", "True", "False"):
  375. res = False
  376. t[i].close()
  377. if (res):
  378. print "Success"
  379. return res
  380. def async_response_test(dut, port):
  381. # Test that an asynchronous work is executed in the HTTPD's context
  382. # This is tested by reading two responses over the same session
  383. print "[test] Test HTTPD Work Queue (Async response) =>",
  384. s = Session(dut, port)
  385. s.send_get('/async_data')
  386. s.read_resp_hdrs()
  387. if not test_val("First Response", "Hello World!", s.read_resp_data()):
  388. s.close()
  389. return False
  390. s.read_resp_hdrs()
  391. if not test_val("Second Response", "Hello Double World!", s.read_resp_data()):
  392. s.close()
  393. return False
  394. s.close()
  395. print "Success"
  396. return True
  397. def leftover_data_test(dut, port):
  398. # Leftover data in POST is purged (valid and invalid URIs)
  399. print "[test] Leftover data in POST is purged (valid and invalid URIs) =>",
  400. s = Session(dut, port)
  401. s.send_post('/leftover_data',
  402. "abcdefghijklmnopqrstuvwxyz\r\nabcdefghijklmnopqrstuvwxyz")
  403. s.read_resp_hdrs()
  404. if not test_val("Partial data", "abcdefghij", s.read_resp_data()):
  405. s.close()
  406. return False
  407. s.send_get('/hello')
  408. s.read_resp_hdrs()
  409. if not test_val("Hello World Data", "Hello World!", s.read_resp_data()):
  410. s.close()
  411. return False
  412. s.send_post('/false_uri',
  413. "abcdefghijklmnopqrstuvwxyz\r\nabcdefghijklmnopqrstuvwxyz")
  414. s.read_resp_hdrs()
  415. if not test_val("False URI Status", str(404), str(s.status)):
  416. s.close()
  417. return False
  418. s.read_resp_data()
  419. s.send_get('/hello')
  420. s.read_resp_hdrs()
  421. if not test_val("Hello World Data", "Hello World!", s.read_resp_data()):
  422. s.close()
  423. return False
  424. s.close()
  425. print "Success"
  426. return True
  427. def timeout_handler(signum, frame):
  428. raise Exception("Timeout")
  429. def spillover_session(dut, port, max):
  430. # Session max_sessions + 1 is rejected
  431. print "[test] Session max_sessions (" + str(max) + ") + 1 is rejected =>",
  432. s = []
  433. # Register a timeout callback
  434. signal.signal(signal.SIGALRM, timeout_handler)
  435. for i in xrange(max + 1):
  436. if (_verbose_):
  437. print "Executing " + str(i)
  438. a = Session(dut, port)
  439. a.send_get('/hello')
  440. try:
  441. # Check for response timeout
  442. signal.alarm(5)
  443. a.read_resp_hdrs()
  444. a.read_resp_data()
  445. signal.alarm(0)
  446. # Control reaches here only if connection was established
  447. s.append(a)
  448. except Exception, msg:
  449. if (_verbose_):
  450. print str(msg) + ": Connection " + str(i) + " rejected"
  451. break
  452. # Close open connections
  453. for a in s:
  454. a.close()
  455. # Check if number of connections is equal to max
  456. print ["Fail","Success"][len(s) == max]
  457. return (len(s) == max)
  458. def recv_timeout_test(dut, port):
  459. print "[test] Timeout occurs if partial packet sent =>",
  460. signal.signal(signal.SIGALRM, timeout_handler)
  461. s = Session(dut, port)
  462. s.client.send("GE")
  463. try:
  464. signal.alarm(15)
  465. s.read_resp_hdrs()
  466. resp = s.read_resp_data()
  467. signal.alarm(0)
  468. if not test_val("Request Timeout", "Server closed this connection", resp):
  469. s.close()
  470. return False
  471. except:
  472. s.close()
  473. return False
  474. s.close()
  475. print "Success"
  476. return True
  477. def pipeline_test(dut, port, max_sess):
  478. print "[test] Pipelining test =>",
  479. s = [Session(dut, port) for _ in range(max_sess)]
  480. path = "/echo"
  481. pipeline_depth = 10
  482. header = "POST " + path + " HTTP/1.1\r\nHost: " + dut + "\r\nContent-Length: "
  483. s[0].client.send(header)
  484. for i in range(1, max_sess):
  485. for j in range(pipeline_depth):
  486. data = str(i) + ":" + str(j)
  487. request = header + str(len(data)) + "\r\n\r\n" + data
  488. s[i].client.send(request)
  489. s[0].client.send(str(len("0:0")) + "\r\n\r\n" + "0:0")
  490. for j in range(1, pipeline_depth):
  491. data = "0:" + str(j)
  492. request = header + str(len(data)) + "\r\n\r\n" + data
  493. s[0].client.send(request)
  494. res = True
  495. for i in range(max_sess):
  496. #time.sleep(1)
  497. for j in range(pipeline_depth):
  498. s[i].read_resp_hdrs()
  499. echo_data = s[i].read_resp_data()
  500. if (_verbose_):
  501. print "[" + str(i) + "][" + str(j) + "] = " + echo_data
  502. if not test_val("Echo Data", str(i) + ":" + str(j), echo_data):
  503. res = False
  504. s[i].close()
  505. #for i in range(max_sess):
  506. #s[i].close()
  507. if (res):
  508. print "Success"
  509. return res
  510. def packet_size_limit_test(dut, port, test_size):
  511. print "[test] LWIP send size limit test =>",
  512. s = Session(dut, port)
  513. random_data = ''.join(string.printable[random.randint(0,len(string.printable))-1] for _ in range(test_size))
  514. path = "/echo"
  515. s.send_post(path, random_data)
  516. s.read_resp_hdrs()
  517. resp = s.read_resp_data()
  518. result = (resp == random_data)
  519. if not result:
  520. test_val("Data size", str(len(random_data)), str(len(resp)))
  521. s.close()
  522. return False
  523. print "Success"
  524. s.close()
  525. return True
  526. def code_500_server_error_test(dut, port):
  527. print "[test] 500 Server Error test =>",
  528. s = Session(dut, port)
  529. s.client.send("abcdefgh\0")
  530. s.read_resp_hdrs()
  531. resp = s.read_resp_data()
  532. # Presently server sends back 400 Bad Request
  533. #if not test_val("Server Error", "500", s.status):
  534. #s.close()
  535. #return False
  536. if not test_val("Server Error", "400", s.status):
  537. s.close()
  538. return False
  539. s.close()
  540. print "Success"
  541. return True
  542. def code_501_method_not_impl(dut, port):
  543. print "[test] 501 Method Not Implemented =>",
  544. s = Session(dut, port)
  545. path = "/hello"
  546. s.client.send("ABC " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n")
  547. s.read_resp_hdrs()
  548. resp = s.read_resp_data()
  549. # Presently server sends back 400 Bad Request
  550. #if not test_val("Server Error", "501", s.status):
  551. #s.close()
  552. #return False
  553. if not test_val("Server Error", "400", s.status):
  554. s.close()
  555. return False
  556. s.close()
  557. print "Success"
  558. return True
  559. def code_505_version_not_supported(dut, port):
  560. print "[test] 505 Version Not Supported =>",
  561. s = Session(dut, port)
  562. path = "/hello"
  563. s.client.send("GET " + path + " HTTP/2.0\r\nHost: " + dut + "\r\n\r\n")
  564. s.read_resp_hdrs()
  565. resp = s.read_resp_data()
  566. if not test_val("Server Error", "505", s.status):
  567. s.close()
  568. return False
  569. s.close()
  570. print "Success"
  571. return True
  572. def code_400_bad_request(dut, port):
  573. print "[test] 400 Bad Request =>",
  574. s = Session(dut, port)
  575. path = "/hello"
  576. s.client.send("XYZ " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n")
  577. s.read_resp_hdrs()
  578. resp = s.read_resp_data()
  579. if not test_val("Client Error", "400", s.status):
  580. s.close()
  581. return False
  582. s.close()
  583. print "Success"
  584. return True
  585. def code_404_not_found(dut, port):
  586. print "[test] 404 Not Found =>",
  587. s = Session(dut, port)
  588. path = "/dummy"
  589. s.client.send("GET " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n")
  590. s.read_resp_hdrs()
  591. resp = s.read_resp_data()
  592. if not test_val("Client Error", "404", s.status):
  593. s.close()
  594. return False
  595. s.close()
  596. print "Success"
  597. return True
  598. def code_405_method_not_allowed(dut, port):
  599. print "[test] 405 Method Not Allowed =>",
  600. s = Session(dut, port)
  601. path = "/hello"
  602. s.client.send("POST " + path + " HTTP/1.1\r\nHost: " + dut + "\r\n\r\n")
  603. s.read_resp_hdrs()
  604. resp = s.read_resp_data()
  605. if not test_val("Client Error", "405", s.status):
  606. s.close()
  607. return False
  608. s.close()
  609. print "Success"
  610. return True
  611. def code_408_req_timeout(dut, port):
  612. print "[test] 408 Request Timeout =>",
  613. signal.signal(signal.SIGALRM, timeout_handler)
  614. s = Session(dut, port)
  615. s.client.send("POST /echo HTTP/1.1\r\nHost: " + dut + "\r\nContent-Length: 10\r\n\r\nABCD")
  616. try:
  617. signal.alarm(15)
  618. s.read_resp_hdrs()
  619. resp = s.read_resp_data()
  620. signal.alarm(0)
  621. if not test_val("Client Error", "408", s.status):
  622. s.close()
  623. return False
  624. except:
  625. s.close()
  626. return False
  627. s.close()
  628. print "Success"
  629. return True
  630. def code_411_length_required(dut, port):
  631. print "[test] 411 Length Required =>",
  632. s = Session(dut, port)
  633. path = "/echo"
  634. s.client.send("POST " + path + " HTTP/1.1\r\nHost: " + dut + "\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n")
  635. s.read_resp_hdrs()
  636. resp = s.read_resp_data()
  637. # Presently server sends back 400 Bad Request
  638. #if not test_val("Client Error", "411", s.status):
  639. #s.close()
  640. #return False
  641. if not test_val("Client Error", "400", s.status):
  642. s.close()
  643. return False
  644. s.close()
  645. print "Success"
  646. return True
  647. def send_getx_uri_len(dut, port, length):
  648. s = Session(dut, port)
  649. method = "GET "
  650. version = " HTTP/1.1\r\n"
  651. path = "/"+"x"*(length - len(method) - len(version) - len("/"))
  652. s.client.send(method)
  653. time.sleep(1)
  654. s.client.send(path)
  655. time.sleep(1)
  656. s.client.send(version + "Host: " + dut + "\r\n\r\n")
  657. s.read_resp_hdrs()
  658. resp = s.read_resp_data()
  659. s.close()
  660. return s.status
  661. def code_414_uri_too_long(dut, port, max_uri_len):
  662. print "[test] 414 URI Too Long =>",
  663. status = send_getx_uri_len(dut, port, max_uri_len)
  664. if not test_val("Client Error", "404", status):
  665. return False
  666. status = send_getx_uri_len(dut, port, max_uri_len + 1)
  667. if not test_val("Client Error", "414", status):
  668. return False
  669. print "Success"
  670. return True
  671. def send_postx_hdr_len(dut, port, length):
  672. s = Session(dut, port)
  673. path = "/echo"
  674. host = "Host: " + dut
  675. custom_hdr_field = "\r\nCustom: "
  676. custom_hdr_val = "x"*(length - len(host) - len(custom_hdr_field) - len("\r\n\r\n") + len("0"))
  677. request = "POST " + path + " HTTP/1.1\r\n" + host + custom_hdr_field + custom_hdr_val + "\r\n\r\n"
  678. s.client.send(request[:length/2])
  679. time.sleep(1)
  680. s.client.send(request[length/2:])
  681. hdr = s.read_resp_hdrs()
  682. resp = s.read_resp_data()
  683. s.close()
  684. if "Custom" in hdr:
  685. return (hdr["Custom"] == custom_hdr_val), resp
  686. return False, s.status
  687. def code_431_hdr_too_long(dut, port, max_hdr_len):
  688. print "[test] 431 Header Too Long =>",
  689. res, status = send_postx_hdr_len(dut, port, max_hdr_len)
  690. if not res:
  691. return False
  692. res, status = send_postx_hdr_len(dut, port, max_hdr_len + 1)
  693. if not test_val("Client Error", "431", status):
  694. return False
  695. print "Success"
  696. return True
  697. def test_upgrade_not_supported(dut, port):
  698. print "[test] Upgrade Not Supported =>",
  699. s = Session(dut, port)
  700. path = "/hello"
  701. s.client.send("OPTIONS * HTTP/1.1\r\nHost:" + dut + "\r\nUpgrade: TLS/1.0\r\nConnection: Upgrade\r\n\r\n");
  702. s.read_resp_hdrs()
  703. resp = s.read_resp_data()
  704. if not test_val("Client Error", "200", s.status):
  705. s.close()
  706. return False
  707. s.close()
  708. print "Success"
  709. return True
  710. if __name__ == '__main__':
  711. ########### Execution begins here...
  712. # Configuration
  713. # Max number of threads/sessions
  714. max_sessions = 7
  715. max_uri_len = 512
  716. max_hdr_len = 512
  717. parser = argparse.ArgumentParser(description='Run HTTPD Test')
  718. parser.add_argument('-4','--ipv4', help='IPv4 address')
  719. parser.add_argument('-6','--ipv6', help='IPv6 address')
  720. parser.add_argument('-p','--port', help='Port')
  721. args = vars(parser.parse_args())
  722. dut4 = args['ipv4']
  723. dut6 = args['ipv6']
  724. port = args['port']
  725. dut = dut4
  726. _verbose_ = True
  727. print "### Basic HTTP Client Tests"
  728. get_hello(dut, port)
  729. post_hello(dut, port)
  730. put_hello(dut, port)
  731. post_echo(dut, port)
  732. get_echo(dut, port)
  733. put_echo(dut, port)
  734. get_hello_type(dut, port)
  735. get_hello_status(dut, port)
  736. get_false_uri(dut, port)
  737. print "### Error code tests"
  738. code_500_server_error_test(dut, port)
  739. code_501_method_not_impl(dut, port)
  740. code_505_version_not_supported(dut, port)
  741. code_400_bad_request(dut, port)
  742. code_404_not_found(dut, port)
  743. code_405_method_not_allowed(dut, port)
  744. code_408_req_timeout(dut, port)
  745. code_414_uri_too_long(dut, port, max_uri_len)
  746. code_431_hdr_too_long(dut, port, max_hdr_len)
  747. test_upgrade_not_supported(dut, port)
  748. # Not supported yet (Error on chunked request)
  749. ###code_411_length_required(dut, port)
  750. print "### Sessions and Context Tests"
  751. parallel_sessions_adder(dut, port, max_sessions)
  752. leftover_data_test(dut, port)
  753. async_response_test(dut, port)
  754. spillover_session(dut, port, max_sessions)
  755. recv_timeout_test(dut, port)
  756. # May timeout in case requests are sent slower than responses are read.
  757. # Instead use httperf stress test
  758. pipeline_test(dut, port, max_sessions)
  759. packet_size_limit_test(dut, port, 50*1024)
  760. get_hello(dut, port)
  761. sys.exit()