27
27
28
28
#include < gmock/gmock.h>
29
29
30
+ #include < fmt/format.h>
30
31
namespace mp = multipass;
31
32
namespace mpt = multipass::test;
32
33
33
34
namespace
34
35
{
36
+ sftp_file get_dummy_sftp_file ()
37
+ {
38
+ return static_cast <sftp_file_struct*>(calloc (1 , sizeof (struct sftp_file_struct )));
39
+ }
40
+
41
+ sftp_attributes get_dummy_sftp_attributes ()
42
+ {
43
+ return static_cast <sftp_attributes_struct*>(calloc (1 , sizeof (struct sftp_attributes_struct )));
44
+ }
45
+
35
46
struct SFTPClient : public testing ::Test
36
47
{
37
48
SFTPClient ()
38
49
: sftp_new{mock_sftp_new,
39
50
[](ssh_session session) -> sftp_session {
40
- sftp_session sftp;
41
- sftp = (sftp_session) std::calloc (1 , sizeof (struct sftp_session_struct ));
51
+ sftp_session sftp =
52
+ static_cast <sftp_session_struct*>( std::calloc (1 , sizeof (struct sftp_session_struct ) ));
42
53
return sftp;
43
54
}},
44
55
free_sftp{mock_sftp_free, [](sftp_session sftp) { std::free (sftp); }}
@@ -80,7 +91,6 @@ TEST_F(SFTPClient, push_throws_when_failed_to_init)
80
91
{
81
92
auto sftp = make_sftp_client ();
82
93
83
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
84
94
REPLACE (sftp_init, [](auto ...) { return SSH_ERROR; });
85
95
86
96
EXPECT_THROW (sftp.push_file (" foo" , " bar" ), std::runtime_error);
@@ -90,21 +100,21 @@ TEST_F(SFTPClient, push_throws_on_sftp_open_failed)
90
100
{
91
101
auto sftp = make_sftp_client ();
92
102
93
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
94
103
REPLACE (sftp_init, [](auto ...) { return SSH_OK; });
95
- REPLACE (sftp_open, [](auto ...) { return nullptr ; });
104
+ REPLACE (sftp_open, [](sftp_session session, auto ...) {
105
+ session->errnum = SSH_ERROR;
106
+ return nullptr ;
107
+ });
96
108
97
109
EXPECT_THROW (sftp.push_file (" foo" , " bar" ), std::runtime_error);
98
110
}
99
111
100
- TEST_F (SFTPClient, push_throws_on_sftp_read_failed )
112
+ TEST_F (SFTPClient, push_throws_on_invalid_source )
101
113
{
102
114
auto sftp = make_sftp_client ();
103
115
104
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
105
116
REPLACE (sftp_init, [](auto ...) { return SSH_OK; });
106
- REPLACE (sftp_open, [](auto ...) { return nullptr ; });
107
- REPLACE (sftp_get_error, [](auto ...) { return SSH_OK; });
117
+ REPLACE (sftp_open, [](auto ...) { return get_dummy_sftp_file (); });
108
118
109
119
EXPECT_THROW (sftp.push_file (" foo" , " bar" ), std::runtime_error);
110
120
}
@@ -117,13 +127,15 @@ TEST_F(SFTPClient, push_throws_on_sftp_write_error)
117
127
118
128
auto sftp = make_sftp_client ();
119
129
120
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
121
130
REPLACE (sftp_init, [](auto ...) { return SSH_OK; });
122
- REPLACE (sftp_open, [](auto ...) { return nullptr ; });
123
- REPLACE (sftp_write, [](auto ...) { return 0 ; });
124
- REPLACE (sftp_get_error, [](auto ...) {
125
- static int errorCount = 0 ;
126
- return (errorCount-- < 0 ) ? SSH_ERROR : SSH_OK;
131
+ REPLACE (sftp_open, [](sftp_session session, auto ...) {
132
+ sftp_file file = get_dummy_sftp_file ();
133
+ file->sftp = session;
134
+ return file;
135
+ });
136
+ REPLACE (sftp_write, [](sftp_file file, auto ...) {
137
+ file->sftp ->errnum = SSH_ERROR;
138
+ return -1 ;
127
139
});
128
140
129
141
EXPECT_THROW (sftp.push_file (file_name.toStdString (), " bar" ), std::runtime_error);
@@ -135,7 +147,6 @@ TEST_F(SFTPClient, pull_throws_when_failed_to_init)
135
147
{
136
148
auto sftp = make_sftp_client ();
137
149
138
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
139
150
REPLACE (sftp_init, [](auto ...) { return SSH_ERROR; });
140
151
141
152
EXPECT_THROW (sftp.pull_file (" foo" , " bar" ), std::runtime_error);
@@ -145,24 +156,29 @@ TEST_F(SFTPClient, pull_throws_on_sftp_get_stat_failed)
145
156
{
146
157
auto sftp = make_sftp_client ();
147
158
148
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
149
159
REPLACE (sftp_init, [](auto ...) { return SSH_OK; });
150
- REPLACE (sftp_stat, [](auto ...) { return nullptr ; });
160
+ REPLACE (sftp_stat, [](sftp_session session, auto ...) {
161
+ session->errnum = SSH_ERROR;
162
+ return nullptr ;
163
+ });
151
164
152
165
EXPECT_THROW (sftp.pull_file (" foo" , " bar" ), std::runtime_error);
153
166
}
154
167
155
168
TEST_F (SFTPClient, pull_throws_on_sftp_open_failed)
156
169
{
157
170
auto sftp = make_sftp_client ();
171
+ const std::string source_path{" foo" };
158
172
159
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
160
173
REPLACE (sftp_init, [](auto ...) { return SSH_OK; });
161
- REPLACE (sftp_stat, [](auto ...) { return new sftp_attributes_struct; });
162
- REPLACE (sftp_open, [](auto ...) { return nullptr ; });
163
- REPLACE (sftp_get_error, [](auto ...) {
164
- static int errorCount = 0 ;
165
- return (errorCount-- < 0 ) ? SSH_ERROR : SSH_OK;
174
+ REPLACE (sftp_stat, [&source_path](auto ...) {
175
+ auto attributes = get_dummy_sftp_attributes ();
176
+ attributes->name = strdup (source_path.c_str ());
177
+ return attributes;
178
+ });
179
+ REPLACE (sftp_open, [](sftp_session session, auto ...) {
180
+ session->errnum = SSH_ERROR;
181
+ return nullptr ;
166
182
});
167
183
168
184
EXPECT_THROW (sftp.pull_file (" foo" , " bar" ), std::runtime_error);
@@ -171,15 +187,22 @@ TEST_F(SFTPClient, pull_throws_on_sftp_open_failed)
171
187
TEST_F (SFTPClient, pull_throws_on_sftp_read_failed)
172
188
{
173
189
auto sftp = make_sftp_client ();
190
+ const std::string source_path{" foo" };
174
191
175
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
176
192
REPLACE (sftp_init, [](auto ...) { return SSH_OK; });
177
- REPLACE (sftp_stat, [](auto ...) { return new sftp_attributes_struct; });
178
- REPLACE (sftp_open, [](auto ...) { return new sftp_file_struct; });
179
- REPLACE (sftp_read, [](auto ...) { return 0 ; });
180
- REPLACE (sftp_get_error, [](auto ...) {
181
- static int errorCount = 1 ;
182
- return (errorCount-- < 0 ) ? SSH_ERROR : SSH_OK;
193
+ REPLACE (sftp_stat, [&source_path](auto ...) {
194
+ auto attributes = get_dummy_sftp_attributes ();
195
+ attributes->name = strdup (source_path.c_str ());
196
+ return attributes;
197
+ });
198
+ REPLACE (sftp_open, [](sftp_session session, auto ...) {
199
+ auto file = get_dummy_sftp_file ();
200
+ file->sftp = session;
201
+ return file;
202
+ });
203
+ REPLACE (sftp_read, [](sftp_file file, auto ...) {
204
+ file->sftp ->errnum = SSH_ERROR;
205
+ return -1 ;
183
206
});
184
207
185
208
EXPECT_THROW (sftp.pull_file (" foo" , " bar" ), std::runtime_error);
@@ -191,7 +214,6 @@ TEST_F(SFTPClient, stream_throws_when_failed_to_init)
191
214
{
192
215
auto sftp = make_sftp_client ();
193
216
194
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
195
217
REPLACE (sftp_init, [](auto ...) { return SSH_ERROR; });
196
218
197
219
EXPECT_THROW (sftp.stream_file (" bar" ), std::runtime_error);
@@ -201,7 +223,6 @@ TEST_F(SFTPClient, steam_throws_on_sftp_open_failed)
201
223
{
202
224
auto sftp = make_sftp_client ();
203
225
204
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
205
226
REPLACE (sftp_init, [](auto ...) { return SSH_OK; });
206
227
REPLACE (sftp_open, [](auto ...) { return nullptr ; });
207
228
REPLACE (sftp_get_error, [](auto ...) { return SSH_ERROR; });
@@ -213,7 +234,6 @@ TEST_F(SFTPClient, stream_throws_on_write_failed)
213
234
{
214
235
auto sftp = make_sftp_client ();
215
236
216
- REPLACE (sftp_new, [](auto ...) { return new sftp_session_struct; });
217
237
REPLACE (sftp_init, [](auto ...) { return SSH_OK; });
218
238
REPLACE (sftp_open, [](auto ...) { return nullptr ; });
219
239
REPLACE (sftp_write, [](auto ...) { return 0 ; });
0 commit comments