proxygen
KeySchedulerTest.cpp
Go to the documentation of this file.
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 <gtest/gtest.h>
10 
12 
13 #include <fizz/crypto/test/Mocks.h>
14 
15 using namespace folly;
16 using namespace testing;
17 
18 namespace fizz {
19 namespace test {
20 
22  public:
23  void SetUp() override {
24  auto kd = std::make_unique<MockKeyDerivation>();
25  kd_ = kd.get();
26  ON_CALL(*kd_, hashLength()).WillByDefault(Return(4));
27  ON_CALL(*kd_, _expandLabel(_, _, _, _))
28  .WillByDefault(InvokeWithoutArgs([]() { return IOBuf::create(0); }));
29  ks_ = std::make_unique<KeyScheduler>(std::move(kd));
30  }
31 
32  protected:
33  StringPiece transcript_{"hash"};
35  std::unique_ptr<KeyScheduler> ks_;
36 };
37 
38 TEST_F(KeySchedulerTest, TestEarly) {
39  StringPiece psk{"psk"};
40  EXPECT_CALL(*kd_, hkdfExtract(_, _));
41  ks_->deriveEarlySecret(psk);
42  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(4);
43  ks_->getSecret(EarlySecrets::ExternalPskBinder, transcript_);
44  ks_->getSecret(EarlySecrets::ResumptionPskBinder, transcript_);
45  ks_->getSecret(EarlySecrets::ResumptionPskBinder, transcript_);
46  ks_->getSecret(EarlySecrets::EarlyExporter, transcript_);
47 
48  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(1);
49  EXPECT_CALL(*kd_, hkdfExtract(_, _));
50  ks_->deriveHandshakeSecret();
51  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(2);
52  ks_->getSecret(HandshakeSecrets::ClientHandshakeTraffic, transcript_);
53  ks_->getSecret(HandshakeSecrets::ServerHandshakeTraffic, transcript_);
54 
55  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(1);
56  EXPECT_CALL(*kd_, hkdfExtract(_, _));
57  ks_->deriveMasterSecret();
58  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(2);
59  ks_->getSecret(MasterSecrets::ExporterMaster, transcript_);
60  ks_->getSecret(MasterSecrets::ResumptionMaster, transcript_);
61 
62  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(2);
63  ks_->deriveAppTrafficSecrets(transcript_);
64  ks_->getSecret(AppTrafficSecrets::ClientAppTraffic);
65  ks_->getSecret(AppTrafficSecrets::ServerAppTraffic);
66 }
67 
68 TEST_F(KeySchedulerTest, TestEarlyEcdhe) {
69  StringPiece psk{"psk"};
70  EXPECT_CALL(*kd_, hkdfExtract(_, _));
71  ks_->deriveEarlySecret(psk);
72  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(4);
73  ks_->getSecret(EarlySecrets::ExternalPskBinder, transcript_);
74  ks_->getSecret(EarlySecrets::ResumptionPskBinder, transcript_);
75  ks_->getSecret(EarlySecrets::ResumptionPskBinder, transcript_);
76  ks_->getSecret(EarlySecrets::EarlyExporter, transcript_);
77 
78  StringPiece ecdhe{"ecdhe"};
79  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(1);
80  EXPECT_CALL(*kd_, hkdfExtract(_, _));
81  ks_->deriveHandshakeSecret(ecdhe);
82  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(2);
83  ks_->getSecret(HandshakeSecrets::ClientHandshakeTraffic, transcript_);
84  ks_->getSecret(HandshakeSecrets::ServerHandshakeTraffic, transcript_);
85 
86  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(1);
87  EXPECT_CALL(*kd_, hkdfExtract(_, _));
88  ks_->deriveMasterSecret();
89  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(2);
90  ks_->getSecret(MasterSecrets::ExporterMaster, transcript_);
91  ks_->getSecret(MasterSecrets::ResumptionMaster, transcript_);
92 
93  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(2);
94  ks_->deriveAppTrafficSecrets(transcript_);
95  ks_->getSecret(AppTrafficSecrets::ClientAppTraffic);
96  ks_->getSecret(AppTrafficSecrets::ServerAppTraffic);
97 }
98 
99 TEST_F(KeySchedulerTest, TestNoEarly) {
100  StringPiece ecdhe{"ecdhe"};
101  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(1);
102  EXPECT_CALL(*kd_, hkdfExtract(_, _)).Times(2);
103  ks_->deriveHandshakeSecret(ecdhe);
104  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(2);
105  ks_->getSecret(HandshakeSecrets::ClientHandshakeTraffic, transcript_);
106  ks_->getSecret(HandshakeSecrets::ServerHandshakeTraffic, transcript_);
107 
108  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(1);
109  EXPECT_CALL(*kd_, hkdfExtract(_, _));
110  ks_->deriveMasterSecret();
111  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(2);
112  ks_->getSecret(MasterSecrets::ExporterMaster, transcript_);
113  ks_->getSecret(MasterSecrets::ResumptionMaster, transcript_);
114 
115  EXPECT_CALL(*kd_, deriveSecret(_, _, _)).Times(2);
116  ks_->deriveAppTrafficSecrets(transcript_);
117  ks_->getSecret(AppTrafficSecrets::ClientAppTraffic);
118  ks_->getSecret(AppTrafficSecrets::ServerAppTraffic);
119 }
120 
121 TEST_F(KeySchedulerTest, TestKeyUpdate) {
122  StringPiece ecdhe{"ecdhe"};
123  ks_->deriveHandshakeSecret(ecdhe);
124  ks_->deriveMasterSecret();
125  ks_->deriveAppTrafficSecrets(transcript_);
126 
127  EXPECT_CALL(*kd_, _expandLabel(_, _, _, _));
128  EXPECT_EQ(ks_->clientKeyUpdate(), 1);
129  EXPECT_CALL(*kd_, _expandLabel(_, _, _, _));
130  EXPECT_EQ(ks_->clientKeyUpdate(), 2);
131 
132  EXPECT_CALL(*kd_, _expandLabel(_, _, _, _));
133  EXPECT_EQ(ks_->serverKeyUpdate(), 1);
134  EXPECT_CALL(*kd_, _expandLabel(_, _, _, _));
135  EXPECT_EQ(ks_->serverKeyUpdate(), 2);
136 }
137 
138 TEST_F(KeySchedulerTest, TestTrafficKey) {
139  EXPECT_CALL(*kd_, _expandLabel(_, _, _, _)).Times(2);
140  StringPiece trafficSecret{"secret"};
141  ks_->getTrafficKey(trafficSecret, 10, 10);
142 }
143 } // namespace test
144 } // namespace fizz
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
std::unique_ptr< KeyScheduler > ks_
Definition: Actions.h:16
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
#define ON_CALL(obj, call)
#define EXPECT_CALL(obj, call)
const internal::AnythingMatcher _
internal::ReturnAction< R > Return(R value)