]>
git.proxmox.com Git - rustc.git/blob - src/vendor/libssh2-sys/libssh2/tests/session_fixture.c
1 /* Copyright (C) 2016 Alexander Lamaison
4 * Redistribution and use in source and binary forms,
5 * with or without modification, are permitted provided
6 * that the following conditions are met:
8 * Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the
10 * following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * Neither the name of the copyright holder nor the names
18 * of any other contributors may be used to endorse or
19 * promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
38 #include "session_fixture.h"
39 #include "libssh2_config.h"
40 #include "openssh_fixture.h"
47 #ifdef HAVE_WINSOCK2_H
50 #ifdef HAVE_SYS_SOCKET_H
51 #include <sys/socket.h>
54 LIBSSH2_SESSION
*connected_session
= NULL
;
55 int connected_socket
= -1;
57 static int connect_to_server()
59 connected_socket
= open_socket_to_openssh_server();
60 if (connected_socket
> -1) {
61 int rc
= libssh2_session_handshake(connected_session
, connected_socket
);
66 print_last_session_error("libssh2_session_handshake");
75 LIBSSH2_SESSION
*start_session_fixture()
77 int rc
= start_openssh_fixture();
81 connected_session
= libssh2_session_init_ex(NULL
, NULL
, NULL
, NULL
);
82 libssh2_session_set_blocking(connected_session
, 1);
83 if (connected_session
!= NULL
) {
84 rc
= connect_to_server();
86 return connected_session
;
93 fprintf(stderr
, "libssh2_session_init_ex failed\n");
98 fprintf(stderr
, "libssh2_init failed (%d)\n", rc
);
107 void print_last_session_error(const char *function
)
109 if (connected_session
) {
112 libssh2_session_last_error(connected_session
, &message
, NULL
, 0);
113 fprintf(stderr
, "%s failed (%d): %s\n", function
, rc
, message
);
116 fprintf(stderr
, "No session");
120 void stop_session_fixture()
122 if (connected_session
) {
123 libssh2_session_disconnect(connected_session
, "test ended");
124 libssh2_session_free(connected_session
);
125 shutdown(connected_socket
, 2);
126 connected_session
= NULL
;
129 fprintf(stderr
, "Cannot stop session - none started");
132 stop_openssh_fixture();