File: test-0rtt-s_client.py

package info (click to toggle)
trafficserver 9.2.5%2Bds-0%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 64,964 kB
  • sloc: cpp: 345,958; ansic: 31,184; python: 25,297; sh: 7,023; makefile: 3,045; perl: 2,255; java: 277; pascal: 119; sql: 94; xml: 2
file content (94 lines) | stat: -rw-r--r-- 3,850 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
'''
'''
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import subprocess
import sys
import os
import shlex
import h2_early_decode
import argparse


def main():
    parser = argparse.ArgumentParser(description='Process some args.')
    parser.add_argument('-p', '--ats-port', type=int, dest='ats_port', required=True, help='ATS port number')
    parser.add_argument('-v', '--http-version', type=str, dest='http_ver', choices=['h1', 'h2'], required=True, help='HTTP version')
    parser.add_argument('-t', '--test-name', type=str, dest='test_name', required=True, help='Name of the test to run')
    parser.add_argument('-r', '--run-dir', type=str, dest='run_dir', required=True, help='Path to the autest run directory')
    parser.add_argument('-s', '--server-name', type=str, dest='sni', required=False, help='Server Name')
    args = parser.parse_args()

    sess_file_path = os.path.join(args.run_dir, 'sess.dat')
    early_data_file_path = os.path.join(args.run_dir, 'early_{0}_{1}.txt'.format(args.http_ver, args.test_name))

    if args.sni != '' and args.sni is not None:
        sni_str = '-servername {0}'.format(args.sni)
    else:
        sni_str = ''

    if args.http_ver == 'h2':
        alpn_str = '-alpn h2'
    else:
        alpn_str = ''
    s_client_cmd_1 = shlex.split(
        f'openssl s_client -connect 127.0.0.1:{args.ats_port} -tls1_3 -quiet -sess_out {sess_file_path} {sni_str} {alpn_str}')
    s_client_cmd_2 = shlex.split(
        f'openssl s_client -connect 127.0.0.1:{args.ats_port} -tls1_3 -quiet -sess_in {sess_file_path} -early_data {early_data_file_path} {sni_str} {alpn_str}'
    )

    create_sess_proc = subprocess.Popen(
        s_client_cmd_1, env=os.environ.copy(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    try:
        output = create_sess_proc.communicate(input=bytes(b'GET / HTTP/1.0\r\n\r\n'), timeout=1)[0]
    except subprocess.TimeoutExpired:
        create_sess_proc.kill()
        output = create_sess_proc.communicate()[0]

    reuse_sess_proc = subprocess.Popen(
        s_client_cmd_2, env=os.environ.copy(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    try:
        output = reuse_sess_proc.communicate(timeout=1)[0]
    except subprocess.TimeoutExpired:
        reuse_sess_proc.kill()
        output = reuse_sess_proc.communicate()[0]

    if args.http_ver == 'h2':
        lines = output.split(bytes('\n', 'utf-8'))
        data = b''
        for line in lines:
            line += b'\n'
            if line.startswith(bytes('Connecting to', 'utf-8')) or \
                    line.startswith(bytes('SSL_connect:', 'utf-8')) or \
                    line.startswith(bytes('SSL3 alert', 'utf-8')) or \
                    bytes('Can\'t use SSL_get_servername', 'utf-8') in line:
                continue
            data += line
        d = h2_early_decode.Decoder()
        frames = d.decode(data)
        for frame in frames:
            print(frame)
    else:
        print(output.decode('utf-8'))


    exit(0)


if __name__ == '__main__':
    main()