|
22 | 22 |
|
23 | 23 | import json
|
24 | 24 | import os
|
| 25 | +import ssl |
25 | 26 | import sys
|
26 | 27 | import warnings
|
27 | 28 | import logging
|
|
33 | 34 | from ryu.base import app_manager # To suppress cyclic import
|
34 | 35 | from ryu.controller import controller
|
35 | 36 | from ryu.controller import handler
|
| 37 | +from ryu.lib import hub |
36 | 38 | from ryu.ofproto import ofproto_v1_3_parser
|
37 | 39 | from ryu.ofproto import ofproto_v1_2_parser
|
38 | 40 | from ryu.ofproto import ofproto_v1_0_parser
|
| 41 | +hub.patch() |
39 | 42 |
|
40 | 43 |
|
41 | 44 | LOG = logging.getLogger('test_controller')
|
@@ -177,3 +180,52 @@ def recv(self, bufsize):
|
177 | 180 | self.assertEqual(state, handler.MAIN_DISPATCHER)
|
178 | 181 | self.assertEqual(kwargs, {})
|
179 | 182 | self.assertEqual(expected_json, output_json)
|
| 183 | + |
| 184 | + |
| 185 | +class TestOpenFlowController(unittest.TestCase): |
| 186 | + """ |
| 187 | + Test cases for OpenFlowController |
| 188 | + """ |
| 189 | + @mock.patch("ryu.controller.controller.CONF") |
| 190 | + def _test_ssl(self, this_dir, port, conf_mock): |
| 191 | + conf_mock.ofp_ssl_listen_port = port |
| 192 | + conf_mock.ofp_listen_host = "127.0.0.1" |
| 193 | + conf_mock.ca_certs = None |
| 194 | + conf_mock.ctl_cert = os.path.join(this_dir, 'cert.crt') |
| 195 | + conf_mock.ctl_privkey = os.path.join(this_dir, 'cert.key') |
| 196 | + c = controller.OpenFlowController() |
| 197 | + c() |
| 198 | + |
| 199 | + def test_ssl(self): |
| 200 | + """Tests SSL server functionality.""" |
| 201 | + # TODO: TLS version enforcement is necessary to avoid |
| 202 | + # vulnerable versions. Currently, this only tests TLS |
| 203 | + # connectivity. |
| 204 | + this_dir = os.path.dirname(sys.modules[__name__].__file__) |
| 205 | + saved_exception = None |
| 206 | + try: |
| 207 | + ssl_version = ssl.PROTOCOL_TLS |
| 208 | + except AttributeError: |
| 209 | + # For compatibility with older pythons. |
| 210 | + ssl_version = ssl.PROTOCOL_TLSv1 |
| 211 | + for i in range(3): |
| 212 | + try: |
| 213 | + # Try a few times as this can fail with EADDRINUSE |
| 214 | + port = random.randint(5000, 10000) |
| 215 | + server = hub.spawn(self._test_ssl, this_dir, port) |
| 216 | + hub.sleep(1) |
| 217 | + client = hub.StreamClient(("127.0.0.1", port), |
| 218 | + timeout=5, |
| 219 | + ssl_version=ssl_version) |
| 220 | + if client.connect() is not None: |
| 221 | + break |
| 222 | + except Exception as e: |
| 223 | + saved_exception = e |
| 224 | + continue |
| 225 | + finally: |
| 226 | + try: |
| 227 | + hub.kill(server) |
| 228 | + except Exception: |
| 229 | + pass |
| 230 | + else: |
| 231 | + self.fail("Failed to connect: " + str(saved_exception)) |
0 commit comments