Set the appropriate AsyncSocketExceptionType from SSLException
[folly.git] / folly / io / async / test / AsyncSocketExceptionTest.cpp
index 662a3737cae10023a1cfaebcb4f7c390700b2649..50c665990ec889fd9248e0f31c3268ddbeb51555 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include <gtest/gtest.h>
+#include <array>
+
 #include <folly/io/async/AsyncSocketException.h>
+#include <folly/io/async/ssl/SSLErrors.h>
+#include <gtest/gtest.h>
+#include <openssl/ssl.h>
 
 using namespace testing;
 
@@ -45,4 +49,47 @@ TEST(AsyncSocketException, SimpleTest) {
       std::string(ex2.what()));
 }
 
+TEST(AsyncSocketException, SSLExceptionType) {
+  {
+    SSLException eof(SSL_ERROR_ZERO_RETURN, 0, 0, 0);
+    EXPECT_EQ(eof.getType(), AsyncSocketException::END_OF_FILE);
+
+    SSLException netEof(SSL_ERROR_SYSCALL, 0, 0, 0);
+    EXPECT_EQ(netEof.getType(), AsyncSocketException::END_OF_FILE);
+
+    SSLException netOther(SSL_ERROR_SYSCALL, 0, 1, 0);
+    EXPECT_EQ(netOther.getType(), AsyncSocketException::NETWORK_ERROR);
+
+    std::array<int, 6> sslErrs{{SSL_ERROR_SSL,
+                                SSL_ERROR_WANT_READ,
+                                SSL_ERROR_WANT_WRITE,
+                                SSL_ERROR_WANT_X509_LOOKUP,
+                                SSL_ERROR_WANT_CONNECT,
+                                SSL_ERROR_WANT_ACCEPT}};
+
+    for (auto& e : sslErrs) {
+      SSLException sslEx(e, 0, 0, 0);
+      EXPECT_EQ(sslEx.getType(), AsyncSocketException::SSL_ERROR);
+    }
+  }
+
+  {
+    SSLException eof(SSLError::EOF_ERROR);
+    EXPECT_EQ(eof.getType(), AsyncSocketException::END_OF_FILE);
+
+    SSLException net(SSLError::NETWORK_ERROR);
+    EXPECT_EQ(net.getType(), AsyncSocketException::NETWORK_ERROR);
+
+    std::array<SSLError, 4> errs{{SSLError::CLIENT_RENEGOTIATION,
+                                  SSLError::INVALID_RENEGOTIATION,
+                                  SSLError::EARLY_WRITE,
+                                  SSLError::SSL_ERROR}};
+
+    for (auto& e : errs) {
+      SSLException sslEx(e);
+      EXPECT_EQ(sslEx.getType(), AsyncSocketException::SSL_ERROR);
+    }
+  }
+}
+
 } // namespace folly