test_hostkey.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "session_fixture.h"
  2. #include <libssh2.h>
  3. #include <stdio.h>
  4. const char *EXPECTED_HOSTKEY =
  5. "AAAAB3NzaC1yc2EAAAABIwAAAQEArrr/JuJmaZligyfS8vcNur+mWR2ddDQtVdhHzdKU"
  6. "UoR6/Om6cvxpe61H1YZO1xCpLUBXmkki4HoNtYOpPB2W4V+8U4BDeVBD5crypEOE1+7B"
  7. "Am99fnEDxYIOZq2/jTP0yQmzCpWYS3COyFmkOL7sfX1wQMeW5zQT2WKcxC6FSWbhDqrB"
  8. "eNEGi687hJJoJ7YXgY/IdiYW5NcOuqRSWljjGS3dAJsHHWk4nJbhjEDXbPaeduMAwQU9"
  9. "i6ELfP3r+q6wdu0P4jWaoo3De1aYxnToV/ldXykpipON4NPamsb6Ph2qlJQKypq7J4iQ"
  10. "gkIIbCU1A31+4ExvcIVoxLQw/aTSbw==";
  11. int test(LIBSSH2_SESSION *session)
  12. {
  13. int rc;
  14. size_t len;
  15. int type;
  16. unsigned int expected_len = 0;
  17. char *expected_hostkey = NULL;
  18. const char *hostkey = libssh2_session_hostkey(session, &len, &type);
  19. if (hostkey == NULL) {
  20. print_last_session_error("libssh2_session_hostkey");
  21. return 1;
  22. }
  23. if (type != LIBSSH2_HOSTKEY_TYPE_RSA) {
  24. /* Hostkey configured in docker container is RSA */
  25. fprintf(stderr, "Wrong type of hostkey\n");
  26. return 1;
  27. }
  28. rc = libssh2_base64_decode(session, &expected_hostkey, &expected_len,
  29. EXPECTED_HOSTKEY, strlen(EXPECTED_HOSTKEY));
  30. if (rc != 0) {
  31. print_last_session_error("libssh2_base64_decode");
  32. return 1;
  33. }
  34. if (len != expected_len) {
  35. fprintf(stderr, "Hostkey does not have the expected length %ld != %d\n",
  36. len, expected_len);
  37. return 1;
  38. }
  39. if (memcmp(hostkey, expected_hostkey, len) != 0) {
  40. fprintf(stderr, "Hostkeys do not match\n");
  41. return 1;
  42. }
  43. return 0;
  44. }