• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# This script reads json files given in the command-line (each file
4# must be written in the format described in
5# https://github.com/Jxck/hpack-test-case). And then it decompresses
6# the sequence of encoded header blocks (which is the value of 'wire'
7# key) and checks that decompressed header set is equal to the input
8# header set (which is the value of 'headers' key). If there is
9# mismatch, exception will be raised.
10#
11import sys, json
12from binascii import a2b_hex
13import nghttp2
14
15def testsuite(testdata):
16    inflater = nghttp2.HDInflater()
17
18    for casenum, item  in enumerate(testdata['cases']):
19        if 'header_table_size' in item:
20            hd_table_size = int(item['header_table_size'])
21            inflater.change_table_size(hd_table_size)
22        compressed = a2b_hex(item['wire'])
23        # sys.stderr.write('#{} WIRE:\n{}\n'.format(casenum+1, item['wire']))
24        # TODO decompressed headers are not necessarily UTF-8 strings
25        hdrs = [(k.decode('utf-8'), v.decode('utf-8')) \
26                for k, v in inflater.inflate(compressed)]
27
28        expected_hdrs = [(list(x.keys())[0],
29                          list(x.values())[0]) for x in item['headers']]
30        if hdrs != expected_hdrs:
31            if 'seqno' in item:
32                seqno = item['seqno']
33            else:
34                seqno = casenum
35
36            sys.stderr.write('FAIL seqno#{}\n'.format(seqno))
37            sys.stderr.write('expected:\n')
38            for k, v in expected_hdrs:
39                sys.stderr.write('{}: {}\n'.format(k, v))
40            sys.stderr.write(', but got:\n')
41            for k, v in hdrs:
42                sys.stderr.write('{}: {}\n'.format(k, v))
43            raise Exception('test failure')
44    sys.stderr.write('PASS\n')
45
46if __name__ == '__main__':
47    for filename in sys.argv[1:]:
48        sys.stderr.write('{}: '.format(filename))
49        with open(filename) as f:
50            input = f.read()
51
52        testdata = json.loads(input)
53
54        testsuite(json.loads(input))
55