copy wangle back into folly
[folly.git] / folly / wangle / ssl / test / SSLContextManagerTest.cpp
1 /*
2  *  Copyright (c) 2015, Facebook, Inc.
3  *  All rights reserved.
4  *
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.
8  *
9  */
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>
16
17 using std::shared_ptr;
18
19 namespace folly {
20
21 TEST(SSLContextManagerTest, Test1)
22 {
23   EventBase eventBase;
24   SSLContextManager sslCtxMgr(&eventBase, "vip_ssl_context_manager_test_",
25                               true, nullptr);
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>();
29
30   sslCtxMgr.insertSSLCtxByDomainName(
31     "www.facebook.com",
32     strlen("www.facebook.com"),
33     www_facebook_com_ctx);
34   sslCtxMgr.insertSSLCtxByDomainName(
35     "www.facebook.com",
36     strlen("www.facebook.com"),
37     www_facebook_com_ctx);
38   try {
39     sslCtxMgr.insertSSLCtxByDomainName(
40       "www.facebook.com",
41       strlen("www.facebook.com"),
42       std::make_shared<SSLContext>());
43   } catch (const std::exception& ex) {
44   }
45   sslCtxMgr.insertSSLCtxByDomainName(
46     "*.facebook.com",
47     strlen("*.facebook.com"),
48     start_facebook_com_ctx);
49   sslCtxMgr.insertSSLCtxByDomainName(
50     "*.abc.facebook.com",
51     strlen("*.abc.facebook.com"),
52     start_abc_facebook_com_ctx);
53   try {
54     sslCtxMgr.insertSSLCtxByDomainName(
55       "*.abc.facebook.com",
56       strlen("*.abc.facebook.com"),
57       std::make_shared<SSLContext>());
58     FAIL();
59   } catch (const std::exception& ex) {
60   }
61
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")));
68
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);
73
74   retCtx = sslCtxMgr.getSSLCtxBySuffix(DNString("www.abc.facebook.com"));
75   EXPECT_EQ(retCtx, start_abc_facebook_com_ctx);
76
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")));
83
84   eventBase.loop(); // Clean up events before SSLContextManager is destructed
85 }
86
87 }