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
19Test.Summary = '''
20Test disabling H2 on a per domain basis
21'''
22
23Test.SkipUnless(
24    Condition.HasCurlFeature('http2')
25)
26
27# Define default ATS
28ts = Test.MakeATSProcess("ts", select_ports=True, enable_tls=True)
29server = Test.MakeOriginServer("server")
30
31request_header = {"headers": "GET / HTTP/1.1\r\n\r\n", "timestamp": "1469733493.993", "body": ""}
32response_header = {"headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", "timestamp": "1469733493.993", "body": ""}
33server.addResponse("sessionlog.json", request_header, response_header)
34
35# add ssl materials like key, certificates for the server
36ts.addDefaultSSLFiles()
37
38ts.Disk.remap_config.AddLine(
39    'map / http://127.0.0.1:{0}'.format(server.Variables.Port))
40
41ts.Disk.ssl_multicert_config.AddLine(
42    'dest_ip=* ssl_cert_name=server.pem ssl_key_name=server.key'
43)
44
45# Case 1, global config policy=permissive properties=signature
46#         override for foo.com policy=enforced properties=all
47ts.Disk.records_config.update({
48    'proxy.config.diags.debug.enabled': 0,
49    'proxy.config.diags.debug.tags': 'http|ssl',
50    'proxy.config.ssl.server.cert.path': '{0}'.format(ts.Variables.SSLDir),
51    'proxy.config.ssl.server.private_key.path': '{0}'.format(ts.Variables.SSLDir),
52    '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',
53    'proxy.config.url_remap.pristine_host_hdr': 1,
54    'proxy.config.accept_threads': 0
55})
56
57ts.Disk.sni_yaml.AddLines([
58    'sni:',
59    '- fqdn: bar.com',
60    '  http2: off',
61    '- fqdn: bob.*.com',
62    '  http2: off',
63])
64
65tr = Test.AddTestRun("Negotiate-h2")
66tr.Processes.Default.Command = "curl -v -k --resolve 'foo.com:{0}:127.0.0.1' https://foo.com:{0}".format(ts.Variables.ssl_port)
67tr.ReturnCode = 0
68tr.Processes.Default.StartBefore(server)
69tr.Processes.Default.StartBefore(Test.Processes.ts)
70tr.StillRunningAfter = server
71tr.StillRunningAfter = ts
72tr.Processes.Default.TimeOut = 5
73tr.Processes.Default.Streams.All = Testers.ExcludesExpression("Could Not Connect", "Curl attempt should have succeeded")
74tr.Processes.Default.Streams.All += Testers.ContainsExpression("Using HTTP2", "Curl should negotiate HTTP2")
75tr.TimeOut = 5
76
77tr2 = Test.AddTestRun("Do not negotiate h2")
78tr2.Processes.Default.Command = "curl -v -k --resolve 'bar.com:{0}:127.0.0.1' https://bar.com:{0}".format(ts.Variables.ssl_port)
79tr2.ReturnCode = 0
80tr2.StillRunningAfter = server
81tr2.Processes.Default.TimeOut = 5
82tr2.StillRunningAfter = ts
83tr2.Processes.Default.Streams.All = Testers.ExcludesExpression("Could Not Connect", "Curl attempt should have succeeded")
84tr2.Processes.Default.Streams.All += Testers.ExcludesExpression("Using HTTP2", "Curl should not negotiate HTTP2")
85tr2.TimeOut = 5
86
87tr2 = Test.AddTestRun("Do not negotiate h2")
88tr2.Processes.Default.Command = "curl -v -k --resolve 'bob.foo.com:{0}:127.0.0.1' https://bob.foo.com:{0}".format(
89    ts.Variables.ssl_port)
90tr2.ReturnCode = 0
91tr2.StillRunningAfter = server
92tr2.Processes.Default.TimeOut = 5
93tr2.StillRunningAfter = ts
94tr2.Processes.Default.Streams.All = Testers.ExcludesExpression("Could Not Connect", "Curl attempt should have succeeded")
95tr2.Processes.Default.Streams.All += Testers.ExcludesExpression("Using HTTP2", "Curl should not negotiate HTTP2")
96tr2.TimeOut = 5
97