1'''
2'''
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#      http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing, software
14#  distributed under the License is distributed on an "AS IS" BASIS,
15#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16#  See the License for the specific language governing permissions and
17#  limitations under the License.
18
19import os
20Test.Summary = '''
21Test a basic remap of a http/2 connection
22'''
23
24Test.SkipUnless(
25    Condition.HasCurlFeature('http2')
26)
27Test.ContinueOnFail = True
28
29# ----
30# Setup Origin Server
31# ----
32server = Test.MakeOriginServer("server")
33
34# For Test Case 1 & 5 - /
35server.addResponse("sessionlog.json",
36                   {"headers": "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
37                    "timestamp": "1469733493.993",
38                    "body": ""},
39                   {"headers": "HTTP/1.1 200 OK\r\nServer: microserver\r\nConnection: close\r\n\r\n",
40                       "timestamp": "1469733493.993",
41                       "body": ""})
42
43# For Test Case 2 - /bigfile
44# Add info for the large H2 download test
45server.addResponse("sessionlog.json",
46                   {"headers": "GET /bigfile HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
47                    "timestamp": "1469733493.993",
48                    "body": ""},
49                   {"headers": "HTTP/1.1 200 OK\r\nServer: microserver\r\nConnection: close\r\nCache-Control: max-age=3600\r\nContent-Length: 191414\r\n\r\n",
50                       "timestamp": "1469733493.993",
51                       "body": ""})
52
53# For Test Case 3 - /test2
54server.addResponse("sessionlog.json",
55                   {"headers": "GET /test2 HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
56                    "timestamp": "1469733493.993",
57                    "body": ""},
58                   {"headers": "HTTP/1.1 200 OK\r\nServer: microserver\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n",
59                       "timestamp": "1469733493.993",
60                       "body": ""})
61
62# For Test Case 6 - /postchunked
63post_body = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
64server.addResponse("sessionlog.json",
65                   {"headers": "POST /postchunked HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
66                    "timestamp": "1469733493.993",
67                    "body": post_body},
68                   {"headers": "HTTP/1.1 200 OK\r\nServer: microserver\r\nConnection: close\r\nContent-Length: 10\r\n\r\n",
69                       "timestamp": "1469733493.993",
70                       "body": "0123456789"})
71
72# For Test Case 7 - /bigpostchunked
73# Make a post body that will be split across at least two frames
74big_post_body = "0123456789" * 131070
75server.addResponse("sessionlog.json",
76                   {"headers": "POST /bigpostchunked HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
77                    "timestamp": "1469733493.993",
78                    "body": big_post_body},
79                   {"headers": "HTTP/1.1 200 OK\r\nServer: microserver\r\nConnection: close\r\nContent-Length: 10\r\n\r\n",
80                       "timestamp": "1469733493.993",
81                       "body": "0123456789"})
82
83big_post_body_file = open(os.path.join(Test.RunDirectory, "big_post_body"), "w")
84big_post_body_file.write(big_post_body)
85big_post_body_file.close()
86
87# For Test Case 8 - /huge_resp_hdrs
88server.addResponse("sessionlog.json",
89                   {"headers": "GET /huge_resp_hdrs HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
90                    "timestamp": "1469733493.993",
91                    "body": ""},
92                   {"headers": "HTTP/1.1 200 OK\r\nServer: microserver\r\nConnection: close\r\nContent-Length: 6\r\n\r\n",
93                       "timestamp": "1469733493.993",
94                       "body": "200 OK"})
95
96# For Test Case 9 - /status/204
97server.addResponse("sessionlog.json",
98                   {"headers": "GET /status/204 HTTP/1.1\r\nHost: www.example.com\r\n\r\n",
99                    "timestamp": "1469733493.993",
100                    "body": ""},
101                   {"headers": "HTTP/1.1 204 No Content\r\nServer: microserver\r\nConnection: close\r\n\r\n",
102                       "timestamp": "1469733493.993",
103                       "body": ""})
104
105# ----
106# Setup ATS
107# ----
108ts = Test.MakeATSProcess("ts", select_ports=True, enable_tls=True, enable_cache=False)
109
110# add ssl materials like key, certificates for the server
111ts.addDefaultSSLFiles()
112
113ts.Setup.CopyAs('rules/huge_resp_hdrs.conf', Test.RunDirectory)
114ts.Disk.remap_config.AddLine(
115    'map /huge_resp_hdrs http://127.0.0.1:{0}/huge_resp_hdrs @plugin=header_rewrite.so @pparam={1}/huge_resp_hdrs.conf '.format(
116        server.Variables.Port, Test.RunDirectory)
117)
118
119ts.Disk.remap_config.AddLine(
120    'map / http://127.0.0.1:{0}'.format(server.Variables.Port)
121)
122
123ts.Disk.ssl_multicert_config.AddLine(
124    'dest_ip=* ssl_cert_name=server.pem ssl_key_name=server.key'
125)
126ts.Disk.records_config.update({
127    'proxy.config.diags.debug.enabled': 1,
128    'proxy.config.diags.debug.tags': 'http',
129    'proxy.config.ssl.server.cert.path': '{0}'.format(ts.Variables.SSLDir),
130    'proxy.config.ssl.server.private_key.path': '{0}'.format(ts.Variables.SSLDir),
131    'proxy.config.ssl.client.verify.server': 0,
132    'proxy.config.ssl.server.cipher_suite': 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:RC4-SHA:RC4-MD5:AES128-SHA:AES256-SHA:DES-CBC3-SHA!SRP:!DSS:!PSK:!aNULL:!eNULL:!SSLv2',
133    'proxy.config.http2.active_timeout_in': 3,
134    'proxy.config.http2.max_concurrent_streams_in': 65535,
135})
136
137ts.Setup.CopyAs('h2client.py', Test.RunDirectory)
138ts.Setup.CopyAs('h2bigclient.py', Test.RunDirectory)
139ts.Setup.CopyAs('h2chunked.py', Test.RunDirectory)
140ts.Setup.CopyAs('h2active_timeout.py', Test.RunDirectory)
141
142# ----
143# Test Cases
144# ----
145
146# Test Case 1:  basic H2 interaction
147tr = Test.AddTestRun()
148tr.Processes.Default.Command = 'python3 h2client.py -p {0}'.format(ts.Variables.ssl_port)
149tr.Processes.Default.ReturnCode = 0
150tr.Processes.Default.StartBefore(server)
151tr.Processes.Default.StartBefore(Test.Processes.ts)
152tr.Processes.Default.Streams.stdout = "gold/remap-200.gold"
153tr.StillRunningAfter = server
154
155# Test Case 2: Make sure all the big file gets back.  Regression test for issue 1646
156tr = Test.AddTestRun()
157tr.Processes.Default.Command = 'python3 h2bigclient.py -p {0}'.format(ts.Variables.ssl_port)
158tr.Processes.Default.ReturnCode = 0
159tr.Processes.Default.Streams.stdout = "gold/bigfile.gold"
160tr.StillRunningAfter = server
161
162# Test Case 3: Chunked content
163tr = Test.AddTestRun()
164tr.Processes.Default.Command = 'python3 h2chunked.py -p {0}  -u /test2'.format(ts.Variables.ssl_port)
165tr.Processes.Default.ReturnCode = 0
166tr.Processes.Default.Streams.stdout = "gold/chunked.gold"
167tr.StillRunningAfter = server
168
169# NOTE: Skipping this test run because traffic-replay doesn't currently support H2
170# Test Case 4: Multiple request
171# client_path = os.path.join(Test.Variables.AtsTestToolsDir, 'traffic-replay/')
172# tr = Test.AddTestRun()
173# tr.Processes.Default.Command = "python3 {0} -type {1} -log_dir {2} -port {3} -host '127.0.0.1' -s_port {4} -v -colorize False".format(
174#     client_path, 'h2', server.Variables.DataDir, ts.Variables.port, ts.Variables.ssl_port)
175# tr.Processes.Default.ReturnCode = 0
176# tr.Processes.Default.Streams.stdout = "gold/replay.gold"
177# tr.StillRunningAfter = server
178
179# Test Case 5: h2_active_timeout
180tr = Test.AddTestRun()
181tr.Processes.Default.Command = 'python3 h2active_timeout.py -p {0} -d 4'.format(ts.Variables.ssl_port)
182tr.Processes.Default.ReturnCode = 0
183tr.Processes.Default.Streams.All = "gold/active_timeout.gold"
184tr.StillRunningAfter = server
185
186# Test Case 6: Post with chunked body
187# While HTTP/2 does not support Tranfer-encoding we pass that into curl to encourage it to not set the content length
188# on the post body
189tr = Test.AddTestRun()
190tr.Processes.Default.Command = 'curl -s -k -H "Transfer-Encoding: chunked" -d "{0}" https://127.0.0.1:{1}/postchunked'.format(
191    post_body, ts.Variables.ssl_port)
192tr.Processes.Default.ReturnCode = 0
193tr.Processes.Default.Streams.All = "gold/post_chunked.gold"
194tr.StillRunningAfter = server
195
196# Test Case 7: Post with big chunked body
197# While HTTP/2 does not support Tranfer-encoding we pass that into curl to encourage it to not set the content length
198# on the post body
199tr = Test.AddTestRun()
200tr.Processes.Default.Command = 'curl -s -k -H "Transfer-Encoding: chunked" -d @big_post_body https://127.0.0.1:{0}/bigpostchunked'.format(
201    ts.Variables.ssl_port)
202tr.Processes.Default.ReturnCode = 0
203tr.Processes.Default.Streams.All = "gold/post_chunked.gold"
204tr.StillRunningAfter = server
205
206# Test Case 8: Huge response header
207tr = Test.AddTestRun()
208tr.Processes.Default.Command = 'curl -vs -k --http2 https://127.0.0.1:{0}/huge_resp_hdrs'.format(ts.Variables.ssl_port)
209tr.Processes.Default.ReturnCode = 0
210tr.Processes.Default.Streams.stdout = "gold/http2_8_stdout.gold"
211# Different versions of curl will have different cases for HTTP/2 field names.
212tr.Processes.Default.Streams.stderr = Testers.GoldFile("gold/http2_8_stderr.gold", case_insensitive=True)
213tr.StillRunningAfter = server
214
215# Test Case 9: Header Only Response - e.g. 204
216tr = Test.AddTestRun()
217tr.Processes.Default.Command = 'curl -vs -k --http2 https://127.0.0.1:{0}/status/204'.format(ts.Variables.ssl_port)
218tr.Processes.Default.ReturnCode = 0
219tr.Processes.Default.Streams.stdout = "gold/http2_9_stdout.gold"
220# Different versions of curl will have different cases for HTTP/2 field names.
221tr.Processes.Default.Streams.stderr = Testers.GoldFile("gold/http2_9_stderr.gold", case_insensitive=True)
222tr.StillRunningAfter = server
223