1 /*
2 BAREOS® - Backup Archiving REcovery Open Sourced
3
4 Copyright (C) 2019-2019 Bareos GmbH & Co. KG
5
6 This program is Free Software; you can redistribute it and/or
7 modify it under the terms of version three of the GNU Affero General Public
8 License as published by the Free Software Foundation and included
9 in the file LICENSE.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Affero General Public License for more details.
15
16 You should have received a copy of the GNU Affero General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20 */
21 /**
22 * Mockup for Network Utility Routines
23 *
24 */
25
26 #include "gmock/gmock.h"
27 #include "gtest/gtest.h"
28
29 #include "include/bareos.h"
30 #include "include/jcr.h"
31 #include "lib/bnet.h"
32 #include "lib/bsock.h"
33 #include "lib/bstringlist.h"
34 #include "lib/cram_md5.h"
35 #include "lib/tls.h"
36 #include "lib/util.h"
37
38 #include <algorithm>
39
40 #ifdef __GNUC__
41 #ifndef __clang__
42 /* ignore the suggest-override warnings caused by MOCK_METHODx */
43 #pragma GCC diagnostic push
44 #pragma GCC diagnostic ignored "-Wsuggest-override"
45 #endif
46 #endif
47
48 class BareosSocketMock : public BareosSocket {
49 public:
BareosSocketMock()50 BareosSocketMock() : BareosSocket() {}
51 ~BareosSocketMock();
52 MOCK_METHOD0(clone, BareosSocket*());
53 MOCK_METHOD9(connect,
54 bool(JobControlRecord*,
55 int,
56 utime_t,
57 utime_t,
58 const char*,
59 const char*,
60 char*,
61 int,
62 bool));
63 MOCK_METHOD0(recv, int32_t());
64 MOCK_METHOD0(send, bool());
65 MOCK_METHOD2(read_nbytes, int32_t(char*, int32_t));
66 MOCK_METHOD2(write_nbytes, int32_t(char*, int32_t));
67 MOCK_METHOD0(close, void());
68 MOCK_METHOD0(destroy, void());
69 MOCK_METHOD2(GetPeer, int(char*, socklen_t));
70 MOCK_METHOD2(SetBufferSize, bool(uint32_t, int));
71 MOCK_METHOD0(SetNonblocking, int());
72 MOCK_METHOD0(SetBlocking, int());
73 MOCK_METHOD1(RestoreBlocking, void(int));
74 MOCK_METHOD0(ConnectionReceivedTerminateSignal, bool());
75 MOCK_METHOD2(WaitData, int(int, int));
76 MOCK_METHOD2(WaitDataIntr, int(int, int));
77 MOCK_METHOD6(FinInit,
78 void(JobControlRecord*,
79 int,
80 const char*,
81 const char*,
82 int,
83 struct sockaddr*));
84 MOCK_METHOD7(open,
85 bool(JobControlRecord*,
86 const char*,
87 const char*,
88 char*,
89 int,
90 utime_t,
91 int*));
92 };
93 #ifdef __GNUC__
94 #ifndef __clang__
95 #pragma GCC diagnostic pop
96 #endif
97 #endif
98 /* define a gmock action that fills bsock->msg so we can recv() a message */
ACTION_P2(BareosSocket_Recv,bsock,msg)99 ACTION_P2(BareosSocket_Recv, bsock, msg)
100 {
101 Bsnprintf(bsock->msg, int32_t(strlen(msg) + 1), msg);
102 }
103 #define BSOCK_RECV(bsock, msg) \
104 DoAll(BareosSocket_Recv(bsock, msg), Return(strlen(msg)))
105
~BareosSocketMock()106 BareosSocketMock::~BareosSocketMock()
107 {
108 if (msg) { /* duplicated */
109 FreePoolMemory(msg);
110 msg = nullptr;
111 }
112 if (errmsg) { /* duplicated */
113 FreePoolMemory(errmsg);
114 errmsg = nullptr;
115 }
116 if (who_) { /* duplicated */
117 free(who_);
118 who_ = nullptr;
119 }
120 if (host_) { /* duplicated */
121 free(host_);
122 host_ = nullptr;
123 }
124 if (src_addr) { /* duplicated */
125 free(src_addr);
126 src_addr = nullptr;
127 }
128 }
129