2 * Copyright 2017-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/portability/GTest.h>
20 #include <folly/ssl/OpenSSLPtrTypes.h>
22 using namespace folly;
23 using namespace folly::ssl;
24 using namespace testing;
26 TEST(OpenSSLPortabilityTest, TestRSASetter) {
27 RsaUniquePtr r(RSA_new());
34 EXPECT_TRUE(BN_set_bit(n, 1));
35 EXPECT_TRUE(BN_set_bit(e, 3));
36 EXPECT_TRUE(BN_set_bit(d, 2));
37 RSA_set0_key(r.get(), n, e, d);
40 (const BIGNUM**)&n_actual,
41 (const BIGNUM**)&e_actual,
42 (const BIGNUM**)&d_actual);
43 // BN_cmp returns 0 if the two BIGNUMs are equal
44 EXPECT_FALSE(BN_cmp(n, n_actual));
45 EXPECT_FALSE(BN_cmp(e, e_actual));
46 EXPECT_FALSE(BN_cmp(d, d_actual));
48 RsaUniquePtr public_key(RSA_new());
49 BIGNUM* n_public = BN_new();
50 BIGNUM* e_public = BN_new();
51 EXPECT_TRUE(BN_set_bit(n_public, 1));
52 EXPECT_TRUE(BN_set_bit(e_public, 3));
53 RSA_set0_key(public_key.get(), n_public, e_public, nullptr);
54 BIGNUM* n_public_actual;
55 BIGNUM* e_public_actual;
58 (const BIGNUM**)&n_public_actual,
59 (const BIGNUM**)&e_public_actual,
61 EXPECT_FALSE(BN_cmp(n_public, n_public_actual));
62 EXPECT_FALSE(BN_cmp(e_public, e_public_actual));
65 TEST(OpenSSLPortabilityTest, TestEcdsaSigPortability) {
66 EcdsaSigUniquePtr ecdsa(ECDSA_SIG_new());
71 EXPECT_TRUE(BN_set_bit(r, 1));
72 EXPECT_TRUE(BN_set_bit(s, 2));
73 EXPECT_TRUE(ECDSA_SIG_set0(ecdsa.get(), r, s));
75 ecdsa.get(), (const BIGNUM**)&r_actual, (const BIGNUM**)&s_actual);
76 // BN_cmp returns 0 if the two BIGNUMs are equal
77 EXPECT_FALSE(BN_cmp(r, r_actual));
78 EXPECT_FALSE(BN_cmp(s, s_actual));
81 TEST(OpenSSLPortabilityTest, TestX509RevokedApi) {
82 X509_REVOKED* rev = X509_REVOKED_new();
84 ASN1_INTEGER* serial = ASN1_INTEGER_new();
85 ASN1_INTEGER_set(serial, 1234L);
87 ASN1_TIME* revocation_date = ASN1_TIME_new();
88 time_t t = time(nullptr);
89 ASN1_TIME_set(revocation_date, t);
91 X509_REVOKED_set_serialNumber(rev, serial);
92 X509_REVOKED_set_revocationDate(rev, revocation_date);
94 const ASN1_INTEGER* retrieved_serial = X509_REVOKED_get0_serialNumber(rev);
95 const ASN1_TIME* retrieved_date = X509_REVOKED_get0_revocationDate(rev);
97 EXPECT_EQ(0, ASN1_INTEGER_cmp(serial, retrieved_serial));
102 ASN1_TIME_diff(&diff_days, &diff_secs, revocation_date, retrieved_date);
103 EXPECT_EQ(0, diff_days);
104 EXPECT_EQ(0, diff_secs);
106 ASN1_INTEGER_free(serial);
107 ASN1_TIME_free(revocation_date);
108 X509_REVOKED_free(rev);