From 5c88e92e96068217af8865e7b500e135280d1d67 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 5 Jan 2014 23:40:04 +0900 Subject: [PATCH] python: Add python script to check hpack-test-case json files See https://github.com/Jxck/hpack-test-case for the json file format. --- python/hpackcheck.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 python/hpackcheck.py diff --git a/python/hpackcheck.py b/python/hpackcheck.py new file mode 100755 index 00000000..a2b256ad --- /dev/null +++ b/python/hpackcheck.py @@ -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))