From: Alan Frindell Date: Thu, 2 Apr 2015 17:26:53 +0000 (-0700) Subject: Add socket mocks X-Git-Tag: v0.33.0~3 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=b6005aa33a79e11025582a80a1f35aa915e1c9b5;p=folly.git Add socket mocks Summary: These parallel the MockTAsync* from thrift/lib/cpp/test. Once all the users of the thrift versions are gone they can be deleted Test Plan: Unit tests Reviewed By: yfeldblum@fb.com, alandau@fb.com Subscribers: doug, net-systems@, folly-diffs@, yfeldblum, chalfant FB internal diff: D1959979 Signature: t1:1959979:1427917909:19af219f88dd6847a064da986dd30765e29bdc99 --- diff --git a/folly/Makefile.am b/folly/Makefile.am index 2dcf6771..d37cb9b1 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -164,6 +164,10 @@ nobase_follyinclude_HEADERS = \ io/async/TimeoutManager.h \ io/async/test/AsyncSSLSocketTest.h \ io/async/test/BlockingSocket.h \ + io/async/test/MockAsyncSocket.h \ + io/async/test/MockAsyncServerSocket.h \ + io/async/test/MockAsyncSSLSocket.h \ + io/async/test/MockAsyncTransport.h \ io/async/test/TimeUtil.h \ io/async/test/UndelayedDestruction.h \ io/async/test/Util.h \ diff --git a/folly/io/async/test/MockAsyncSSLSocket.h b/folly/io/async/test/MockAsyncSSLSocket.h new file mode 100644 index 00000000..47df2f77 --- /dev/null +++ b/folly/io/async/test/MockAsyncSSLSocket.h @@ -0,0 +1,72 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once +#include + +#include + +namespace folly { namespace test { + +class MockAsyncSSLSocket : public AsyncSSLSocket { + public: + MockAsyncSSLSocket( + const std::shared_ptr& ctx, + EventBase* base) : + AsyncSSLSocket(ctx, base) { + } + + GMOCK_METHOD5_(, noexcept, , + connect, + void(AsyncSocket::ConnectCallback*, + const folly::SocketAddress&, + int, + const OptionMap&, + const folly::SocketAddress&)); + MOCK_CONST_METHOD1(getLocalAddress, void(folly::SocketAddress*)); + MOCK_CONST_METHOD1(getPeerAddress, void(folly::SocketAddress*)); + MOCK_METHOD0(closeNow, void()); + MOCK_CONST_METHOD0(good, bool()); + MOCK_CONST_METHOD0(readable, bool()); + MOCK_CONST_METHOD0(hangup, bool()); + MOCK_CONST_METHOD2( + getSelectedNextProtocol, + void(const unsigned char**, unsigned*)); + MOCK_CONST_METHOD2( + getSelectedNextProtocolNoThrow, + bool(const unsigned char**, unsigned*)); + + void sslConn( + AsyncSSLSocket::HandshakeCB* cb, + uint64_t timeout, + const SSLContext::SSLVerifyPeerEnum& verify) + override { + if (timeout > 0) { + handshakeTimeout_.scheduleTimeout((uint32_t)timeout); + } + + state_ = StateEnum::ESTABLISHED; + sslState_ = STATE_CONNECTING; + handshakeCallback_ = cb; + + sslConnectMockable(cb, timeout, verify); + } + MOCK_METHOD3( + sslConnectMockable, + void(AsyncSSLSocket::HandshakeCB*, uint64_t, + const SSLContext::SSLVerifyPeerEnum&)); +}; + +}} diff --git a/folly/io/async/test/MockAsyncServerSocket.h b/folly/io/async/test/MockAsyncServerSocket.h new file mode 100644 index 00000000..ac0b8d5e --- /dev/null +++ b/folly/io/async/test/MockAsyncServerSocket.h @@ -0,0 +1,52 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#include +#include + +namespace folly { + +namespace test { + +class MockAsyncServerSocket : public AsyncServerSocket { +public: + typedef std::unique_ptr UniquePtr; + + // We explicitly do not mock destroy(), since the base class implementation + // in TDelayedDestruction is what actually deletes the object. + //MOCK_METHOD0(destroy, + // void()); + MOCK_METHOD1(bind, + void(const folly::SocketAddress& address)); + MOCK_METHOD2(bind, + void(const std::vector& ipAddresses, + uint16_t port)); + MOCK_METHOD1(bind, + void(uint16_t port)); + MOCK_METHOD1(listen, + void(int backlog)); + MOCK_METHOD0(startAccepting, + void()); + MOCK_METHOD3(addAcceptCallback, + void(AcceptCallback *callback, + EventBase *eventBase, + uint32_t maxAtOnce)); +}; + +}} diff --git a/folly/io/async/test/MockAsyncSocket.h b/folly/io/async/test/MockAsyncSocket.h new file mode 100644 index 00000000..2da6e8e6 --- /dev/null +++ b/folly/io/async/test/MockAsyncSocket.h @@ -0,0 +1,51 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#include +#include + +namespace folly { + +namespace test { + +class MockAsyncSocket : public AsyncSocket { + public: + typedef std::unique_ptr UniquePtr; + + explicit MockAsyncSocket(EventBase* base) : AsyncSocket(base) { + } + + GMOCK_METHOD5_(, noexcept, , connect, + void(AsyncSocket::ConnectCallback*, + const folly::SocketAddress&, + int, + const OptionMap&, + const folly::SocketAddress&)); + + MOCK_CONST_METHOD1(getPeerAddress, + void(folly::SocketAddress*)); + MOCK_METHOD0(detachFd, int()); + MOCK_CONST_METHOD0(getFd, int()); + MOCK_METHOD0(closeNow, void()); + MOCK_CONST_METHOD0(good, bool()); + MOCK_CONST_METHOD0(readable, bool()); + MOCK_CONST_METHOD0(hangup, bool()); +}; + +}} diff --git a/folly/io/async/test/MockAsyncTransport.h b/folly/io/async/test/MockAsyncTransport.h new file mode 100644 index 00000000..e4f559c5 --- /dev/null +++ b/folly/io/async/test/MockAsyncTransport.h @@ -0,0 +1,90 @@ +/* + * Copyright 2015 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once + +#include + +#include + +namespace folly { namespace test { + +class MockAsyncTransport: public AsyncTransportWrapper { + public: + MOCK_METHOD1(setReadCB, void(ReadCallback*)); + MOCK_CONST_METHOD0(getReadCallback, ReadCallback*()); + MOCK_CONST_METHOD0(getReadCB, ReadCallback*()); + MOCK_METHOD4(write, void(WriteCallback*, + const void*, size_t, + WriteFlags)); + MOCK_METHOD4(writev, void(WriteCallback*, + const iovec*, size_t, + WriteFlags)); + MOCK_METHOD3(writeChain, + void(WriteCallback*, + std::shared_ptr, + WriteFlags)); + + + void writeChain(WriteCallback* callback, + std::unique_ptr&& iob, + WriteFlags flags = + WriteFlags::NONE) override { + writeChain(callback, std::shared_ptr(iob.release()), flags); + } + + MOCK_METHOD0(close, void()); + MOCK_METHOD0(closeNow, void()); + MOCK_METHOD0(closeWithReset, void()); + MOCK_METHOD0(shutdownWrite, void()); + MOCK_METHOD0(shutdownWriteNow, void()); + MOCK_CONST_METHOD0(good, bool()); + MOCK_CONST_METHOD0(readable, bool()); + MOCK_CONST_METHOD0(connecting, bool()); + MOCK_CONST_METHOD0(error, bool()); + MOCK_METHOD1(attachEventBase, void(EventBase*)); + MOCK_METHOD0(detachEventBase, void()); + MOCK_CONST_METHOD0(isDetachable, bool()); + MOCK_CONST_METHOD0(getEventBase, EventBase*()); + MOCK_METHOD1(setSendTimeout, void(uint32_t)); + MOCK_CONST_METHOD0(getSendTimeout, uint32_t()); + MOCK_CONST_METHOD1(getLocalAddress, void(folly::SocketAddress*)); + MOCK_CONST_METHOD1(getPeerAddress, void(folly::SocketAddress*)); + MOCK_CONST_METHOD0(getAppBytesWritten, size_t()); + MOCK_CONST_METHOD0(getRawBytesWritten, size_t()); + MOCK_CONST_METHOD0(getAppBytesReceived, size_t()); + MOCK_CONST_METHOD0(getRawBytesReceived, size_t()); + MOCK_CONST_METHOD0(isEorTrackingEnabled, bool()); + MOCK_METHOD1(setEorTracking, void(bool)); + +}; + +class MockReadCallback: public AsyncTransportWrapper::ReadCallback { + public: + MOCK_METHOD2(getReadBuffer, void(void**, size_t*)); + GMOCK_METHOD1_(, noexcept, , readDataAvailable, void(size_t)); + GMOCK_METHOD0_(, noexcept, , readEOF, void()); + GMOCK_METHOD1_(, noexcept, , readErr, + void(const AsyncSocketException&)); +}; + +class MockWriteCallback: public AsyncTransportWrapper::WriteCallback { + public: + GMOCK_METHOD0_(, noexcept, , writeSuccess, void()); + GMOCK_METHOD2_(, noexcept, , writeErr, + void(size_t, const AsyncSocketException&)); +}; + +}}