session_fixture.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright (C) 2016 Alexander Lamaison
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms,
  5. * with or without modification, are permitted provided
  6. * that the following conditions are met:
  7. *
  8. * Redistributions of source code must retain the above
  9. * copyright notice, this list of conditions and the
  10. * following disclaimer.
  11. *
  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.
  16. *
  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.
  21. *
  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
  35. * OF SUCH DAMAGE.
  36. */
  37. #include "session_fixture.h"
  38. #include "libssh2_config.h"
  39. #include "openssh_fixture.h"
  40. #include <stdio.h>
  41. #ifdef HAVE_WINDOWS_H
  42. #include <windows.h>
  43. #endif
  44. #ifdef HAVE_WINSOCK2_H
  45. #include <winsock2.h>
  46. #endif
  47. #ifdef HAVE_SYS_SOCKET_H
  48. #include <sys/socket.h>
  49. #endif
  50. LIBSSH2_SESSION *connected_session = NULL;
  51. int connected_socket = -1;
  52. static int connect_to_server()
  53. {
  54. connected_socket = open_socket_to_openssh_server();
  55. if (connected_socket > -1) {
  56. int rc = libssh2_session_handshake(connected_session, connected_socket);
  57. if (rc == 0) {
  58. return 0;
  59. }
  60. else {
  61. print_last_session_error("libssh2_session_handshake");
  62. return -1;
  63. }
  64. }
  65. else {
  66. return -1;
  67. }
  68. }
  69. LIBSSH2_SESSION *start_session_fixture()
  70. {
  71. int rc = start_openssh_fixture();
  72. if (rc == 0) {
  73. rc = libssh2_init(0);
  74. if (rc == 0) {
  75. connected_session = libssh2_session_init_ex(NULL, NULL, NULL, NULL);
  76. libssh2_session_set_blocking(connected_session, 1);
  77. if (connected_session != NULL) {
  78. rc = connect_to_server();
  79. if (rc == 0) {
  80. return connected_session;
  81. }
  82. else {
  83. return NULL;
  84. }
  85. }
  86. else {
  87. fprintf(stderr, "libssh2_session_init_ex failed\n");
  88. return NULL;
  89. }
  90. }
  91. else {
  92. fprintf(stderr, "libssh2_init failed (%d)\n", rc);
  93. return NULL;
  94. }
  95. }
  96. else {
  97. return NULL;
  98. }
  99. }
  100. void print_last_session_error(const char *function)
  101. {
  102. if (connected_session) {
  103. char *message;
  104. int rc =
  105. libssh2_session_last_error(connected_session, &message, NULL, 0);
  106. fprintf(stderr, "%s failed (%d): %s\n", function, rc, message);
  107. }
  108. else {
  109. fprintf(stderr, "No session");
  110. }
  111. }
  112. void stop_session_fixture()
  113. {
  114. if (connected_session) {
  115. libssh2_session_disconnect(connected_session, "test ended");
  116. libssh2_session_free(connected_session);
  117. shutdown(connected_socket, 2);
  118. connected_session = NULL;
  119. }
  120. else {
  121. fprintf(stderr, "Cannot stop session - none started");
  122. }
  123. stop_openssh_fixture();
  124. }