2 * Copyright (c) 2015, Facebook, Inc.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
10 #include <folly/io/async/EventBase.h>
11 #include <folly/io/async/SSLContext.h>
12 #include <glog/logging.h>
13 #include <gtest/gtest.h>
14 #include <folly/wangle/ssl/SSLContextManager.h>
15 #include <folly/wangle/acceptor/DomainNameMisc.h>
17 using std::shared_ptr;
21 TEST(SSLContextManagerTest, Test1)
24 SSLContextManager sslCtxMgr(&eventBase, "vip_ssl_context_manager_test_",
26 auto www_facebook_com_ctx = std::make_shared<SSLContext>();
27 auto start_facebook_com_ctx = std::make_shared<SSLContext>();
28 auto start_abc_facebook_com_ctx = std::make_shared<SSLContext>();
30 sslCtxMgr.insertSSLCtxByDomainName(
32 strlen("www.facebook.com"),
33 www_facebook_com_ctx);
34 sslCtxMgr.insertSSLCtxByDomainName(
36 strlen("www.facebook.com"),
37 www_facebook_com_ctx);
39 sslCtxMgr.insertSSLCtxByDomainName(
41 strlen("www.facebook.com"),
42 std::make_shared<SSLContext>());
43 } catch (const std::exception& ex) {
45 sslCtxMgr.insertSSLCtxByDomainName(
47 strlen("*.facebook.com"),
48 start_facebook_com_ctx);
49 sslCtxMgr.insertSSLCtxByDomainName(
51 strlen("*.abc.facebook.com"),
52 start_abc_facebook_com_ctx);
54 sslCtxMgr.insertSSLCtxByDomainName(
56 strlen("*.abc.facebook.com"),
57 std::make_shared<SSLContext>());
59 } catch (const std::exception& ex) {
62 shared_ptr<SSLContext> retCtx;
63 retCtx = sslCtxMgr.getSSLCtx(DNString("www.facebook.com"));
64 EXPECT_EQ(retCtx, www_facebook_com_ctx);
65 retCtx = sslCtxMgr.getSSLCtx(DNString("WWW.facebook.com"));
66 EXPECT_EQ(retCtx, www_facebook_com_ctx);
67 EXPECT_FALSE(sslCtxMgr.getSSLCtx(DNString("xyz.facebook.com")));
69 retCtx = sslCtxMgr.getSSLCtxBySuffix(DNString("xyz.facebook.com"));
70 EXPECT_EQ(retCtx, start_facebook_com_ctx);
71 retCtx = sslCtxMgr.getSSLCtxBySuffix(DNString("XYZ.facebook.com"));
72 EXPECT_EQ(retCtx, start_facebook_com_ctx);
74 retCtx = sslCtxMgr.getSSLCtxBySuffix(DNString("www.abc.facebook.com"));
75 EXPECT_EQ(retCtx, start_abc_facebook_com_ctx);
77 // ensure "facebook.com" does not match "*.facebook.com"
78 EXPECT_FALSE(sslCtxMgr.getSSLCtxBySuffix(DNString("facebook.com")));
79 // ensure "Xfacebook.com" does not match "*.facebook.com"
80 EXPECT_FALSE(sslCtxMgr.getSSLCtxBySuffix(DNString("Xfacebook.com")));
81 // ensure wildcard name only matches one domain up
82 EXPECT_FALSE(sslCtxMgr.getSSLCtxBySuffix(DNString("abc.xyz.facebook.com")));
84 eventBase.loop(); // Clean up events before SSLContextManager is destructed