1 /*
2 * Copyright (c) 2018-present, 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.
7 */
8
9 #include <folly/portability/GMock.h>
10 #include <folly/portability/GTest.h>
11
12 #include <fizz/client/FizzClient.h>
13 #include <fizz/client/PskCache.h>
14 #include <fizz/client/test/Mocks.h>
15
16 using namespace folly;
17 using namespace testing;
18
19 namespace fizz {
20 namespace client {
21 namespace test {
22
23 class MockClientStateMachineInstance : public MockClientStateMachine {
24 public:
MockClientStateMachineInstance()25 MockClientStateMachineInstance() {
26 instance = this;
27 }
28 static MockClientStateMachineInstance* instance;
29 };
30 MockClientStateMachineInstance* MockClientStateMachineInstance::instance;
31
32 class ActionMoveVisitor {
33 public:
34 template <typename T>
operator ()(T &)35 void operator()(T&) {}
36 };
37
38 class TestFizzClient : public DelayedDestruction {
39 public:
TestFizzClient()40 TestFizzClient()
41 : fizzClient_(state_, queue_, readAeadOptions_, visitor_, this) {}
42
43 State state_;
44 IOBufQueue queue_;
45 ActionMoveVisitor visitor_;
46 Aead::AeadOptions readAeadOptions_;
47 FizzClient<ActionMoveVisitor, MockClientStateMachineInstance> fizzClient_;
48 };
49
50 class FizzClientTest : public Test {
51 public:
SetUp()52 void SetUp() override {
53 context_ = std::make_shared<FizzClientContext>();
54 fizzClient_.reset(new TestFizzClient());
55 }
56
57 protected:
58 std::shared_ptr<FizzClientContext> context_;
59 EventBase evb_;
60 std::unique_ptr<TestFizzClient, DelayedDestruction::Destructor> fizzClient_;
61 };
62
TEST_F(FizzClientTest,TestConnect)63 TEST_F(FizzClientTest, TestConnect) {
64 EXPECT_CALL(
65 *MockClientStateMachineInstance::instance,
66 _processConnect(_, _, _, _, _, _, _))
67 .WillOnce(InvokeWithoutArgs([] { return Actions(); }));
68 const auto sni = std::string("www.example.com");
69 fizzClient_->fizzClient_.connect(
70 context_,
71 nullptr,
72 sni,
73 folly::none,
74 folly::Optional<std::vector<fizz::ech::ECHConfig>>(folly::none));
75 }
76
TEST_F(FizzClientTest,TestConnectPskIdentity)77 TEST_F(FizzClientTest, TestConnectPskIdentity) {
78 std::string psk("psk");
79 EXPECT_CALL(
80 *MockClientStateMachineInstance::instance,
81 _processConnect(_, _, _, _, _, _, _))
82 .WillOnce(
83 Invoke([psk](
84 const State&,
85 std::shared_ptr<const FizzClientContext> context,
86 std::shared_ptr<const CertificateVerifier> verifier,
87 folly::Optional<std::string> sni,
88 folly::Optional<CachedPsk> cachedPsk,
89 const std::shared_ptr<ClientExtensions>& /* unused */,
90 const folly::Optional<
91 std::vector<ech::ECHConfig>>& /* unused */) {
92 EXPECT_TRUE(cachedPsk);
93 EXPECT_EQ(cachedPsk->psk, psk);
94 EXPECT_EQ(sni, "www.example.com");
95 return Actions();
96 }));
97 const auto sni = std::string("www.example.com");
98 CachedPsk cachedPsk;
99 cachedPsk.psk = psk;
100 fizzClient_->fizzClient_.connect(
101 context_,
102 nullptr,
103 sni,
104 std::move(cachedPsk),
105 folly::Optional<std::vector<ech::ECHConfig>>(folly::none));
106 }
107
TEST(FizzClientContextTest,TestCopy)108 TEST(FizzClientContextTest, TestCopy) {
109 FizzClientContext ctx;
110 auto ctx2 = ctx;
111 (void)ctx2;
112 }
113 } // namespace test
114 } // namespace client
115 } // namespace fizz
116