proxygen
Mocks.h
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 #pragma once
10 
13 #include <fizz/crypto/test/Mocks.h>
18 #include <fizz/protocol/Factory.h>
21 #include <fizz/protocol/Types.h>
22 #include <fizz/record/test/Mocks.h>
23 
25 
26 /* using override */
27 using namespace testing;
28 
29 namespace fizz {
30 namespace test {
31 
33  public:
35 
36  MOCK_METHOD1(deriveEarlySecret, void(folly::ByteRange psk));
37  MOCK_METHOD0(deriveHandshakeSecret, void());
38  MOCK_METHOD1(deriveHandshakeSecret, void(folly::ByteRange ecdhe));
39  MOCK_METHOD0(deriveMasterSecret, void());
40  MOCK_METHOD1(deriveAppTrafficSecrets, void(folly::ByteRange transcript));
41  MOCK_METHOD0(clearMasterSecret, void());
42  MOCK_METHOD0(clientKeyUpdate, uint32_t());
43  MOCK_METHOD0(serverKeyUpdate, uint32_t());
45  getSecret,
46  std::vector<uint8_t>(EarlySecrets s, folly::ByteRange transcript));
48  getSecret,
49  std::vector<uint8_t>(HandshakeSecrets s, folly::ByteRange transcript));
51  getSecret,
52  std::vector<uint8_t>(MasterSecrets s, folly::ByteRange transcript));
53  MOCK_CONST_METHOD1(getSecret, std::vector<uint8_t>(AppTrafficSecrets s));
55  getTrafficKey,
56  TrafficKey(
57  folly::ByteRange trafficSecret,
58  size_t keyLength,
59  size_t ivLength));
61  getResumptionSecret,
63 
64  void setDefaults() {
65  ON_CALL(*this, getTrafficKey(_, _, _))
66  .WillByDefault(InvokeWithoutArgs([]() {
69  }));
70  ON_CALL(*this, getResumptionSecret(_, _))
71  .WillByDefault(InvokeWithoutArgs(
72  []() { return folly::IOBuf::copyBuffer("resumesecret"); }));
73  }
74 };
75 
77  public:
78  MOCK_METHOD1(appendToTranscript, void(const Buf& transcript));
79  MOCK_CONST_METHOD0(getHandshakeContext, Buf());
81  MOCK_CONST_METHOD0(getBlankContext, folly::ByteRange());
82 
83  void setDefaults() {
84  ON_CALL(*this, getHandshakeContext()).WillByDefault(InvokeWithoutArgs([]() {
85  return folly::IOBuf::copyBuffer("context");
86  }));
87 
88  ON_CALL(*this, getFinishedData(_)).WillByDefault(InvokeWithoutArgs([]() {
89  return folly::IOBuf::copyBuffer("verifydata");
90  }));
91  }
92 };
93 
94 class MockCert : public Cert {
95  public:
96  MOCK_CONST_METHOD0(getIdentity, std::string());
98 };
99 
100 class MockSelfCert : public SelfCert {
101  public:
102  MOCK_CONST_METHOD0(getIdentity, std::string());
103  MOCK_CONST_METHOD0(getAltIdentities, std::vector<std::string>());
104  MOCK_CONST_METHOD0(getSigSchemes, std::vector<SignatureScheme>());
105 
106  MOCK_CONST_METHOD1(_getCertMessage, CertificateMsg(Buf&));
107  CertificateMsg getCertMessage(Buf buf) const override {
108  return _getCertMessage(buf);
109  }
111  getCompressedCert,
113 
115  sign,
116  Buf(SignatureScheme scheme,
118  folly::ByteRange toBeSigned));
120 };
121 
122 class MockPeerCert : public PeerCert {
123  public:
124  MOCK_CONST_METHOD0(getIdentity, std::string());
126  verify,
127  void(
128  SignatureScheme scheme,
130  folly::ByteRange toBeSigned,
131  folly::ByteRange signature));
133 };
134 
136  public:
138  verify,
139  void(const std::vector<std::shared_ptr<const PeerCert>>&));
140 
141  MOCK_CONST_METHOD0(getCertificateRequestExtensions, std::vector<Extension>());
142 };
143 
144 class MockFactory : public Factory {
145  public:
147  makePlaintextReadRecordLayer,
148  std::unique_ptr<PlaintextReadRecordLayer>());
150  makePlaintextWriteRecordLayer,
151  std::unique_ptr<PlaintextWriteRecordLayer>());
153  makeEncryptedReadRecordLayer,
154  std::unique_ptr<EncryptedReadRecordLayer>(
155  EncryptionLevel encryptionLevel));
157  makeEncryptedWriteRecordLayer,
158  std::unique_ptr<EncryptedWriteRecordLayer>(
159  EncryptionLevel encryptionLevel));
161  makeKeyScheduler,
162  std::unique_ptr<KeyScheduler>(CipherSuite cipher));
164  makeHandshakeContext,
165  std::unique_ptr<HandshakeContext>(CipherSuite cipher));
167  makeKeyExchange,
168  std::unique_ptr<KeyExchange>(NamedGroup group));
169  MOCK_CONST_METHOD1(makeAead, std::unique_ptr<Aead>(CipherSuite cipher));
171  MOCK_CONST_METHOD0(makeTicketAgeAdd, uint32_t());
172 
173  MOCK_CONST_METHOD1(_makePeerCert, std::shared_ptr<PeerCert>(Buf&));
174  std::shared_ptr<PeerCert> makePeerCert(Buf certData) const override {
175  return _makePeerCert(certData);
176  }
177 
178  void setDefaults() {
179  ON_CALL(*this, makePlaintextReadRecordLayer())
180  .WillByDefault(InvokeWithoutArgs(
181  []() { return std::make_unique<MockPlaintextReadRecordLayer>(); }));
182 
183  ON_CALL(*this, makePlaintextWriteRecordLayer())
184  .WillByDefault(InvokeWithoutArgs([]() {
185  auto ret = std::make_unique<MockPlaintextWriteRecordLayer>();
186  ret->setDefaults();
187  return ret;
188  }));
189  ON_CALL(*this, makeEncryptedReadRecordLayer(_))
190  .WillByDefault(Invoke([](EncryptionLevel encryptionLevel) {
191  return std::make_unique<MockEncryptedReadRecordLayer>(
192  encryptionLevel);
193  }));
194 
195  ON_CALL(*this, makeEncryptedWriteRecordLayer(_))
196  .WillByDefault(Invoke([](EncryptionLevel encryptionLevel) {
197  auto ret =
198  std::make_unique<MockEncryptedWriteRecordLayer>(encryptionLevel);
199  ret->setDefaults();
200  return ret;
201  }));
202 
203  ON_CALL(*this, makeKeyScheduler(_)).WillByDefault(InvokeWithoutArgs([]() {
204  auto ret = std::make_unique<MockKeyScheduler>();
205  ret->setDefaults();
206  return ret;
207  }));
208  ON_CALL(*this, makeHandshakeContext(_))
209  .WillByDefault(InvokeWithoutArgs([]() {
210  auto ret = std::make_unique<MockHandshakeContext>();
211  ret->setDefaults();
212  return ret;
213  }));
214  ON_CALL(*this, makeKeyExchange(_)).WillByDefault(InvokeWithoutArgs([]() {
215  auto ret = std::make_unique<MockKeyExchange>();
216  ret->setDefaults();
217  return ret;
218  }));
219  ON_CALL(*this, makeAead(_)).WillByDefault(InvokeWithoutArgs([]() {
220  auto ret = std::make_unique<MockAead>();
221  ret->setDefaults();
222  return ret;
223  }));
224  ON_CALL(*this, makeRandom()).WillByDefault(InvokeWithoutArgs([]() {
225  Random random;
226  random.fill(0x44);
227  return random;
228  }));
229  ON_CALL(*this, makeTicketAgeAdd()).WillByDefault(InvokeWithoutArgs([]() {
230  return 0x44444444;
231  }));
232  ON_CALL(*this, _makePeerCert(_)).WillByDefault(InvokeWithoutArgs([]() {
233  return std::make_unique<MockPeerCert>();
234  }));
235  }
236 };
237 
239  public:
242  void setDefaults() {
243  ON_CALL(*this, getAlgorithm()).WillByDefault(InvokeWithoutArgs([]() {
244  return CertificateCompressionAlgorithm::zlib;
245  }));
246  }
247 };
248 
250  public:
253  void setDefaults() {
254  ON_CALL(*this, getAlgorithm()).WillByDefault(InvokeWithoutArgs([]() {
255  return CertificateCompressionAlgorithm::zlib;
256  }));
257  }
258 };
259 
261  public:
264  new folly::test::MockAsyncTransport())) {}
265  MOCK_CONST_METHOD0(good, bool());
266  MOCK_CONST_METHOD0(readable, bool());
267  MOCK_CONST_METHOD0(connecting, bool());
268  MOCK_CONST_METHOD0(error, bool());
270  MOCK_CONST_METHOD0(getSelfCert, const X509*());
271  MOCK_CONST_METHOD0(isReplaySafe, bool());
272  MOCK_METHOD1(
273  setReplaySafetyCallback,
275  MOCK_CONST_METHOD0(getSelfCertificate, const Cert*());
276  MOCK_CONST_METHOD0(getPeerCertificate, const Cert*());
277  MOCK_CONST_METHOD0(getApplicationProtocol_, std::string());
278 
280  return getApplicationProtocol_();
281  }
282 
284  MOCK_CONST_METHOD0(getSupportedSigSchemes, std::vector<SignatureScheme>());
286 
287  MOCK_METHOD3(
288  writeAppDataInternal,
289  void(
291  std::shared_ptr<folly::IOBuf>,
293 
296  std::unique_ptr<folly::IOBuf>&& buf,
298  writeAppDataInternal(
299  callback, std::shared_ptr<folly::IOBuf>(buf.release()), flags);
300  }
301 
302  MOCK_METHOD1(transportError, void(const folly::AsyncSocketException&));
303 
304  MOCK_METHOD0(transportDataAvailable, void());
305 };
306 
307 } // namespace test
308 } // namespace fizz
AppTrafficSecrets
Definition: KeyScheduler.h:28
Integral2 random(Integral1 low, Integral2 up)
#define MOCK_CONST_METHOD1(m,...)
flags
Definition: http_parser.h:127
void verify(int extras)
CertificateCompressionAlgorithm
Definition: Types.h:167
std::unique_ptr< X509, X509Deleter > X509UniquePtr
context
Definition: CMakeCache.txt:563
void writeAppData(folly::AsyncTransportWrapper::WriteCallback *callback, std::unique_ptr< folly::IOBuf > &&buf, folly::WriteFlags flags=folly::WriteFlags::NONE) override
Definition: Mocks.h:294
NamedGroup
Definition: Types.h:302
SignatureScheme
Definition: Types.h:257
CipherSuite
Definition: Types.h:153
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
CipherSuite cipher
EarlySecrets
Definition: KeyScheduler.h:17
#define MOCK_METHOD3(m,...)
MasterSecrets
Definition: KeyScheduler.h:26
EncryptionLevel
Definition: Types.h:29
std::string getApplicationProtocol() const noexceptoverride
Definition: Mocks.h:279
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
CertificateMsg getCertMessage(Buf buf) const override
Definition: Mocks.h:107
Definition: Actions.h:16
std::unique_ptr< folly::IOBuf > makeRandom(size_t n)
HandshakeSecrets
Definition: KeyScheduler.h:24
std::array< uint8_t, 32 > Random
Definition: Types.h:184
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Definition: Memory.h:259
Buf getFinishedData(std::unique_ptr< KeyDerivation > &deriver, Buf &finishedMacKey, const Buf &finishedTranscript)
#define MOCK_CONST_METHOD3(m,...)
CertificateVerifyContext
Definition: Certificate.h:20
Optional< NamedGroup > group
std::shared_ptr< PeerCert > getPeerCert(const CertAndKey &cert)
Definition: Utilities.h:122
#define ON_CALL(obj, call)
std::unique_ptr< AsyncFizzBase, folly::DelayedDestruction::Destructor > UniquePtr
Definition: AsyncFizzBase.h:30
std::unique_ptr< Aead > getCipher(const CipherParams &params)
#define MOCK_METHOD1(m,...)
const char * string
Definition: Conv.cpp:212
#define MOCK_CONST_METHOD4(m,...)
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
static set< string > s
const
Definition: upload.py:398
#define MOCK_CONST_METHOD2(m,...)
#define MOCK_CONST_METHOD0(m,...)
std::shared_ptr< PeerCert > makePeerCert(Buf certData) const override
Definition: Mocks.h:174
const internal::AnythingMatcher _
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
#define MOCK_METHOD0(m,...)