Blob Blame History Raw
From a1f400c56c7ad0ca241d044b4d283dc0d2291dbb Mon Sep 17 00:00:00 2001
From: Mark Williams <mrw@enotuniq.org>
Date: Sun, 10 Feb 2019 16:40:10 -0800
Subject: [PATCH] ProperlyCloseFilesMixin.getHandleErrorCode now returns a
 matcher.

twisted.test.test_ssl.StolenTCPTests checks that the TLS connection is
closed upon when the corresponding transport connection is lost.  This
involves checking an error message that varies between OpenSSL
versions.

getHandleErrorCode becomes getHandleErrorCodeMatcher and returns a
hamcrest Matcher to allow StolenTCPTests to match the possible OpenSSL
error messages.
---
 src/twisted/test/test_ssl.py | 27 +++++++++++++++++++--------
 src/twisted/test/test_tcp.py | 19 ++++++++++++-------
 2 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/src/twisted/test/test_ssl.py b/src/twisted/test/test_ssl.py
index d580573627..7099d0f829 100644
--- a/src/twisted/test/test_ssl.py
+++ b/src/twisted/test/test_ssl.py
@@ -15,7 +15,8 @@
 from twisted.test.test_tcp import ProperlyCloseFilesMixin
 from twisted.test.proto_helpers import waitUntilAllDisconnected
 
-import os, errno
+import os
+import hamcrest
 
 try:
     from OpenSSL import SSL, crypto
@@ -302,16 +303,26 @@ def getHandleExceptionType(self):
         return SSL.Error
 
 
-    def getHandleErrorCode(self):
+    def getHandleErrorCodeMatcher(self):
         """
-        Return the argument L{OpenSSL.SSL.Error} will be constructed with for
-        this case. This is basically just a random OpenSSL implementation
-        detail. It would be better if this test worked in a way which did not
+        Return a L{hamcrest.core.matcher.Matcher} for the argument
+        L{OpenSSL.SSL.Error} will be constructed with for this case.
+        This is basically just a random OpenSSL implementation detail.
+        It would be better if this test worked in a way which did not
         require this.
         """
-        # Otherwise, we expect an error about how we tried to write to a
-        # shutdown connection.  This is terribly implementation-specific.
-        return [('SSL routines', 'SSL_write', 'protocol is shutdown')]
+        # We expect an error about how we tried to write to a shutdown
+        # connection.  This is terribly implementation-specific.
+        return hamcrest.contains(
+            hamcrest.contains(
+                hamcrest.equal_to('SSL routines'),
+                hamcrest.any_of(
+                    hamcrest.equal_to('SSL_write'),
+                    hamcrest.equal_to('ssl_write_internal'),
+                ),
+                hamcrest.equal_to('protocol is shutdown'),
+            ),
+        )
 
 
 
diff --git a/src/twisted/test/test_tcp.py b/src/twisted/test/test_tcp.py
index b8791bb9ca..5a114d6dca 100644
--- a/src/twisted/test/test_tcp.py
+++ b/src/twisted/test/test_tcp.py
@@ -8,6 +8,7 @@
 from __future__ import division, absolute_import
 
 import socket, random, errno
+import hamcrest
 from functools import wraps
 
 from zope.interface import implementer
@@ -1135,17 +1136,18 @@ def getHandleExceptionType(self):
         raise NotImplementedError()
 
 
-    def getHandleErrorCode(self):
+    def getHandleErrorCodeMatcher(self):
         """
-        Return the errno expected to result from writing to a closed
-        platform socket handle.
+        Return a L{hamcrest.core.matcher.Matcher} that matches the
+        errno expected to result from writing to a closed platform
+        socket handle.
         """
         # Windows and Python 3: returns WSAENOTSOCK
         # Windows and Python 2: returns EBADF
         # Linux, FreeBSD, macOS: returns EBADF
         if platform.isWindows() and _PY3:
-            return errno.WSAENOTSOCK
-        return errno.EBADF
+            return hamcrest.equal_to(errno.WSAENOTSOCK)
+        return hamcrest.equal_to(errno.EBADF)
 
 
     def test_properlyCloseFiles(self):
@@ -1190,10 +1192,13 @@ def clientDisconnected(result):
             if not server.lostConnectionReason.check(error.ConnectionClosed):
                 err(server.lostConnectionReason,
                     "Server lost connection for unexpected reason")
-            expectedErrorCode = self.getHandleErrorCode()
+            errorCodeMatcher = self.getHandleErrorCodeMatcher()
             exception = self.assertRaises(
                 self.getHandleExceptionType(), client.handle.send, b'bytes')
-            self.assertEqual(exception.args[0], expectedErrorCode)
+            hamcrest.assert_that(
+                exception.args[0],
+                errorCodeMatcher,
+            )
         clientDeferred.addCallback(clientDisconnected)
 
         def cleanup(passthrough):