2020-12-29 10:23:54 +01:00
|
|
|
#!/usr/bin/env python3
|
2014-01-05 15:40:04 +01:00
|
|
|
#
|
|
|
|
# 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):
|
2014-02-13 15:22:52 +01:00
|
|
|
inflater = nghttp2.HDInflater()
|
2014-01-05 15:40:04 +01:00
|
|
|
|
|
|
|
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']]
|
|
|
|
if hdrs != expected_hdrs:
|
2014-04-22 17:55:54 +02:00
|
|
|
if 'seqno' in item:
|
|
|
|
seqno = item['seqno']
|
|
|
|
else:
|
|
|
|
seqno = casenum
|
|
|
|
|
|
|
|
sys.stderr.write('FAIL seqno#{}\n'.format(seqno))
|
2014-01-05 15:40:04 +01:00
|
|
|
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:]:
|
2014-02-13 15:22:52 +01:00
|
|
|
sys.stderr.write('{}: '.format(filename))
|
2014-01-05 15:40:04 +01:00
|
|
|
with open(filename) as f:
|
|
|
|
input = f.read()
|
2014-06-30 17:39:54 +02:00
|
|
|
|
|
|
|
testdata = json.loads(input)
|
|
|
|
|
2014-01-05 15:40:04 +01:00
|
|
|
testsuite(json.loads(input))
|