Add OpenSSL portability layer
[folly.git] / folly / io / async / ssl / test / SSLErrorsTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/io/async/ssl/SSLErrors.h>
18
19 #include <folly/portability/GTest.h>
20
21 #include <openssl/err.h>
22 #include <openssl/x509.h>
23
24 using namespace testing;
25 using namespace folly;
26
27 TEST(SSLErrorsTest, TestMessage) {
28   ERR_load_crypto_strings();
29   unsigned long err;
30 #ifdef OPENSSL_IS_BORINGSSL
31   err = ERR_PACK(ERR_LIB_X509, X509_R_CERT_ALREADY_IN_HASH_TABLE);
32 #else
33   err = ERR_PACK(
34       ERR_LIB_X509,
35       X509_F_X509_STORE_ADD_CERT,
36       X509_R_CERT_ALREADY_IN_HASH_TABLE);
37 #endif
38   SSLException ex(0, err, 0, 0);
39
40 // This is flaky - we should not be testing error strings
41 // which may change version to version
42 #if defined(OPENSSL_IS_BORINGSSL)
43   std::string expectedMsg =
44       "AsyncSocketException: error:0b000069:X.509 certificate routines:"
45       "OPENSSL_internal:CERT_ALREADY_IN_HASH_TABLE, type = SSL error";
46 #else
47   std::string expectedMsg =
48       "AsyncSocketException: error:0B07C065:"
49       "x509 certificate routines:X509_STORE_add_cert:"
50       "cert already in hash table, type = SSL error";
51 #endif
52   std::string actual = ex.what();
53   EXPECT_EQ(expectedMsg, actual);
54 }