@@ -501,6 +501,74 @@ void test_online_clone__ssh_cert(void)
501501 cl_git_fail_with (GIT_EUSER , git_clone (& g_repo , "ssh://localhost/foo" , "./foo" , & g_options ));
502502}
503503
504+ static char * read_key_file (const char * path )
505+ {
506+ FILE * f ;
507+ char * buf ;
508+ long key_length ;
509+
510+ if (!path || !* path )
511+ return NULL ;
512+
513+ cl_assert ((f = fopen (path , "r" )) != NULL );
514+ cl_assert (fseek (f , 0 , SEEK_END ) != -1 );
515+ cl_assert ((key_length = ftell (f )) != -1 );
516+ cl_assert (fseek (f , 0 , SEEK_SET ) != -1 );
517+ cl_assert ((buf = malloc (key_length )) != NULL );
518+ cl_assert (fread (buf , key_length , 1 , f ) == 1 );
519+ fclose (f );
520+
521+ return buf ;
522+ }
523+
524+ static int ssh_memory_cred_cb (git_cred * * cred , const char * url , const char * user_from_url ,
525+ unsigned int allowed_types , void * payload )
526+ {
527+ const char * remote_user = cl_getenv ("GITTEST_REMOTE_USER" );
528+ const char * pubkey_path = cl_getenv ("GITTEST_REMOTE_SSH_PUBKEY" );
529+ const char * privkey_path = cl_getenv ("GITTEST_REMOTE_SSH_KEY" );
530+ const char * passphrase = cl_getenv ("GITTEST_REMOTE_SSH_PASSPHRASE" );
531+
532+ GIT_UNUSED (url ); GIT_UNUSED (user_from_url ); GIT_UNUSED (payload );
533+
534+ if (allowed_types & GIT_CREDTYPE_USERNAME )
535+ return git_cred_username_new (cred , remote_user );
536+
537+ if (allowed_types & GIT_CREDTYPE_SSH_KEY )
538+ {
539+ char * pubkey = read_key_file (pubkey_path );
540+ char * privkey = read_key_file (privkey_path );
541+
542+ int ret = git_cred_ssh_key_memory_new (cred , remote_user , pubkey , privkey , passphrase );
543+
544+ if (privkey )
545+ free (privkey );
546+ if (pubkey )
547+ free (pubkey );
548+ return ret ;
549+ }
550+
551+ giterr_set (GITERR_NET , "unexpected cred type" );
552+ return -1 ;
553+ }
554+
555+ void test_online_clone__ssh_memory_auth (void )
556+ {
557+ const char * remote_url = cl_getenv ("GITTEST_REMOTE_URL" );
558+ const char * remote_user = cl_getenv ("GITTEST_REMOTE_USER" );
559+ const char * privkey = cl_getenv ("GITTEST_REMOTE_SSH_KEY" );
560+
561+ #ifndef GIT_SSH_MEMORY_CREDENTIALS
562+ clar__skip ();
563+ #endif
564+ if (!remote_url || !remote_user || !privkey || strncmp (remote_url , "ssh://" , 5 ) != 0 )
565+ clar__skip ();
566+
567+ g_options .fetch_opts .callbacks .credentials = ssh_memory_cred_cb ;
568+
569+ cl_git_pass (git_clone (& g_repo , remote_url , "./foo" , & g_options ));
570+ }
571+
504572void test_online_clone__url_with_no_path_returns_EINVALIDSPEC (void )
505573{
506574 cl_git_fail_with (git_clone (& g_repo , "http://github.com" , "./foo" , & g_options ),
0 commit comments