forked from libssh2/libssh2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_read.c
131 lines (112 loc) · 4.08 KB
/
test_read.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Copyright (C) The libssh2 project and its contributors.
*
* libssh2 test receiving large amounts of data through a channel
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "runner.h"
#include "openssh_fixture.h"
#include <stdlib.h> /* for getenv() */
/* set in Dockerfile */
static const char *username = "libssh2";
static const char *key_file_private = "key_rsa";
static const char *key_file_public = "key_rsa.pub";
int test(LIBSSH2_SESSION *session)
{
int rc;
unsigned long xfer_bytes = 0;
LIBSSH2_CHANNEL *channel;
/* Size and number of blocks to transfer
* This needs to be large to increase the chance of timing effects causing
* different code paths to be hit in the unframing code, but not so long
* that the integration tests take too long. 5 seconds of run time is
* probably a reasonable compromise. The block size is an odd number to
* increase the chance that various internal buffer and block boundaries
* are overlapped. */
const unsigned long xfer_bs = 997;
unsigned long xfer_count = 140080;
char remote_command[256];
const char *env;
const char *userauth_list;
/* Ignore our hard-wired Dockerfile user when not running under Docker */
if(!openssh_fixture_have_docker()) {
username = getenv("USER");
#ifdef _WIN32
if(!username)
username = getenv("USERNAME");
#endif
}
userauth_list = libssh2_userauth_list(session, username,
(unsigned int)strlen(username));
if(!userauth_list) {
print_last_session_error("libssh2_userauth_list");
return 1;
}
if(!strstr(userauth_list, "publickey")) {
fprintf(stderr, "'publickey' was expected in userauth list: %s\n",
userauth_list);
return 1;
}
rc = libssh2_userauth_publickey_fromfile_ex(session, username,
(unsigned int)strlen(username),
srcdir_path(key_file_public),
srcdir_path(key_file_private),
NULL);
if(rc) {
print_last_session_error("libssh2_userauth_publickey_fromfile_ex");
return 1;
}
/* Request a session channel on which to run a shell */
channel = libssh2_channel_open_session(session);
if(!channel) {
fprintf(stderr, "Unable to open a session\n");
goto shutdown;
}
env = getenv("FIXTURE_XFER_COUNT");
if(env) {
xfer_count = (unsigned long)strtol(env, NULL, 0);
fprintf(stderr, "Custom xfer_count: %lu\n", xfer_count);
}
/* command to transfer the desired amount of data */
snprintf(remote_command, sizeof(remote_command),
"dd if=/dev/zero bs=%lu count=%lu status=none",
xfer_bs, xfer_count);
/* Send the command to transfer data */
if(libssh2_channel_exec(channel, remote_command)) {
fprintf(stderr, "Unable to request command on channel\n");
goto shutdown;
}
/* Read data */
while(!libssh2_channel_eof(channel)) {
char buf[1024];
ssize_t err = libssh2_channel_read(channel, buf, sizeof(buf));
if(err < 0)
fprintf(stderr, "Unable to read response: %ld\n", (long)err);
else {
unsigned int i;
for(i = 0; i < (unsigned long)err; ++i) {
if(buf[i]) {
fprintf(stderr, "Bad data received\n");
/* Test will fail below due to bad data length */
break;
}
}
xfer_bytes += i;
}
}
/* Shut down */
if(libssh2_channel_close(channel))
fprintf(stderr, "Unable to close channel\n");
if(channel) {
libssh2_channel_free(channel);
channel = NULL;
}
shutdown:
/* Test check */
if(xfer_bytes != xfer_count * xfer_bs) {
fprintf(stderr, "Not enough bytes received: %lu not %lu\n",
xfer_bytes, xfer_count * xfer_bs);
return 1; /* error */
}
return 0;
}