Debian Codegraph @

python/tests/proton_tests/connect.py

raw
1 from __future__ import absolute_import
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,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
18 # under the License.
19 #
20
21 import time
22 import sys
23 import json
24 from .common import Test, SkipTest, TestServer, free_tcp_port, ensureCanTestExtendedSASL
25 from proton import SSLDomain
26 from proton.reactor import Container
27 from proton.handlers import MessagingHandler
28 from .ssl import _testpath
29
30
31 def write_connect_conf(obj):
32 with open('connect.json', 'w') as outfile:
33 json.dump(obj, outfile)
34
35
36 class Server(MessagingHandler):
37 def __init__(self, expected_user=None, scheme='amqps'):
38 super(Server, self).__init__()
39 self.port = free_tcp_port()
40 self.scheme = scheme
41 self.url = '%s://localhost:%i' % (self.scheme, self.port)
42 self.expected_user = expected_user
43 self.verified_user = False
44
45 def on_start(self, event):
46 self.listener = event.container.listen(self.url)
47
48 def on_connection_opening(self, event):
49 if self.expected_user:
50 assert event.connection.transport.user == self.expected_user
51 self.verified_user = True
52
53 def on_connection_closing(self, event):
54 event.connection.close()
55 self.listener.close()
56
57
58 class Client(MessagingHandler):
59 def __init__(self):
60 super(Client, self).__init__()
61 self.opened = False
62
63 def on_connection_opened(self, event):
64 self.opened = True
65 event.connection.close()
66
67
68 class ConnectConfigTest(Test):
69 def test_port(self):
70 ensureCanTestExtendedSASL()
71 server = Server(scheme='amqp')
72 container = Container(server)
73 client = Client()
74 write_connect_conf({'port': server.port, 'scheme': 'amqp'})
75 container.connect(handler=client, reconnect=False)
76 container.run()
77 assert client.opened
78
79 def test_user(self):
80 ensureCanTestExtendedSASL()
81 user = 'user@proton'
82 password = 'password'
83 server = Server(user)
84 container = Container(server)
85 container.ssl.server.set_credentials(_testpath('server-certificate.pem'),
86 _testpath('server-private-key.pem'),
87 'server-password')
88 client = Client()
89 config = {
90 'user': user,
91 'password': password,
92 'port': server.port,
93 'tls': {
94 'verify': False,
95 'ca': _testpath('ca-certificate.pem')
96 }
97 }
98 write_connect_conf(config)
99 container.connect(handler=client, reconnect=False)
100 container.run()
101 assert client.opened
102 assert server.verified_user
103
104 def test_ssl(self):
105 ensureCanTestExtendedSASL()
106 server = Server(scheme='amqps')
107 container = Container(server)
108 container.ssl.server.set_credentials(_testpath('server-certificate-lh.pem'),
109 _testpath('server-private-key-lh.pem'),
110 'server-password')
111 client = Client()
112 config = {
113 'scheme': 'amqps',
114 'port': server.port,
115 'tls': {
116 'verify': True,
117 'ca': _testpath('ca-certificate.pem')
118 }
119 }
120 write_connect_conf(config)
121 container.connect(handler=client, reconnect=False)
122 container.run()
123 assert client.opened
124
125 def test_ssl_external(self):
126 ensureCanTestExtendedSASL()
127 server = Server(scheme='amqps')
128 container = Container(server)
129 container.ssl.server.set_credentials(_testpath('server-certificate-lh.pem'),
130 _testpath('server-private-key-lh.pem'),
131 'server-password')
132 container.ssl.server.set_trusted_ca_db(_testpath('ca-certificate.pem'))
133 container.ssl.server.set_peer_authentication(SSLDomain.VERIFY_PEER,
134 _testpath('ca-certificate.pem'))
135
136 client = Client()
137 config = {
138 'scheme': 'amqps',
139 'port': server.port,
140 'sasl': {
141 'mechanisms': 'EXTERNAL'
142 },
143 'tls': {
144 'cert': _testpath('client-certificate.pem'),
145 'key': _testpath('client-private-key-no-password.pem'),
146 'ca': _testpath('ca-certificate.pem'),
147 'verify': True
148 }
149 }
150 write_connect_conf(config)
151 container.connect(handler=client, reconnect=False)
152 container.run()
153 assert client.opened
154
155 def test_ssl_plain(self):
156 ensureCanTestExtendedSASL()
157 user = 'user@proton'
158 password = 'password'
159 server = Server(expected_user=user, scheme='amqps')
160 container = Container(server)
161 container.ssl.server.set_credentials(_testpath('server-certificate-lh.pem'),
162 _testpath('server-private-key-lh.pem'),
163 'server-password')
164 container.ssl.server.set_trusted_ca_db(_testpath('ca-certificate.pem'))
165 container.ssl.server.set_peer_authentication(SSLDomain.VERIFY_PEER,
166 _testpath('ca-certificate.pem'))
167
168 client = Client()
169 config = {
170 'scheme': 'amqps',
171 'port': server.port,
172 'user': user,
173 'password': password,
174 'sasl': {
175 'mechanisms': 'PLAIN'
176 },
177 'tls': {
178 'cert': _testpath('client-certificate.pem'),
179 'key': _testpath('client-private-key-no-password.pem'),
180 'ca': _testpath('ca-certificate.pem'),
181 'verify': True
182 }
183 }
184 write_connect_conf(config)
185 container.connect(handler=client, reconnect=False)
186 container.run()
187 assert client.opened