preliminary test suite

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@153 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Aris Adamantiadis 2008-03-17 02:31:22 +00:00
parent ebbca06b04
commit 46b96a145c
5 changed files with 198 additions and 0 deletions

9
tests/Makefile Normal file
View File

@ -0,0 +1,9 @@
all: test_tunnel
CFLAGS=-I../include/ -g -Wall
LDFLAGS=-lssh -L../libssh/.libs
test_tunnel: test_tunnel.o authentication.o connection.o
gcc -o $@ $^ $(LDFLAGS)
clean:
rm -f *.o test_tunnel

74
tests/authentication.c Normal file
View File

@ -0,0 +1,74 @@
/*
This file is distributed in public domain. You can do whatever you want
with its content.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libssh/libssh.h>
#include "tests.h"
static int auth_kbdint(SSH_SESSION *session){
int err=ssh_userauth_kbdint(session,NULL,NULL);
char *name,*instruction,*prompt,*ptr;
char buffer[128];
int i,n;
char echo;
while (err==SSH_AUTH_INFO){
name=ssh_userauth_kbdint_getname(session);
instruction=ssh_userauth_kbdint_getinstruction(session);
n=ssh_userauth_kbdint_getnprompts(session);
if(strlen(name)>0)
printf("%s\n",name);
if(strlen(instruction)>0)
printf("%s\n",instruction);
for(i=0;i<n;++i){
prompt=ssh_userauth_kbdint_getprompt(session,i,&echo);
if(echo){
printf("%s",prompt);
fgets(buffer,sizeof(buffer),stdin);
buffer[sizeof(buffer)-1]=0;
if((ptr=strchr(buffer,'\n')))
*ptr=0;
ssh_userauth_kbdint_setanswer(session,i,buffer);
memset(buffer,0,strlen(buffer));
} else {
ptr=getpass(prompt);
ssh_userauth_kbdint_setanswer(session,i,ptr);
}
}
err=ssh_userauth_kbdint(session,NULL,NULL);
}
return err;
}
int authenticate (SSH_SESSION *session){
int auth=ssh_userauth_autopubkey(session);
char *password;
if(auth==SSH_AUTH_ERROR){
fprintf(stderr,"Authenticating with pubkey: %s\n",ssh_get_error(session));
return auth;
}
if(auth!=SSH_AUTH_SUCCESS){
auth=auth_kbdint(session);
if(auth==SSH_AUTH_ERROR){
fprintf(stderr,"authenticating with keyb-interactive: %s\n",
ssh_get_error(session));
return auth;
}
}
if(auth!=SSH_AUTH_SUCCESS){
password=getpass("Password : ");
auth = ssh_userauth_password(session,NULL,password);
memset(password,0,strlen(password));
if (auth==SSH_AUTH_ERROR){
fprintf(stderr,"Authentication with password failed: %s\n",ssh_get_error(session));
return auth;
}
}
return auth;
}

31
tests/connection.c Normal file
View File

@ -0,0 +1,31 @@
/*
This file is distributed in public domain. You can do whatever you want
with its content.
*/
#include <libssh/libssh.h>
#include <stdio.h>
#include "tests.h"
SSH_OPTIONS *set_opts(int argc, char **argv){
SSH_OPTIONS *options=ssh_options_new();
char *host=NULL;
if(ssh_options_getopt(options,&argc, argv)){
fprintf(stderr,"error parsing command line :%s\n",ssh_get_error(options));
return NULL;
}
int i;
while((i=getopt(argc,argv,""))!=-1){
switch(i){
default:
fprintf(stderr,"unknown option %c\n",optopt);
}
}
if(optind < argc)
host=argv[optind++];
if(host==NULL){
fprintf(stderr,"must provide an host name\n");
return NULL;
}
ssh_options_set_host(options,host);
return options;
}

76
tests/test_tunnel.c Normal file
View File

@ -0,0 +1,76 @@
/*
This file is distributed in public domain. You can do whatever you want
with its content.
*/
#include <libssh/libssh.h>
#include <stdio.h>
#include <string.h>
#include "tests.h"
#define ECHO_PORT 7
void do_connect(SSH_SESSION *session){
int error=ssh_connect(session);
if(error != SSH_OK){
fprintf(stderr,"Error at connection :%s\n",ssh_get_error(session));
return;
}
printf("Connected\n");
ssh_is_server_known(session);
// we don't care what happens here
error=authenticate(session);
if(error != SSH_AUTH_SUCCESS){
fprintf(stderr,"Error at authentication :%s\n",ssh_get_error(session));
return;
}
printf("Authenticated\n");
CHANNEL *channel=channel_new(session);
error=channel_open_forward(channel,"localhost",ECHO_PORT,"localhost",42);
if(error!=SSH_OK){
fprintf(stderr,"Error when opening forward:%s\n",ssh_get_error(session));
return;
}
printf("Forward opened\n");
int i=0;
char string[20];
char buffer[20];
for(i=0;i<2000;++i){
sprintf(string,"%d\n",i);
channel_write(channel,string,strlen(string));
do {
error=channel_poll(channel,0);
//if(error < strlen(string))
//usleep(10);
} while(error < strlen(string) && error >= 0);
if(error>0){
error=channel_read_nonblocking(channel,buffer,strlen(string),0);
if(error>=0){
if(memcmp(buffer,string,strlen(string))!=0){
fprintf(stderr,"Problem with answer: wanted %s got %s\n",string,buffer);
} else {
printf(".");
fflush(stdout);
}
}
}
if(error==-1){
fprintf(stderr,"Channel reading error : %s\n",ssh_get_error(session));
break;
}
}
printf("\nChannel test finished\n");
channel_close(channel);
channel_free(channel);
}
int main(int argc, char **argv){
SSH_OPTIONS *options=set_opts(argc, argv);
SSH_SESSION *session=ssh_new();
if(options==NULL){
return 1;
}
ssh_set_options(session,options);
do_connect(session);
ssh_disconnect(session);
ssh_finalize();
return 0;
}

8
tests/tests.h Normal file
View File

@ -0,0 +1,8 @@
/*
This file is distributed in public domain. You can do whatever you want
with its content.
*/
#include <libssh/libssh.h>
int authenticate (SSH_SESSION *session);
SSH_OPTIONS *set_opts(int argc, char **argv);