Commit c9f2df1b authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

identity

Summary: [Folly] `identity`, an initialized function object matching `Identity`, which is renamed `identity_fn`.

Reviewed By: markisaa, lewissbaker

Differential Revision: D21265889

fbshipit-source-id: 5cc9f2e624da9a4acd0f65985ff10a4dd306efdc
parent bb79aaa5
...@@ -247,25 +247,27 @@ struct transparent : T { ...@@ -247,25 +247,27 @@ struct transparent : T {
* Example: * Example:
* *
* int i = 42; * int i = 42;
* int &j = Identity()(i); * int &j = identity(i);
* assert(&i == &j); * assert(&i == &j);
* *
* Warning: passing a prvalue through Identity turns it into an xvalue, * Warning: passing a prvalue through identity turns it into an xvalue,
* which can effect whether lifetime extension occurs or not. For instance: * which can effect whether lifetime extension occurs or not. For instance:
* *
* auto&& x = std::make_unique<int>(42); * auto&& x = std::make_unique<int>(42);
* cout << *x ; // OK, x refers to a valid unique_ptr. * cout << *x ; // OK, x refers to a valid unique_ptr.
* *
* auto&& y = Identity()(std::make_unique<int>(42)); * auto&& y = identity(std::make_unique<int>(42));
* cout << *y ; // ERROR: y did not lifetime-extend the unique_ptr. It * cout << *y ; // ERROR: y did not lifetime-extend the unique_ptr. It
* // is no longer valid * // is no longer valid
*/ */
struct Identity { struct identity_fn {
template <class T> template <class T>
constexpr T&& operator()(T&& x) const noexcept { constexpr T&& operator()(T&& x) const noexcept {
return static_cast<T&&>(x); return static_cast<T&&>(x);
} }
}; };
using Identity = identity_fn;
FOLLY_INLINE_VARIABLE constexpr identity_fn identity;
namespace moveonly_ { // Protection from unintended ADL. namespace moveonly_ { // Protection from unintended ADL.
......
...@@ -171,17 +171,17 @@ static void validateTestCertBundle( ...@@ -171,17 +171,17 @@ static void validateTestCertBundle(
const std::vector<folly::ssl::X509UniquePtr>& certs) { const std::vector<folly::ssl::X509UniquePtr>& certs) {
EXPECT_EQ(certs.size(), 3); EXPECT_EQ(certs.size(), 3);
for (auto i : folly::enumerate(certs)) { for (auto i : folly::enumerate(certs)) {
auto identity = folly::ssl::OpenSSLCertUtils::getCommonName(**i); auto cn = folly::ssl::OpenSSLCertUtils::getCommonName(**i);
EXPECT_TRUE(identity); EXPECT_TRUE(cn);
EXPECT_EQ(*identity, folly::sformat("test cert {}", i.index + 1)); EXPECT_EQ(*cn, folly::sformat("test cert {}", i.index + 1));
} }
} }
// Validate parsed cert from kTestCertWithSan. // Validate parsed cert from kTestCertWithSan.
static void validateTestCertWithSAN(X509* x509) { static void validateTestCertWithSAN(X509* x509) {
ASSERT_NE(nullptr, x509); ASSERT_NE(nullptr, x509);
auto identity = folly::ssl::OpenSSLCertUtils::getCommonName(*x509); auto cn = folly::ssl::OpenSSLCertUtils::getCommonName(*x509);
EXPECT_EQ("127.0.0.1", identity.value()); EXPECT_EQ("127.0.0.1", cn.value());
auto altNames = folly::ssl::OpenSSLCertUtils::getSubjectAltNames(*x509); auto altNames = folly::ssl::OpenSSLCertUtils::getSubjectAltNames(*x509);
EXPECT_EQ(2, altNames.size()); EXPECT_EQ(2, altNames.size());
EXPECT_EQ("anotherexample.com", altNames[0]); EXPECT_EQ("anotherexample.com", altNames[0]);
...@@ -191,8 +191,8 @@ static void validateTestCertWithSAN(X509* x509) { ...@@ -191,8 +191,8 @@ static void validateTestCertWithSAN(X509* x509) {
TEST_P(OpenSSLCertUtilsTest, TestX509CN) { TEST_P(OpenSSLCertUtilsTest, TestX509CN) {
auto x509 = readCertFromFile(kTestCertWithoutSan); auto x509 = readCertFromFile(kTestCertWithoutSan);
EXPECT_NE(x509, nullptr); EXPECT_NE(x509, nullptr);
auto identity = folly::ssl::OpenSSLCertUtils::getCommonName(*x509); auto cn = folly::ssl::OpenSSLCertUtils::getCommonName(*x509);
EXPECT_EQ(identity.value(), "Asox Company"); EXPECT_EQ(cn.value(), "Asox Company");
auto sans = folly::ssl::OpenSSLCertUtils::getSubjectAltNames(*x509); auto sans = folly::ssl::OpenSSLCertUtils::getSubjectAltNames(*x509);
EXPECT_EQ(sans.size(), 0); EXPECT_EQ(sans.size(), 0);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment