python: Add python script to check hpack-test-case json files

See https://github.com/Jxck/hpack-test-case for the json file
format.
This commit is contained in:
Tatsuhiro Tsujikawa 2014-01-05 23:40:04 +09:00
parent df0b59cc94
commit 5c88e92e96

53
python/hpackcheck.py Executable file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env python
#
# This script reads json files given in the command-line (each file
# must be written in the format described in
# https://github.com/Jxck/hpack-test-case). And then it decompresses
# the sequence of encoded header blocks (which is the value of 'wire'
# key) and checks that decompressed header set is equal to the input
# header set (which is the value of 'headers' key). If there is
# mismatch, exception will be raised.
#
import sys, json
from binascii import a2b_hex
import nghttp2
def testsuite(testdata):
if testdata['context'] == 'request':
side = nghttp2.HD_SIDE_REQUEST
else:
side = nghttp2.HD_SIDE_RESPONSE
inflater = nghttp2.HDInflater(side)
for casenum, item in enumerate(testdata['cases']):
if 'header_table_size' in item:
hd_table_size = int(item['header_table_size'])
inflater.change_table_size(hd_table_size)
compressed = a2b_hex(item['wire'])
# sys.stderr.write('#{} WIRE:\n{}\n'.format(casenum+1, item['wire']))
# TODO decompressed headers are not necessarily UTF-8 strings
hdrs = [(k.decode('utf-8'), v.decode('utf-8')) \
for k, v in inflater.inflate(compressed)]
expected_hdrs = [(list(x.keys())[0],
list(x.values())[0]) for x in item['headers']]
hdrs.sort()
expected_hdrs.sort()
if hdrs != expected_hdrs:
sys.stderr.write('FAIL case#{}\n'.format(casenum + 1))
sys.stderr.write('expected:\n')
for k, v in expected_hdrs:
sys.stderr.write('{}: {}\n'.format(k, v))
sys.stderr.write(', but got:\n')
for k, v in hdrs:
sys.stderr.write('{}: {}\n'.format(k, v))
raise Exception('test failure')
sys.stderr.write('PASS\n')
if __name__ == '__main__':
for filename in sys.argv[1:]:
sys.stderr.write('{}\n'.format(filename))
with open(filename) as f:
input = f.read()
testsuite(json.loads(input))