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 
11 #include <fizz/protocol/Types.h>
14 
15 /* using override */
16 using namespace testing;
17 
18 namespace fizz {
19 
20 template <typename T>
21 void setWriteDefaults(T* obj) {
22  ON_CALL(*obj, _write(_)).WillByDefault(Invoke([obj](TLSMessage& msg) {
23  TLSContent content;
24  content.contentType = msg.type;
25  content.encryptionLevel = obj->getEncryptionLevel();
26 
27  if (msg.type == ContentType::application_data) {
28  content.data = folly::IOBuf::copyBuffer("appdata");
29  } else if (msg.type == ContentType::handshake) {
30  content.data = folly::IOBuf::copyBuffer("handshake");
31  } else if (msg.type == ContentType::alert) {
32  auto buf = folly::IOBuf::copyBuffer("alert");
33  buf->prependChain(std::move(msg.fragment));
34  buf->coalesce();
35  content.data = std::move(buf);
36  } else {
37  content.data = std::unique_ptr<folly::IOBuf>();
38  }
39  return content;
40  }));
41 }
42 
44  public:
46  MOCK_CONST_METHOD0(hasUnparsedHandshakeData, bool());
47  MOCK_METHOD1(setSkipEncryptedRecords, void(bool));
48 };
49 
51  public:
53  : EncryptedReadRecordLayer(encryptionLevel) {}
54 
56  MOCK_CONST_METHOD0(hasUnparsedHandshakeData, bool());
57 
58  MOCK_METHOD2(_setAead, void(folly::ByteRange, Aead*));
59  void setAead(folly::ByteRange baseSecret, std::unique_ptr<Aead> aead)
60  override {
61  _setAead(baseSecret, aead.get());
62  }
63 
64  MOCK_METHOD1(setSkipFailedDecryption, void(bool));
65 };
66 
68  public:
70  TLSContent write(TLSMessage&& msg) const override {
71  return _write(msg);
72  }
73 
74  MOCK_CONST_METHOD1(_writeInitialClientHello, TLSContent(Buf&));
75  TLSContent writeInitialClientHello(Buf encoded) const override {
76  return _writeInitialClientHello(encoded);
77  }
78 
79  void setDefaults() {
80  setWriteDefaults(this);
81  ON_CALL(*this, _writeInitialClientHello(_))
82  .WillByDefault(InvokeWithoutArgs([]() {
83  TLSContent record;
84  record.contentType = ContentType::handshake;
85  record.data = folly::IOBuf::copyBuffer("handshake");
86  record.encryptionLevel = EncryptionLevel::Plaintext;
87  return record;
88  }));
89  }
90 };
91 
93  public:
95  : EncryptedWriteRecordLayer(encryptionLevel) {}
96 
98  TLSContent write(TLSMessage&& msg) const override {
99  return _write(msg);
100  }
101 
102  MOCK_METHOD2(_setAead, void(folly::ByteRange, Aead*));
103  void setAead(folly::ByteRange baseSecret, std::unique_ptr<Aead> aead)
104  override {
105  _setAead(baseSecret, aead.get());
106  }
107 
108  void setDefaults() {
109  setWriteDefaults(this);
110  }
111 };
112 } // namespace fizz
Buf fragment
Definition: Types.h:56
#define T(v)
Definition: http_parser.c:233
#define MOCK_CONST_METHOD1(m,...)
void setWriteDefaults(T *obj)
Definition: Mocks.h:21
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
MockEncryptedReadRecordLayer(EncryptionLevel encryptionLevel)
Definition: Mocks.h:52
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
TLSContent write(TLSMessage &&msg) const override
Definition: Mocks.h:98
EncryptionLevel
Definition: Types.h:29
MockEncryptedWriteRecordLayer(EncryptionLevel encryptionLevel)
Definition: Mocks.h:94
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
void setAead(folly::ByteRange baseSecret, std::unique_ptr< Aead > aead) override
Definition: Mocks.h:59
EncryptionLevel encryptionLevel
Definition: RecordLayer.h:21
Definition: Actions.h:16
TLSContent write(TLSMessage &&msg) const override
Definition: Mocks.h:70
TLSContent writeInitialClientHello(Buf encoded) const override
Definition: Mocks.h:75
void setAead(folly::ByteRange baseSecret, std::unique_ptr< Aead > aead) override
Definition: Mocks.h:103
#define ON_CALL(obj, call)
#define MOCK_METHOD1(m,...)
ContentType contentType
Definition: RecordLayer.h:20
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
#define MOCK_CONST_METHOD0(m,...)
const internal::AnythingMatcher _
ContentType type
Definition: Types.h:55
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_METHOD2(m,...)